diff --git a/src/librelp.h b/src/librelp.h index 7fefadc7..de1a7780 100644 --- a/src/librelp.h +++ b/src/librelp.h @@ -193,6 +193,7 @@ relpRetVal relpEngineSetOnGenericErr(relpEngine_t *pThis, /* exposed server property set functions */ relpRetVal relpSrvSetLstnPort(relpSrv_t *pThis, unsigned char *pLstnPort); +relpRetVal relpSrvSetLstnAddr(relpSrv_t *pThis, unsigned char *pLstnAddr); relpRetVal relpSrvSetUsrPtr(relpSrv_t *pThis, void *pUsr); void relpSrvEnableTLS(relpSrv_t *pThis) __attribute__ ((deprecated)); void relpSrvEnableTLSZip(relpSrv_t *pThis) __attribute__ ((deprecated)); diff --git a/src/relpsrv.c b/src/relpsrv.c index 22cbbf72..c9633871 100644 --- a/src/relpsrv.c +++ b/src/relpsrv.c @@ -96,6 +96,8 @@ relpSrvDestruct(relpSrv_t **ppThis) if(pThis->pLstnPort != NULL) free(pThis->pLstnPort); + if(pThis->pLstnAddr != NULL) + free(pThis->pLstnAddr); free(pThis->pristring); free(pThis->caCertFile); @@ -189,6 +191,31 @@ relpSrvSetLstnPort(relpSrv_t *pThis, unsigned char *pLstnPort) LEAVE_RELPFUNC; } +/* set the address inside the relp server. If NULL is provided, the server + * will bind to all interfaces. The provided string is always copied, it is the caller's duty to + * free the passed-in string. + * perlei, 2018-04-19 + */ +relpRetVal +relpSrvSetLstnAddr(relpSrv_t *pThis, unsigned char *pLstnAddr) +{ + ENTER_RELPFUNC; + RELPOBJ_assert(pThis, Srv); + + /* first free old value */ + if(pThis->pLstnAddr != NULL) + free(pThis->pLstnAddr); + pThis->pLstnAddr = NULL; + + if(pLstnAddr != NULL) { + if((pThis->pLstnAddr = (unsigned char*) strdup((char*)pLstnAddr)) == NULL) + ABORT_FINALIZE(RELP_RET_OUT_OF_MEMORY); + } + +finalize_it: + LEAVE_RELPFUNC; +} + /* mode==NULL is valid and means "no change" */ relpRetVal relpSrvSetAuthMode(relpSrv_t *pThis, char *mode) @@ -364,7 +391,9 @@ relpSrvRun(relpSrv_t *pThis) CHKRet(relpTcpSetPermittedPeers(pTcp, &(pThis->permittedPeers))); } CHKRet(relpTcpLstnInit(pTcp, (pThis->pLstnPort == NULL) ? - (unsigned char*) RELP_DFLT_PORT : pThis->pLstnPort, pThis->ai_family)); + (unsigned char*) RELP_DFLT_PORT : pThis->pLstnPort, + (unsigned char*) pThis->pLstnAddr, + pThis->ai_family)); pThis->pTcp = pTcp; diff --git a/src/relpsrv.h b/src/relpsrv.h index 20ac567d..2dc56926 100644 --- a/src/relpsrv.h +++ b/src/relpsrv.h @@ -43,6 +43,7 @@ struct relpSrv_s { BEGIN_RELP_OBJ; relpEngine_t *pEngine; unsigned char *pLstnPort; + unsigned char *pLstnAddr; int ai_family; relpTcp_t *pTcp; /**< our tcp support object */ size_t maxDataSize; /**< maximum size of a DATA element */ diff --git a/src/tcp.c b/src/tcp.c index e37cee6a..36454471 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -1400,9 +1400,11 @@ relpTcpLstnInitTLS(relpTcp_t *const pThis) /* initialize the tcp socket for a listner * pLstnPort is a pointer to a port name. NULL is not permitted. * gerhards, 2008-03-17 + * pLstnAddr is a pointer to a bind address. NULL is permitted. + * perlei, 2018-04-19 */ relpRetVal -relpTcpLstnInit(relpTcp_t *const pThis, unsigned char *pLstnPort, const int ai_family) +relpTcpLstnInit(relpTcp_t *pThis, unsigned char *pLstnPort, unsigned char *pLstnAddr, int ai_family) { struct addrinfo hints, *res = NULL, *r; int error, maxs, *s, on = 1; @@ -1422,7 +1424,7 @@ relpTcpLstnInit(relpTcp_t *const pThis, unsigned char *pLstnPort, const int ai_f hints.ai_family = ai_family; hints.ai_socktype = SOCK_STREAM; - error = getaddrinfo(NULL, (char*) pLstnPt, &hints, &res); + error = getaddrinfo((char*)pLstnAddr, (char*) pLstnPt, &hints, &res); if(error) { pThis->pEngine->dbgprint("error %d querying port '%s'\n", error, pLstnPt); ABORT_FINALIZE(RELP_RET_INVALID_PORT); diff --git a/src/tcp.h b/src/tcp.h index dade46eb..70039c3c 100644 --- a/src/tcp.h +++ b/src/tcp.h @@ -140,7 +140,7 @@ relpTcpRtryOp(relpTcp_t *pThis) relpRetVal relpTcpConstruct(relpTcp_t **ppThis, relpEngine_t *pEngine, int connType, void *pParent); relpRetVal relpTcpDestruct(relpTcp_t **ppThis); relpRetVal relpTcpAbortDestruct(relpTcp_t **ppThis); -relpRetVal relpTcpLstnInit(relpTcp_t *pThis, unsigned char *pLstnPort, int ai_family); +relpRetVal relpTcpLstnInit(relpTcp_t *pThis, unsigned char *pLstnPort, unsigned char *pLstnAddr, int ai_family); relpRetVal relpTcpAcceptConnReq(relpTcp_t **ppThis, int sock, relpSrv_t *pSrv); relpRetVal relpTcpRcv(relpTcp_t *pThis, relpOctet_t *pRcvBuf, ssize_t *pLenBuf); relpRetVal relpTcpSend(relpTcp_t *pThis, relpOctet_t *pBuf, ssize_t *pLenBuf);