CS43 Lab7 - A Router Stack
1.2019
|
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"
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_if * | router_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_rt * | rtable_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... | |
The set of functions which accept, queue, and send packets; Most of your work will go here.
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.
my_address | the router interface's MAC address |
addr_s | the MAC address we would like to check against |
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.
sr | the router instance |
packet | the raw ARP packet |
len | the size of the raw ARP packet |
interface | the name of the interface which the packet arrived on |
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.
sr | the router instance |
req | the ARP request to be resolved |
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.
sr | the router instance |
packet | the raw IP packet |
len | the size of the raw IP packet |
interface | the name of the interface which the packet arrived on |
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.
router | the router instance |
dst | the destination address |
src | the source address |
ip_packet | the IP packet which triggered the creation of this ICMP packet, if any |
len | the length of the IP packet, if any |
type | type of ICMP packet to be created |
code | code of ICMP packet to be created |
id | id of the ICMP packet |
seq | sequence number of the ICMP 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.
router | the router instance |
dst | the destination address |
proto | the protocol of the encapsulated buffer |
payload | buffer to encapsulate with IP header |
len | size of the buffer |
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.
sr_instance | the router instance |
dst | the destination address |
src | the source address |
proto | the protocol of the encapsulated buffer |
buf | buffer to encapsulate with IP header |
len | size of the 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.
sr_instance | the router instance |
uint32_t | the destination address in question |
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.
router | the router instance |
rti | the routing table |
intf | the outgoing interface |
type | the type of ethernet traffic |
payload | the packet to be encapsulated in an ethernet frame |
payload_len | the size of the payload |
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.
router | the router instance |
dst_ip | the destination IP address |
type | the type of ethernet traffic |
payload | the packet to be encapsulated in an ethernet frame |
len | the size of the payload |
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.