Sponge
CS144's user-space TCP library
ethernet_header.cc
Go to the documentation of this file.
1 #include "ethernet_header.hh"
2 
3 #include "util.hh"
4 
5 #include <iomanip>
6 #include <sstream>
7 
8 using namespace std;
9 
11  if (p.buffer().size() < EthernetHeader::LENGTH) {
13  }
14 
15  /* read destination address */
16  for (auto &byte : dst) {
17  byte = p.u8();
18  }
19 
20  /* read source address */
21  for (auto &byte : src) {
22  byte = p.u8();
23  }
24 
25  /* read the frame's type (e.g. IPv4, ARP, or something else) */
26  type = p.u16();
27 
28  return p.get_error();
29 }
30 
31 string EthernetHeader::serialize() const {
32  string ret;
33  ret.reserve(LENGTH);
34 
35  /* write destination address */
36  for (auto &byte : dst) {
37  NetUnparser::u8(ret, byte);
38  }
39 
40  /* write source address */
41  for (auto &byte : src) {
42  NetUnparser::u8(ret, byte);
43  }
44 
45  /* write the frame's type (e.g. IPv4, ARP or something else) */
46  NetUnparser::u16(ret, type);
47 
48  return ret;
49 }
50 
52 string to_string(const EthernetAddress address) {
53  stringstream ss{};
54  for (auto it = address.begin(); it != address.end(); it++) {
55  ss.width(2);
56  ss << setfill('0') << hex << int(*it);
57  if (it != address.end() - 1) {
58  ss << ":";
59  }
60  }
61  return ss.str();
62 }
63 
65 string EthernetHeader::to_string() const {
66  stringstream ss{};
67  ss << "dst=" << ::to_string(dst);
68  ss << ", src=" << ::to_string(src);
69  ss << ", type=";
70  switch (type) {
71  case TYPE_IPv4:
72  ss << "IPv4";
73  break;
74  case TYPE_ARP:
75  ss << "ARP";
76  break;
77  default:
78  ss << "[unknown type " << hex << type << "!]";
79  break;
80  }
81 
82  return ss.str();
83 }
NetParser::u16
uint16_t u16()
Parse a 16-bit integer in network byte order from the data stream.
Definition: parser.cc:64
std::stringstream::width
T width(T... args)
util.hh
NetUnparser::u8
static void u8(std::string &s, const uint8_t val)
Write an 8-bit integer into the data stream in network byte order.
Definition: parser.cc:72
EthernetHeader::parse
ParseResult parse(NetParser &p)
Parse the Ethernet fields from the provided NetParser.
Definition: ethernet_header.cc:10
std::string::reserve
T reserve(T... args)
std::stringstream
EthernetHeader::serialize
std::string serialize() const
Serialize the Ethernet fields to a string.
Definition: ethernet_header.cc:31
std::setfill
T setfill(T... args)
NetParser::buffer
Buffer buffer() const
Definition: parser.hh:40
std::hex
T hex(T... args)
EthernetHeader::to_string
std::string to_string() const
Return a string containing a header in human-readable format.
Definition: ethernet_header.cc:65
ethernet_header.hh
NetUnparser::u16
static void u16(std::string &s, const uint16_t val)
Write a 16-bit integer into the data stream in network byte order.
Definition: parser.cc:70
std::to_string
T to_string(T... args)
std::array
ParseResult
ParseResult
The result of parsing or unparsing an IP datagram, TCP segment, Ethernet frame, or ARP message.
Definition: parser.hh:12
NetParser
Definition: parser.hh:25
NetParser::u8
uint8_t u8()
Parse an 8-bit integer in network byte order from the data stream.
Definition: parser.cc:66
std::array::begin
T begin(T... args)
EthernetHeader::LENGTH
static constexpr size_t LENGTH
Ethernet header length in bytes.
Definition: ethernet_header.hh:19
std
ParseResult::PacketTooShort
@ PacketTooShort
Not enough data to finish parsing.
NetParser::get_error
ParseResult get_error() const
Get the current value stored in BaseParser::_error.
Definition: parser.hh:43
Buffer::size
size_t size() const
Size of the string.
Definition: buffer.hh:41
std::array::end
T end(T... args)