void PacketSender::send_l2(PDU &pdu, struct sockaddr* link_addr, uint32_t len_addr, const NetworkInterface &iface) { int sock = get_ether_socket(iface); PDU::serialization_type buffer = pdu.serialize(); if(!buffer.empty()) { #if defined(BSD) || defined(__FreeBSD_kernel__) if(::write(sock, &buffer[0], buffer.size()) == -1) #else if(::sendto(sock, &buffer[0], buffer.size(), 0, link_addr, len_addr) == -1) #endif throw socket_write_error(make_error_string()); } }
PDU *IPv4Stream::allocate_pdu() const { PDU::serialization_type buffer; buffer.reserve(total_size); // Check if we actually have all the data we need. Otherwise return nullptr; uint16_t expected = 0; for(fragments_type::const_iterator it = fragments.begin(); it != fragments.end(); ++it) { if(expected != it->offset()) return 0; expected = static_cast<uint16_t>(it->offset() + it->payload().size()); buffer.insert(buffer.end(), it->payload().begin(), it->payload().end()); } return Internals::pdu_from_flag( static_cast<Constants::IP::e>(transport_proto), buffer.empty() ? 0 : &buffer[0], static_cast<uint32_t>(buffer.size()) ); }