15 #include <netinet/in.h> 23 #define min(a,b) ( (a) < (b) ? (a) : (b) ) 27 #define Debug(x, args...) printf(x, ## args) 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) 32 #define Debug(x, args...) do{}while(0) 33 #define DebugMAC(x) do{}while(0) 37 #define PACKET_DUMP_SIZE 1024 40 #define ETH_HEADER_LEN 14 43 #define ETH_MAX_DATA_LEN 2048 46 #define ETH_MAX_LEN (ETH_HEADER_LEN + ETH_MAX_DATA_LEN) 49 #define ETH_MIN_LEN 60 52 #define IPV4_HEADER_LEN 20 65 unsigned short topo_id;
66 struct sockaddr_in sr_addr;
67 struct sr_if* if_list;
75 int sr_verify_routing_table(
struct sr_instance* sr);
79 int sr_connect_to_server(
struct sr_instance* ,
unsigned short ,
char* );
100 int ether_to_me(
unsigned char* my_address,
unsigned char* addr_s);
126 uint16_t
checksum( uint16_t* buf,
unsigned len );
226 unsigned payload_len ) ;
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* );
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
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'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 'len' 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's interfaces' 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