c - socket accept only specific addresses? -
I am writing a small TCP server based on the example below.
Add # & lt; Sys / socket.h & gt; # Include & lt; Sys / un.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Stdio.h & gt; #include & lt; String.h & gt; # Defined MY_SOCK_PATH "/ somepath" #define LISTEN_BACKLOG 50 #define handle_error (msg) \ do {perror (msg); Exhaust (EXIT_FAILURE); } While (0) int main (int argc, char * argv []) {int sfd, cfd; Struct sockaddr_un my_addr, peer_addr; Socklen_t peer_addr_size; Sfd = Socket (AF_UNIX, SOCK_STREAM, 0); If (sfd == -1) handle_error ("socket"); Memc (& amp; my_addr, 0, size (struct sockaddr_un)); / * Clear Configuration * / my_addr.sun_family = AF_UNIX; Strncpy (my_addr.sun_path, MY_SOCK_PATH, size (my_addr.sun_path) - 1); If (Tie (SFD, (Structured Socder *) and MyEADDR, Psychophag (Structure Secunder_an)) == -1) Hand_error ("Bind"); If (listen (sfd, LISTEN_BACKLOG) == -1) handle_error ("listen"); / * Now we can accept incoming connections at one time using (2) * / peer_addr_size = sizeof (struct sockaddr_un); CFD = Acceptance (SFD, (Structure SOCADAR *) and PEER_ADD, and PEAR_ADR_IZE); If (cfd == -1) handle_error ("accept"); / * Code to deal with incoming connection (s) ... * / / * is no longer required, name of fault path, unlink (2) or remove MY_SOCK_PATH (3) * /} When using acceptance () to open a new connection, is it possible to accept a connection from a whitelisted IP address or it is possible to deny blacklist IP address in any way ?
Any suggestions?
POSIX With API and TCP, you will have to accept () to view the client address, and then you are free to close, receive, send or ignore
Normally you will use the operating system firewall for this type of work
And remember that ignoring only a few clients will not increase the security, because you still have to Are susceptible to men in, play again and sniff the attacks.
Comments
Post a Comment