type
status
slug
date
summary
tags
category
password
icon
Lab Checkpoint 5: down the stack (the network interface)
接口定义
void NetworkInterface::send_datagram(const InternetDatagram &dgram, const Address &next hop);
This method is called when the caller (e.g., your TCPConnection or a router) wants to send an outbound Internet (IP) datagram to the next hop. It’s your interface’s job to translate this datagram into an Ethernet frame and (eventually) send it. • If the destination Ethernet address is already known, send it right away. Create an Ethernet frame (with type = EthernetHeader::TYPE IPv4), set the payload to be the serialized datagram, and set the source and destination addresses. • If the destination Ethernet address is unknown, broadcast an ARP request for the next hop’s Ethernet address, and queue the IP datagram so it can be sent after the ARP reply is received.
Except: You don’t want to flood the network with ARP requests. If the network
interface already sent an ARP request about the same IP address in the last
five seconds, don’t send a second request—just wait for a reply to the first one.
Again, queue the datagram until you learn the destination Ethernet address.
void NetworkInterface::recv_frame(const EthernetFrame &frame);
This method is called when an Ethernet frame arrives from the network. The code should ignore any frames not destined for the network interface (meaning, the Ethernet destination is either the broadcast address or the interface’s own Ethernet address stored in the ethernet address member variable).- If the inbound frame is IPv4 , parse the payload as an InternetDatagram and, if successful (meaning the parse() method returned ParseResult::NoError), push the resulting datagram on to the datagrams_received_queue.
- If the inbound frame is ARP, parse the payload as an ARPMessage and, if successful, remember the mapping between the sender’s IP address and Ethernet address for 30 seconds. (Learn mappings from both requests and replies.) In addition, if it’s an ARP request asking for our IP address, send an appropriate ARP reply.
void NetworkInterface::tick(const size t ms since last tick);
This is called as time passes. Expire any IP-to-Ethernet mappings that have expired.
Q&A
How do I convert an IP address that comes in the form of an Address object, into a raw 32-bit integer that I can write into the ARP message?
Use the Address::ipv4_numeric() method.
确实难度不高,给出代码仓库吧:
minnow
KMSorSMS • Updated Jun 30, 2024
- 作者:liamY
- 链接:https://liamy.clovy.top/article/cs144/lab45
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。