-
Notifications
You must be signed in to change notification settings - Fork 25
Tip: Figuring out the source IP address to use
When sending out a packet, the TCP layer has to figure out its own source IP address, because a host may have multiple IP addresses. For example, if an end-host has an Ethernet NIC and a WiFi NIC, it has two IP addresses, each corresponding to a NIC, respsectively. In such a case, the TCP layer needs to choose, among the two IP addresses, which IP address to use as the source IP address for outgoing packets. Given a destination address, the IP layer decides which outgoing NIC port to use and thus the source IP address, depending on the routing table. The TCP layer must refer to the IP layer for this information.
In KENS, you do not need to implement an IP layer routing protocol or set up routing manually.
KENS offers a prebuilt routing table and the longest prefix matching algorithm.
You just call RoutingInfoInterface
class to determine the source IP address, its corresponding outgoing NIC port, and its MAC address.
Use the getRoutingTable
method to determine which NIC port to use for a specific destination IP adress.
// Inside TCPAssignment class
ipv4_t ip = /* destination IP address */ ;
int port = getRoutingTable(ip); // retrieve NIC port
Look up the ARP table to obtain a MAC address for an IP address. Check std::optional to learn how to use a return value. This method is used not in the TCP layer, but in the IP layer.
// Inside TCPAssignment class
ipv4_t ip = /* some IP address */ ;
std::optional<mac_t> mac = getARPTable(ip); // retrieve MAC address
You can retrieve a specific port's IP and MAC addresses. Check std::optional to learn how to use a return value.
// Inside TCPAssignment class
int port = /* NIC port number */ ;
std::optional<ipv4_t> ip = getIPAddr(port); // retrieve the source IP address
std::optional<mac_t> mac = getMACAddr(port); // retrieve the source MAC address; used only in the IP layer