-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsr_protocol.h
executable file
·195 lines (170 loc) · 5.73 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
/*
* 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
#ifdef _CYGWIN_
#ifndef __BYTE_ORDER
#define __BYTE_ORDER __LITTLE_ENDIAN
#endif
#endif
#ifdef _LINUX_
#ifndef __BYTE_ORDER
#define __BYTE_ORDER __LITTLE_ENDIAN
#endif
#endif
#ifdef _SOLARIS_
#ifndef __BYTE_ORDER
#define __BYTE_ORDER __BIG_ENDIAN
#endif
#endif
#ifdef _DARWIN_
#ifndef __BYTE_ORDER
#define __BYTE_ORDER __BIG_ENDIAN
#endif
#endif
/*
* Structure of an internet header, naked of options.
*/
struct ip
{
#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 */
struct in_addr ip_src, ip_dst; /* source and dest address */
} __attribute__ ((packed)) ;
/*
* 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)) ;
#ifndef ARPHDR_ETHER
#define ARPHDR_ETHER 1
#endif
/* IP Protocol Types */
#ifndef IPPROTO_TCP
#define IPPROTO_TCP 6 /* TCP protocol */
#endif
#ifndef IPPROTO_UDP
#define IPPROTO_UDP 17 /* TCP protocol */
#endif
#ifndef IPPROTO_ICMP
#define IPPROTO_ICMP 0x0001 /* ICMP protocol */
#endif
/* Ethernet Types */
#ifndef ETHERTYPE_IP
#define ETHERTYPE_IP 0x0800 /* IP protocol */
#endif
#ifndef ETHERTYPE_ARP
#define ETHERTYPE_ARP 0x0806 /* Addr. resolution protocol */
#endif
/* ARP Types */
#define ARP_REQUEST 1
#define ARP_REPLY 2
/* ICMP Types */
#define ICMP_ECHO_REPLY 0
#define ICMP_ECHO_REQUEST 8
#define ICMP_TTL_EXPIRED 11
struct sr_arphdr
{
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)) ;
struct icmp
{
uint8_t icmp_type; /* type of icmp packet*/
uint8_t icmp_code; /* numeric specification of icmp type */
uint16_t icmp_sum; /* checksum for error checking*/
uint16_t icmp_id; /* identification */
uint16_t icmp_seq; /* sequence value */
} __attribute__ ((packed)) ;
struct etherARP
{
struct sr_ethernet_hdr ether; /* Ethernet Header Struct */
struct sr_arphdr arp; /* ARP Header Struct */
} __attribute__ ((packed)) ;
struct etherICMP
{
struct sr_ethernet_hdr ether_hdr; /* Ethernet Header Struct */
struct ip ip_hdr; /* IP Header Struct */
struct icmp icmp_hdr; /* ICMP Header Struct */
uint32_t padding[8]; /* Padding bits for */
} __attribute__ ((packed)) ;
#endif /* -- SR_PROTOCOL_H -- */