void init() noexcept { ip4_header().version_ihl = 0x45; ip4_header().tos = 0; ip4_header().id = 0; ip4_header().frag_off_flags = 0; ip4_header().ttl = DEFAULT_TTL; }
void IP4::transmit(Packet_ptr pckt) { assert(pckt->size() > sizeof(IP4::full_header)); auto ip4_pckt = std::static_pointer_cast<PacketIP4>(pckt); ip4_pckt->make_flight_ready(); IP4::ip_header& hdr = ip4_pckt->ip4_header(); // Create local and target subnets addr target = hdr.daddr & stack_.netmask(); addr local = stack_.ip_addr() & stack_.netmask(); // Compare subnets to know where to send packet pckt->next_hop(target == local ? hdr.daddr : stack_.router()); debug("<IP4 TOP> Next hop for %s, (netmask %s, local IP: %s, gateway: %s) == %s\n", hdr.daddr.str().c_str(), stack_.netmask().str().c_str(), stack_.ip_addr().str().c_str(), stack_.router().str().c_str(), target == local ? "DIRECT" : "GATEWAY"); debug("<IP4 transmit> my ip: %s, Next hop: %s, Packet size: %i IP4-size: %i\n", stack_.ip_addr().str().c_str(), pckt->next_hop().str().c_str(), pckt->size(), ip4_pckt->ip4_segment_size() ); linklayer_out_(pckt); }
void set_ip4_checksum() noexcept { auto& hdr = ip4_header(); hdr.check = 0; hdr.check = net::checksum(&hdr, sizeof(IP4::ip_header)); }
/** * Set IP4 header length * * Inferred from packet size and linklayer header size */ void set_segment_length() noexcept { ip4_header().tot_len = htons(size() - sizeof(LinkLayer::header)); }
/** Last modifications before transmission */ void make_flight_ready() noexcept { assert( ip4_header().protocol ); set_segment_length(); set_ip4_checksum(); }
uint16_t ip4_segment_size() const noexcept { return ntohs(ip4_header().tot_len); }
uint8_t protocol() const noexcept { return ip4_header().protocol; }
void set_protocol(IP4::proto p) noexcept { ip4_header().protocol = p; }
void set_dst(const IP4::addr& addr) noexcept { ip4_header().daddr = addr; }
const IP4::addr& dst() const noexcept { return ip4_header().daddr; }
void set_src(const IP4::addr& addr) noexcept { ip4_header().saddr = addr; }
const IP4::addr& src() const noexcept { return ip4_header().saddr; }