CS43 Lab7 - A Router Stack  1.2019
Classes | Macros | Functions | Variables
sr_router.h File Reference

The set of functions which routes packets. More...

#include <netinet/in.h>
#include <sys/time.h>
#include <stdio.h>
#include "sr_protocol.h"
#include "sr_arpcache.h"
Include dependency graph for sr_router.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  sr_instance
 Struct which encapsulates all the state of a single router. More...
 

Macros

#define min(a, b)   ( (a) < (b) ? (a) : (b) )
 
#define Debug(x, args...)   do{}while(0)
 
#define DebugMAC(x)   do{}while(0)
 
#define INIT_TTL   255
 
#define PACKET_DUMP_SIZE   1024
 
#define ETH_HEADER_LEN   14
 number of uint8_ts in the Ethernet header (dst MAC + src MAC + type)
 
#define ETH_MAX_DATA_LEN   2048
 max uint8_ts in the payload (usually 1500B, but jumbo may be larger
 
#define ETH_MAX_LEN   (ETH_HEADER_LEN + ETH_MAX_DATA_LEN)
 max uint8_ts in the Ethernet frame (header + data uint8_ts)
 
#define ETH_MIN_LEN   60
 min uint8_ts in the Ethernet frame
 
#define IPV4_HEADER_LEN   20
 number of uint8_ts in an IPv4 header without options. More...
 

Functions

int sr_verify_routing_table (struct sr_instance *sr)
 
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. More...
 
int sr_connect_to_server (struct sr_instance *, unsigned short, char *)
 
int sr_read_from_server (struct sr_instance *)
 
void sr_init (struct sr_instance *)
 Initializes router state.
 
void sr_handlepacket (struct sr_instance *, uint8_t *, unsigned int, char *)
 Called whenever an incoming packet is received; You are responsible for writing this. More...
 
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. More...
 
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. More...
 
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. More...
 
uint16_t checksum_ip (struct sr_ip_hdr *hdr)
 Helper function which computes and sets the checksum on an IP header.
 
uint16_t checksum (uint16_t *buf, unsigned len)
 Helper function which computes the checksum over the specified length of the given buffer.
 
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' addresses. More...
 
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 this. More...
 
uint16_t checksum_icmp (sr_icmp_hdr_t *icmp_hdr, unsigned total_len)
 Computes and sets the checksum on an ICMP header.
 
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.
 
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. More...
 
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 already implemented for you. More...
 
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 destination IP, then queues the packet; This is already implemented for you. More...
 
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 be sent immediately, otherwise it must be queued while the ARP cache resolves the next hop IP address's MAC address; You are responsible for writing this. More...
 
struct sr_rtrtable_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 implemented for you.
 
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. More...
 
void sr_add_interface (struct sr_instance *, const char *)
 
void sr_set_ether_ip (struct sr_instance *, uint32_t)
 
void sr_set_ether_addr (struct sr_instance *, const unsigned char *)
 
void sr_print_if_list (struct sr_instance *)
 Prints the interface list.
 

Variables

struct sr_instance __attribute__
 

Detailed Description

The set of functions which routes packets.

Macro Definition Documentation

◆ IPV4_HEADER_LEN

#define IPV4_HEADER_LEN   20

number of uint8_ts in an IPv4 header without options.

Function Documentation

◆ ether_to_me()

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.

Parameters
my_addressthe router interface's MAC address
addr_sthe MAC address we would like to check against
Returns
Nonzero (true) if the addresses match, or the second argument is the broadcast address. Zero (false) otherwise.

◆ handle_arp()

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.

Parameters
srthe router instance
packetthe raw ARP packet
lenthe size of the raw ARP packet
interfacethe name of the interface which the packet arrived on

◆ handle_arpreq()

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.

Parameters
srthe router instance
reqthe ARP request to be resolved

◆ handle_ip()

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.

Parameters
srthe router instance
packetthe raw IP packet
lenthe size of the raw IP packet
interfacethe name of the interface which the packet arrived on

◆ icmp_send()

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 this.

Parameters
routerthe router instance
dstthe destination address
srcthe source address
ip_packetthe IP packet which triggered the creation of this ICMP packet, if any
lenthe length of the IP packet, if any
typetype of ICMP packet to be created
codecode of ICMP packet to be created
idid of the ICMP packet
seqsequence number of the ICMP packet

◆ ip_send_packet()

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 already implemented for you.

Parameters
routerthe router instance
dstthe destination address
protothe protocol of the encapsulated buffer
payloadbuffer to encapsulate with IP header
lensize of the buffer

◆ ip_send_packet_from()

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.

Parameters
sr_instancethe router instance
dstthe destination address
srcthe source address
protothe protocol of the encapsulated buffer
bufbuffer to encapsulate with IP header
lensize of the buffer

◆ ip_to_me()

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' addresses.

Parameters
sr_instancethe router instance
uint32_tthe destination address in question

◆ router_queue_ethernet_frame()

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 be sent immediately, otherwise it must be queued while the ARP cache resolves the next hop IP address's MAC address; You are responsible for writing this.

Parameters
routerthe router instance
rtithe routing table
intfthe outgoing interface
typethe type of ethernet traffic
payloadthe packet to be encapsulated in an ethernet frame
payload_lenthe size of the payload

◆ router_send_ethernet_frame()

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 destination IP, then queues the packet; This is already implemented for you.

Parameters
routerthe router instance
dst_ipthe destination IP address
typethe type of ethernet traffic
payloadthe packet to be encapsulated in an ethernet frame
lenthe size of the payload

◆ sr_handlepacket()

void sr_handlepacket ( struct sr_instance ,
uint8_t *  ,
unsigned  int,
char *   
)

Called whenever an incoming packet is received; You are responsible for writing this.

This function checks that a received packet is well structured, decapsulates it, and then calls another handler based on the packet type.

◆ sr_send_packet()

int sr_send_packet ( struct sr_instance sr,
uint8_t *  buf,
unsigned int  len,
const char *  iface 
)

Send a packet (ethernet header included!) of length 'len' onto the wire.

Parameters
srthe router instance
bufthe ethernet frame to be sent
lenthe size of the ethernet frame
ifacethe name of the outgoing interface