Exemplo n.º 1
0
  void ICMPv4::ping_reply(full_header* full_hdr, uint16_t size) {
    auto packet_ptr = inet_.create_packet(size);
    auto buf = packet_ptr->buffer();

    icmp_header* hdr = &reinterpret_cast<full_header*>(buf)->icmp_hdr;
    hdr->type = ICMP_ECHO_REPLY;
    hdr->code = 0;
    hdr->identifier = full_hdr->icmp_hdr.identifier;
    hdr->sequence   = full_hdr->icmp_hdr.sequence;

    debug("<ICMP> Rest of header IN: 0x%lx OUT: 0x%lx\n",
          full_hdr->icmp_hdr.rest, hdr->rest);

    debug("<ICMP> Transmitting answer\n");

    // Populate response IP header
    auto ip4_pckt = static_unique_ptr_cast<PacketIP4>(std::move(packet_ptr));
    ip4_pckt->init();
    ip4_pckt->set_src(full_hdr->ip_hdr.daddr);
    ip4_pckt->set_dst(full_hdr->ip_hdr.saddr);
    ip4_pckt->set_protocol(IP4::IP4_ICMP);
    ip4_pckt->set_ip_data_length(sizeof(icmp_header) + size - sizeof(full_header));

    // Copy payload from old to new packet
    uint8_t* payload = reinterpret_cast<uint8_t*>(hdr) + sizeof(icmp_header);
    uint8_t* source  = reinterpret_cast<uint8_t*>(&full_hdr->icmp_hdr) + sizeof(icmp_header);
    memcpy(payload, source, size - sizeof(full_header));

    hdr->checksum = 0;
    hdr->checksum = net::checksum(reinterpret_cast<uint16_t*>(hdr),
                                  size - sizeof(full_header) + sizeof(icmp_header));

    network_layer_out_(std::move(ip4_pckt));
  }
Exemplo n.º 2
0
  void UDP::transmit(UDP::Packet_ptr udp) {
    debug2("<UDP> Transmitting %i bytes (seg=%i) from %s to %s:%i\n",
           udp->length(), udp->ip4_segment_size(),
           udp->src().str().c_str(),
           udp->dst().str().c_str(), udp->dst_port());

    assert(udp->length() >= sizeof(udp_header));
    assert(udp->protocol() == IP4::IP4_UDP);

    auto pckt = Packet::packet(udp);
    network_layer_out_(pckt);
  }