From 4e1d173185cf3f57b231a12a403b903ae5496ab0 Mon Sep 17 00:00:00 2001 From: teodor Date: Mon, 27 Sep 2004 16:23:54 +0000 Subject: [PATCH] Add setlinger to Accept --- tcp.c | 82 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/tcp.c b/tcp.c index 76b0a19..525a57e 100644 --- a/tcp.c +++ b/tcp.c @@ -52,6 +52,47 @@ #include "tlog.h" #include "tmalloc.h" +static u_int32_t +setlinger( TC_Connection *cs ) { + struct linger ling; + int val = 0; + socklen_t size = sizeof(val); + + if (getsockopt(cs->fd, SOL_SOCKET,SO_ERROR,&val,&size) == -1) { + tlog(TL_ALARM,"getsockopt: %s:%d - %s(%d)",inet_ntoa(cs->serv_addr.sin_addr), + ntohs(cs->serv_addr.sin_port), strerror(errno), errno); + shutdown(cs->fd,SHUT_RDWR); + close(cs->fd); + cs->fd = 0; + cs->state = CS_ERROR; + return CS_ERROR; + } + + if ( val ) { + tlog(TL_ALARM,"getsockopt return: %s:%d - %s(%d)",inet_ntoa(cs->serv_addr.sin_addr), + ntohs(cs->serv_addr.sin_port), strerror(val), val); + shutdown(cs->fd,SHUT_RDWR); + close(cs->fd); + cs->fd = 0; + cs->state = CS_ERROR; + return CS_ERROR; + } + + + ling.l_onoff = ling.l_linger = 0; + if (setsockopt(cs->fd, SOL_SOCKET,SO_LINGER,(char *)&ling,sizeof(ling))==-1) { + tlog(TL_ALARM,"setsockopt: LINGER %s:%d - %s",inet_ntoa(cs->serv_addr.sin_addr), ntohs(cs->serv_addr.sin_port), + strerror(errno)); + shutdown(cs->fd,SHUT_RDWR); + close(cs->fd); + cs->fd = 0; + cs->state = CS_ERROR; + return CS_ERROR; + } + cs->state = CS_CONNECTED; + return CS_CONNECTED; +} + u_int32_t TC_ClientInitConnection(TC_Connection *cs, char *name, u_int32_t port) { int flags; @@ -103,6 +144,7 @@ TC_AcceptTcp(TC_Connection *cs) { memcpy( &(nc->serv_addr), &cli_addr, clilen ); nc->state = CS_CONNECTED; + setlinger(nc); return nc; } @@ -117,46 +159,6 @@ TC_fillConnection(TC_Connection *sc, char *name, u_int32_t port) { return sc; } -static u_int32_t -setlinger( TC_Connection *cs ) { - struct linger ling; - int val = 0; - socklen_t size = sizeof(val); - - if (getsockopt(cs->fd, SOL_SOCKET,SO_ERROR,&val,&size) == -1) { - tlog(TL_ALARM,"getsockopt: %s:%d - %s(%d)",inet_ntoa(cs->serv_addr.sin_addr), - ntohs(cs->serv_addr.sin_port), strerror(errno), errno); - shutdown(cs->fd,SHUT_RDWR); - close(cs->fd); - cs->fd = 0; - cs->state = CS_ERROR; - return CS_ERROR; - } - - if ( val ) { - tlog(TL_ALARM,"getsockopt return: %s:%d - %s(%d)",inet_ntoa(cs->serv_addr.sin_addr), - ntohs(cs->serv_addr.sin_port), strerror(val), val); - shutdown(cs->fd,SHUT_RDWR); - close(cs->fd); - cs->fd = 0; - cs->state = CS_ERROR; - return CS_ERROR; - } - - - ling.l_onoff = ling.l_linger = 0; - if (setsockopt(cs->fd, SOL_SOCKET,SO_LINGER,(char *)&ling,sizeof(ling))==-1) { - tlog(TL_ALARM,"setsockopt: LINGER %s:%d - %s",inet_ntoa(cs->serv_addr.sin_addr), ntohs(cs->serv_addr.sin_port), - strerror(errno)); - shutdown(cs->fd,SHUT_RDWR); - close(cs->fd); - cs->fd = 0; - cs->state = CS_ERROR; - return CS_ERROR; - } - cs->state = CS_CONNECTED; - return CS_CONNECTED; -} u_int32_t TC_ServerInitConnect( TC_Connection *cs ) { -- 2.46.1