Skip to content

Instantly share code, notes, and snippets.

@compor
Created February 25, 2013 14:40
Show Gist options
  • Save compor/5030214 to your computer and use it in GitHub Desktop.
Save compor/5030214 to your computer and use it in GitHub Desktop.
c linux tcpd api example
/*
simple usage of the tcpd/tcp wrapper API
a full-fledged program would probalby call hosts_access() upon accepting a new
connection and if denied it would close()
the established connection with FIN,ACK or most preferably RST
build with :
gcc -c tcpd_test.c
static libs
gcc -o tcpd_test tcpd_test.o /usr/lib/libwrap.a /usr/lib/libnsl.a
dynamic libs
gcc -o tcpd_test tcpd_test.o /usr/lib/libwrap.so /usr/lib/libnsl.so
for denying access add this line to /etc/hosts.deny
8888: *
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <tcpd.h>
int main( int argc, char *argv[] ) {
struct request_info rq;
struct sockaddr_in myserver_address;
int rc = 0;
/* setup the criteria */
myserver_address.sin_family = AF_INET;
myserver_address.sin_port = htons( 8888 );
myserver_address.sin_addr.s_addr = INADDR_ANY;
/* initialize the request structure with our criteria */
request_init( &rq, RQ_SERVER_SIN, &myserver_address, 0 );
/* call fromhosts() before calling hosts_access() as mentioned in the manual */
fromhost( &rq );
rc = hosts_access( &rq );
fprintf( stderr, "access %s (%d)\n", (rc) ? "granted" : "denied", rc );
return rc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment