-
Notifications
You must be signed in to change notification settings - Fork 0
/
sr_protocol.h
238 lines (205 loc) · 7.33 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
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright (c) 1998, 1999, 2000 Mike D. Schiffman <mike@infonexus.com>
* 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_id;
uint16_t icmp_sn;
} __attribute__ ((packed)) ;
typedef struct sr_icmp_hdr sr_icmp_hdr_t;
enum sr_icmp_type {
icmp_echo_reply = 0x0000,
icmp_echo_request = 0x0008,
};
/* 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;
enum sr_icmp_t3_type {
icmp_t3_dest_unreachable = 0x0003,
icmp_t3_time_exceeded = 0x000B,
icmp_t3_traceroute = 0x0030,
};
enum sr_icmp_t3_code {
net_unreachable = 0x0000,
host_unreachable = 0x0001,
port_unreachable = 0x0003,
};
/*
* 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_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
/*
* TCP packet header
*/
struct sr_tcp_hdr {
uint16_t tcp_src_port, tcp_dst_port; /* Source, Destination Port Numbers */
uint32_t tcp_seq_num; /* Sequence Number */
uint32_t tcp_ack_num; /* Acknowledgment Number */
unsigned int offset:4; /* Data Offset */
unsigned int reserved:3; /* Reserved */
unsigned int ecn:3; /* Explicit Congestion Notification */
unsigned int control:6; /* Control Bits */
#define TCP_FIN 0x01 /* FIN: No more data from sender */
#define TCP_SYN 0x02 /* SYN: Synchronize sequence numbers */
#define TCP_RST 0x04 /* RST: Reset the connection */
#define TCP_PSH 0x08 /* PSH: Push Function */
#define TCP_ACK 0x10 /* ACK: Acknowledgment field significant */
#define TCP_URG 0x20 /* URG: Urgent Pointer field significant 0x64 */
#define TCP_SYN_ACK 0x12 /* SYN-ACK: Synchronize sequence numbers */
uint16_t tcp_win; /* Window */
uint16_t tcp_sum; /* Checksum */
uint16_t tcp_urg; /* Urgent Pointer */
/* Options and Padding */
/* Data */
} __attribute__ ((packed)) ;
typedef struct sr_tcp_hdr sr_tcp_hdr_t;
/*
* TCP pseudo header (for TCP checksum)
*/
struct sr_tcp_pseudo_hdr {
uint32_t ip_src, ip_dst;
uint8_t reserved;
uint8_t protocol;
uint32_t tcp_length;
} __attribute__ ((packed)) ;
typedef struct sr_tcp_pseudo_hdr sr_tcp_psdo_hdr_t;
#endif /* -- SR_PROTOCOL_H -- */