From 716f102dd6d017a1e207fb4465db8c7d6c7746b0 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Fri, 15 Jun 2018 10:47:05 -0500 Subject: [PATCH] rsrv: avoid redundant getpeername() also do some extra, and redundant checks, on accepted sockets. --- modules/database/src/ioc/rsrv/caservertask.c | 28 ++++++++------------ modules/database/src/ioc/rsrv/server.h | 2 +- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/modules/database/src/ioc/rsrv/caservertask.c b/modules/database/src/ioc/rsrv/caservertask.c index 5ef746faa..f377d837f 100644 --- a/modules/database/src/ioc/rsrv/caservertask.c +++ b/modules/database/src/ioc/rsrv/caservertask.c @@ -83,20 +83,22 @@ static void req_server (void *pParm) while (TRUE) { SOCKET clientSock; - struct sockaddr sockAddr; + osiSockAddr sockAddr; osiSocklen_t addLen = sizeof(sockAddr); while (castcp_ctl == ctlPause) { epicsThreadSleep(0.1); } - clientSock = epicsSocketAccept ( IOC_sock, &sockAddr, &addLen ); - if ( clientSock == INVALID_SOCKET ) { + clientSock = epicsSocketAccept ( IOC_sock, &sockAddr.sa, &addLen ); + if ( clientSock == INVALID_SOCKET || + sockAddr.sa.sa_family != AF_INET || + addLen < sizeof(sockAddr.ia) ) { char sockErrBuf[64]; epicsSocketConvertErrnoToString ( sockErrBuf, sizeof ( sockErrBuf ) ); - errlogPrintf("CAS: Client accept error: %s\n", - sockErrBuf ); + errlogPrintf("CAS: Client accept error: %s (%d)\n", + sockErrBuf, (int)addLen ); epicsThreadSleep(15.0); continue; } @@ -105,7 +107,7 @@ static void req_server (void *pParm) struct client *pClient; /* socket passed in is closed if unsuccessful here */ - pClient = create_tcp_client ( clientSock ); + pClient = create_tcp_client ( clientSock, &sockAddr ); if ( ! pClient ) { epicsThreadSleep ( 15.0 ); continue; @@ -1405,12 +1407,11 @@ void casExpandRecvBuffer ( struct client *pClient, ca_uint32_t size ) /* * create_tcp_client () */ -struct client *create_tcp_client ( SOCKET sock ) +struct client *create_tcp_client (SOCKET sock , const osiSockAddr *peerAddr) { int status; struct client *client; int intTrue = TRUE; - osiSocklen_t addrSize; unsigned priorityOfEvents; /* socket passed in is destroyed here if unsuccessful */ @@ -1419,6 +1420,8 @@ struct client *create_tcp_client ( SOCKET sock ) return NULL; } + client->addr = peerAddr->ia; + /* * see TCP(4P) this seems to make unsolicited single events much * faster. I take care of queue up as load increases. @@ -1470,15 +1473,6 @@ struct client *create_tcp_client ( SOCKET sock ) } #endif - addrSize = sizeof ( client->addr ); - status = getpeername ( sock, (struct sockaddr *)&client->addr, - &addrSize ); - if ( status < 0 ) { - epicsPrintf ("CAS: peer address fetch failed\n"); - destroy_tcp_client (client); - return NULL; - } - client->evuser = (struct event_user *) db_init_events (); if ( ! client->evuser ) { errlogPrintf ("CAS: unable to init the event facility\n"); diff --git a/modules/database/src/ioc/rsrv/server.h b/modules/database/src/ioc/rsrv/server.h index ef9730ba8..4d502f77f 100644 --- a/modules/database/src/ioc/rsrv/server.h +++ b/modules/database/src/ioc/rsrv/server.h @@ -230,7 +230,7 @@ void rsrv_online_notify_task (void *); void cast_server (void *); struct client *create_client ( SOCKET sock, int proto ); void destroy_client ( struct client * ); -struct client *create_tcp_client ( SOCKET sock ); +struct client *create_tcp_client ( SOCKET sock, const osiSockAddr* peerAddr ); void destroy_tcp_client ( struct client * ); void casAttachThreadToClient ( struct client * ); int camessage ( struct client *client );