Sponge
CS144's user-space TCP library
Public Member Functions | Private Attributes | List of all members
NetworkInterface Class Reference

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>

Inheritance diagram for NetworkInterface:
Inheritance graph
[legend]

Public Member Functions

 NetworkInterface (const EthernetAddress &ethernet_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< InternetDatagramrecv_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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ NetworkInterface()

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.

Parameters
[in]ethernet_addressEthernet (what ARP calls "hardware") address of the interface
[in]ip_addressIP (what ARP calls "protocol") address of the interface

Definition at line 23 of file network_interface.cc.

Member Function Documentation

◆ frames_out()

std::queue<EthernetFrame>& NetworkInterface::frames_out ( )
inline

Access queue of Ethernet frames awaiting transmission.

Definition at line 48 of file network_interface.hh.

◆ recv_frame()

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 "sender" fields.

Parameters
[in]framethe incoming Ethernet frame

Definition at line 40 of file network_interface.cc.

◆ send_datagram()

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.)

Parameters
[in]dgramthe IPv4 datagram to be sent
[in]next_hopthe 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.

◆ tick()

void NetworkInterface::tick ( const size_t  ms_since_last_tick)

Called periodically when time elapses.

Parameters
[in]ms_since_last_tickthe number of milliseconds since the last call to this method

Definition at line 46 of file network_interface.cc.

Member Data Documentation

◆ _ethernet_address

EthernetAddress NetworkInterface::_ethernet_address
private

Ethernet (known as hardware, network-access-layer, or link-layer) address of the interface.

Definition at line 35 of file network_interface.hh.

◆ _frames_out

std::queue<EthernetFrame> NetworkInterface::_frames_out {}
private

outbound queue of Ethernet frames that the NetworkInterface wants sent

Definition at line 41 of file network_interface.hh.

◆ _ip_address

Address NetworkInterface::_ip_address
private

IP (known as internet-layer or network-layer) address of the interface.

Definition at line 38 of file network_interface.hh.


The documentation for this class was generated from the following files: