| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | /*! \file net.h \brief Network support library. */ |
| 2 | //***************************************************************************** |
||
| 3 | // |
||
| 4 | // File Name : 'net.h' |
||
| 5 | // Title : Network support library |
||
| 6 | // Author : Pascal Stang |
||
| 7 | // Created : 8/30/2004 |
||
| 8 | // Revised : 7/3/2005 |
||
| 9 | // Version : 0.1 |
||
| 10 | // Target MCU : Atmel AVR series |
||
| 11 | // Editor Tabs : 4 |
||
| 12 | // |
||
| 13 | /// \ingroup network |
||
| 14 | /// \defgroup net Network support library (net.c) |
||
| 15 | /// \code #include "net/net.h" \endcode |
||
| 16 | /// \par Description |
||
| 17 | /// This is a general network support library including a multitude of |
||
| 18 | /// structure definitions for various types of network packets, functions |
||
| 19 | /// and macros for switching byte order, and an RFC-compliant function |
||
| 20 | /// for calculating checksums. |
||
| 21 | // |
||
| 22 | // This code is distributed under the GNU Public License |
||
| 23 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
| 24 | //***************************************************************************** |
||
| 25 | //@{ |
||
| 26 | |||
| 27 | #ifndef NET_H |
||
| 28 | #define NET_H |
||
| 29 | |||
| 30 | #include <inttypes.h> |
||
| 31 | #include "global.h" |
||
| 32 | |||
| 33 | // Representation of a 48-bit Ethernet address. |
||
| 34 | struct netEthAddr |
||
| 35 | { |
||
| 36 | uint8_t addr[6]; |
||
| 37 | } GNUC_PACKED; |
||
| 38 | |||
| 39 | // The Ethernet header |
||
| 40 | struct netEthHeader |
||
| 41 | { |
||
| 42 | struct netEthAddr dest; |
||
| 43 | struct netEthAddr src; |
||
| 44 | uint16_t type; |
||
| 45 | } GNUC_PACKED; |
||
| 46 | |||
| 47 | #define ETH_HEADER_LEN 14 |
||
| 48 | |||
| 49 | #define ETHTYPE_ARP 0x0806 |
||
| 50 | #define ETHTYPE_IP 0x0800 |
||
| 51 | #define ETHTYPE_IP6 0x86dd |
||
| 52 | |||
| 53 | // The ARP header |
||
| 54 | struct netArpHeader |
||
| 55 | { |
||
| 56 | uint16_t hwtype; |
||
| 57 | uint16_t protocol; |
||
| 58 | uint8_t hwlen; |
||
| 59 | uint8_t protolen; |
||
| 60 | uint16_t opcode; |
||
| 61 | struct netEthAddr shwaddr; |
||
| 62 | uint32_t sipaddr; |
||
| 63 | struct netEthAddr dhwaddr; |
||
| 64 | uint32_t dipaddr; |
||
| 65 | } GNUC_PACKED; |
||
| 66 | |||
| 67 | #define ARP_OPCODE_REQUEST 1 |
||
| 68 | #define ARP_OPCODE_REPLY 2 |
||
| 69 | #define ARP_HWTYPE_ETH 1 |
||
| 70 | |||
| 71 | // The IP header |
||
| 72 | struct netIpHeader |
||
| 73 | { |
||
| 74 | uint8_t vhl; |
||
| 75 | uint8_t tos; |
||
| 76 | uint16_t len; |
||
| 77 | uint16_t ipid; |
||
| 78 | uint16_t ipoffset; |
||
| 79 | uint8_t ttl; |
||
| 80 | uint8_t proto; |
||
| 81 | uint16_t ipchksum; |
||
| 82 | uint32_t srcipaddr; |
||
| 83 | uint32_t destipaddr; |
||
| 84 | } GNUC_PACKED; |
||
| 85 | #define IP_HEADER_LEN 20 |
||
| 86 | |||
| 87 | #define IP_PROTO_ICMP 1 |
||
| 88 | #define IP_PROTO_TCP 6 |
||
| 89 | #define IP_PROTO_UDP 17 |
||
| 90 | |||
| 91 | // The ICMP header |
||
| 92 | struct netIcmpHeader |
||
| 93 | { |
||
| 94 | uint8_t type; |
||
| 95 | uint8_t icode; |
||
| 96 | uint16_t icmpchksum; |
||
| 97 | uint16_t id; |
||
| 98 | uint16_t seqno; |
||
| 99 | } GNUC_PACKED; |
||
| 100 | #define ICMP_HEADER_LEN 8 |
||
| 101 | |||
| 102 | #define ICMP_TYPE_ECHOREPLY 0 |
||
| 103 | #define ICMP_TYPE_ECHOREQUEST 8 |
||
| 104 | |||
| 105 | // The UDP header |
||
| 106 | struct netUdpHeader |
||
| 107 | { |
||
| 108 | uint16_t srcport; |
||
| 109 | uint16_t destport; |
||
| 110 | uint16_t udplen; |
||
| 111 | uint16_t udpchksum; |
||
| 112 | } GNUC_PACKED; |
||
| 113 | #define UDP_HEADER_LEN 8 |
||
| 114 | |||
| 115 | // The TCP header |
||
| 116 | struct netTcpHeader |
||
| 117 | { |
||
| 118 | uint16_t srcport; |
||
| 119 | uint16_t destport; |
||
| 120 | uint32_t seqno; |
||
| 121 | uint32_t ackno; |
||
| 122 | uint8_t tcpoffset; |
||
| 123 | uint8_t flags; |
||
| 124 | uint16_t wnd; |
||
| 125 | uint16_t tcpchksum; |
||
| 126 | uint16_t urgp; |
||
| 127 | // uint8_t optdata[4]; |
||
| 128 | } GNUC_PACKED; |
||
| 129 | #define TCP_HEADER_LEN 20 |
||
| 130 | |||
| 131 | #define TCP_FLAGS_FIN 0x01 |
||
| 132 | #define TCP_FLAGS_SYN 0x02 |
||
| 133 | #define TCP_FLAGS_RST 0x04 |
||
| 134 | #define TCP_FLAGS_PSH 0x08 |
||
| 135 | #define TCP_FLAGS_ACK 0x10 |
||
| 136 | #define TCP_FLAGS_URG 0x20 |
||
| 137 | |||
| 138 | // Ethernet/ARP header |
||
| 139 | struct netEthArpHeader |
||
| 140 | { |
||
| 141 | struct netEthHeader eth; |
||
| 142 | struct netArpHeader arp; |
||
| 143 | } GNUC_PACKED; |
||
| 144 | |||
| 145 | // Ethernet/IP header |
||
| 146 | struct netEthIpHeader |
||
| 147 | { |
||
| 148 | struct netEthHeader eth; |
||
| 149 | struct netIpHeader ip; |
||
| 150 | } GNUC_PACKED; |
||
| 151 | |||
| 152 | // The IP header |
||
| 153 | typedef struct netIpHeader ip_hdr; |
||
| 154 | |||
| 155 | // The IP/TCP headers |
||
| 156 | typedef struct |
||
| 157 | { |
||
| 158 | struct netIpHeader ip; |
||
| 159 | struct netTcpHeader tcp; |
||
| 160 | } tcpip_hdr; |
||
| 161 | |||
| 162 | // The IP/ICMP headers |
||
| 163 | typedef struct { |
||
| 164 | struct netIpHeader ip; |
||
| 165 | struct netIcmpHeader icmp; |
||
| 166 | } icmpip_hdr; |
||
| 167 | |||
| 168 | |||
| 169 | // The UDP and IP headers |
||
| 170 | typedef struct { |
||
| 171 | struct netIpHeader ip; |
||
| 172 | struct netUdpHeader udp; |
||
| 173 | } udpip_hdr; |
||
| 174 | |||
| 175 | |||
| 176 | //! Convert dot-notation IP address into 32-bit word. |
||
| 177 | /// Example: IPDOT(192l,168l,1l,1l) |
||
| 178 | #define IPDOT(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|(d)) |
||
| 179 | |||
| 180 | //! Host-to-Network SHORT (16-bit) byte-order swap (macro). |
||
| 181 | #define HTONS(s) ((s<<8) | (s>>8)) |
||
| 182 | //! Host-to-Network LONG (32-bit) byte-order swap (macro). |
||
| 183 | #define HTONL(l) ((l<<24) | ((l&0x00FF0000l)>>8) | ((l&0x0000FF00l)<<8) | (l>>24)) |
||
| 184 | |||
| 185 | //! Host-to-Network SHORT (16-bit) byte-order swap (function). |
||
| 186 | uint16_t htons(uint16_t val); |
||
| 187 | //! Host-to-Network LONG (32-bit) byte-order swap (function). |
||
| 188 | uint32_t htonl(uint32_t val); |
||
| 189 | |||
| 190 | //! Calculate IP-style checksum from data. |
||
| 191 | uint16_t netChecksum(void *data, uint16_t len); |
||
| 192 | |||
| 193 | //! Print Ethernet address in XX:XX:XX:XX:XX:XX format. |
||
| 194 | void netPrintEthAddr(struct netEthAddr* ethaddr); |
||
| 195 | //! Print IP address in dot notation. |
||
| 196 | void netPrintIPAddr(uint32_t ipaddr); |
||
| 197 | //! Print Ethernet header information. |
||
| 198 | void netPrintEthHeader(struct netEthHeader* eth_hdr); |
||
| 199 | //! Print IP header information. |
||
| 200 | void netPrintIpHeader(struct netIpHeader* ipheader); |
||
| 201 | //! Print TCP header information. |
||
| 202 | void netPrintTcpHeader(struct netTcpHeader* tcpheader); |
||
| 203 | |||
| 204 | #endif |
||
| 205 | //@} |
||
| 206 |
Powered by WebSVN v2.8.3