ANSC_STATUS AnscDstoPoOverview ( ANSC_HANDLE hThisObject, ANSC_HANDLE hForm ) { ANSC_STATUS returnStatus = ANSC_STATUS_SUCCESS; PANSC_DAEMON_SERVER_TCP_OBJECT pMyObject = (PANSC_DAEMON_SERVER_TCP_OBJECT)hThisObject; PANSC_DSTO_OVERVIEW_FORM pOverviewForm = (PANSC_DSTO_OVERVIEW_FORM )hForm; pOverviewForm->StartTime = pMyObject->StartTime; pOverviewForm->CurrentTime = AnscGetTickInSecondsAbs(); pOverviewForm->EngineCount = pMyObject->EngineCount; pOverviewForm->MinSocketCount = pMyObject->MinSocketCount; pOverviewForm->MaxSocketCount = pMyObject->MaxSocketCount; pOverviewForm->CurSocketCount = pMyObject->CurSocketCount; pOverviewForm->TccCount = pMyObject->TccCount; pOverviewForm->MccCount = pMyObject->MccCount; pOverviewForm->TrcCount = pMyObject->TrcCount; pOverviewForm->MrcCount = pMyObject->MrcCount; pOverviewForm->TscCount = pMyObject->TscCount; pOverviewForm->MscCount = pMyObject->MscCount; return ANSC_STATUS_SUCCESS; }
ANSC_STATUS AnscDetoStart ( ANSC_HANDLE hThisObject ) { ANSC_STATUS returnStatus = ANSC_STATUS_SUCCESS; PANSC_DAEMON_ENGINE_TCP_OBJECT pMyObject = (PANSC_DAEMON_ENGINE_TCP_OBJECT)hThisObject; if ( pMyObject->bStarted ) { return ANSC_STATUS_SUCCESS; } else { pMyObject->Reset((ANSC_HANDLE)pMyObject); pMyObject->StartTime = AnscGetTickInSecondsAbs(); pMyObject->bStarted = TRUE; } if ( TRUE ) { AnscResetEvent(&pMyObject->RecvEvent); returnStatus = AnscSpawnTask3 ( (void*)pMyObject->RecvTask, (ANSC_HANDLE)pMyObject, ANSC_DETO_RECV_TASK_NAME, ANSC_TASK_PRIORITY_NORMAL, ANSC_DETO_RECV_TASK_STACK_SIZE ); } if ( pMyObject->ControlFlags & ANSC_DETO_FLAG_ASYNC_SEND ) { AnscResetEvent(&pMyObject->SendEvent); returnStatus = AnscSpawnTask ( (void*)pMyObject->SendTask, (ANSC_HANDLE)pMyObject, ANSC_DETO_SEND_TASK_NAME ); } return ANSC_STATUS_SUCCESS; }
ANSC_STATUS AnscDetoCloseUp ( ANSC_HANDLE hThisObject, ANSC_HANDLE hForm ) { ANSC_STATUS returnStatus = ANSC_STATUS_SUCCESS; PANSC_DAEMON_ENGINE_TCP_OBJECT pMyObject = (PANSC_DAEMON_ENGINE_TCP_OBJECT)hThisObject; PANSC_DSTO_CLOSE_UP_FORM pCloseUpForm = (PANSC_DSTO_CLOSE_UP_FORM )hForm; PANSC_DSTO_SOCKET_INFO pSocketInfo = NULL; PANSC_DAEMON_SOCKET_TCP_OBJECT pSocket = NULL; PSINGLE_LINK_ENTRY pSLinkEntry = NULL; ULONG ulCount = 0; ULONG i = 0; ULONG ulCurTime = AnscGetTickInSecondsAbs(); AnscAcquireLock(&pMyObject->SocketTableLock); for ( i = 0; i < ANSC_DETO_SOCKET_TABLE_SIZE; i++ ) { pSLinkEntry = AnscSListGetFirstEntry(&pMyObject->SocketTable[i]); while ( pSLinkEntry ) { pSocket = ACCESS_ANSC_DAEMON_SOCKET_TCP_OBJECT(pSLinkEntry); pSLinkEntry = AnscSListGetNextEntry(pSLinkEntry); pSocketInfo = (PANSC_DSTO_SOCKET_INFO)&pCloseUpForm->SocketArray[ulCount++]; pSocketInfo->StartTime = pSocket->StartTime; pSocketInfo->CurrentTime = ulCurTime; pSocketInfo->LastRecvAt = pSocket->LastRecvAt; pSocketInfo->LastSendAt = pSocket->LastSendAt; pSocketInfo->RecvBytesCount = pSocket->RecvBytesCount; pSocketInfo->SendBytesCount = pSocket->SendBytesCount; } } AnscReleaseLock(&pMyObject->SocketTableLock); pCloseUpForm->SocketCount = ulCount; return ANSC_STATUS_SUCCESS; }
ANSC_STATUS AnscDetoClean ( ANSC_HANDLE hThisObject ) { ANSC_STATUS returnStatus = ANSC_STATUS_SUCCESS; PANSC_DAEMON_ENGINE_TCP_OBJECT pMyObject = (PANSC_DAEMON_ENGINE_TCP_OBJECT)hThisObject; PANSC_DAEMON_SERVER_TCP_OBJECT pServer = (PANSC_DAEMON_SERVER_TCP_OBJECT)pMyObject->hDaemonServer; PANSC_DSTO_WORKER_OBJECT pWorker = (PANSC_DSTO_WORKER_OBJECT )pServer->hWorker; PANSC_DAEMON_SOCKET_TCP_OBJECT pSocket = NULL; PSINGLE_LINK_ENTRY pSLinkEntry = NULL; ULONG ulHashIndex = 0; BOOL bSocketFound = FALSE; ULONG ulCurTime = AnscGetTickInSecondsAbs(); ULONG ulTimeout = ANSC_DETO_SOCKET_TIMEOUT; if ( !pMyObject->bStarted ) { return ANSC_STATUS_SUCCESS; } /* else if ( pMyObject->ControlFlags & ANSC_DETO_FLAG_NO_TIMEOUT ) { return ANSC_STATUS_SUCCESS; } */ if ( pMyObject->CurSocketCount >= pMyObject->MaxSocketCount ) { ulTimeout = ANSC_DETO_SOCKET_TIMEOUT2; } else { ulTimeout = ANSC_DETO_SOCKET_TIMEOUT; } ulHashIndex = 0; bSocketFound = FALSE; while ( ulHashIndex < ANSC_DETO_SOCKET_TABLE_SIZE ) { AnscAcquireLock(&pMyObject->SocketTableLock); bSocketFound = FALSE; pSLinkEntry = AnscSListGetFirstEntry(&pMyObject->SocketTable[ulHashIndex]); while ( pSLinkEntry ) { pSocket = ACCESS_ANSC_DAEMON_SOCKET_TCP_OBJECT(pSLinkEntry); pSLinkEntry = AnscSListGetNextEntry(pSLinkEntry); if ( pSocket->bLocked ) { continue; } else if ( pSocket->bToBeCleaned ) { bSocketFound = TRUE; break; } else if ( pMyObject->ControlFlags & ANSC_DETO_FLAG_NO_TIMEOUT ) { continue; } else { ulCurTime = AnscGetTickInSecondsAbs(); } if ( ((ulCurTime - pSocket->LastRecvAt) >= ulTimeout) && ((ulCurTime - pSocket->LastSendAt) >= ulTimeout) ) { bSocketFound = TRUE; break; } } AnscReleaseLock(&pMyObject->SocketTableLock); if ( bSocketFound ) { if ( !pSocket->bTlsEnabled || (pSocket->bTlsEnabled && pSocket->bTlsConnected) ) { returnStatus = pWorker->Notify ( pWorker->hWorkerContext, (ANSC_HANDLE)pSocket, ANSC_DSTOWO_EVENT_TIME_OUT, (ANSC_HANDLE)NULL ); } returnStatus = pMyObject->DelSocket ( (ANSC_HANDLE)pMyObject, (ANSC_HANDLE)pSocket ); pMyObject->TtcCount++; } else { ulHashIndex++; } } return ANSC_STATUS_SUCCESS; }
ANSC_STATUS AnscDstoEngage ( ANSC_HANDLE hThisObject ) { ANSC_STATUS returnStatus = ANSC_STATUS_SUCCESS; PANSC_DAEMON_SERVER_TCP_OBJECT pMyObject = (PANSC_DAEMON_SERVER_TCP_OBJECT)hThisObject; PANSC_DSTO_WORKER_OBJECT pWorker = (PANSC_DSTO_WORKER_OBJECT )pMyObject->hWorker; int s_result = 0; #ifdef _ANSC_IPV6_COMPATIBLE_ ansc_addrinfo ansc_hints = {0}; ansc_addrinfo* pansc_local_addrinfo = NULL; xskt_addrinfo xskt_hints = {0}; xskt_addrinfo* pxskt_local_addrinfo = NULL; USHORT usPort = 0; char port[6] = {0}; #else /*RDKB-6151, CID-24487,24794; initializing variable before use*/ ansc_socket_addr_in local_addr1 = {0}; xskt_socket_addr_in local_addr2 = {0}; #endif if ( pMyObject->bActive ) { return ANSC_STATUS_SUCCESS; } else if ( !pWorker ) { return ANSC_STATUS_UNAPPLICABLE; } else { pWorker->Init(pWorker->hWorkerContext); pMyObject->StartTime = AnscGetTickInSecondsAbs(); pMyObject->bActive = TRUE; } /* * The underlying socket wrapper may require an explicit startup() call, such is the case on * Microsoft windows platforms. The wrapper initialization has to done for each task. On most * real-time operating systems, this call is not required. */ if ( pMyObject->Mode & ANSC_DSTO_MODE_XSOCKET ) { AnscStartupXsocketWrapper((ANSC_HANDLE)pMyObject); } else { AnscStartupSocketWrapper((ANSC_HANDLE)pMyObject); } #ifdef _ANSC_IPV6_COMPATIBLE_ if ( pMyObject->Mode & ANSC_DSTO_MODE_XSOCKET ) { xskt_hints.ai_family = AF_UNSPEC; xskt_hints.ai_socktype = XSKT_SOCKET_STREAM; xskt_hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; usPort = pMyObject->GetHostPort((ANSC_HANDLE)pMyObject); _ansc_sprintf(port, "%d", usPort); CcspTraceInfo(("!!! Host Name: %s, Host Port: %s !!!\n", pMyObject->HostName, port)); if ( _xskt_getaddrinfo ( pMyObject->HostName[0] ? pMyObject->HostName : "::", port, &xskt_hints, &pxskt_local_addrinfo ) ) { CcspTraceError(("!!! error 1 !!!\n")); returnStatus = ANSC_STATUS_FAILURE; goto EXIT1; } pMyObject->pHostAddr2 = pxskt_local_addrinfo; } else { ansc_hints.ai_family = AF_UNSPEC; ansc_hints.ai_socktype = ANSC_SOCKET_STREAM; ansc_hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; usPort = pMyObject->GetHostPort((ANSC_HANDLE)pMyObject); _ansc_sprintf(port, "%d", usPort); CcspTraceInfo(("!!! Host Name: %s, Host Port: %s !!!\n", pMyObject->HostName, port)); if ( _ansc_getaddrinfo ( pMyObject->HostName[0] ? pMyObject->HostName : "::", port, &ansc_hints, &pansc_local_addrinfo ) ) { returnStatus = ANSC_STATUS_FAILURE; goto EXIT1; } pMyObject->pHostAddr1 = pansc_local_addrinfo; } #endif /* * To engage the Tcp Daemon, we need to perform following acts in the respective order: * * (1) create the os-dependent socket * (2) manufacture and start all the engines objects * (3) manufacture the global socket object pool * (4) bind to the socket and listen on it */ if ( pMyObject->Mode & ANSC_DSTO_MODE_XSOCKET ) { #ifdef _ANSC_IPV6_COMPATIBLE_ pMyObject->Socket = _xskt_socket(pxskt_local_addrinfo->ai_family, pxskt_local_addrinfo->ai_socktype, 0); #else pMyObject->Socket = _xskt_socket(XSKT_SOCKET_AF_INET, XSKT_SOCKET_STREAM, 0); #endif } else { #ifdef _ANSC_IPV6_COMPATIBLE_ pMyObject->Socket = _ansc_socket(pansc_local_addrinfo->ai_family, pansc_local_addrinfo->ai_socktype, 0); #else pMyObject->Socket = _ansc_socket(ANSC_SOCKET_AF_INET, ANSC_SOCKET_STREAM, 0); #endif } if ( ((pMyObject->Socket == XSKT_SOCKET_INVALID_SOCKET) && (pMyObject->Mode & ANSC_DSTO_MODE_XSOCKET)) || ((pMyObject->Socket == ANSC_SOCKET_INVALID_SOCKET) && !(pMyObject->Mode & ANSC_DSTO_MODE_XSOCKET)) ) { returnStatus = ANSC_STATUS_FAILURE; goto EXIT1; } _ansc_en_reuseaddr(pMyObject->Socket); #ifndef _ANSC_IPV6_COMPATIBLE_ if ( pMyObject->Mode & ANSC_DSTO_MODE_XSOCKET ) { local_addr2.sin_family = XSKT_SOCKET_AF_INET; local_addr2.sin_port = _xskt_htons(pMyObject->HostPort); if ( pMyObject->HostAddress.Value == 0 ) { ((pansc_socket_addr_in)&local_addr2)->sin_addr.s_addr = XSKT_SOCKET_ANY_ADDRESS; } else { ((pansc_socket_addr_in)&local_addr2)->sin_addr.s_addr = pMyObject->HostAddress.Value; } } else { local_addr1.sin_family = ANSC_SOCKET_AF_INET; local_addr1.sin_port = _ansc_htons(pMyObject->HostPort); if ( pMyObject->HostAddress.Value == 0 ) { local_addr1.sin_addr.s_addr = ANSC_SOCKET_ANY_ADDRESS; } else { local_addr1.sin_addr.s_addr = pMyObject->HostAddress.Value; } } #endif #if !defined(_ANSC_KERNEL) || !defined(_ANSC_LINUX) if ( pMyObject->Mode & ANSC_DSTO_MODE_XSOCKET ) { #ifdef _ANSC_IPV6_COMPATIBLE_ s_result = _xskt_bind(pMyObject->Socket, pxskt_local_addrinfo->ai_addr, pxskt_local_addrinfo->ai_addrlen); #else AnscTrace("AnscDstoEngage -- the address is 0x%lX:%d, familty %d.\n", _ansc_ntohl(local_addr2.sin_addr.s_addr), _ansc_ntohs(local_addr2.sin_port), local_addr2.sin_family); s_result = _xskt_bind(pMyObject->Socket, (xskt_socket_addr*)&local_addr2, sizeof(local_addr2)); #endif } else { #ifdef _ANSC_IPV6_COMPATIBLE_ s_result = _ansc_bind(pMyObject->Socket, pansc_local_addrinfo->ai_addr, pansc_local_addrinfo->ai_addrlen); #else s_result = _ansc_bind(pMyObject->Socket, (ansc_socket_addr*)&local_addr1, sizeof(local_addr1)); #endif } #else if ( pMyObject->Mode & ANSC_DSTO_MODE_XSOCKET ) { while ( _xskt_bind(pMyObject->Socket, (ansc_socket_addr*)&local_addr2, sizeof(local_addr2)) != 0 ) { AnscTrace ( "AnscDstoEngage -- failure to bind try again !socket %d family %d port %d address %X \n", pMyObject->Socket, local_addr2.sin_family, local_addr2.sin_port, ((pansc_socket_addr_in)&local_addr2)->sin_addr.s_addr ); AnscSleep(10); } } else { while ( _ansc_bind(pMyObject->Socket, (ansc_socket_addr*)&local_addr1, sizeof(local_addr1)) != 0 ) { AnscTrace ( "AnscDstoEngage -- failure to bind try again !socket %d family %d port %d address %X \n", pMyObject->Socket, local_addr1.sin_family, local_addr1.sin_port, local_addr1.sin_addr.s_addr ); AnscSleep(10); } } #endif if ( s_result != 0 ) { AnscTrace ( "AnscDstoEngage -- failed to bind to the socket, error code is %d!!!\n", (pMyObject->Mode & ANSC_DSTO_MODE_XSOCKET) ? _xskt_get_last_error() : _ansc_get_last_error() ); returnStatus = ANSC_STATUS_FAILURE; goto EXIT2; } pMyObject->ManufactureEnginePool((ANSC_HANDLE)pMyObject); pMyObject->ManufactureSocketPool((ANSC_HANDLE)pMyObject); pMyObject->StartEngines ((ANSC_HANDLE)pMyObject); if ( pMyObject->Mode & ANSC_DSTO_MODE_XSOCKET ) { s_result = _xskt_listen(pMyObject->Socket, ANSC_SOCKET_BACKLOG_VALUE); } else { s_result = _ansc_listen(pMyObject->Socket, ANSC_SOCKET_BACKLOG_VALUE); } if ( s_result != 0 ) { AnscTrace("AnscDstoEngage -- failed to listen on the socket!\n"); returnStatus = ANSC_STATUS_FAILURE; goto EXIT2; } /* * If the compilation option '_ANSC_SOCKET_TLS_LAYER_' is enabled, we can simply let the ANSC * socket layer to perform the SSL/TLS functionality; otherwise, we need to prepare for doing * SSL/TLS internally. */ if ( pMyObject->Mode & ANSC_DSTO_MODE_TLS_ENABLED ) { #ifdef _ANSC_USE_OPENSSL_ pMyObject->bTlsEnabled = TRUE; if ( !openssl_init(SSL_SERVER_CALLS) ) { AnscTrace("AnscSctoEngage - openssl_init() failed!\n"); returnStatus = ANSC_STATUS_FAILURE; goto EXIT2; } #else #ifdef _ANSC_SOCKET_TLS_LAYER_ { _ansc_en_usetls(pMyObject->Socket); pMyObject->bTlsEnabled = FALSE; } #else { pMyObject->hTlsScsIf = (pMyObject->hTlsScsIf != NULL)? pMyObject->hTlsScsIf : AnscSocketTlsGetScsIf(); pMyObject->bTlsEnabled = TRUE; pMyObject->bTlsReqCert = (pMyObject->Mode & ANSC_DSTO_MODE_TLS_REQ_CERT); } #endif #endif } AnscResetEvent(&pMyObject->AcceptEvent); returnStatus = pMyObject->SpawnTask3 ( (ANSC_HANDLE)pMyObject, (void* )pMyObject->AcceptTask, (ANSC_HANDLE)pMyObject, ANSC_DSTO_ACCEPT_TASK_NAME, USER_DEFAULT_TASK_PRIORITY, 2*USER_DEFAULT_TASK_STACK_SIZE ); return ANSC_STATUS_SUCCESS; /****************************************************************** GRACEFUL ROLLBACK PROCEDURES AND EXIT DOORS ******************************************************************/ EXIT2: if ( pMyObject->Mode & ANSC_DSTO_MODE_XSOCKET ) { _xskt_closesocket(pMyObject->Socket); } else { _ansc_closesocket(pMyObject->Socket); } EXIT1: if ( returnStatus != ANSC_STATUS_SUCCESS ) { pMyObject->bActive = FALSE; } return returnStatus; }
ANSC_STATUS AnscDktoTsaRecvAppMessage ( ANSC_HANDLE hThisObject, ANSC_HANDLE hMessageBdo ) { ANSC_STATUS returnStatus = ANSC_STATUS_SUCCESS; PANSC_DAEMON_SOCKET_TCP_OBJECT pMyObject = (PANSC_DAEMON_SOCKET_TCP_OBJECT)hThisObject; PANSC_DAEMON_SERVER_TCP_OBJECT pServer = (PANSC_DAEMON_SERVER_TCP_OBJECT)pMyObject->hDaemonServer; PANSC_DAEMON_ENGINE_TCP_OBJECT pEngine = (PANSC_DAEMON_ENGINE_TCP_OBJECT)pMyObject->hDaemonEngine; PANSC_DSTO_WORKER_OBJECT pWorker = (PANSC_DSTO_WORKER_OBJECT )pServer->hWorker; PANSC_BUFFER_DESCRIPTOR pPayloadBdo = (PANSC_BUFFER_DESCRIPTOR )hMessageBdo; void* pRecvBuffer = (void* )NULL; ANSC_HANDLE hRecvHandle = (ANSC_HANDLE )NULL; ULONG ulRecvSize = (ULONG )0; ULONG ulLeftSize = (ULONG )AnscBdoGetBlockSize(pPayloadBdo); ULONG ulCopySize = (ULONG )0; ULONG ulServingT1 = 0; ULONG ulServingT2 = 0; ULONG ulWaitCount = 0; /* * Since the TLS handshake is done in the asynchronous task instead of the socket_recv task, * it's possible that we start receiving TLS client data traffic (i.e. HTTP) before handshake * task considers the handshaking is done. To accommodate such race condition, we need to sleep * for a while if there's no buffer available. */ while ( !pMyObject->GetRecvBuffer ( (ANSC_HANDLE)pMyObject, &ulRecvSize ) ) { AnscSleep(10); ulWaitCount++; if ( ulWaitCount > 30 ) /* sleep for 300ms */ { returnStatus = ANSC_STATUS_INTERNAL_ERROR; goto EXIT1; } } while ( ulLeftSize > 0 ) { pRecvBuffer = pMyObject->GetRecvBuffer ( (ANSC_HANDLE)pMyObject, &ulRecvSize ); if ( !pRecvBuffer ) { returnStatus = pWorker->Notify ( pWorker->hWorkerContext, (ANSC_HANDLE)pMyObject, ANSC_DSTOWO_EVENT_RESOURCES, (ANSC_HANDLE)NULL ); break; } else if ( ulRecvSize == 0 ) { pMyObject->ToClean((ANSC_HANDLE)pMyObject, TRUE, 2); break; } if ( ulRecvSize >= ulLeftSize ) { ulCopySize = ulLeftSize; } else { ulCopySize = ulRecvSize; } if ( TRUE ) { AnscCopyMemory ( pRecvBuffer, AnscBdoGetBlock(pPayloadBdo), ulCopySize ); AnscBdoShrinkRight(pPayloadBdo, ulCopySize); ulLeftSize -= ulCopySize; } pMyObject->RecvBytesCount += ulCopySize; pMyObject->LastRecvAt = AnscGetTickInSecondsAbs(); /* * We have successfully transferred the received data into the buffer supplied by the * socket owener though may not use up the while buffer. Now is time to notify our loyal * socket owner about this exciting event. */ ulServingT1 = AnscGetTickInMilliSeconds(); returnStatus = pMyObject->Recv ( (ANSC_HANDLE)pMyObject, pRecvBuffer, ulCopySize ); ulServingT2 = AnscGetTickInMilliSeconds(); pEngine->AvgServingTime = (pEngine->AvgServingTime == 0)? (ulServingT2 - ulServingT1) : ((ulServingT2 - ulServingT1) + pEngine->AvgServingTime * 7) / 8; pEngine->TscCount++; } goto EXIT1; /****************************************************************** GRACEFUL ROLLBACK PROCEDURES AND EXIT DOORS ******************************************************************/ EXIT1: if ( pPayloadBdo ) { AnscFreeBdo((ANSC_HANDLE)pPayloadBdo); } return returnStatus; }
ANSC_STATUS AnscDetoRecvTask ( ANSC_HANDLE hThisObject ) { ANSC_STATUS returnStatus = ANSC_STATUS_SUCCESS; PANSC_DAEMON_ENGINE_TCP_OBJECT pMyObject = (PANSC_DAEMON_ENGINE_TCP_OBJECT)hThisObject; PANSC_DAEMON_SERVER_TCP_OBJECT pServer = (PANSC_DAEMON_SERVER_TCP_OBJECT)pMyObject->hDaemonServer; PANSC_DSTO_WORKER_OBJECT pWorker = (PANSC_DSTO_WORKER_OBJECT )pServer->hWorker; #if !defined(_ANSC_KERNEL) || !defined(_ANSC_LINUX) ansc_fd_set* pRecvSet1 = (ansc_fd_set* )pMyObject->RecvSocketSet; xskt_fd_set* pRecvSet2 = (xskt_fd_set* )pMyObject->RecvSocketSet; #endif PANSC_DAEMON_SOCKET_TCP_OBJECT pSocket = NULL; ULONG ulLastCleanAt = AnscGetTickInSecondsAbs(); ANSC_SOCKET s_socket = ANSC_SOCKET_INVALID_SOCKET; int s_result = 0; int s_result_excp = 0; int s_error = 0; int i = 0; #if !defined(_ANSC_KERNEL) || !defined(_ANSC_LINUX) ansc_fd_set* read_fd_set1 = NULL; xskt_fd_set* read_fd_set2 = NULL; ansc_fd_set* excp_fd_set1 = NULL; xskt_fd_set* excp_fd_set2 = NULL; ansc_timeval timeval1; xskt_timeval timeval2; #endif AnscTrace("AnscDetoRecvTask is activated ...!\n"); #if !defined(_ANSC_KERNEL) || !defined(_ANSC_LINUX) read_fd_set1 = (ansc_fd_set*)AnscAllocateMemory(sizeof(ansc_fd_set)); read_fd_set2 = (xskt_fd_set*)AnscAllocateMemory(sizeof(xskt_fd_set)); excp_fd_set1 = (ansc_fd_set*)AnscAllocateMemory(sizeof(ansc_fd_set)); excp_fd_set2 = (xskt_fd_set*)AnscAllocateMemory(sizeof(xskt_fd_set)); if ( !read_fd_set1 || !read_fd_set2 || !excp_fd_set1 || !excp_fd_set2 ) { goto EXIT1; } #endif /* * As a scalable server implemention, we shall accept as many incoming client connections as * possible and can only be limited by the system resources. Once the listening socket becomes * readable, which means an incoming connection attempt has arrived. We create a new socket * object and associate it with the client. This is a repeated process until the socket owner * closes the socket. */ while ( pMyObject->bStarted ) { ANSC_COMMIT_TASK(); /* * To avoid letting the old half-dead sockets hogging up the system resource, we need to * periodically invoke the cleaning routine. The default interval is 10 seconds, and the * idle timeout value is 90 seconds. */ #if !defined(_ANSC_KERNEL) || !defined(_ANSC_LINUX) if ( pMyObject->bCleaningDemanded ) #else if ( FALSE ) /*if ( pMyObject->bCleaningDemanded )*/ #endif { pMyObject->Clean((ANSC_HANDLE)pMyObject); ulLastCleanAt = AnscGetTickInSecondsAbs(); pMyObject->bCleaningDemanded = FALSE; } else if ( (AnscGetTickInSecondsAbs() - ulLastCleanAt) >= ANSC_DETO_CLEAN_TASK_INTERVAL ) { pMyObject->Clean((ANSC_HANDLE)pMyObject); ulLastCleanAt = AnscGetTickInSecondsAbs(); pMyObject->bCleaningDemanded = FALSE; } /* * Since the original bsd compatible socket api doesn't support asynchronous operation, the * nonblocking status polling is the best we can get. As a matter of fact, the current unix * and linux actually still don't support asynchronous notification on any socket operation. */ #if defined(_ANSC_KERNEL) && defined(_ANSC_LINUX) if ( !pMyObject->CurSocketCount) { if ( pServer->Mode & ANSC_DSTO_MODE_EVENT_SYNC ) { AnscWaitEvent (&pMyObject->NewSocketEvent, ANSC_DETO_WAIT_EVENT_INTERVAL); AnscResetEvent(&pMyObject->NewSocketEvent); if (!pMyObject->CurSocketCount) { AnscTaskRelinquish(); continue; } } else { AnscSleep(ANSC_DETO_TASK_BREAK_INTERVAL); continue; } } #else if ( pServer->Mode & ANSC_DSTO_MODE_XSOCKET ) { AnscAcquireLock(&pMyObject->RecvSocketSetLock); *read_fd_set2 = *pRecvSet2; AnscReleaseLock(&pMyObject->RecvSocketSetLock); } else { AnscAcquireLock(&pMyObject->RecvSocketSetLock); *read_fd_set1 = *pRecvSet1; AnscReleaseLock(&pMyObject->RecvSocketSetLock); } if ( ( (pServer->Mode & ANSC_DSTO_MODE_XSOCKET) && XSKT_SOCKET_FD_ISNUL(read_fd_set2)) || (!(pServer->Mode & ANSC_DSTO_MODE_XSOCKET) && ANSC_SOCKET_FD_ISNUL(read_fd_set1)) ) { if ( pServer->Mode & ANSC_DSTO_MODE_EVENT_SYNC ) { AnscWaitEvent (&pMyObject->NewSocketEvent, ANSC_DETO_WAIT_EVENT_INTERVAL); AnscResetEvent(&pMyObject->NewSocketEvent); if ( pServer->Mode & ANSC_DSTO_MODE_XSOCKET ) { AnscAcquireLock(&pMyObject->RecvSocketSetLock); *read_fd_set2 = *pRecvSet2; AnscReleaseLock(&pMyObject->RecvSocketSetLock); } else { AnscAcquireLock(&pMyObject->RecvSocketSetLock); *read_fd_set1 = *pRecvSet1; AnscReleaseLock(&pMyObject->RecvSocketSetLock); } if ( ( (pServer->Mode & ANSC_DSTO_MODE_XSOCKET) && XSKT_SOCKET_FD_ISNUL(read_fd_set2)) || (!(pServer->Mode & ANSC_DSTO_MODE_XSOCKET) && ANSC_SOCKET_FD_ISNUL(read_fd_set1)) ) { AnscTaskRelinquish(); continue; } } else { AnscSleep(ANSC_DETO_TASK_BREAK_INTERVAL); continue; } } if ( pServer->Mode & ANSC_DSTO_MODE_XSOCKET ) { timeval2.tv_sec = (ANSC_DETO_POLL_INTERVAL_MS / 1000); /* number of seconds */ timeval2.tv_usec = (ANSC_DETO_POLL_INTERVAL_MS % 1000) * 1000; /* number of microseconds */ } else { timeval1.tv_sec = (ANSC_DETO_POLL_INTERVAL_MS / 1000); /* number of seconds */ timeval1.tv_usec = (ANSC_DETO_POLL_INTERVAL_MS % 1000) * 1000; /* number of microseconds */ } /* * The _ansc_select() function returns the total number of socket handles that are ready * and contained in the fd_set structures, zero if the time limit expired, or SOCKET_ERROR * if an error occurred. Upon return, the structures are updated to reflect the subset of * these sockets that meet the specified condition. */ if ( pServer->Mode & ANSC_DSTO_MODE_XSOCKET ) { s_result = _xskt_select(XSKT_SOCKET_FD_SETSIZE, read_fd_set2, NULL, NULL, &timeval2); } else { s_result = _ansc_select(ANSC_SOCKET_FD_SETSIZE, read_fd_set1, NULL, NULL, &timeval1); } if ( s_result == 0 ) { continue; } else if ( ( (pServer->Mode & ANSC_DSTO_MODE_XSOCKET) && (s_result == XSKT_SOCKET_ERROR)) || (!(pServer->Mode & ANSC_DSTO_MODE_XSOCKET) && (s_result == ANSC_SOCKET_ERROR)) ) { s_error = (pServer->Mode & ANSC_DSTO_MODE_XSOCKET)? _xskt_get_last_error() : _ansc_get_last_error(); /* * Previously we simply reset everything when _ansc_select() fails, which is not a good * solution: we shall notify the worker module and gracefully shutdown the socket(s) * that caused the error. */ /* pMyObject->Reset((ANSC_HANDLE)pMyObject); */ pMyObject->ExpAllSockets((ANSC_HANDLE)pMyObject); continue; } else if ( !pMyObject->bStarted ) { break; } #endif /* * If there're multiple sockets are receiving data, we loop through the returned fd_set * structure and process them one-by-one. However, we have a slight problem: the resulted * fd_set consists of only the native socket handles, not the associated Socket Objects. * We have to first retrieve the peer's IP address from the socket, and use it to find * the associated socket object. */ #if defined(_ANSC_KERNEL) && defined(_ANSC_LINUX) if (TRUE) { int i; PSINGLE_LINK_ENTRY pSLinkEntry; for ( i = 0; i < ANSC_DETO_SOCKET_TABLE_SIZE ; i++) { if (!AnscSListQueryDepth(&pMyObject->SocketTable[i])) { continue; } AnscAcquireLock(&pMyObject->SocketTableLock); pSLinkEntry = AnscSListGetFirstEntry(&pMyObject->SocketTable[i]); AnscReleaseLock(&pMyObject->SocketTableLock); while ( pSLinkEntry ) { pSocket = ACCESS_ANSC_DAEMON_SOCKET_TCP_OBJECT(pSLinkEntry); pSLinkEntry = AnscSListGetNextEntry(pSLinkEntry); if ( pSocket->bTlsEnabled ) { pMyObject->bBusy = TRUE; returnStatus = pMyObject->Recv2 ( (ANSC_HANDLE)pMyObject, (ANSC_HANDLE)pSocket ); pMyObject->bBusy = FALSE; } else { pMyObject->bBusy = TRUE; returnStatus = pMyObject->Recv ( (ANSC_HANDLE)pMyObject, (ANSC_HANDLE)pSocket ); pMyObject->bBusy = FALSE; } } if ( !pMyObject->bStarted ) { break; } } AnscSleep(10); } #else for ( i = 0; i < s_result; i++ ) { if ( pServer->Mode & ANSC_DSTO_MODE_XSOCKET ) { XSKT_SOCKET_FD_GET(read_fd_set2, s_socket, (ULONG)i); } else { ANSC_SOCKET_FD_GET(read_fd_set1, s_socket, (ULONG)i); } if ( ( (pServer->Mode & ANSC_DSTO_MODE_XSOCKET) && (s_socket == XSKT_SOCKET_INVALID_SOCKET)) || (!(pServer->Mode & ANSC_DSTO_MODE_XSOCKET) && (s_socket == ANSC_SOCKET_INVALID_SOCKET)) ) { break; } else { pSocket = (PANSC_DAEMON_SOCKET_TCP_OBJECT)pMyObject->GetSocketByOsocket ( (ANSC_HANDLE)pMyObject, s_socket ); /* * We should make sure this socket is still valid before proceeding with the socket * receive operations. For example, the peer may have already closed or reset the * TCP connection while we're serving the previous socket request. * * 10/06/04 - It's believed this modification is slowing down the GUI and we're not * seeing tangible evidence that GUI responsivenss has been improved. So we disable * it for now. */ /* if ( pServer->Mode & ANSC_DSTO_MODE_XSOCKET ) { XSKT_SOCKET_FD_ZERO(excp_fd_set2); XSKT_SOCKET_FD_SET ((XSKT_SOCKET)s_socket, excp_fd_set2); timeval2.tv_sec = 0; timeval2.tv_usec = 0; s_result_excp = _xskt_select(XSKT_SOCKET_FD_SETSIZE, NULL, NULL, excp_fd_set2, &timeval2); } else { ANSC_SOCKET_FD_ZERO(excp_fd_set1); ANSC_SOCKET_FD_SET (s_socket, excp_fd_set1); timeval1.tv_sec = 0; timeval1.tv_usec = 0; s_result_excp = _ansc_select(ANSC_SOCKET_FD_SETSIZE, NULL, NULL, excp_fd_set1, &timeval1); } */ } /* if ( ((s_result_excp == 1 ) ) || ((s_result_excp == XSKT_SOCKET_ERROR) && (pServer->Mode & ANSC_DSTO_MODE_XSOCKET)) || ((s_result_excp == ANSC_SOCKET_ERROR) && !(pServer->Mode & ANSC_DSTO_MODE_XSOCKET)) ) { if ( TRUE ) { pSocket->bBroken = TRUE; pMyObject->EnableRecv((ANSC_HANDLE)pMyObject, (ANSC_HANDLE)pSocket, FALSE); pMyObject->EnableSend((ANSC_HANDLE)pMyObject, (ANSC_HANDLE)pSocket, FALSE); } if ( pSocket->bTlsEnabled ) { if ( pSocket->bTlsEnabled && pSocket->bTlsConnected && !pSocket->bTlsInitializing ) { returnStatus = pWorker->Notify ( pWorker->hWorkerContext, (ANSC_HANDLE)pSocket, ANSC_DSTOWO_EVENT_SOCKET_ERROR, (ANSC_HANDLE)NULL ); } else { AnscSetEvent(&pSocket->TlsConnEvent); } } else { returnStatus = pWorker->Notify ( pWorker->hWorkerContext, (ANSC_HANDLE)pSocket, ANSC_DSTOWO_EVENT_SOCKET_ERROR, (ANSC_HANDLE)NULL ); } if ( pServer->Mode & ANSC_DSTO_MODE_AUTO_CLOSE ) { pMyObject->DelSocket((ANSC_HANDLE)pMyObject, (ANSC_HANDLE)pSocket); } pMyObject->TrcCount++; continue; } else { pSocket = (PANSC_DAEMON_SOCKET_TCP_OBJECT)pMyObject->GetSocketByOsocket ( (ANSC_HANDLE)pMyObject, s_socket ); } */ if ( !pSocket ) { continue; } else if ( pSocket->bTlsEnabled ) { #ifdef _ANSC_USE_OPENSSL_ pMyObject->bBusy = TRUE; returnStatus = pMyObject->Recv ( (ANSC_HANDLE)pMyObject, (ANSC_HANDLE)pSocket ); pMyObject->bBusy = FALSE; #else pMyObject->bBusy = TRUE; returnStatus = pMyObject->Recv2 ( (ANSC_HANDLE)pMyObject, (ANSC_HANDLE)pSocket ); pMyObject->bBusy = FALSE; #endif } else { pMyObject->bBusy = TRUE; returnStatus = pMyObject->Recv ( (ANSC_HANDLE)pMyObject, (ANSC_HANDLE)pSocket ); pMyObject->bBusy = FALSE; } /* * Check whether 'bToBeCleaned' flag is set for this socket: if it is, we should close * this socket right away; otherwise, we continue the processing. WARNING!!! This new * change seems to incur instability in SLAP, we have to roll back to the initial * approach. */ /* if ( pSocket->bToBeCleaned ) { returnStatus = pMyObject->DelSocket ( (ANSC_HANDLE)pMyObject, (ANSC_HANDLE)pSocket ); } */ if ( !pMyObject->bStarted ) { break; } } #endif } /****************************************************************** GRACEFUL ROLLBACK PROCEDURES AND EXIT DOORS ******************************************************************/ EXIT1: AnscSetEvent(&pMyObject->RecvEvent); #if !defined(_ANSC_KERNEL) || !defined(_ANSC_LINUX) if ( read_fd_set1 ) { AnscFreeMemory(read_fd_set1); } if ( read_fd_set2 ) { AnscFreeMemory(read_fd_set2); } if ( excp_fd_set1 ) { AnscFreeMemory(excp_fd_set1); } if ( excp_fd_set2 ) { AnscFreeMemory(excp_fd_set2); } #endif return ANSC_STATUS_SUCCESS; }