开发者

Enable incoming connections from specified IP-address only

I have a server application with a listening socket opened on a specifi开发者_如何学Cc IP port. How can I allow the socket to enable incoming connections from just one specified IP address?


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.


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.


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.


Try: libauth, it's a robust way of access control http://linux.die.net/man/3/libauth

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜