简体   繁体   English

boost :: asio async_accept拒绝连接

[英]boost::asio async_accept Refuse a connection

My application have an asio server socket that must accept connections from a defined List of IPs. 我的应用程序有一个asio服务器套接字,必须接受来自已定义的IP列表的连接。

This filter must be done by the application, (not by the system), because it can change at any time (i must be able to update this list at any time) 此过滤器必须由应用程序(而不是系统)完成,因为它可以随时更改(我必须能够随时更新此列表)

The client must receive an acces_denied error. 客户端必须收到acces_denied错误。

I suppose when the handle_accept callback is called, SYN/ACK has already be sent, so don't want to accept then close brutally when i detect the connected ip est not allowed. 我想当调用handle_accept回调时,已经发送了SYN / ACK,所以当我检测到连接的ip est不允许时,不要接受然后粗暴地关闭。 I don't manage the client behavior, maybe it doesn't act the same when the connection is refused and just closed by peer, so i want to do everything clean. 我不管理客户端行为,也许当连接被拒绝并且只是被对等关闭时,它的行为不一样,所以我想做一切都干净。 (but it's what im soing for the moment) (但这就是我当下的事情)

Do you know how i can do that??? 你知道我怎么做吗???

My access list is a container of std::strings (but i can convert it to a countainer of something else....) 我的访问列表是std :: strings的容器(但我可以将它转换为其他东西的计数器....)

Thank you very much 非常感谢你

The async_accept method has an overload to obtain the peer endpoint. async_accept方法具有获取对等端点的重载。 You can compare that value inside your async_accept handler. 您可以在async_accept处理程序中比较该值。 If it does not match an entry in your container, let the socket go out of scope. 如果它与容器中的条目不匹配,请让套接字超出范围。 Otherwise, handle it as required by your appliation. 否则,根据您的应用程序处理它。

I don't know the details of your app, but this is how I'd do it. 我不知道你的应用程序的细节,但这就是我的方式。

In the accept handler/lambda 在accept handler / lambda中

void onAccept(shared_ptr<connection> c, error_code ec)
{
    if (ec) { /*... */ }
    if (isOnBlackList(c->endpoint_))
    {
       c->socket_.async_write( /* a refusal message */, 
         [c](error_code, n) 
         { 
            c->socket_.shutdown();
            // c fizzles out of all contexts... 
         });
    }
    else
    {
        // successful connection execution path
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM