CS43 Lab7 - A Router Stack  1.2019
sr_protocol.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1998, 1999, 2000 Mike D. Schiffman <mike@infonexus.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 /** \file
29  * A collection of macros, structs, and enums to represent packet types and
30  * headers.
31  */
32 
33 #ifndef SR_PROTOCOL_H
34 #define SR_PROTOCOL_H
35 
36 #ifdef _LINUX_
37 #include <stdint.h>
38 #endif /* _LINUX_ */
39 
40 #include <sys/types.h>
41 #include <arpa/inet.h>
42 
43 
44 #ifndef IP_MAXPACKET
45 #define IP_MAXPACKET 65535
46 #endif
47 
48 
49 #ifndef __LITTLE_ENDIAN
50 #define __LITTLE_ENDIAN 1
51 #endif
52 
53 #ifndef __BIG_ENDIAN
54 #define __BIG_ENDIAN 2
55 #endif
56 
57 #ifndef __BYTE_ORDER
58  #ifdef _CYGWIN_
59  #define __BYTE_ORDER __LITTLE_ENDIAN
60  #endif
61  #ifdef _LINUX_
62  #define __BYTE_ORDER __LITTLE_ENDIAN
63  #endif
64  #ifdef _SOLARIS_
65  #define __BYTE_ORDER __BIG_ENDIAN
66  #endif
67  #ifdef _DARWIN_
68  #define __BYTE_ORDER __BIG_ENDIAN
69  #endif
70 #endif
71 #define ICMP_DATA_SIZE 28
72 #define ICMP_ZERO_HEADER_SIZE 10
73 #define ICMP_ELEVEN_HEADER_SIZE 36
74 
75 
76 /** General struct of a ICMP header.
77  * See <a href="https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol">here</a> for more details.
78  */
79 struct sr_icmp_hdr {
80  uint8_t icmp_type; /**< icmp type*/
81  uint8_t icmp_code; /**< icmp code, if needed*/
82  uint16_t icmp_sum; /**< icmp checksum*/
83  uint16_t icmp_id; /**< icmp identifier, you shouldn't need to use this*/
84  uint16_t icmp_seq; /**< icmp sequence number, you shouldn't need to use this*/
85 
86 } __attribute__ ((packed)) ;
87 typedef struct sr_icmp_hdr sr_icmp_hdr_t;
88 
89 /** Struct of a Type-0 (echo reply) ICMP header.
90  * See <a href="https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol">here</a> for more details.
91  */
93  uint8_t icmp_type;
94  uint8_t icmp_code;
95  uint16_t icmp_sum;
96  uint16_t icmp_id;
97  uint16_t icmp_seq;
98  uint16_t timestamp;
99  uint16_t data[ICMP_DATA_SIZE/2];
100 } __attribute__ ((packed)) ;
101 typedef struct sr_icmp_t0_hdr sr_icmp_t0_hdr_t;
102 
103 /** Struct of a Type-3 (destination unreachable) ICMP header.
104  * See <a href="https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol">here</a> for more details.
105  */
107  uint8_t icmp_type;
108  uint8_t icmp_code;
109  uint16_t icmp_sum;
110  uint16_t unused;
111  uint16_t next_mtu;
112  uint8_t data[ICMP_DATA_SIZE];
113 
114 } __attribute__ ((packed)) ;
115 typedef struct sr_icmp_t3_hdr sr_icmp_t3_hdr_t;
116 
117 /** Struct of a Type-11 (TTL exceeded) ICMP header.
118  * See <a href="https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol">here</a> for more details.
119  */
121  uint8_t icmp_type;
122  uint8_t icmp_code;
123  uint16_t icmp_sum;
124  uint32_t unused;
125  uint32_t data[ICMP_DATA_SIZE/4];
126 } __attribute__ ((packed)) ;
127 typedef struct sr_icmp_t11_hdr sr_icmp_t11_hdr_t;
128 
129 
130 /** Struct of an IP header, without options
131  */
132 struct sr_ip_hdr
133  {
134 #if __BYTE_ORDER == __LITTLE_ENDIAN
135  unsigned int ip_hl:4; /**< header length*/
136  unsigned int ip_v:4; /**< version (always IPv4) */
137 #elif __BYTE_ORDER == __BIG_ENDIAN
138  unsigned int ip_v:4; /**< version (always IPv4) */
139  unsigned int ip_hl:4; /**< header length*/
140 #else
141 #error "Byte ordering not specified "
142 #endif
143  uint8_t ip_tos; /**< type of service */
144  uint16_t ip_len; /**< total length */
145  uint16_t ip_id; /**< identification */
146  uint16_t ip_off; /**< fragment offset field */
147 #define IP_RF 0x8000 /**< reserved fragment flag */
148 #define IP_DF 0x4000 /**< dont fragment flag */
149 #define IP_MF 0x2000 /**< more fragments flag */
150 #define IP_OFFMASK 0x1fff /**< mask for fragmenting bits */
151  uint8_t ip_ttl; /**< time to live */
152  uint8_t ip_p; /**< protocol of encapsulated packet*/
153  uint16_t ip_sum; /**< IP checksum */
154  uint32_t ip_src; /**< src IP address */
155  uint32_t ip_dst; /**< dest IP address */
156  } __attribute__ ((packed)) ;
157 typedef struct sr_ip_hdr sr_ip_hdr_t;
158 
159 /** Struct of an Ethernet Header
160  */
162 {
163 #ifndef ETHER_ADDR_LEN
164 #define ETHER_ADDR_LEN 6
165 #endif
166  uint8_t ether_dhost[ETHER_ADDR_LEN]; /**< destination ethernet address */
167  uint8_t ether_shost[ETHER_ADDR_LEN]; /**< source ethernet address */
168  uint16_t ether_type; /**< type (protocol) of encapsulated packet */
169 } __attribute__ ((packed)) ;
170 typedef struct sr_ethernet_hdr sr_ethernet_hdr_t;
171 
172 /* Types */
173 #define ICMP_TYPE_ECHO_REPLY 0
174 #define ICMP_TYPE_DEST_UNREACH 3
175 #define ICMP_TYPE_ECHO_REQUEST 8
176 #define ICMP_TYPE_TIME_EXCEEDED 11
177 
178 /* Codes */
179 #define ICMP_CODE_NET_UNREACH 0
180 #define ICMP_CODE_HOST_UNREACH 1
181 #define ICMP_CODE_PROTO_UNREACH 2
182 #define ICMP_CODE_PORT_UNREACH 3
183 #define ICMP_CODE_TTL_EXPIRED 0
184 
185 /* IP constants */
186 #define IP_PROTO_ICMP 0x0001
187 #define IPV4_HEADER_LEN 20
188 #define ICMP_HEADER_LEN 8
189 
190 /** Enumeration of useful codes for protocols encapsulated in IP */
192  ip_protocol_icmp = 0x0001,
193 };
194 
195 /** Enumeration of useful codes for protocols encapsulated in Ethernet */
197  ethertype_arp = 0x0806,
198  ethertype_ip = 0x0800,
199 };
200 
201 /** Enumeration of useful codes for ARP operations */
203  arp_op_request = 0x0001,
204  arp_op_reply = 0x0002,
205 };
206 
207 /** Enumeration of useful hardware format codes for ARP */
209  arp_hrd_ethernet = 0x0001,
210 };
211 
212 /** Struct of an ARP header */
214 {
215  unsigned short ar_hrd; /**< format of hardware address */
216  unsigned short ar_pro; /**< format of protocol address */
217  unsigned char ar_hln; /**< length of hardware address */
218  unsigned char ar_pln; /**< length of protocol address */
219  unsigned short ar_op; /**< ARP opcode (command) */
220  unsigned char ar_sha[ETHER_ADDR_LEN]; /**< sender hardware address */
221  uint32_t ar_sip; /**< sender IP address */
222  unsigned char ar_tha[ETHER_ADDR_LEN]; /**< target hardware address */
223  uint32_t ar_tip; /**< target IP address */
224 } __attribute__ ((packed)) ;
225 typedef struct sr_arp_hdr sr_arp_hdr_t;
226 
227 #define sr_IFACE_NAMELEN 32
228 
229 #endif /* -- SR_PROTOCOL_H -- */
sr_arp_hrd_fmt
Enumeration of useful hardware format codes for ARP.
Definition: sr_protocol.h:208
Struct of an ARP header.
Definition: sr_protocol.h:213
sr_ip_protocol
Enumeration of useful codes for protocols encapsulated in IP.
Definition: sr_protocol.h:191
uint32_t ip_src
src IP address
Definition: sr_protocol.h:154
uint8_t ip_ttl
time to live
Definition: sr_protocol.h:151
uint16_t icmp_id
icmp identifier, you shouldn&#39;t need to use this
Definition: sr_protocol.h:83
uint16_t ip_id
identification
Definition: sr_protocol.h:145
uint16_t ip_off
fragment offset field
Definition: sr_protocol.h:146
uint32_t ar_sip
sender IP address
Definition: sr_protocol.h:221
uint32_t ip_dst
dest IP address
Definition: sr_protocol.h:155
unsigned char ar_sha[ETHER_ADDR_LEN]
sender hardware address
Definition: sr_protocol.h:311
uint16_t ip_sum
IP checksum.
Definition: sr_protocol.h:153
uint8_t ip_tos
type of service
Definition: sr_protocol.h:143
uint16_t icmp_seq
icmp sequence number, you shouldn&#39;t need to use this
Definition: sr_protocol.h:84
unsigned char ar_tha[ETHER_ADDR_LEN]
target hardware address
Definition: sr_protocol.h:313
Struct of a Type-11 (TTL exceeded) ICMP header.
Definition: sr_protocol.h:120
sr_arp_opcode
Enumeration of useful codes for ARP operations.
Definition: sr_protocol.h:202
uint16_t ip_len
total length
Definition: sr_protocol.h:144
Struct of an Ethernet Header.
Definition: sr_protocol.h:161
uint8_t icmp_code
icmp code, if needed
Definition: sr_protocol.h:81
General struct of a ICMP header.
Definition: sr_protocol.h:79
unsigned short ar_pro
format of protocol address
Definition: sr_protocol.h:216
sr_ethertype
Enumeration of useful codes for protocols encapsulated in Ethernet.
Definition: sr_protocol.h:196
uint8_t ether_shost[ETHER_ADDR_LEN]
source ethernet address
Definition: sr_protocol.h:310
Struct of an IP header, without options.
Definition: sr_protocol.h:132
unsigned char ar_pln
length of protocol address
Definition: sr_protocol.h:218
uint32_t ar_tip
target IP address
Definition: sr_protocol.h:223
uint16_t icmp_sum
icmp checksum
Definition: sr_protocol.h:82
unsigned char ar_hln
length of hardware address
Definition: sr_protocol.h:217
unsigned short ar_op
ARP opcode (command)
Definition: sr_protocol.h:219
unsigned short ar_hrd
format of hardware address
Definition: sr_protocol.h:215
uint8_t icmp_type
icmp type
Definition: sr_protocol.h:80
Struct of a Type-3 (destination unreachable) ICMP header.
Definition: sr_protocol.h:106
Struct of a Type-0 (echo reply) ICMP header.
Definition: sr_protocol.h:92
uint8_t ether_dhost[ETHER_ADDR_LEN]
destination ethernet address
Definition: sr_protocol.h:309
uint16_t ether_type
type (protocol) of encapsulated packet
Definition: sr_protocol.h:168
uint8_t ip_p
protocol of encapsulated packet
Definition: sr_protocol.h:152