CS43 Lab7 - A Router Stack  1.2019
sr_router.h
Go to the documentation of this file.
1 /*-----------------------------------------------------------------------------
2  * File: sr_router.h
3  * Date: ?
4  * Authors: Guido Apenzeller, Martin Casado, Virkam V.
5  * Contact: casado@stanford.edu
6  *---------------------------------------------------------------------------*/
7 
8 /** \file
9  * The set of functions which routes packets
10  */
11 
12 #ifndef SR_ROUTER_H
13 #define SR_ROUTER_H
14 
15 #include <netinet/in.h>
16 #include <sys/time.h>
17 #include <stdio.h>
18 
19 #include "sr_protocol.h"
20 #include "sr_arpcache.h"
21 
22 
23 #define min(a,b) ( (a) < (b) ? (a) : (b) )
24 
25 /* we dont like this debug , but what to do for varargs ? */
26 #ifdef _DEBUG_
27 #define Debug(x, args...) printf(x, ## args)
28 #define DebugMAC(x) \
29  do { int ivyl; for(ivyl=0; ivyl<5; ivyl++) printf("%02x:", \
30  (unsigned char)(x[ivyl])); printf("%02x",(unsigned char)(x[5])); } while (0)
31 #else
32 #define Debug(x, args...) do{}while(0)
33 #define DebugMAC(x) do{}while(0)
34 #endif
35 
36 #define INIT_TTL 255
37 #define PACKET_DUMP_SIZE 1024
38 
39 /** number of uint8_ts in the Ethernet header (dst MAC + src MAC + type) */
40 #define ETH_HEADER_LEN 14
41 
42 /** max uint8_ts in the payload (usually 1500B, but jumbo may be larger */
43 #define ETH_MAX_DATA_LEN 2048
44 
45 /** max uint8_ts in the Ethernet frame (header + data uint8_ts) */
46 #define ETH_MAX_LEN (ETH_HEADER_LEN + ETH_MAX_DATA_LEN)
47 
48 /** min uint8_ts in the Ethernet frame */
49 #define ETH_MIN_LEN 60
50 
51 /** number of uint8_ts in an IPv4 header without options. */
52 #define IPV4_HEADER_LEN 20
53 
54 /* forward declare */
55 struct sr_if;
56 struct sr_rt;
57 
58 /** Struct which encapsulates all the state of a single router */
60 {
61  int sockfd; /* socket to server */
62  char user[32]; /* user name */
63  char host[32]; /* host name */
64  char template[30]; /* template name if any */
65  unsigned short topo_id;
66  struct sockaddr_in sr_addr; /* address to server */
67  struct sr_if* if_list; /** list of interfaces */
68  struct sr_rt* routing_table; /** routing table */
69  struct sr_arpcache cache; /** ARP cache */
70  pthread_attr_t attr;
71  FILE* logfile;
72 };
73 
74 /* -- sr_main.c -- */
75 int sr_verify_routing_table(struct sr_instance* sr);
76 
77 /* -- sr_vns_comm.c -- */
78 int sr_send_packet(struct sr_instance* , uint8_t* , unsigned int , const char*);
79 int sr_connect_to_server(struct sr_instance* ,unsigned short , char* );
80 int sr_read_from_server(struct sr_instance* );
81 
82 /* -- sr_router.c -- */
83 /** Initializes router state */
84 void sr_init(struct sr_instance* );
85 
86 /** Called whenever an incoming packet is received; You are
87  * responsible for writing this.
88  * This function checks that a received packet is well
89  * structured, decapsulates it, and then calls another handler based on the
90  * packet type. */
91 void sr_handlepacket(struct sr_instance* , uint8_t * , unsigned int , char* );
92 
93 /** Determine if an ethernet frame is addressed to a given interface's MAC
94  * address.
95  * @param my_address the router interface's MAC address
96  * @param addr_s the MAC address we would like to check against
97  * @return Nonzero (true) if the addresses match, or the second argument is the
98  * broadcast address. Zero (false) otherwise.
99  */
100 int ether_to_me(unsigned char* my_address, unsigned char* addr_s);
101 
102 /** Handles all incoming packets with an ARP header; You are responsible
103  * for writing this.
104  * @param sr: the router instance
105  * @param packet: the raw ARP packet
106  * @param len: the size of the raw ARP packet
107  * @param interface: the name of the interface which the packet arrived on
108  * */
109 int handle_arp(struct sr_instance * sr, uint8_t * packet, unsigned int len, char * interface);
110 
111 /** Handles all incoming packets with an IP header; You are responsible for writing this.
112  * @param sr: the router instance
113  * @param packet: the raw IP packet
114  * @param len: the size of the raw IP packet
115  * @param interface: the name of the interface which the packet arrived on
116  * */
117 int handle_ip(struct sr_instance *sr, uint8_t * packet,unsigned int len,char * interface);
118 
119 /** Helper function which computes and sets the checksum on an IP header.
120  * */
121 uint16_t checksum_ip( struct sr_ip_hdr * hdr );
122 
123 /** Helper function which computes the checksum over the specified length of
124  * the given buffer.
125  * */
126 uint16_t checksum( uint16_t* buf, unsigned len );
127 
128 /** Helper function to determine whether an IP packet is destined for any of the
129  * router's interfaces' addresses.
130  * @param sr_instance: the router instance
131  * @param uint32_t: the destination address in question
132  */
133 int ip_to_me(struct sr_instance * , uint32_t );
134 
135 /** Creates an ICMP header and encapsulates the given buffer (if
136  * any); You are responsible for writing this.
137  * @param router: the router instance
138  * @param dst: the destination address
139  * @param src: the source address
140  * @param ip_packet: the IP packet which triggered the creation of this ICMP packet, if any
141  * @param len: the length of the IP packet, if any
142  * @param type: type of ICMP packet to be created
143  * @param code: code of ICMP packet to be created
144  * @param id: id of the ICMP packet
145  * @param seq: sequence number of the ICMP packet
146  * */
147 void icmp_send(struct sr_instance * router,
148  uint32_t dst,
149  uint32_t src,
150  uint8_t * ip_packet,
151  unsigned len,
152  uint8_t type,
153  uint8_t code,
154  uint16_t id,
155  uint16_t seq);
156 
157 /** Computes and sets the checksum on an ICMP header.
158  * */
159 uint16_t checksum_icmp( sr_icmp_hdr_t* icmp_hdr, unsigned total_len );
160 
161 /** Handles all incoming packets with an ICMP header; This is already
162  * implemented for you */
163 void icmp_handle_packet( struct sr_instance * router, uint8_t* ip_packet, unsigned len );
164 
165 /** Creates an IP header and encapsulates the given buffer; You
166  * are responsible for writing this.
167  * @param sr_instance: the router instance
168  * @param dst: the destination address
169  * @param src: the source address
170  * @param proto: the protocol of the encapsulated buffer
171  * @param buf: buffer to encapsulate with IP header
172  * @param len: size of the buffer
173  */
174 int ip_send_packet_from( struct sr_instance *,
175  uint32_t dst,
176  uint32_t src,
177  uint8_t proto,
178  uint8_t* buf,
179  unsigned len );
180 
181 /** Wrapper function for \link ip_send_packet_from \endlink when the outbound
182  * address & interface are not known; This is already implemented for you
183  * @param router: the router instance
184  * @param dst: the destination address
185  * @param proto: the protocol of the encapsulated buffer
186  * @param payload: buffer to encapsulate with IP header
187  * @param len: size of the buffer
188  * */
189 int ip_send_packet(struct sr_instance * router,
190  uint32_t dst,
191  uint8_t proto,
192  uint8_t* payload,
193  unsigned len );
194 
195 /** Searches the routing table to find the correct
196  * interface on which to send the payload based on the destination IP, then
197  * queues the packet; This is already implemented for you
198  * @param router: the router instance
199  * @param dst_ip: the destination IP address
200  * @param type: the type of ethernet traffic
201  * @param payload: the packet to be encapsulated in an ethernet frame
202  * @param len: the size of the payload
203  * */
204 int router_send_ethernet_frame( struct sr_instance * router,
205  uint32_t dst_ip,
206  uint16_t type,
207  uint8_t* payload,
208  unsigned len );
209 
210 /** Constructs an ethernet packet to the correct MAC address -
211  * if this exists in the cache the packet can be sent immediately, otherwise it
212  * must be queued while the ARP cache resolves the next hop IP address's MAC
213  * address; You are responsible for writing this.
214  * @param router: the router instance
215  * @param rti: the routing table
216  * @param intf: the outgoing interface
217  * @param type: the type of ethernet traffic
218  * @param payload: the packet to be encapsulated in an ethernet frame
219  * @param payload_len: the size of the payload
220  */
221 int router_queue_ethernet_frame(struct sr_instance * router,
222  struct sr_rt * rti,
223  struct sr_if * intf,
224  uint16_t type,
225  uint8_t* payload,
226  unsigned payload_len ) ;
227 
228 /** Given a destination IP address, looks up the routing
229  * table entry for the next hop; This is already implemented for you.
230  */
231 struct sr_rt * rtable_find_route(struct sr_instance * sr, uint32_t ip);
232 
233 /** Handles sending ARP requests to get IP/MAC mappings; You are
234  * responsible for writing this.
235  * @param sr: the router instance
236  * @param req: the ARP request to be resolved
237  */
238 int handle_arpreq(struct sr_instance * sr,struct sr_arpreq * req);
239 
240 /* -- sr_if.c -- */
241 void sr_add_interface(struct sr_instance* , const char* );
242 void sr_set_ether_ip(struct sr_instance* , uint32_t );
243 void sr_set_ether_addr(struct sr_instance* , const unsigned char* );
244 void sr_print_if_list(struct sr_instance* );
245 
246 #endif /* SR_ROUTER_H */
This file defines an ARP cache (sr_arpcache), which is made of two structures: an ARP request queue (...
void icmp_handle_packet(struct sr_instance *router, uint8_t *ip_packet, unsigned len)
Handles all incoming packets with an ICMP header; This is already implemented for you...
Definition: sr_router.c:341
Definition: sr_rt.h:29
struct sr_rt * routing_table
list of interfaces
Definition: sr_router.h:68
void icmp_send(struct sr_instance *router, uint32_t dst, uint32_t src, uint8_t *ip_packet, unsigned len, uint8_t type, uint8_t code, uint16_t id, uint16_t seq)
Creates an ICMP header and encapsulates the given buffer (if any); You are responsible for writing th...
Definition: sr_router.c:253
uint16_t checksum(uint16_t *buf, unsigned len)
Helper function which computes the checksum over the specified length of the given buffer...
Definition: sr_router.c:215
int handle_arp(struct sr_instance *sr, uint8_t *packet, unsigned int len, char *interface)
Handles all incoming packets with an ARP header; You are responsible for writing this.
Definition: sr_router.c:126
int router_send_ethernet_frame(struct sr_instance *router, uint32_t dst_ip, uint16_t type, uint8_t *payload, unsigned len)
Searches the routing table to find the correct interface on which to send the payload based on the de...
Definition: sr_router.c:450
struct sr_rt * rtable_find_route(struct sr_instance *sr, uint32_t ip)
Given a destination IP address, looks up the routing table entry for the next hop; This is already im...
Definition: sr_router.c:421
int handle_arpreq(struct sr_instance *sr, struct sr_arpreq *req)
Handles sending ARP requests to get IP/MAC mappings; You are responsible for writing this...
Definition: sr_router.c:531
int ip_send_packet(struct sr_instance *router, uint32_t dst, uint8_t proto, uint8_t *payload, unsigned len)
Wrapper function for ip_send_packet_from when the outbound address & interface are not known; This is...
Definition: sr_router.c:298
int handle_ip(struct sr_instance *sr, uint8_t *packet, unsigned int len, char *interface)
Handles all incoming packets with an IP header; You are responsible for writing this.
Definition: sr_router.c:179
An ARP cache which tracks IP/MAC mappings, as well as outstanding ARP requests.
Definition: sr_arpcache.h:108
uint16_t checksum_ip(struct sr_ip_hdr *hdr)
Helper function which computes and sets the checksum on an IP header.
Definition: sr_router.c:207
void sr_print_if_list(struct sr_instance *)
Prints the interface list.
Definition: sr_if.c:151
An outstanding ARP request with queued packets waiting on the response.
Definition: sr_arpcache.h:92
uint16_t checksum_icmp(sr_icmp_hdr_t *icmp_hdr, unsigned total_len)
Computes and sets the checksum on an ICMP header.
Definition: sr_router.c:383
General struct of a ICMP header.
Definition: sr_protocol.h:79
int router_queue_ethernet_frame(struct sr_instance *router, struct sr_rt *rti, struct sr_if *intf, uint16_t type, uint8_t *payload, unsigned payload_len)
Constructs an ethernet packet to the correct MAC address - if this exists in the cache the packet can...
Definition: sr_router.c:483
Struct of an IP header, without options.
Definition: sr_protocol.h:132
int ether_to_me(unsigned char *my_address, unsigned char *addr_s)
Determine if an ethernet frame is addressed to a given interface&#39;s MAC address.
Definition: sr_router.c:63
int sr_send_packet(struct sr_instance *, uint8_t *, unsigned int, const char *)
Send a packet (ethernet header included!) of length &#39;len&#39; onto the wire.
Definition: sr_vns_comm.c:568
void sr_init(struct sr_instance *)
Initializes router state.
Definition: sr_router.c:41
A collection of macros, structs, and enums to represent packet types and headers. ...
A node in the interface list of the router.
Definition: sr_if.h:38
struct sr_arpcache cache
routing table
Definition: sr_router.h:69
int ip_to_me(struct sr_instance *, uint32_t)
Helper function to determine whether an IP packet is destined for any of the router&#39;s interfaces&#39; add...
Definition: sr_router.c:327
Struct which encapsulates all the state of a single router.
Definition: sr_router.h:59
int ip_send_packet_from(struct sr_instance *, uint32_t dst, uint32_t src, uint8_t proto, uint8_t *buf, unsigned len)
Creates an IP header and encapsulates the given buffer; You are responsible for writing this...
Definition: sr_router.c:399
pthread_attr_t attr
ARP cache.
Definition: sr_router.h:70
void sr_handlepacket(struct sr_instance *, uint8_t *, unsigned int, char *)
Called whenever an incoming packet is received; You are responsible for writing this.
Definition: sr_router.c:81