3 IPHeader::IPHeader(uint8_t version, uint16_t total_len, uint16_t id, bool df,
4 bool mf, uint16_t offset, uint8_t ttl, uint8_t proto,
5 const IPAddr& src, const IPAddr& dst):
6 version(version), total_len(total_len), id(id), reserved_flag(0),
7 df(df), mf(mf), offset(offset), ttl(ttl), proto(proto), checksum(0),
13 IPHeader::IPHeader(const std::string& s)
15 *this = *((IPHeader*)s.c_str());
18 size_t IPHeader::header_len()
20 return sizeof(IPHeader);
23 bool IPHeader::check_checksum() const
27 char* raw = (char*) &iph;
29 for (unsigned i = 0; i < sizeof(IPHeader); ++i)
31 return sum == checksum;
34 void IPHeader::do_checksum()
37 char* raw = (char*) this;
39 for (unsigned i = 0; i < sizeof(IPHeader); ++i)
44 std::ostream& operator<<(std::ostream& os, const IPHeader& iph)
47 << "version=" << unsigned(iph.version)
48 << " total_len=" << iph.total_len
50 << " DF=" << bool(iph.df)
51 << " MF=" << bool(iph.mf)
52 << " offset=" << unsigned(iph.offset)
53 << " TTL=" << unsigned(iph.ttl)
54 << " proto=" << unsigned(iph.proto)
55 << " checksum=" << iph.checksum
56 << " src=" << IPAddr(iph.src)
57 << " dst=" << IPAddr(iph.dst);
60 // vim: set et sw=4 sts=4 :