/*----------------------------------------------------------------------* rtp_net_getifaceaddr *----------------------------------------------------------------------*/ int rtp_net_getifaceaddr (unsigned char *localAddr, unsigned char *remoteAddr, int remotePort, int remoteType) { RTP_SOCKET tempSock; int localPort; int localType; if (rtp_net_socket_datagram(&tempSock) >= 0) { /* determine the local IP address that is receiving this request by creating a temporary UDP socket and connecting it back to the sender; we then query the IP address of the temp socket using getsockname. */ if (rtp_net_connect(tempSock, remoteAddr, remotePort, remoteType) >= 0) { if (rtp_net_getsockname(tempSock, localAddr, &localPort, &localType) >= 0) { rtp_net_closesocket(tempSock); return (0); } } rtp_net_closesocket(tempSock); } return (-1); }
/*---------------------------------------------------------------------------*/ HTTP_UINT16 HTTP_ServerGetPort (HTTPServerContext *server) { RTP_NET_ADDR serverAddr; if (rtp_net_getsockname(server->serverSocket, serverAddr.ipAddr, &serverAddr.port, &serverAddr.type) < 0) { return (0); } return (serverAddr.port); }
int RTIP_SOCKETS_Driver::GetSockName( SOCK_SOCKET socket, SOCK_sockaddr* name, int* namelen ) { NATIVE_PROFILE_PAL_NETWORK(); int ret; int port; int type = RTP_NET_TYPE_IPV4; ret = rtp_net_getsockname ((RTP_HANDLE) socket, (unsigned char *) &((SOCK_sockaddr_in *) name)->sin_addr, &port, &type); ((SOCK_sockaddr_in *) name)->sin_family = RTP_NET_AF_INET; ((SOCK_sockaddr_in *) name)->sin_port = port; return ret; }
int HTTP_ServerInit (HTTPServerContext *server, /** pointer to uninitialized \Ref{HTTPServerContext} struct */ const HTTP_CHAR *name, /** Server name. Sent in HTTP header. */ const HTTP_CHAR *rootDir, /** Path to the root directory for content. */ const HTTP_CHAR *defaultFile, /** Default Filename if "\" or "path\" is passed with no file name. */ HTTP_INT8 httpMajorVersion, /** Normally 1 for HTTP 1.1 */ HTTP_INT8 httpMinorVersion, /** Normally 1 for HTTP 1.1 */ HTTP_UINT8 *ipAddr, /** V4/V6 examples: ipaddr[4] = {192, 168, 0, 6}; ipAddr = "fe80::20b:dbff:fe2f:c162"; */ HTTP_INT16 port, /** port number (80) usually */ HTTP_INT16 ipType, /** ipversion 4 or 6 */ HTTP_BOOL allowKeepAlive, /** If HTTP version < 1.1 enables keep-alive support. For 1,1 and greater keep-alive is the default.*/ HTTPServerConnection* connectCtxArray, /** pointer to uninitialized bytes, must be (sizeof(HTTPServerConnection) * maxConnections)) bytes. */ HTTP_INT16 maxConnections, /** The maximum limit on simultaneous HTTP server connections */ HTTP_INT16 maxHelperThreads /** If HTTP_SERVER_MULTITHREAD is defined, the max number of helper threads to spawn */ ) { rtp_memset((unsigned char *) server, 0, sizeof(HTTPServerContext)); if (rtp_net_socket_stream_dual(&server->serverSocket, ipType) >= 0) { if (rtp_net_bind_dual(server->serverSocket, RTP_NET_STREAM, ipAddr, port, ipType) >= 0 && rtp_net_listen(server->serverSocket, maxConnections) >= 0) { if (!ipAddr) { if (rtp_net_getsockname(server->serverSocket, server->serverAddress.ipAddr, &server->serverAddress.port, &server->serverAddress.type) < 0) { rtp_memset(server->serverAddress.ipAddr, 0, RTP_NET_IP_ALEN); } } else { rtp_memcpy(server->serverAddress.ipAddr, ipAddr, RTP_NET_IP_ALEN); server->serverAddress.port = port; } if (name) { rtp_strncpy(server->serverName, name, HTTP_SERVER_NAME_LEN-1); } else { rtp_strncpy(server->serverName, HTTP_SERVER_NAME_STRING, HTTP_SERVER_NAME_LEN-1); } server->httpMajorVersion = httpMajorVersion; server->httpMinorVersion = httpMinorVersion; if (rootDir) { rtp_strncpy(server->rootDir, rootDir, HTTP_SERVER_PATH_LEN-1); } else { /* default to the current working directory */ server->rootDir[0] = '.'; server->rootDir[1] = 0; } if (defaultFile) { rtp_strncpy(server->defaultFile, defaultFile, HTTP_SERVER_DEFAULT_FILE_LEN-1); } else { rtp_strncpy(server->defaultFile, HTTP_SERVER_DEFAULT_FILE, HTTP_SERVER_DEFAULT_FILE_LEN-1); } if (!allowKeepAlive || (server->httpMajorVersion == 1 && server->httpMinorVersion > 0) || (server->httpMajorVersion > 1)) { server->allowKeepAlive = allowKeepAlive; server->maxConnections = maxConnections; DLLIST_INIT(&server->pathList); DLLIST_INIT(&server->realmList); server->allowedClientList = 0; server->numAllowedClients = 0; server->blockedClientList = 0; server->numBlockedClients = 0; #ifdef HTTP_SERVER_KEEP_ALIVE DLLIST_INIT(&server->openConnections); #endif /* HTTP_SERVER_KEEP_ALIVE */ #ifdef HTTP_SERVER_MULTITHREAD server->connectionContextArray = connectCtxArray; server->maxHelperThreads = maxHelperThreads; /* allocate the server lock */ if (rtp_sig_mutex_alloc(&server->lock, 0) >= 0) { RTP_MUTEX helperThreadMutex; RTP_SEMAPHORE helperThreadSemaphore; /* initialize the session queue */ if (rtp_sig_mutex_alloc(&helperThreadMutex, 0) >= 0) { if (rtp_sig_semaphore_alloc(&helperThreadSemaphore, 0) >= 0) { if (rtp_helper_thread_ctx_init ( &server->helperContext, helperThreadMutex, helperThreadSemaphore ) >= 0) { int n; // initialize the free job list server->jobFreeList = connectCtxArray; for (n = 0; n < maxConnections-1; n++) { connectCtxArray[n].job.next = &(connectCtxArray[n+1].job); connectCtxArray[n].job.execute = _HTTP_ServerExecuteJob; connectCtxArray[n].server = server; } connectCtxArray[n].job.next = 0; connectCtxArray[n].job.execute = _HTTP_ServerExecuteJob; connectCtxArray[n].server = server; server->status = HTTP_SERVER_INITIALIZED; return (0); } rtp_sig_semaphore_free(helperThreadSemaphore); } rtp_sig_mutex_free(helperThreadMutex); } rtp_sig_mutex_free(server->lock); } #else /* HTTP_SERVER_MULTITHREAD */ //server->queueFirst = 0; //server->queueLast = 0; server->status = HTTP_SERVER_INITIALIZED; return (0); #endif /* HTTP_SERVER_MULTITHREAD */ } } rtp_net_closesocket(server->serverSocket); } return (-1); }
/*---------------------------------------------------------------------------*/ int HTTP_ServerRequestGetLocalAddr ( HTTPServerRequestContext *ctx, RTP_NET_ADDR *local) { return (rtp_net_getsockname(ctx->socketHandle, local->ipAddr, &local->port, &local->type)); }