简体   繁体   English

仅启用来自指定 IP 地址的传入连接

[英]Enable incoming connections from specified IP-address only

I have a server application with a listening socket opened on a specific IP port.我有一个服务器应用程序,在特定的 IP 端口上打开了一个监听套接字。 How can I allow the socket to enable incoming connections from just one specified IP address?如何允许套接字仅从一个指定的 IP 地址启用传入连接?

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 ).您必须使用某些防火墙软件来限制对该端口的传入请求,或者关闭您不想服务的已接受连接(基于accept返回的套接字地址)。

There might be libraries out there that do that for you, but the socket API doesn't have anything to do it automatically.那里可能有库可以为您执行此操作,但套接字 API 没有任何自动执行此操作的功能。

When you accept a connection you can examine the sockaddr after accepting to see if it came from the right address.当您接受连接时,您可以在接受后检查 sockaddr 以查看它是否来自正确的地址。 If not you immediately close the connect socket returned by 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).您必须使用 accept() 接受连接,如果您不想要它,则关闭它(如果您的协议支持,可能会发送错误响应)。 This is good enough for most applications.这对于大多数应用程序来说已经足够了。

Try: libauth, it's a robust way of access control http://linux.die.net/man/3/libauth试试:libauth,这是一种强大的访问控制方式http://linux.die.net/man/3/libauth

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

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