Skip to content

Commit

Permalink
Ability to set a optional bindAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
Per Leino committed Apr 19, 2018
1 parent 1ea01dc commit 96eb5be
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/librelp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
31 changes: 30 additions & 1 deletion src/relpsrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/relpsrv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
6 changes: 4 additions & 2 deletions src/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 96eb5be

Please sign in to comment.