Initial commit of files
This commit is contained in:
46
src/libnet/tcp_accept.c
Normal file
46
src/libnet/tcp_accept.c
Normal file
@ -0,0 +1,46 @@
|
||||
//
|
||||
// TCP Accept
|
||||
//
|
||||
|
||||
#include "netlib.h"
|
||||
#include "strlib.h"
|
||||
|
||||
int tcp_accept( int sock, struct sockaddr_storage *sa )
|
||||
{
|
||||
/* storage? */
|
||||
struct sockaddr_storage ss;
|
||||
if ( !sa )
|
||||
sa = &ss;
|
||||
|
||||
/* tcp request */
|
||||
socklen_t len = sizeof(struct sockaddr_storage);
|
||||
|
||||
/* accept the incoming connection */
|
||||
int cs = accept(sock, (struct sockaddr *)sa, &len );
|
||||
|
||||
/* check for errors. if any, ignore new connection */
|
||||
if (cs < 0 )
|
||||
{
|
||||
log_error("tcp_accept");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* turn off nagle, writes are shipped immediately */
|
||||
int flag = 1;
|
||||
setsockopt( cs, /* socket affected */
|
||||
IPPROTO_TCP, /* set option at TCP level */
|
||||
TCP_NODELAY, /* name of option */
|
||||
(char *) &flag, /* the cast is historical cruft */
|
||||
sizeof(int)); /* length of option value */
|
||||
|
||||
/* log new client */
|
||||
if ( cs > 0 )
|
||||
{
|
||||
char szBuf[256];
|
||||
ip_host2name( szBuf, sizeof(szBuf), sa );
|
||||
strlcat( szBuf, " client connected", sizeof(szBuf) );
|
||||
log_write( szBuf );
|
||||
}
|
||||
|
||||
return cs;
|
||||
}
|
||||
Reference in New Issue
Block a user