From 8312080c27c9572fd5889b8d9ee5074462f7a7fb Mon Sep 17 00:00:00 2001 From: Sandro Kalatozishvili Date: Mon, 25 Nov 2024 03:36:32 +0400 Subject: [PATCH] Updated XSocket API, changed socket type with flags --- src/data/array.c | 9 +- src/net/api.c | 16 ++- src/net/http.c | 14 +-- src/net/sock.c | 269 ++++++++++++++++++++++++----------------------- src/net/sock.h | 73 +++++++------ src/xdef.h | 4 + src/xver.h | 2 +- 7 files changed, 200 insertions(+), 187 deletions(-) diff --git a/src/data/array.c b/src/data/array.c index 0c16890..04d06b9 100644 --- a/src/data/array.c +++ b/src/data/array.c @@ -158,16 +158,18 @@ void XArray_Destroy(xarray_t *pArr) XArray_Clear(pArr); xpool_t *pPool = pArr->pPool; + uint8_t nAlloc = pArr->nAlloc; uint8_t nOwnPool = pArr->nOwnPool; xfree(pPool, pArr->pData); pArr->pData = NULL; + pArr->pPool = NULL; pArr->nSize = 0; pArr->nFixed = 0; - pArr->pPool = NULL; + pArr->nAlloc = 0; pArr->nOwnPool = 0; - if (pArr->nAlloc) xfreen(pPool, pArr, sizeof(xarray_t)); + if (nAlloc) xfreen(pPool, pArr, sizeof(xarray_t)); if (nOwnPool) XPool_Destroy(pPool); } @@ -232,8 +234,11 @@ size_t XArray_CheckSpace(xarray_t *pArr) if (pArr->pData == NULL) { uint8_t nAlloc = pArr->nAlloc; + uint8_t nOwnPool = pArr->nOwnPool; xarray_clear_cb_t clearCb = pArr->clearCb; + XArray_Init(pArr, pArr->pPool, XARRAY_INITIAL_SIZE, 0); + pArr->nOwnPool = nOwnPool; pArr->clearCb = clearCb; pArr->nAlloc = nAlloc; } diff --git a/src/net/api.c b/src/net/api.c index ace9e31..f314b89 100644 --- a/src/net/api.c +++ b/src/net/api.c @@ -1170,11 +1170,10 @@ XSTATUS XAPI_Listen(xapi_t *pApi, xapi_endpoint_t *pEndpt) pApiData->nPort = pEndpt->nPort; pApiData->eRole = XAPI_SERVER; - xsock_type_t eType = pEndpt->bTLS ? - XSOCK_SSL_PREFERED_SERVER : - XSOCK_TCP_SERVER; + uint32_t nFlags = XSOCK_TCP_SERVER; + if (pEndpt->bTLS) nFlags |= XSOCK_SSL; - XSock_Create(pSock, eType, pEndpt->pAddr, pEndpt->nPort); + XSock_Create(pSock, nFlags, pEndpt->pAddr, pEndpt->nPort); if (pEndpt->bTLS) XSock_SetSSLCert(pSock, &pEndpt->certs); XSock_ReuseAddr(pSock, XTRUE); @@ -1247,12 +1246,11 @@ XSTATUS XAPI_Connect(xapi_t *pApi, xapi_endpoint_t *pEndpt) pApiData->pSessionData = pEndpt->pSessionData; pApiData->nPort = pEndpt->nPort; pApiData->eRole = XAPI_CLIENT; - - xsock_type_t eType = pEndpt->bTLS ? - XSOCK_SSL_PREFERED_CLIENT : - XSOCK_TCP_CLIENT; - XSock_Create(pSock, eType, pApiData->sAddr, pEndpt->nPort); + uint32_t nFlags = XSOCK_TCP_CLIENT; + if (pEndpt->bTLS) nFlags |= XSOCK_SSL; + + XSock_Create(pSock, nFlags, pApiData->sAddr, pEndpt->nPort); XSock_NonBlock(pSock, XTRUE); if (pApiData->sock.nFD == XSOCK_INVALID) diff --git a/src/net/http.c b/src/net/http.c index e3d6693..c428203 100644 --- a/src/net/http.c +++ b/src/net/http.c @@ -1011,12 +1011,12 @@ xhttp_status_t XHTTP_LinkExchange(xhttp_t *pRequest, xhttp_t *pResponse, xlink_t if (strncmp(pLink->sProtocol, "http", 4)) return XHTTP_StatusCb(pRequest, XHTTP_ERRPROTO); - xsock_type_t eType = XSOCK_TCP_CLIENT; xsock_t sock; + uint32_t nFlags = XSOCK_TCP_CLIENT; if (!strncmp(pLink->sProtocol, "https", 5)) { - eType = XSOCK_SSL_PREFERED_CLIENT; + nFlags |= XSOCK_SSL; XSock_InitSSL(); } @@ -1024,7 +1024,7 @@ xhttp_status_t XHTTP_LinkExchange(xhttp_t *pRequest, xhttp_t *pResponse, xlink_t XHTTP_SetAuthBasic(pRequest, pLink->sUser, pLink->sPass) <= 0) return XHTTP_StatusCb(pRequest, XHTTP_ERRAUTH); - if (XSock_Setup(&sock, eType, pLink->sHost) == XSOCK_INVALID) + if (XSock_Setup(&sock, nFlags, pLink->sHost) == XSOCK_INVALID) return XHTTP_StatusCb(pRequest, XHTTP_ERRCONNECT); if (pRequest->nTimeout) @@ -1083,13 +1083,13 @@ xhttp_status_t XHTTP_LinkPerform(xhttp_t *pHttp, xlink_t *pLink, const uint8_t * if (strncmp(pLink->sProtocol, "http", 4)) return XHTTP_StatusCb(pHttp, XHTTP_ERRPROTO); - xsock_type_t eType = XSOCK_TCP_CLIENT; + uint32_t nFlags = XSOCK_TCP_CLIENT; xsock_addr_t addrInfo; xsock_t sock; if (!strncmp(pLink->sProtocol, "https", 5)) { - eType = XSOCK_SSL_PREFERED_CLIENT; + nFlags |= XSOCK_SSL; XSock_InitSSL(); } @@ -1101,7 +1101,7 @@ xhttp_status_t XHTTP_LinkPerform(xhttp_t *pHttp, xlink_t *pLink, const uint8_t * return XHTTP_StatusCb(pHttp, XHTTP_ERRRESOLVE); addrInfo.nPort = addrInfo.nPort ? addrInfo.nPort : - (XSockType_IsSSL(eType) ? XHTTP_SSL_PORT : XHTTP_DEF_PORT); + (XSockFlags_CheckSSL(nFlags) ? XHTTP_SSL_PORT : XHTTP_DEF_PORT); if (XHTTP_CHECK_FLAG(pHttp->nCbTypes, XHTTP_STATUS) && pHttp->callback != NULL) { @@ -1120,7 +1120,7 @@ xhttp_status_t XHTTP_LinkPerform(xhttp_t *pHttp, xlink_t *pLink, const uint8_t * pHttp->callback(pHttp, &cbCtx); } - if (XSock_Open(&sock, eType, &addrInfo) == XSOCK_INVALID) + if (XSock_Open(&sock, nFlags, &addrInfo) == XSOCK_INVALID) return XHTTP_StatusCb(pHttp, XHTTP_ERRCONNECT); if (pHttp->nTimeout) diff --git a/src/net/sock.c b/src/net/sock.c index c320b2b..e0c1322 100644 --- a/src/net/sock.c +++ b/src/net/sock.c @@ -35,7 +35,7 @@ typedef SSIZE_T ssize_t; xsock_inaddr_t* XSock_InAddr(xsock_t *pSock) { return &pSock->inAddr; } xsock_status_t XSock_Status(const xsock_t *pSock) { return pSock->eStatus; } -xsock_type_t XSock_GetType(const xsock_t *pSock) { return pSock->eType; } +uint32_t XSock_GetFlags(const xsock_t *pSock) { return pSock->nFlags; } XSOCKET XSock_GetFD(const xsock_t *pSock) { return pSock->nFD; } xbool_t XSock_IsSSL(const xsock_t *pSock) { return (xbool_t)pSock->nSSL; } @@ -47,21 +47,34 @@ size_t XSock_GetFDMax(const xsock_t *pSock) { return pSock->nFdMax; } int XSock_GetSockType(const xsock_t *pSock) { return pSock->nType; } int XSock_GetProto(const xsock_t *pSock) { return pSock->nProto; } -xbool_t XSockType_IsSSL(xsock_type_t eType) +xbool_t XSockFlags_CheckSSL(uint32_t nFlags) { - if (eType == XSOCK_SSL_PREFERED_CLIENT || - eType == XSOCK_SSL_PREFERED_SERVER || - eType == XSOCK_SSLV2_SERVER || - eType == XSOCK_SSLV3_SERVER || - eType == XSOCK_SSLV2_CLIENT || - eType == XSOCK_SSLV3_CLIENT || - eType == XSOCK_SSLV2_PEER || - eType == XSOCK_SSLV3_PEER) + if (XFLAGS_CHECK(nFlags, XSOCK_SSL) || + XFLAGS_CHECK(nFlags, XSOCK_SSLV2) || + XFLAGS_CHECK(nFlags, XSOCK_SSLV3)) return XTRUE; return XFALSE; } +uint32_t XSockFlags_Adjust(uint32_t nFlags) +{ + if (XFLAGS_CHECK(nFlags, XSOCK_SSLV2) || + XFLAGS_CHECK(nFlags, XSOCK_SSLV3)) + { + nFlags |= XSOCK_SSL; + } + + if (XFLAGS_CHECK(nFlags, XSOCK_BROADCAST) || + XFLAGS_CHECK(nFlags, XSOCK_MULTICAST) || + XFLAGS_CHECK(nFlags, XSOCK_UNICAST)) + { + nFlags |= XSOCK_UDP; + } + + return nFlags; +} + #ifdef XSOCK_USE_SSL static XATOMIC g_nSSLInit = 0; @@ -140,58 +153,70 @@ static void XSock_SSLConnected(xsock_t *pSock, xbool_t bConnected) if (pPriv != NULL) pPriv->bConnected = bConnected; } -static xsock_type_t XSock_GetPrefredSSL(xsock_type_t eType) +static uint32_t XSock_GetPrefredSSL(uint32_t nFlags) { - if (eType == XSOCK_SSL_PREFERED_CLIENT) + if (!XFLAGS_CHECK(nFlags, XSOCK_SSL)) + return nFlags; + + if (XFLAGS_CHECK(nFlags, XSOCK_CLIENT)) { #ifdef SSLv3_client_method - return XSOCK_SSLV3_CLIENT; + nFlags |= XSOCK_SSLV3; #else - return XSOCK_SSLV2_CLIENT; + nFlags |= XSOCK_SSLV2; #endif } - else if (eType == XSOCK_SSL_PREFERED_SERVER) + else if (XFLAGS_CHECK(nFlags, XSOCK_SERVER)) { #ifdef SSLv3_server_method - return XSOCK_SSLV3_SERVER; + nFlags |= XSOCK_SSLV3; #else - return XSOCK_SSLV2_SERVER; + nFlags |= XSOCK_SSLV2; #endif } - return eType; + return nFlags; } static const SSL_METHOD* XSock_GetSSLMethod(xsock_t *pSock) { - switch (pSock->eType) + if (XFLAGS_CHECK(pSock->nFlags, XSOCK_CLIENT)) { - case XSOCK_SSLV2_CLIENT: -#ifdef SSLv23_client_method - return SSLv23_client_method(); + if (XFLAGS_CHECK(pSock->nFlags, XSOCK_SSLV3)) + { +#ifdef SSLv3_client_method + return SSLv3_client_method(); #else - break; + return NULL; #endif - case XSOCK_SSLV2_SERVER: -#ifdef SSLv23_server_method - return SSLv23_server_method(); + } + else if (XFLAGS_CHECK(pSock->nFlags, XSOCK_SSLV2)) + { +#ifdef SSLv23_client_method + return SSLv23_client_method(); #else - break; + return NULL; #endif - case XSOCK_SSLV3_CLIENT: -#ifdef SSLv3_client_method - return SSLv3_client_method(); + } + } + else if (XFLAGS_CHECK(pSock->nFlags, XSOCK_SERVER)) + { + if (XFLAGS_CHECK(pSock->nFlags, XSOCK_SSLV3)) + { +#ifdef SSLv3_server_method + return SSLv3_server_method(); #else - break; + return NULL; #endif - case XSOCK_SSLV3_SERVER: -#ifdef SSLv3_server_method - return SSLv3_server_method(); + } + else if (XFLAGS_CHECK(pSock->nFlags, XSOCK_SSLV2)) + { +#ifdef SSLv23_server_method + return SSLv23_server_method(); #else - break; + return NULL; #endif - default: - break; + } } return NULL; @@ -334,6 +359,8 @@ const char* XSock_GetStatusStr(xsock_status_t eStatus) return "Can not read from SSL socket"; case XSOCK_ERR_SSLWRITE: return "Can not write to SSL socket"; + case XSOCK_ERR_FLAGS: + return "Invalid or empty socket flags"; case XSOCK_ERR_INVSSL: return "Invalid SSL or SSL context"; case XSOCK_ERR_SYSCALL: @@ -384,52 +411,38 @@ XSTATUS XSock_Check(xsock_t *pSock) return XSOCK_SUCCESS; } -XSTATUS XSock_SetType(xsock_t *pSock, xsock_type_t eType) +static XSTATUS XSock_SetFlags(xsock_t *pSock, uint32_t nFlags) { pSock->eStatus = XSOCK_ERR_NONE; - pSock->eType = eType; - - switch(eType) - { - case XSOCK_SSLV2_PEER: - case XSOCK_SSLV3_PEER: - case XSOCK_SSLV2_CLIENT: - case XSOCK_SSLV3_CLIENT: - case XSOCK_SSLV2_SERVER: - case XSOCK_SSLV3_SERVER: - case XSOCK_TCP_CLIENT: - case XSOCK_TCP_SERVER: - case XSOCK_TCP_PEER: - pSock->nProto = IPPROTO_TCP; - pSock->nType = SOCK_STREAM; - break; - case XSOCK_UDP_CLIENT: - case XSOCK_UDP_BCAST: - case XSOCK_UDP_MCAST: - case XSOCK_UDP_UCAST: - pSock->nProto = IPPROTO_UDP; - pSock->nType = SOCK_DGRAM; - break; - case XSOCK_TCP_RAW: - pSock->nProto = IPPROTO_TCP; - pSock->nType = SOCK_RAW; - break; - case XSOCK_UDP_RAW: - pSock->nProto = IPPROTO_UDP; - pSock->nType = SOCK_DGRAM; - break; - case XSOCK_UNDEFINED: - default: - pSock->eStatus = XSOCK_ERR_SUPPORT; - pSock->nProto = XSOCK_ERROR; - pSock->nType = XSOCK_ERROR; - return XSOCK_ERROR; + pSock->nFlags = nFlags; + + if (XFLAGS_CHECK(nFlags, XSOCK_TCP)) + { + pSock->nProto = IPPROTO_TCP; + pSock->nType = SOCK_STREAM; + } + else if (XFLAGS_CHECK(nFlags, XSOCK_UDP)) + { + pSock->nProto = IPPROTO_UDP; + pSock->nType = SOCK_DGRAM; + } + else if (XFLAGS_CHECK(nFlags, XSOCK_RAW)) + { + pSock->nProto = IPPROTO_RAW; + pSock->nType = SOCK_RAW; + } + else + { + pSock->eStatus = XSOCK_ERR_SUPPORT; + pSock->nProto = XSOCK_ERROR; + pSock->nType = XSOCK_ERROR; + return XSOCK_ERROR; } return XSOCK_SUCCESS; } -XSTATUS XSock_Init(xsock_t *pSock, xsock_type_t eType, XSOCKET nFD, uint8_t nNB) +XSTATUS XSock_Init(xsock_t *pSock, uint32_t nFlags, XSOCKET nFD, uint8_t nNB) { memset(&pSock->inAddr, 0, sizeof(pSock->inAddr)); pSock->pPrivate = NULL; @@ -440,10 +453,17 @@ XSTATUS XSock_Init(xsock_t *pSock, xsock_type_t eType, XSOCKET nFD, uint8_t nNB) pSock->nFD = nFD; pSock->nNB = nNB; + nFlags = XSockFlags_Adjust(nFlags); + if (nFlags == XSOCK_UNDEFINED) + { + pSock->eStatus = XSOCK_ERR_FLAGS; + return XSOCK_ERROR; + } + #ifdef XSOCK_USE_SSL - eType = XSock_GetPrefredSSL(eType); + nFlags = XSock_GetPrefredSSL(nFlags); - if (XSockType_IsSSL(eType)) + if (XSockFlags_CheckSSL(nFlags)) { pSock->pPrivate = XSock_AllocPriv(); if (pSock->pPrivate == NULL) @@ -455,7 +475,7 @@ XSTATUS XSock_Init(xsock_t *pSock, xsock_type_t eType, XSOCKET nFD, uint8_t nNB) } #endif - return XSock_SetType(pSock, eType); + return XSock_SetFlags(pSock, nFlags); } void XSock_Close(xsock_t *pSock) @@ -799,11 +819,11 @@ XSOCKET XSock_Accept(xsock_t *pSock, xsock_t *pNewSock) if (!XSock_Check(pSock)) return XSOCK_INVALID; xsocklen_t len = sizeof(pNewSock->inAddr); - xsock_type_t eType = XSock_IsSSL(pSock) ? - (pSock->eType == XSOCK_SSLV2_SERVER ? - XSOCK_SSLV2_PEER : XSOCK_SSLV3_PEER) : pSock->eType; + uint32_t nFlags = pSock->nFlags; + XFLAGS_DISABLE(nFlags, XSOCK_SERVER); + XFLAGS_ENABLE(nFlags, XSOCK_PEER); - if (XSock_Init(pNewSock, eType, XSOCK_INVALID, XFALSE) < 0) return XSOCK_INVALID; + if (XSock_Init(pNewSock, nFlags, XSOCK_INVALID, XFALSE) < 0) return XSOCK_INVALID; pNewSock->nFD = accept(pSock->nFD, (struct sockaddr*)&pNewSock->inAddr, &len); if (pNewSock->nFD == XSOCK_INVALID) @@ -1139,7 +1159,7 @@ XSOCKET XSock_NoDelay(xsock_t *pSock, xbool_t nEnabled) if (!XSock_Check(pSock)) return XSOCK_INVALID; unsigned int nOpt = (unsigned int)nEnabled; - if (setsockopt(pSock->nFD, IPPROTO_TCP, TCP_NODELAY, (char*)&nOpt, sizeof(nOpt)) < 0) + if (setsockopt(pSock->nFD, pSock->nProto, TCP_NODELAY, (char*)&nOpt, sizeof(nOpt)) < 0) { pSock->eStatus = XSOCK_ERR_SETOPT; XSock_Close(pSock); @@ -1438,6 +1458,7 @@ XSOCKET XSock_InitSSLClient(xsock_t *pSock) const SSL_METHOD *pMethod = XSock_GetSSLMethod(pSock); if (pMethod == NULL) { + printf("SSL Method is NULL\n"); pSock->eStatus = XSOCK_ERR_SSLMET; XSock_Close(pSock); return XSOCK_INVALID; @@ -1491,11 +1512,11 @@ XSOCKET XSock_InitSSLClient(xsock_t *pSock) static XSOCKET XSock_SetupTCP(xsock_t *pSock) { if (!XSock_Check(pSock)) return XSOCK_INVALID; - if (pSock->eType == XSOCK_SSLV2_SERVER || - pSock->eType == XSOCK_SSLV3_SERVER || - pSock->eType == XSOCK_TCP_SERVER) + + if (XFLAGS_CHECK(pSock->nFlags, XSOCK_SERVER | XSOCK_TCP)) { - if (XSock_Bind(pSock) == XSOCK_INVALID) return XSOCK_INVALID; + if (XSock_Bind(pSock) == XSOCK_INVALID) + return XSOCK_INVALID; if (listen(pSock->nFD, (int)pSock->nFdMax) < 0) { @@ -1504,13 +1525,10 @@ static XSOCKET XSock_SetupTCP(xsock_t *pSock) return XSOCK_INVALID; } - if (pSock->eType == XSOCK_SSLV2_SERVER || - pSock->eType == XSOCK_SSLV3_SERVER) - XSock_InitSSLServer(pSock); + if (XFLAGS_CHECK(pSock->nFlags, XSOCK_SSL)) + XSock_InitSSLServer(pSock); } - else if (pSock->eType == XSOCK_SSLV2_CLIENT || - pSock->eType == XSOCK_SSLV3_CLIENT || - pSock->eType == XSOCK_TCP_CLIENT) + else if (XFLAGS_CHECK(pSock->nFlags, XSOCK_CLIENT | XSOCK_TCP)) { if (connect(pSock->nFD, (struct sockaddr *)&pSock->inAddr, sizeof(pSock->inAddr)) < 0) { @@ -1519,9 +1537,8 @@ static XSOCKET XSock_SetupTCP(xsock_t *pSock) return XSOCK_INVALID; } - if (pSock->eType == XSOCK_SSLV2_CLIENT || - pSock->eType == XSOCK_SSLV3_CLIENT) - XSock_InitSSLClient(pSock); + if (XFLAGS_CHECK(pSock->nFlags, XSOCK_SSL)) + XSock_InitSSLClient(pSock); } return pSock->nFD; @@ -1530,27 +1547,27 @@ static XSOCKET XSock_SetupTCP(xsock_t *pSock) static XSOCKET XSock_SetupUDP(xsock_t *pSock) { if (!XSock_Check(pSock)) return XSOCK_INVALID; - int nEnableFlag = 1; + int nEnabled = 1; - if (pSock->eType == XSOCK_UDP_BCAST) + if (XFLAGS_CHECK(pSock->nFlags, XSOCK_CLIENT | XSOCK_UDP)) { - if (setsockopt(pSock->nFD, SOL_SOCKET, SO_BROADCAST, (char*)&nEnableFlag, sizeof nEnableFlag) < 0) + if (connect(pSock->nFD, (struct sockaddr *)&pSock->inAddr, sizeof(pSock->inAddr)) < 0) { - pSock->eStatus = XSOCK_ERR_SETOPT; + pSock->eStatus = XSOCK_ERR_CONNECT; XSock_Close(pSock); return XSOCK_INVALID; } } - else if (pSock->eType == XSOCK_UDP_CLIENT) + else if (XFLAGS_CHECK(pSock->nFlags, XSOCK_BROADCAST | XSOCK_UDP)) { - if (connect(pSock->nFD, (struct sockaddr *)&pSock->inAddr, sizeof(pSock->inAddr)) < 0) + if (setsockopt(pSock->nFD, SOL_SOCKET, SO_BROADCAST, (char*)&nEnabled, sizeof nEnabled) < 0) { - pSock->eStatus = XSOCK_ERR_CONNECT; + pSock->eStatus = XSOCK_ERR_SETOPT; XSock_Close(pSock); return XSOCK_INVALID; } } - else if (pSock->eType == XSOCK_UDP_MCAST) + else if (XFLAGS_CHECK(pSock->nFlags, XSOCK_MULTICAST | XSOCK_UDP)) { if (XSock_ReuseAddr(pSock, 1) == XSOCK_INVALID) return XSOCK_INVALID; else if (XSock_Bind(pSock) == XSOCK_INVALID) return XSOCK_INVALID; @@ -1560,27 +1577,12 @@ static XSOCKET XSock_SetupUDP(xsock_t *pSock) return pSock->nFD; } -XSOCKET XSock_CreateRAW(xsock_t *pSock, int nProtocol) -{ - xsock_type_t eType = (nProtocol == IPPROTO_TCP) ? - XSOCK_TCP_RAW : XSOCK_UDP_RAW; - - XSock_Init(pSock, eType, XSOCK_INVALID, 0); - pSock->nFD = socket(AF_INET, SOCK_RAW, nProtocol); - - if (pSock->nFD == XSOCK_INVALID) - pSock->eStatus = XSOCK_ERR_CREATE; - - return pSock->nFD; -} - -XSOCKET XSock_CreateAdv(xsock_t *pSock, xsock_type_t eType, size_t nFdMax, const char *pAddr, uint16_t nPort) +XSOCKET XSock_CreateAdv(xsock_t *pSock, uint32_t nFlags, size_t nFdMax, const char *pAddr, uint16_t nPort) { - int nStatus = XSock_Init(pSock, eType, XSOCK_INVALID, 0); + int nStatus = XSock_Init(pSock, nFlags, XSOCK_INVALID, XFALSE); if (nStatus == XSOCK_ERROR) return XSOCK_INVALID; - if (pSock->eType != XSOCK_TCP_RAW && - pSock->eType != XSOCK_UDP_RAW) + if (!XFLAGS_CHECK(nFlags, XSOCK_RAW)) { pSock->nFdMax = XSTD_FIRSTOF(nFdMax, XSOCK_FD_MAX); pSock->nAddr = XSock_NetAddr(pAddr); @@ -1592,7 +1594,6 @@ XSOCKET XSock_CreateAdv(xsock_t *pSock, xsock_type_t eType, size_t nFdMax, const } int nType = pSock->nType; - #ifndef _WIN32 nType |= FD_CLOEXEC; #endif @@ -1610,12 +1611,12 @@ XSOCKET XSock_CreateAdv(xsock_t *pSock, xsock_type_t eType, size_t nFdMax, const return pSock->nFD; } -XSOCKET XSock_Create(xsock_t *pSock, xsock_type_t eType, const char *pAddr, uint16_t nPort) +XSOCKET XSock_Create(xsock_t *pSock, uint32_t nFlags, const char *pAddr, uint16_t nPort) { - return XSock_CreateAdv(pSock, eType, 0, pAddr, nPort); + return XSock_CreateAdv(pSock, nFlags, 0, pAddr, nPort); } -XSOCKET XSock_Open(xsock_t *pSock, xsock_type_t eType, xsock_addr_t *pAddr) +XSOCKET XSock_Open(xsock_t *pSock, uint32_t nFlags, xsock_addr_t *pAddr) { if (pAddr->sAddr[0] == XSTR_NUL || !pAddr->nPort) { @@ -1624,10 +1625,10 @@ XSOCKET XSock_Open(xsock_t *pSock, xsock_type_t eType, xsock_addr_t *pAddr) return XSOCK_INVALID; } - return XSock_Create(pSock, eType, pAddr->sAddr, pAddr->nPort); + return XSock_Create(pSock, nFlags, pAddr->sAddr, pAddr->nPort); } -XSOCKET XSock_Setup(xsock_t *pSock, xsock_type_t eType, const char *pAddr) +XSOCKET XSock_Setup(xsock_t *pSock, uint32_t nFlags, const char *pAddr) { xsock_addr_t addrInfo; @@ -1638,22 +1639,22 @@ XSOCKET XSock_Setup(xsock_t *pSock, xsock_type_t eType, const char *pAddr) return XSOCK_INVALID; } - return XSock_Open(pSock, eType, &addrInfo); + return XSock_Open(pSock, nFlags, &addrInfo); } -xsock_t* XSock_Alloc(xsock_type_t eType, const char *pAddr, uint16_t nPort) +xsock_t* XSock_Alloc(uint32_t nFlags, const char *pAddr, uint16_t nPort) { xsock_t *pSock = (xsock_t*)malloc(sizeof(xsock_t)); if (!pSock) return NULL; - XSock_Create(pSock, eType, pAddr, nPort); + XSock_Create(pSock, nFlags, pAddr, nPort); return pSock; } -xsock_t* XSock_New(xsock_type_t eType, xsock_addr_t *pAddr) +xsock_t* XSock_New(uint32_t nFlags, xsock_addr_t *pAddr) { if (strlen(pAddr->sAddr) <= 0 || pAddr->nPort == 0) return NULL; - return XSock_Alloc(eType, pAddr->sAddr, pAddr->nPort); + return XSock_Alloc(nFlags, pAddr->sAddr, pAddr->nPort); } void XSock_Free(xsock_t *pSock) diff --git a/src/net/sock.h b/src/net/sock.h index d6d1817..607edd0 100644 --- a/src/net/sock.h +++ b/src/net/sock.h @@ -94,6 +94,7 @@ typedef enum { XSOCK_ERR_SSLERR, XSOCK_ERR_SSLCA, XSOCK_ERR_NOSSL, + XSOCK_ERR_FLAGS, XSOCK_ERR_INVSSL, XSOCK_ERR_SYSCALL, XSOCK_WANT_READ, @@ -107,29 +108,26 @@ typedef enum { XF_IPV6 = 6 } xsock_family_t; -/* Supported socket types */ +/* Supported socket types and roles */ typedef enum { - XSOCK_TCP_RAW = (uint8_t)0, - XSOCK_UDP_RAW, - XSOCK_TCP_PEER, - XSOCK_TCP_CLIENT, - XSOCK_TCP_SERVER, - - XSOCK_SSL_PREFERED_CLIENT, - XSOCK_SSL_PREFERED_SERVER, - XSOCK_SSLV2_CLIENT, - XSOCK_SSLV2_SERVER, - XSOCK_SSLV3_CLIENT, - XSOCK_SSLV3_SERVER, - XSOCK_SSLV2_PEER, - XSOCK_SSLV3_PEER, - - XSOCK_UDP_CLIENT, - XSOCK_UDP_MCAST, - XSOCK_UDP_BCAST, - XSOCK_UDP_UCAST, - XSOCK_UNDEFINED -} xsock_type_t; + XSOCK_TCP = (1 << 0), + XSOCK_UDP = (1 << 1), + XSOCK_RAW = (1 << 2), + XSOCK_UNIX = (1 << 3), + + XSOCK_SSL = (1 << 4), + XSOCK_SSLV2 = (1 << 5), + XSOCK_SSLV3 = (1 << 6), + + XSOCK_PEER = (1 << 7), + XSOCK_CLIENT = (1 << 8), + XSOCK_SERVER = (1 << 9), + + XSOCK_BROADCAST = (1 << 10), + XSOCK_MULTICAST = (1 << 11), + XSOCK_UNICAST = (1 << 12), + XSOCK_UNDEFINED = 0 +} xsock_flags_t; typedef struct XSocketAddr { xsock_family_t eFamily; @@ -160,8 +158,8 @@ typedef struct XSocketCert { typedef struct XSocket { xsock_status_t eStatus; xsock_inaddr_t inAddr; - xsock_type_t eType; + uint32_t nFlags; uint32_t nAddr; uint16_t nPort; @@ -176,6 +174,15 @@ typedef struct XSocket { void *pPrivate; } xsock_t; +// Backward compatibility +#define XSOCK_TCP_PEER (XSOCK_TCP | XSOCK_PEER) +#define XSOCK_TCP_SERVER (XSOCK_TCP | XSOCK_SERVER) +#define XSOCK_TCP_CLIENT (XSOCK_TCP | XSOCK_CLIENT) +#define XSOCK_UDP_CLIENT (XSOCK_UDP | XSOCK_CLIENT) +#define XSOCK_UDP_BCAST (XSOCK_UDP | XSOCK_BROADCAST) +#define XSOCK_UDP_MCAST (XSOCK_UDP | XSOCK_MULTICAST) +#define XSOCK_UDP_UCAST (XSOCK_UDP | XSOCK_UNICAST) + const char* XSock_GetStatusStr(xsock_status_t eStatus); const char* XSock_ErrStr(xsock_t* pSock); @@ -186,8 +193,8 @@ SSL* XSock_GetSSL(xsock_t *pSock); xsock_inaddr_t* XSock_GetInAddr(xsock_t *pSock); xsock_status_t XSock_Status(const xsock_t *pSock); -xsock_type_t XSock_GetType(const xsock_t *pSock); -xbool_t XSockType_IsSSL(xsock_type_t eType); +xbool_t XSockFlags_CheckSSL(uint32_t nFlags); +uint32_t XSock_GetFlags(const xsock_t *pSock); uint32_t XSock_GetNetAddr(const xsock_t *pSock); uint16_t XSock_GetPort(const xsock_t *pSock); @@ -205,8 +212,7 @@ void XSock_Close(xsock_t* pSock); XSTATUS XSock_MsgPeek(xsock_t* pSock); XSTATUS XSock_IsOpen(xsock_t* pSock); XSTATUS XSock_Check(xsock_t* pSock); -XSTATUS XSock_Init(xsock_t* pSock, xsock_type_t eType, XSOCKET nFD, xbool_t nNB); -XSTATUS XSock_SetType(xsock_t* pSock, xsock_type_t eType); +XSTATUS XSock_Init(xsock_t* pSock, uint32_t nFlags, XSOCKET nFD, xbool_t nNB); void XSock_InitSSL(void); void XSock_DeinitSSL(void); @@ -257,14 +263,13 @@ XSOCKET XSock_TimeOutS(xsock_t* pSock, int nSec, int nUsec); XSOCKET XSock_Linger(xsock_t* pSock, int nSec); XSOCKET XSock_Bind(xsock_t *pSock); -XSOCKET XSock_CreateAdv(xsock_t* pSock, xsock_type_t eType, size_t nFdMax, const char* pAddr, uint16_t nPort); -XSOCKET XSock_Create(xsock_t* pSock, xsock_type_t eType, const char* pAddr, uint16_t nPort); -XSOCKET XSock_Open(xsock_t* pSock, xsock_type_t eType, xsock_addr_t* pAddr); -XSOCKET XSock_Setup(xsock_t* pSock, xsock_type_t eType, const char* pAddr); -XSOCKET XSock_CreateRAW(xsock_t *pSock, int nProtocol); +XSOCKET XSock_CreateAdv(xsock_t* pSock, uint32_t nFlags, size_t nFdMax, const char* pAddr, uint16_t nPort); +XSOCKET XSock_Create(xsock_t* pSock, uint32_t nFlags, const char* pAddr, uint16_t nPort); +XSOCKET XSock_Open(xsock_t* pSock, uint32_t nFlags, xsock_addr_t* pAddr); +XSOCKET XSock_Setup(xsock_t* pSock, uint32_t nFlags, const char* pAddr); -xsock_t* XSock_Alloc(xsock_type_t eType, const char* pAddr, uint16_t nPort); -xsock_t* XSock_New(xsock_type_t eType, xsock_addr_t* pAddr); +xsock_t* XSock_Alloc(uint32_t nFlags, const char* pAddr, uint16_t nPort); +xsock_t* XSock_New(uint32_t nFlags, xsock_addr_t* pAddr); void XSock_Free(xsock_t* pSock); #ifdef __cplusplus diff --git a/src/xdef.h b/src/xdef.h index 936e017..76fac78 100644 --- a/src/xdef.h +++ b/src/xdef.h @@ -36,6 +36,10 @@ typedef uint8_t xbool_t; #define XFTON(x) ((x)>=0.0f?(int)((x)+0.5f):(int)((x)-0.5f)) +#define XFLAGS_CHECK(c, f) (((c) & (f)) == (f)) +#define XFLAGS_ENABLE(c, f) ((c) |= (f)) +#define XFLAGS_DISABLE(c, f) ((c) &= ~(f)) + #ifndef XMSG_MIN #define XMSG_MIN 2048 #endif diff --git a/src/xver.h b/src/xver.h index d14304d..4ec72ac 100644 --- a/src/xver.h +++ b/src/xver.h @@ -12,7 +12,7 @@ #define XUTILS_VERSION_MAX 2 #define XUTILS_VERSION_MIN 6 -#define XUTILS_BUILD_NUMBER 2 +#define XUTILS_BUILD_NUMBER 3 #ifdef __cplusplus extern "C" {