Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# net.h

Go to the documentation of this file.

    00001 /*! \file net.h \brief Network support library. */
    00002 //*****************************************************************************
    00003 //
    00004 // File Name    : 'net.h'
    00005 // Title        : Network support library
    00006 // Author       : Pascal Stang
    00007 // Created      : 8/30/2004
    00008 // Revised      : 7/3/2005
    00009 // Version      : 0.1
    00010 // Target MCU   : Atmel AVR series
    00011 // Editor Tabs  : 4
    00012 //
    00013 /// \ingroup network
    00014 /// \defgroup net Network support library (net.c)
    00015 /// \code #include "net/net.h" \endcode
    00016 /// \par Description
    00017 ///     This is a general network support library including a multitude of
    00018 ///     structure definitions for various types of network packets, functions
    00019 ///     and macros for switching byte order, and an RFC-compliant function
    00020 ///     for calculating checksums.
    00021 //
    00022 //  This code is distributed under the GNU Public License
    00023 //      which can be found at http://www.gnu.org/licenses/gpl.txt
    00024 //*****************************************************************************
    00025 //@{
    00026
    00027 #ifndef NET_H
    00028 #define NET_H
    00029
    00030 #include <inttypes.h>
    00031 #include "global.h"
    00032
    00033 // Representation of a 48-bit Ethernet address.
    00034 struct netEthAddr
    00035 {
    00036     uint8_t addr[6];
    00037 } GNUC_PACKED;
    00038
    00039 // The Ethernet header
    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 // The ARP header
    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 // The IP header
    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 // The ICMP header
    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 // The UDP header
    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 // The TCP header
    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 //  uint8_t     optdata[4];
    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 // Ethernet/ARP header
    00139 struct netEthArpHeader
    00140 {
    00141     struct netEthHeader eth;
    00142     struct netArpHeader arp;
    00143 } GNUC_PACKED;
    00144
    00145 // Ethernet/IP header
    00146 struct netEthIpHeader
    00147 {
    00148     struct netEthHeader eth;
    00149     struct netIpHeader  ip;
    00150 } GNUC_PACKED;
    00151
    00152 // The IP header
    00153 typedef struct netIpHeader ip_hdr;
    00154
    00155 // The IP/TCP headers
    00156 typedef struct
    00157 {
    00158     struct netIpHeader  ip;
    00159     struct netTcpHeader tcp;
    00160 } tcpip_hdr;
    00161
    00162 // The IP/ICMP headers
    00163 typedef struct {
    00164     struct netIpHeader  ip;
    00165     struct netIcmpHeader icmp;
    00166 } icmpip_hdr;
    00167
    00168
    00169 // The UDP and IP headers
    00170 typedef struct {
    00171     struct netIpHeader  ip;
    00172     struct netUdpHeader udp;
    00173 } udpip_hdr;
    00174
    00175
    00176 //! Convert dot-notation IP address into 32-bit word.
    00177 /// Example: IPDOT(192l,168l,1l,1l)
    00178 #define IPDOT(a,b,c,d)  ((a<<24)|(b<<16)|(c<<8)|(d))
    00179
    00180 //! Host-to-Network SHORT (16-bit) byte-order swap (macro).
    00181 #define HTONS(s)        ((s<<8) | (s>>8))
    00182 //! Host-to-Network LONG (32-bit) byte-order swap (macro).
    00183 #define HTONL(l)        ((l<<24) | ((l&0x00FF0000l)>>8) | ((l&0x0000FF00l)<<8) | (l>>24))
    00184
    00185 //! Host-to-Network SHORT (16-bit) byte-order swap (function).
    00186 uint16_t htons(uint16_t val);
    00187 //! Host-to-Network LONG (32-bit) byte-order swap (function).
    00188 uint32_t htonl(uint32_t val);
    00189
    00190 //! Calculate IP-style checksum from data.
    00191 uint16_t netChecksum(void *data, uint16_t len);
    00192
    00193 //! Print Ethernet address in XX:XX:XX:XX:XX:XX format.
    00194 void netPrintEthAddr(struct netEthAddr* ethaddr);
    00195 //! Print IP address in dot notation.
    00196 void netPrintIPAddr(uint32_t ipaddr);
    00197 //! Print Ethernet header information.
    00198 void netPrintEthHeader(struct netEthHeader* eth_hdr);
    00199 //! Print IP header information.
    00200 void netPrintIpHeader(struct netIpHeader* ipheader);
    00201 //! Print TCP header information.
    00202 void netPrintTcpHeader(struct netTcpHeader* tcpheader);
    00203
    00204 #endif
    00205 //@}
    00206