CS144-2021|Lab5 个人实验记录

89 阅读1分钟

lab5

个人实验留档:cs144-2021

Overview

image.png

lab5要实现的是数据链路层的以太网协议。

The Address Resolution Protocol

需要实现arp协议,主要是发送arp请求以及响应arp请求。

image.png

arp request

  • 硬件类型:Type of the link-layer protocol(generally Ethernet/Wi-Fi)
  • 上层协议类型:Type of the Internet-layer protocol (generally IPv4)
  • Mac地址长度
  • IP地址长度
  • 操作类型:1,表示request。
  • 源Mac:发送请求的主机的mac
  • 源ip:发送请求的主机的ip
  • 目的mac:全0
  • 目的ip

此外,mac帧的目的地址为广播地址,类型为arp类型(0x806)

arp reply

与arp request不同的部分:

  • 操作类型:2,表示reply
  • 目的mac,目的ip:从相对应的arp request中获取

mac帧的目的地址从arp request中获取

实现

  • void NetworkInterface::send_datagram(const InternetDatagram &dgram, const Address &next_hop) 发送报文
    • 如果eth地址已知:封装eth头部后直接发送
    • 如果未知:先缓存报文,发送arp request(注意防止 ARP请求淹没网络,可以记录已经发送过请求的IP)
  • optional<InternetDatagram> NetworkInterface::recv_frame(const EthernetFrame &frame) 接收到报文,丢弃不符合要求的报文
    • 如果报文类型是ipv4:将payload解析成ip数据报文并返回
    • 如果是arp:先将ip address→eth address 映射缓存,再查看之前缓存的报文能否被发送,最后,如果是arp request,则发送arp reply
  • void NetworkInterface::tick(const size_t ms_since_last_tick) 在这个函数里面处理arp表(ip→eth)过期(30s),以及删除发送过请求的ip记录(5s)

具体实现见:

network_interface.hh

network_interface.cc

测试

image.png