Accepting Sockets Only From Specific IPs
My game server is now accepting sockets from every one. But how can I block specific IPs from attack? You know,开发者_Python百科 if they would like to crash my server or something. I'm using SFML library, C++.
With TCP, when your program (or the library you are using) calls accept(2)
, the second argument is an output which tells you the client's address.
With UDP there are no connections, but there is recvfrom(2)
, which just like accept(2)
, gives you the peer's address. So you can ignore the peers you don't like.
Or you can run your server behind some sort of firewall appliance and add rules there, or use iptables or similar as a software firewall on the host machine.
In SFML you have SocketTCP::Accept
and SocketUDP::Receive
, both of which will give you the peer's address if you pass an empty address as an argument.
I don't know of any specific method for blocking some ips, but you can surely reject the request (probably connection request in your case) after determining the originating ip. Maintain a list of blocked ip (or BlackListed ip) (make it configurable as well for easier additions/deletions) and reject the request if it is coming from one of the Black Listed ips.
Under Windows, WinSock 2.x has an optional callback parameter for WSAAccept()
that can be used to conditionally accept/reject connection requests. Client IP/Port is one of the available parameters.
精彩评论