CS43 Lab7 - A Router Stack
1.2019
|
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"
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_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 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__ |
The set of functions which routes packets.
#define IPV4_HEADER_LEN 20 |
number of uint8_ts in an IPv4 header without options.
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.
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.
sr | the router instance |
buf | the ethernet frame to be sent |
len | the size of the ethernet frame |
iface | the name of the outgoing interface |