boost::asio async_accept 拒绝连接

发布于 2024-09-15 10:20:36 字数 363 浏览 1 评论 0原文

我的应用程序有一个 asio 服务器套接字,它必须接受来自定义的 IP 列表的连接。

此过滤器必须由应用程序(而不是系统)完成,因为它可以随时更改(我必须能够随时更新此列表)

客户端必须收到 acces_denied 错误。

我想当调用handle_accept回调时,SYN/ACK已经被发送,所以当我检测到连接的ip est不允许时,不想接受然后残酷地关闭。我不管理客户端行为,也许当连接被拒绝并被对等方关闭时它的行为不一样,所以我想把一切都做干净。 (但这就是我现在想要的)

你知道我该怎么做吗???

我的访问列表是 std::strings 的容器(但我可以将其转换为其他东西的计数器......)

非常感谢

My application have an asio server socket that must accept connections from a defined List of IPs.

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.

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. 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....)

Thank you very much

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

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

发布评论

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

评论(2

腹黑女流氓 2024-09-22 10:20:36

async_accept 方法获取对等端点时存在过载。您可以在 async_accept 处理程序中比较该值。如果它与容器中的条目不匹配,则让套接字超出范围。否则,请根据您的应用程序的要求进行处理。

The async_accept method has an overload to obtain the peer endpoint. You can compare that value inside your async_accept handler. 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.

懒的傷心 2024-09-22 10:20:36

我不知道你的应用程序的详细信息,但这就是我要做的。

在接受处理程序/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
    }
}

I don't know the details of your app, but this is how I'd do it.

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