Sponge
CS144's user-space TCP library
|
A "network interface" that connects IP (the internet layer, or network layer) with Ethernet (the network access layer, or link layer). More...
#include <network_interface.hh>
Public Member Functions | |
NetworkInterface (const EthernetAddress ðernet_address, const Address &ip_address) | |
Construct a network interface with given Ethernet (network-access-layer) and IP (internet-layer) addresses. More... | |
std::queue< EthernetFrame > & | frames_out () |
Access queue of Ethernet frames awaiting transmission. More... | |
std::optional< InternetDatagram > | recv_frame (const EthernetFrame &frame) |
Receives an Ethernet frame and responds appropriately. More... | |
void | send_datagram (const InternetDatagram &dgram, const Address &next_hop) |
Sends an IPv4 datagram, encapsulated in an Ethernet frame (if it knows the Ethernet destination address). More... | |
void | tick (const size_t ms_since_last_tick) |
Called periodically when time elapses. More... | |
Private Attributes | |
EthernetAddress | _ethernet_address |
Ethernet (known as hardware, network-access-layer, or link-layer) address of the interface. More... | |
std::queue< EthernetFrame > | _frames_out {} |
outbound queue of Ethernet frames that the NetworkInterface wants sent More... | |
Address | _ip_address |
IP (known as internet-layer or network-layer) address of the interface. More... | |
A "network interface" that connects IP (the internet layer, or network layer) with Ethernet (the network access layer, or link layer).
This module is the lowest layer of a TCP/IP stack (connecting IP with the lower-layer network protocol, e.g. Ethernet). But the same module is also used repeatedly as part of a router: a router generally has many network interfaces, and the router's job is to route Internet datagrams between the different interfaces. The network interface translates datagrams (coming from the "customer," e.g. a TCP/IP stack or router) into Ethernet frames. To fill in the Ethernet destination address, it looks up the Ethernet address of the next IP hop of each datagram, making requests with the Address Resolution Protocol. In the opposite direction, the network interface accepts Ethernet frames, checks if they are intended for it, and if so, processes the the payload depending on its type. If it's an IPv4 datagram, the network interface passes it up the stack. If it's an ARP request or reply, the network interface processes the frame and learns or replies as necessary.
Definition at line 32 of file network_interface.hh.
NetworkInterface::NetworkInterface | ( | const EthernetAddress & | ethernet_address, |
const Address & | ip_address | ||
) |
Construct a network interface with given Ethernet (network-access-layer) and IP (internet-layer) addresses.
[in] | ethernet_address | Ethernet (what ARP calls "hardware") address of the interface |
[in] | ip_address | IP (what ARP calls "protocol") address of the interface |
Definition at line 23 of file network_interface.cc.
|
inline |
Access queue of Ethernet frames awaiting transmission.
Definition at line 48 of file network_interface.hh.
optional< InternetDatagram > NetworkInterface::recv_frame | ( | const EthernetFrame & | frame | ) |
Receives an Ethernet frame and responds appropriately.
If type is IPv4, returns the datagram. If type is ARP request, learn a mapping from the "sender" fields, and send an ARP reply. If type is ARP reply, learn a mapping from the "target" fields.
[in] | frame | the incoming Ethernet frame |
Definition at line 40 of file network_interface.cc.
void NetworkInterface::send_datagram | ( | const InternetDatagram & | dgram, |
const Address & | next_hop | ||
) |
Sends an IPv4 datagram, encapsulated in an Ethernet frame (if it knows the Ethernet destination address).
Will need to use ARP to look up the Ethernet destination address for the next hop ("Sending" is accomplished by pushing the frame onto the frames_out queue.)
[in] | dgram | the IPv4 datagram to be sent |
[in] | next_hop | the IP address of the interface to send it to (typically a router or default gateway, but may also be another host if directly connected to the same network as the destination) (Note: the Address type can be converted to a uint32_t (raw 32-bit IP address) with the Address::ipv4_numeric() method.) |
Definition at line 32 of file network_interface.cc.
void NetworkInterface::tick | ( | const size_t | ms_since_last_tick | ) |
Called periodically when time elapses.
[in] | ms_since_last_tick | the number of milliseconds since the last call to this method |
Definition at line 46 of file network_interface.cc.
|
private |
Ethernet (known as hardware, network-access-layer, or link-layer) address of the interface.
Definition at line 35 of file network_interface.hh.
|
private |
outbound queue of Ethernet frames that the NetworkInterface wants sent
Definition at line 41 of file network_interface.hh.
|
private |
IP (known as internet-layer or network-layer) address of the interface.
Definition at line 38 of file network_interface.hh.