Skip to content

Commit

Permalink
Refactored XSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Dec 2, 2024
1 parent 8cccb66 commit 6fdbf8e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/net/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1255,10 +1255,10 @@ XSTATUS XAPI_Connect(xapi_t *pApi, xapi_endpoint_t *pEndpt)

if (pEndpt->bUnix)
{
XSock_InitAddr(&addrInfo);
XSock_InitInfo(&addrInfo);
xstrncpy(addrInfo.sAddr, sizeof(addrInfo.sAddr), pEndpt->pAddr);
}
else if (XSock_GetAddr(&addrInfo, pEndpt->pAddr) < 0)
else if (XSock_GetAddrInfo(&addrInfo, pEndpt->pAddr) < 0)
{
XAPI_ErrorCb(pApi, NULL, XAPI_NONE, XAPI_ERR_RESOLVE);
return XSTDERR;
Expand Down
4 changes: 2 additions & 2 deletions src/net/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ xhttp_status_t XHTTP_Connect(xhttp_t *pHttp, xsock_t *pSock, xlink_t *pLink)
}

xsock_info_t addrInfo;
XSock_InitAddr(&addrInfo);
XSock_InitInfo(&addrInfo);

if (!xstrused(pHttp->sUnixAddr))
{
Expand All @@ -1032,7 +1032,7 @@ xhttp_status_t XHTTP_Connect(xhttp_t *pHttp, xsock_t *pSock, xlink_t *pLink)
xstrncat(pLink->sHost, sizeof(pLink->sHost), ":%d", pLink->nPort);
}

if (XSock_GetAddr(&addrInfo, pLink->sHost) < 0)
if (XSock_GetAddrInfo(&addrInfo, pLink->sHost) < 0)
return XHTTP_StatusCb(pHttp, XHTTP_ERRRESOLVE);

if (!addrInfo.nPort) addrInfo.nPort = pLink->nPort;
Expand Down
2 changes: 1 addition & 1 deletion src/net/ntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int XNTP_GetDate(const char *pAddr, uint16_t nPort, xtime_t *pTime)
if (!nPort) nPort = XNTP_DEF_PORT;

xsock_info_t sockAddr;
XSock_GetAddr(&sockAddr, pAddr);
XSock_GetAddrInfo(&sockAddr, pAddr);
sockAddr.nPort = nPort;

xsock_t sock;
Expand Down
24 changes: 11 additions & 13 deletions src/net/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -955,10 +955,10 @@ size_t XSock_SinAddr(const struct in_addr inAddr, char *pAddr, size_t nSize)
return XSock_IPStr(inAddr.s_addr, pAddr, nSize);
}

size_t XSock_IPAddr(xsock_t *pSock, char *pAddr, size_t nSize)
size_t XSock_IPAddr(const xsock_t *pSock, char *pAddr, size_t nSize)
{
if (XFLAGS_CHECK(pSock->nFlags, XSOCK_UNIX)) return 0;
struct sockaddr_in *pInAddr = &pSock->sockAddr.inAddr;
if (XFLAGS_CHECK(pSock->nFlags, XSOCK_UNIX)) return XSOCK_NONE;
const struct sockaddr_in *pInAddr = &pSock->sockAddr.inAddr;
return XSock_SinAddr(pInAddr->sin_addr, pAddr, nSize);
}

Expand All @@ -977,7 +977,6 @@ XSTATUS XSock_AddrInfo(xsock_info_t *pAddr, xsock_family_t eFam, const char *pHo
hints.ai_next = NULL;

if (getaddrinfo(pHost, NULL, &hints, &res)) return nRetVal;
xstrncpy(pAddr->sHost, sizeof(pAddr->sHost), pHost);
pAddr->eFamily = XF_UNDEF;

for (rp = res; rp != NULL; rp = rp->ai_next)
Expand Down Expand Up @@ -1019,19 +1018,18 @@ XSTATUS XSock_AddrInfo(xsock_info_t *pAddr, xsock_family_t eFam, const char *pHo
return nRetVal;
}

void XSock_InitAddr(xsock_info_t *pAddr)
void XSock_InitInfo(xsock_info_t *pAddr)
{
pAddr->sHost[0] = XSTR_NUL;
pAddr->sName[0] = XSTR_NUL;
pAddr->sAddr[0] = XSTR_NUL;
pAddr->nAddr = XSTDNON;
pAddr->nPort = XSTDNON;
pAddr->eFamily = XF_UNDEF;
}

XSTATUS XSock_GetAddr(xsock_info_t *pAddr, const char *pHost)
XSTATUS XSock_GetAddrInfo(xsock_info_t *pAddr, const char *pHost)
{
XSock_InitAddr(pAddr);
XSock_InitInfo(pAddr);
if (pHost == NULL) return XSOCK_ERROR;

char sHost[XSOCK_INFO_MAX + XSOCK_ADDR_MAX];
Expand All @@ -1049,9 +1047,9 @@ XSTATUS XSock_GetAddr(xsock_info_t *pAddr, const char *pHost)
return pAddr->nPort ? XSOCK_SUCCESS : XSOCK_NONE;
}

XSTATUS XSock_Addr(xsock_info_t *pInfo, struct sockaddr_in *pAddr, size_t nSize)
XSTATUS XSock_GetAddr(xsock_info_t *pInfo, struct sockaddr_in *pAddr, size_t nSize)
{
XSock_InitAddr(pInfo);
XSock_InitInfo(pInfo);
pInfo->eFamily = XF_IPV4;

struct hostent *pHostInfo = gethostbyaddr((char*)&pAddr->sin_addr.s_addr, (int)nSize, AF_INET);
Expand Down Expand Up @@ -1259,7 +1257,7 @@ XSOCKET XSock_AddMembership(xsock_t* pSock, const char* pGroup)
return pSock->nFD;
}

XSTATUS XSock_LoadPKCS12(xsocket_ssl_cert_t *pCert, const char *p12Path, const char *p12Pass)
XSTATUS XSock_LoadPKCS12(xsock_ssl_cert_t *pCert, const char *p12Path, const char *p12Pass)
{
pCert->nStatus = 0;
#ifdef XSOCK_USE_SSL
Expand Down Expand Up @@ -1335,7 +1333,7 @@ XSOCKET XSock_SetSSLCert(xsock_t *pSock, xsock_cert_t *pCert)

if (xstrused(pCert->p12Path))
{
xsocket_ssl_cert_t sslCert;
xsock_ssl_cert_t sslCert;
if (!XSock_LoadPKCS12(&sslCert, pCert->p12Path, pCert->p12Pass))
{
pSock->eStatus = XSOCK_ERR_PKCS12;
Expand Down Expand Up @@ -1715,7 +1713,7 @@ XSOCKET XSock_Setup(xsock_t *pSock, uint32_t nFlags, const char *pAddr)
return XSock_Create(pSock, nFlags, pAddr, 0);

xsock_info_t addrInfo;
if (XSock_GetAddr(&addrInfo, pAddr) <= 0)
if (XSock_GetAddrInfo(&addrInfo, pAddr) <= 0)
{
pSock->eStatus = XSOCK_ERR_ADDR;
pSock->nFD = XSOCK_INVALID;
Expand Down
15 changes: 7 additions & 8 deletions src/net/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ typedef enum {
XF_IPV6 = 6
} xsock_family_t;

/* Supported socket types and roles */
/* Supported socket types, roles and flags */
typedef enum {
XSOCK_TCP = (1 << 0),
XSOCK_UDP = (1 << 1),
Expand All @@ -133,7 +133,6 @@ typedef enum {

typedef struct XSocketInfo {
char sAddr[XSOCK_ADDR_MAX];
char sHost[XSOCK_INFO_MAX];
char sName[XSOCK_INFO_MAX];
xsock_family_t eFamily;
uint32_t nAddr;
Expand All @@ -145,7 +144,7 @@ typedef struct XSocketSSLCert {
void *pCert;
void *pKey;
void *pCa;
} xsocket_ssl_cert_t;
} xsock_ssl_cert_t;

typedef struct XSocketCert {
const char *pCertPath;
Expand Down Expand Up @@ -226,7 +225,7 @@ void XSock_InitSSL(void);
void XSock_DeinitSSL(void);
int XSock_LastSSLError(char* pDst, size_t nSize);

XSTATUS XSock_LoadPKCS12(xsocket_ssl_cert_t* pCert, const char* p12Path, const char* p12Pass);
XSTATUS XSock_LoadPKCS12(xsock_ssl_cert_t* pCert, const char* p12Path, const char* p12Pass);
XSOCKET XSock_SetSSLCert(xsock_t* pSock, xsock_cert_t* pCert);
void XSock_InitCert(xsock_cert_t *pCert);

Expand All @@ -253,13 +252,13 @@ XSOCKET XSock_AcceptNB(xsock_t* pSock);

uint32_t XSock_NetAddr(const char* pAddr);
size_t XSock_SinAddr(const struct in_addr inAddr, char* pAddr, size_t nSize);
size_t XSock_IPAddr(xsock_t* pSock, char* pAddr, size_t nSize);
size_t XSock_IPAddr(const xsock_t* pSock, char* pAddr, size_t nSize);
size_t XSock_IPStr(const uint32_t nAddr, char* pStr, size_t nSize);

void XSock_InitAddr(xsock_info_t* pAddr);
void XSock_InitInfo(xsock_info_t* pAddr);
XSTATUS XSock_GetAddrInfo(xsock_info_t* pAddr, const char* pHost);
XSTATUS XSock_AddrInfo(xsock_info_t* pAddr, xsock_family_t eFam, const char* pHost);
XSTATUS XSock_GetAddr(xsock_info_t* pAddr, const char* pHost);
XSTATUS XSock_Addr(xsock_info_t* pInfo, struct sockaddr_in* pAddr, size_t nSize);
XSTATUS XSock_GetAddr(xsock_info_t* pInfo, struct sockaddr_in* pAddr, size_t nSize);

XSOCKET XSock_AddMembership(xsock_t* pSock, const char* pGroup);
XSOCKET XSock_ReuseAddr(xsock_t* pSock, xbool_t nEnabled);
Expand Down
2 changes: 1 addition & 1 deletion src/xver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#define XUTILS_VERSION_MAX 2
#define XUTILS_VERSION_MIN 6
#define XUTILS_BUILD_NUMBER 14
#define XUTILS_BUILD_NUMBER 15

#ifdef __cplusplus
extern "C" {
Expand Down

0 comments on commit 6fdbf8e

Please sign in to comment.