Sponge
CS144's user-space TCP library
network_interface.cc
Go to the documentation of this file.
1 #include "network_interface.hh"
2 
3 #include "arp_message.hh"
4 #include "ethernet_frame.hh"
5 
6 #include <iostream>
7 
8 // Dummy implementation of a network interface
9 // Translates from {IP datagram, next hop address} to link-layer frame, and from link-layer frame to IP datagram
10 
11 // For Lab 5, please replace with a real implementation that passes the
12 // automated checks run by `make check_lab5`.
13 
14 // You will need to add private members to the class declaration in `network_interface.hh`
15 
16 template <typename... Targs>
17 void DUMMY_CODE(Targs &&... /* unused */) {}
18 
19 using namespace std;
20 
23 NetworkInterface::NetworkInterface(const EthernetAddress &ethernet_address, const Address &ip_address)
24  : _ethernet_address(ethernet_address), _ip_address(ip_address) {
25  cerr << "DEBUG: Network interface has Ethernet address " << to_string(_ethernet_address) << " and IP address "
26  << ip_address.ip() << "\n";
27 }
28 
32 void NetworkInterface::send_datagram(const InternetDatagram &dgram, const Address &next_hop) {
33  // convert IP address of next hop to raw 32-bit representation (used in ARP header)
34  const uint32_t next_hop_ip = next_hop.ipv4_numeric();
35 
36  DUMMY_CODE(dgram, next_hop, next_hop_ip);
37 }
38 
40 optional<InternetDatagram> NetworkInterface::recv_frame(const EthernetFrame &frame) {
41  DUMMY_CODE(frame);
42  return {};
43 }
44 
46 void NetworkInterface::tick(const size_t ms_since_last_tick) { DUMMY_CODE(ms_since_last_tick); }
NetworkInterface::recv_frame
std::optional< InternetDatagram > recv_frame(const EthernetFrame &frame)
Receives an Ethernet frame and responds appropriately.
Definition: network_interface.cc:40
arp_message.hh
EthernetFrame
Ethernet frame.
Definition: ethernet_frame.hh:8
DUMMY_CODE
void DUMMY_CODE(Targs &&...)
Definition: network_interface.cc:17
NetworkInterface::_ethernet_address
EthernetAddress _ethernet_address
Ethernet (known as hardware, network-access-layer, or link-layer) address of the interface.
Definition: network_interface.hh:35
IPv4Datagram
IPv4 Internet datagram
Definition: ipv4_datagram.hh:8
Address
Wrapper around IPv4 addresses and DNS operations.
Definition: address.hh:13
NetworkInterface::send_datagram
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 addre...
Definition: network_interface.cc:32
std::cerr
Address::ipv4_numeric
uint32_t ipv4_numeric() const
Numeric IP address as an integer (i.e., in host byte order).
Definition: address.cc:108
std::array
std::uint32_t
std
NetworkInterface::tick
void tick(const size_t ms_since_last_tick)
Called periodically when time elapses.
Definition: network_interface.cc:46
to_string
string to_string(const EthernetAddress address)
Printable representation of an EthernetAddress.
Definition: ethernet_header.cc:52
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) addr...
Definition: network_interface.cc:23
Address::ip
std::string ip() const
Dotted-quad IP address string ("18.243.0.1").
Definition: address.hh:51
ethernet_frame.hh
network_interface.hh