CS43 Lab7 - A Router Stack  1.2019
Functions
sr_router.c File Reference

The set of functions which accept, queue, and send packets; Most of your work will go here. More...

#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "sr_if.h"
#include "sr_rt.h"
#include "sr_router.h"
#include "sr_protocol.h"
#include "sr_arpcache.h"
#include "sr_utils.h"
Include dependency graph for sr_router.c:

Functions

void sr_init (struct sr_instance *sr)
 Initializes router state.
 
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...
 
void sr_handlepacket (struct sr_instance *sr, uint8_t *packet, unsigned int len, char *interface)
 Called whenever an incoming packet is received; You are responsible for writing this. 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.
 
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...
 
struct sr_ifrouter_lookup_interface_via_ip (struct sr_instance *sr, uint32_t dst)
 
int ip_send_packet (struct sr_instance *router, uint32_t dst, uint8_t proto_id, 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 ip_to_me (struct sr_instance *sr, uint32_t dst)
 Helper function to determine whether an IP packet is destined for any of the router's interfaces' addresses. More...
 
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.
 
uint16_t checksum_icmp (sr_icmp_hdr_t *icmp_hdr, unsigned total_len)
 Computes and sets the checksum on an ICMP header.
 
int ip_send_packet_from (struct sr_instance *router, uint32_t dst, uint32_t src, uint8_t proto_id, uint8_t *buf, unsigned len)
 Creates an IP header and encapsulates the given buffer; You are responsible for writing this. More...
 
struct sr_rtrtable_find_route (struct sr_instance *sr, uint32_t dst_ip)
 Given a destination IP address, looks up the routing table entry for the next hop; This is already implemented for you.
 
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 *sr, 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...
 
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...
 

Detailed Description

The set of functions which accept, queue, and send packets; Most of your work will go here.

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.