// #include <boost/asio/detail/config.hpp>
// #include <boost/asio/detail/socket_types.hpp>
// #include <boost/asio/basic_raw_socket.hpp>
// #include <boost/asio/ip/basic_endpoint.hpp>
// #include <boost/asio/ip/basic_resolver.hpp>
// #include <boost/asio/ip/basic_resolver_iterator.hpp>
// #include <boost/asio/ip/basic_resolver_query.hpp>
// #include <boost/asio/detail/push_options.hpp>
// #include <boost/asio.hpp>
#include <netinet/if_ether.h>
#include <net/ethernet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <cstddef>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
class ipv4_header
{
public:
ipv4_header() {
std::fill(rep_, rep_ + sizeof(rep_), 0); }
unsigned char version() const {
return (rep_[0] >> 4) & 0xF; }
unsigned short header_length() const {
return (rep_[0] & 0xF) * 4; }
unsigned char type_of_service() const {
return rep_[1]; }
unsigned short total_length() const {
return decode(2, 3); }
unsigned short identification() const {
return decode(4, 5); }
bool dont_fragment() const {
return (rep_[6] & 0x40) != 0; }
bool more_fragments() const {
return (rep_[6] & 0x20) != 0; }
unsigned short fragment_offset() const {
return decode(6, 7) & 0x1FFF; }
unsigned int time_to_live() const {
return rep_[8]; }
unsigned char protocol() const {
return rep_[9]; }
unsigned short header_checksum() const {
return decode(10, 11); }
boost::asio::ip::address_v4 source_address() const
{
boost::asio::ip::address_v4::bytes_type bytes
使用boost asio raw socket创建第2层/ ethernet套接字(在C ++中)
于 2022-05-11 22:06:46 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)