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