IPHeader(uint8_t version, uint16_t total_len, uint16_t id, bool df,
bool mf, uint16_t offset, uint8_t ttl, uint8_t proto,
- const IPAddr& src, const IPAddr& dst):
- version(version), total_len(total_len), id(id), reserved_flag(0),
- df(df), mf(mf), offset(offset), ttl(ttl), proto(proto), checksum(0),
- src(src), dst(dst)
- {
- do_checksum();
- }
+ const IPAddr& src, const IPAddr& dst);
- IPHeader(const std::string& s)
- {
- *this = *((IPHeader*)s.c_str());
- }
+ IPHeader(const std::string& s);
- bool check_checksum() const
- {
- IPHeader iph = *this;
- iph.checksum = 0;
- char* raw = (char*) &iph;
- uint16_t sum = 0;
- for (unsigned i = 0; i < sizeof(IPHeader); ++i)
- sum += raw[i];
- return sum == checksum;
- }
+ static size_t header_len();
- void do_checksum()
- {
- checksum = 0;
- char* raw = (char*) this;
- uint16_t sum = 0;
- for (unsigned i = 0; i < sizeof(IPHeader); ++i)
- sum += raw[i];
- checksum = sum;
- }
+ bool check_checksum() const;
+
+ void do_checksum();
};
-std::ostream& operator<<(std::ostream& os, const IPHeader& iph)
-{
- return os
- << "version=" << iph.version
- << " total_len=" << iph.total_len
- << " id=" << iph.id
- << " DF=" << bool(iph.df)
- << " MF=" << bool(iph.mf)
- << " offset=" << unsigned(iph.offset)
- << " TTL=" << unsigned(iph.ttl)
- << " proto=" << unsigned(iph.proto)
- << " checksum=" << iph.checksum
- << " src=" << IPAddr(iph.src)
- << " dst=" << IPAddr(iph.dst);
-}
+std::ostream& operator<<(std::ostream& os, const IPHeader& iph);
#endif // _IPHEADER_H_