-
Notifications
You must be signed in to change notification settings - Fork 2
/
sr_protocol.h
230 lines (190 loc) · 5.96 KB
/
sr_protocol.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (c) 1998, 1999, 2000 Mike D. Schiffman <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/**
* sr_protocol.h
*
*/
#ifndef SR_PROTOCOL_H
#define SR_PROTOCOL_H
#ifdef _LINUX_
#include <stdint.h>
#endif /* _LINUX_ */
#include <sys/types.h>
#include <arpa/inet.h>
#ifndef IP_MAXPACKET
#define IP_MAXPACKET 65535
#endif
/* FIXME
* ohh how lame .. how very, very lame... how can I ever go out in public
* again?! /mc
*/
#ifndef __LITTLE_ENDIAN
#define __LITTLE_ENDIAN 1
#endif
#ifndef __BIG_ENDIAN
#define __BIG_ENDIAN 2
#endif
#ifndef __BYTE_ORDER
#ifdef _CYGWIN_
#define __BYTE_ORDER __LITTLE_ENDIAN
#endif
#ifdef _LINUX_
#define __BYTE_ORDER __LITTLE_ENDIAN
#endif
#ifdef _SOLARIS_
#define __BYTE_ORDER __BIG_ENDIAN
#endif
#ifdef _DARWIN_
#define __BYTE_ORDER __BIG_ENDIAN
#endif
#endif
#define ICMP_DATA_SIZE 28
/* Structure of a ICMP header
*/
struct sr_icmp_hdr {
uint8_t icmp_type;
uint8_t icmp_code;
uint16_t icmp_sum;
uint16_t icmp_identifier;
uint16_t icmp_seqno;
} __attribute__ ((packed)) ;
typedef struct sr_icmp_hdr sr_icmp_hdr_t;
/* Structure of a type3 ICMP header
*/
struct sr_icmp_t3_hdr {
uint8_t icmp_type;
uint8_t icmp_code;
uint16_t icmp_sum;
uint16_t unused;
uint16_t next_mtu;
uint8_t data[ICMP_DATA_SIZE];
} __attribute__ ((packed)) ;
typedef struct sr_icmp_t3_hdr sr_icmp_t3_hdr_t;
struct sr_tcp_hdr {
uint16_t src_port;
uint16_t dest_port;
uint32_t seq_num;
uint32_t ack_num;
uint8_t unused;
uint8_t flags;
uint16_t window;
uint16_t sum;
uint16_t urgent;
} __attribute__ ((packed)) ;
typedef struct sr_tcp_hdr sr_tcp_hdr_t;
struct sr_tcp_pseudo_hdr {
uint32_t ip_src;
uint32_t ip_dst;
uint8_t reserved;
uint8_t protocol;
uint16_t len;
} __attribute__ ((packed)) ;
typedef struct sr_tcp_pseudo_hdr sr_tcp_pseudo_hdr_t;
/*
* Structure of an internet header, naked of options.
*/
struct sr_ip_hdr
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int ip_hl:4; /* header length */
unsigned int ip_v:4; /* version */
#elif __BYTE_ORDER == __BIG_ENDIAN
unsigned int ip_v:4; /* version */
unsigned int ip_hl:4; /* header length */
#else
#error "Byte ordering ot specified "
#endif
uint8_t ip_tos; /* type of service */
uint16_t ip_len; /* total length */
uint16_t ip_id; /* identification */
uint16_t ip_off; /* fragment offset field */
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
uint8_t ip_ttl; /* time to live */
uint8_t ip_p; /* protocol */
uint16_t ip_sum; /* checksum */
uint32_t ip_src, ip_dst; /* source and dest address */
} __attribute__ ((packed)) ;
typedef struct sr_ip_hdr sr_ip_hdr_t;
/*
* Ethernet packet header prototype. Too many O/S's define this differently.
* Easy enough to solve that and define it here.
*/
struct sr_ethernet_hdr
{
#ifndef ETHER_ADDR_LEN
#define ETHER_ADDR_LEN 6
#endif
uint8_t ether_dhost[ETHER_ADDR_LEN]; /* destination ethernet address */
uint8_t ether_shost[ETHER_ADDR_LEN]; /* source ethernet address */
uint16_t ether_type; /* packet type ID */
} __attribute__ ((packed)) ;
typedef struct sr_ethernet_hdr sr_ethernet_hdr_t;
enum sr_icmp_type {
icmp_echo_reply_type = 0x0000,
icmp_unreachable_type = 0x0003,
icmp_echo_req_type = 0x0008,
icmp_time_exceeded_type = 0x000b,
};
enum sr_icmp_code{
icmp_net_unreachable = 0x0000,
icmp_host_unreachable = 0x0001,
icmp_port_unreachable = 0x0003
};
enum sr_ip_protocol {
ip_protocol_icmp = 0x0001,
ip_protocol_tcp = 0x0006,
ip_protocol_udp = 0x0011
};
enum sr_ethertype {
ethertype_arp = 0x0806,
ethertype_ip = 0x0800,
};
enum sr_arp_opcode {
arp_op_request = 0x0001,
arp_op_reply = 0x0002,
};
enum sr_arp_hrd_fmt {
arp_hrd_ethernet = 0x0001,
};
struct sr_arp_hdr
{
unsigned short ar_hrd; /* format of hardware address */
unsigned short ar_pro; /* format of protocol address */
unsigned char ar_hln; /* length of hardware address */
unsigned char ar_pln; /* length of protocol address */
unsigned short ar_op; /* ARP opcode (command) */
unsigned char ar_sha[ETHER_ADDR_LEN]; /* sender hardware address */
uint32_t ar_sip; /* sender IP address */
unsigned char ar_tha[ETHER_ADDR_LEN]; /* target hardware address */
uint32_t ar_tip; /* target IP address */
} __attribute__ ((packed)) ;
typedef struct sr_arp_hdr sr_arp_hdr_t;
#define sr_IFACE_NAMELEN 32
#endif /* -- SR_PROTOCOL_H -- */