00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef NET_H
00028 #define NET_H
00029
00030 #include <inttypes.h>
00031 #include "global.h"
00032
00033
00034 struct netEthAddr
00035 {
00036 uint8_t addr[6];
00037 } GNUC_PACKED;
00038
00039
00040 struct netEthHeader
00041 {
00042 struct netEthAddr dest;
00043 struct netEthAddr src;
00044 uint16_t type;
00045 } GNUC_PACKED;
00046
00047 #define ETH_HEADER_LEN 14
00048
00049 #define ETHTYPE_ARP 0x0806
00050 #define ETHTYPE_IP 0x0800
00051 #define ETHTYPE_IP6 0x86dd
00052
00053
00054 struct netArpHeader
00055 {
00056 uint16_t hwtype;
00057 uint16_t protocol;
00058 uint8_t hwlen;
00059 uint8_t protolen;
00060 uint16_t opcode;
00061 struct netEthAddr shwaddr;
00062 uint32_t sipaddr;
00063 struct netEthAddr dhwaddr;
00064 uint32_t dipaddr;
00065 } GNUC_PACKED;
00066
00067 #define ARP_OPCODE_REQUEST 1
00068 #define ARP_OPCODE_REPLY 2
00069 #define ARP_HWTYPE_ETH 1
00070
00071
00072 struct netIpHeader
00073 {
00074 uint8_t vhl;
00075 uint8_t tos;
00076 uint16_t len;
00077 uint16_t ipid;
00078 uint16_t ipoffset;
00079 uint8_t ttl;
00080 uint8_t proto;
00081 uint16_t ipchksum;
00082 uint32_t srcipaddr;
00083 uint32_t destipaddr;
00084 } GNUC_PACKED;
00085 #define IP_HEADER_LEN 20
00086
00087 #define IP_PROTO_ICMP 1
00088 #define IP_PROTO_TCP 6
00089 #define IP_PROTO_UDP 17
00090
00091
00092 struct netIcmpHeader
00093 {
00094 uint8_t type;
00095 uint8_t icode;
00096 uint16_t icmpchksum;
00097 uint16_t id;
00098 uint16_t seqno;
00099 } GNUC_PACKED;
00100 #define ICMP_HEADER_LEN 8
00101
00102 #define ICMP_TYPE_ECHOREPLY 0
00103 #define ICMP_TYPE_ECHOREQUEST 8
00104
00105
00106 struct netUdpHeader
00107 {
00108 uint16_t srcport;
00109 uint16_t destport;
00110 uint16_t udplen;
00111 uint16_t udpchksum;
00112 } GNUC_PACKED;
00113 #define UDP_HEADER_LEN 8
00114
00115
00116 struct netTcpHeader
00117 {
00118 uint16_t srcport;
00119 uint16_t destport;
00120 uint32_t seqno;
00121 uint32_t ackno;
00122 uint8_t tcpoffset;
00123 uint8_t flags;
00124 uint16_t wnd;
00125 uint16_t tcpchksum;
00126 uint16_t urgp;
00127
00128 } GNUC_PACKED;
00129 #define TCP_HEADER_LEN 20
00130
00131 #define TCP_FLAGS_FIN 0x01
00132 #define TCP_FLAGS_SYN 0x02
00133 #define TCP_FLAGS_RST 0x04
00134 #define TCP_FLAGS_PSH 0x08
00135 #define TCP_FLAGS_ACK 0x10
00136 #define TCP_FLAGS_URG 0x20
00137
00138
00139 struct netEthArpHeader
00140 {
00141 struct netEthHeader eth;
00142 struct netArpHeader arp;
00143 } GNUC_PACKED;
00144
00145
00146 struct netEthIpHeader
00147 {
00148 struct netEthHeader eth;
00149 struct netIpHeader ip;
00150 } GNUC_PACKED;
00151
00152
00153 typedef struct netIpHeader ip_hdr;
00154
00155
00156 typedef struct
00157 {
00158 struct netIpHeader ip;
00159 struct netTcpHeader tcp;
00160 } tcpip_hdr;
00161
00162
00163 typedef struct {
00164 struct netIpHeader ip;
00165 struct netIcmpHeader icmp;
00166 } icmpip_hdr;
00167
00168
00169
00170 typedef struct {
00171 struct netIpHeader ip;
00172 struct netUdpHeader udp;
00173 } udpip_hdr;
00174
00175
00176
00177
00178 #define IPDOT(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|(d))
00179
00180
00181 #define HTONS(s) ((s<<8) | (s>>8))
00182
00183 #define HTONL(l) ((l<<24) | ((l&0x00FF0000l)>>8) | ((l&0x0000FF00l)<<8) | (l>>24))
00184
00185
00186 uint16_t htons(uint16_t val);
00187
00188 uint32_t htonl(uint32_t val);
00189
00190
00191 uint16_t netChecksum(void *data, uint16_t len);
00192
00193
00194 void netPrintEthAddr(struct netEthAddr* ethaddr);
00195
00196 void netPrintIPAddr(uint32_t ipaddr);
00197
00198 void netPrintEthHeader(struct netEthHeader* eth_hdr);
00199
00200 void netPrintIpHeader(struct netIpHeader* ipheader);
00201
00202 void netPrintTcpHeader(struct netTcpHeader* tcpheader);
00203
00204 #endif
00205
00206