boost async_receive_from ip 过滤器

发布于 2024-12-20 18:09:00 字数 126 浏览 1 评论 0原文

我正在使用 boost::asio 捕获 udp 端口​​上的数据包。我只是新手,想加强一下。 如何使 async_receive_from 复制数据以仅缓冲具有指定源 IP 的数据包?

I'm using boost::asio to capture packets on udp port. I'm just new to boost.
How can I make async_receive_from copy data to buffer only packets with specified source ip?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2024-12-27 18:09:00

根据捕获数据包的含义,类似这样的一些代码可以工作。这是从 Boost Asio Async UDP 示例修改而来的。如果将端口设置为 0,socket_ 将连接到指定端口的本地接口,我相信它会侦听所有端口。

一旦您使用async_receive_from接收到数据包,它也会从解码的数据报中返回sender_endpoint_(即有问题的数据包来自哪里。)在您的handle_receive_from< /code> 函数只需添加一个条件语句来检查所需的 sender_endpoint_ 并“将数据复制到缓冲区”。

class server
{ 
public: 
    server(boost::asio::io_service& io_service, short port)
    : io_service_(io_service),
    socket_(io_service, udp::endpoint(udp::v4(), port))
    {
        boost::asio::socket_base::receive_buffer_size option(1<<12); <br>
        socket_.set_option(option);
        start_receive(); 
    }

    void handle_receive_from(const boost::system::error_code& error,
        size_t bytes_recvd)
    {
        if (!error && bytes_recvd > 0)
        {
           if(sender_endpoint_ == <desired_IP_here>) 
               messages_.push(data_);
        } 
        start_receive();
    }

private: 
    boost::asio::io_service& io_service_; 
    udp::socket socket_;
    udp::endpoint sender_endpoint_;
    enum { max_length = 256}; 
    boost::array < boost::uint32_t, max_length > data_; 
    std::queue<boost::array<boost::uint32_t, max_length> messages_; 

    void start_receive()
    {
        socket_.async_receive_from(
            boost::asio::buffer(data_, (sizeof(boost::uint32_t)*max_length)),
            sender_endpoint_,
            boost::bind(&server::handle_receive_from, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));

    }
};

差点忘了——主要功能!

int main(void) 
{
    boost::asio::io_service io_service;
    int port_rx = 0;


    using namespace std;
    server rx(io_service, port_rx);
    io_service.run();

    return 0;
}

希望这有帮助!

Depending on what you mean by capture packets, some code like this would work. This is modified from Boost Asio Async UDP example. socket_ is connected to a local interface at a specified port if you set port to 0 I believe it listens on all the ports.

Once you receive a packet using async_receive_from, it will also return sender_endpoint_ from the decoded datagram(i.e. where did the packet in question come from.) In your handle_receive_from function just add a conditional statement to check for desired sender_endpoint_ and "copy the data to buffer".

class server
{ 
public: 
    server(boost::asio::io_service& io_service, short port)
    : io_service_(io_service),
    socket_(io_service, udp::endpoint(udp::v4(), port))
    {
        boost::asio::socket_base::receive_buffer_size option(1<<12); <br>
        socket_.set_option(option);
        start_receive(); 
    }

    void handle_receive_from(const boost::system::error_code& error,
        size_t bytes_recvd)
    {
        if (!error && bytes_recvd > 0)
        {
           if(sender_endpoint_ == <desired_IP_here>) 
               messages_.push(data_);
        } 
        start_receive();
    }

private: 
    boost::asio::io_service& io_service_; 
    udp::socket socket_;
    udp::endpoint sender_endpoint_;
    enum { max_length = 256}; 
    boost::array < boost::uint32_t, max_length > data_; 
    std::queue<boost::array<boost::uint32_t, max_length> messages_; 

    void start_receive()
    {
        socket_.async_receive_from(
            boost::asio::buffer(data_, (sizeof(boost::uint32_t)*max_length)),
            sender_endpoint_,
            boost::bind(&server::handle_receive_from, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));

    }
};

Almost forgot - main function!

int main(void) 
{
    boost::asio::io_service io_service;
    int port_rx = 0;


    using namespace std;
    server rx(io_service, port_rx);
    io_service.run();

    return 0;
}

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文