仅启用来自指定 IP 地址的传入连接
我有一个服务器应用程序,在特定 IP 端口上打开了侦听套接字。 如何允许套接字仅启用来自一个指定 IP 地址的传入连接?
I have a server application with a listening socket opened on a specific IP port.
How can I allow the socket to enable incoming connections from just one specified IP address?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须使用某些防火墙软件来限制对该端口的传入请求,或者关闭您不想提供服务的已接受连接(基于
accept
返回的套接字地址)。可能有一些库可以为您执行此操作,但套接字 API 没有任何自动执行此操作的功能。
You'll have to either use some firewall software to restrict incoming requests to that port, or shut down accepted connections that you do not want to service (based on the socket address returned by
accept
).There might be libraries out there that do that for you, but the socket API doesn't have anything to do it automatically.
当您接受连接时,您可以在接受后检查 sockaddr 以查看它是否来自正确的地址。如果没有,请立即关闭accept返回的连接套接字。
When you accept a connection you can examine the sockaddr after accepting to see if it came from the right address. If not you immediately close the connect socket returned by accept.
您必须使用accept() 接受连接,然后如果您不想要则将其关闭(如果您的协议支持此操作,则可能会发送错误响应)。这对于大多数应用来说已经足够了。
You have to accept the connection with accept(), then close it if you don't want it (perhaps sending an error response if your protocol supports this). This is good enough for most applications.
尝试:libauth,这是一种强大的访问控制方式 http://linux.die.net/man/ 3/libauth
Try: libauth, it's a robust way of access control http://linux.die.net/man/3/libauth