Skip to content

Commit fdf90a0

Browse files
authored
Merge pull request #284 from rsyslog/codex/analyze-api-client-name/ip-retrieval
Add callback exposing client source port
2 parents c477adc + 9a7d8bf commit fdf90a0

File tree

6 files changed

+86
-40
lines changed

6 files changed

+86
-40
lines changed

src/librelp.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* This file is meant to be included by applications using the relp library.
44
* For relp library files themselves, include "relp.h".
55
*
6-
* Copyright 2008-2018 by Rainer Gerhards and Adiscon GmbH.
6+
* Copyright 2008-2025 by Rainer Gerhards and Adiscon GmbH.
77
*
88
* This file is part of librelp.
99
*
@@ -207,9 +207,11 @@ relpRetVal relpEngineRun(relpEngine_t *pThis);
207207
relpRetVal relpEngineCltDestruct(relpEngine_t *pThis, relpClt_t **ppClt);
208208
relpRetVal relpEngineCltConstruct(relpEngine_t *pThis, relpClt_t **ppClt);
209209
relpRetVal relpEngineSetSyslogRcv(relpEngine_t *pThis,
210-
relpRetVal (*pCB)(unsigned char*, unsigned char*, unsigned char*, size_t));
210+
relpRetVal (*pCB)(unsigned char*, unsigned char*, unsigned char*, size_t));
211211
relpRetVal relpEngineSetSyslogRcv2(relpEngine_t *pThis,
212-
relpRetVal (*pCB)(void*, unsigned char*, unsigned char*, unsigned char*, size_t));
212+
relpRetVal (*pCB)(void*, unsigned char*, unsigned char*, unsigned char*, size_t));
213+
relpRetVal relpEngineSetSyslogRcv3(relpEngine_t *pThis,
214+
relpRetVal (*pCB)(void*, unsigned char*, unsigned char*, unsigned char*, unsigned char*, size_t));
213215
relpRetVal relpEngineSetEnableCmd(relpEngine_t *pThis, unsigned char *pszCmd, relpCmdEnaState_t stateCmd);
214216
relpRetVal relpEngineSetDnsLookupMode(relpEngine_t *pThis, int iMode);
215217
relpRetVal relpEngineSetOnAuthErr(relpEngine_t *pThis,

src/relp.c

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* The RELP (reliable event logging protocol) core protocol library.
22
*
3-
* Copyright 2008-2020 by Rainer Gerhards and Adiscon GmbH.
3+
* Copyright 2008-2025 by Rainer Gerhards and Adiscon GmbH.
44
*
55
* This file is part of librelp.
66
*
@@ -442,24 +442,47 @@ relpEngineListnerConstructFinalize(relpEngine_t *const pThis, relpSrv_t *pSrv)
442442

443443
/* a dummy for callbacks not set by the caller */
444444
static relpRetVal relpSrvSyslogRcvDummy2(void LIBRELP_ATTR_UNUSED *pUsr,
445-
unsigned char LIBRELP_ATTR_UNUSED *pHostName,
446-
unsigned char LIBRELP_ATTR_UNUSED *pIP, unsigned char LIBRELP_ATTR_UNUSED *pMsg,
447-
size_t LIBRELP_ATTR_UNUSED lenMsg)
445+
unsigned char LIBRELP_ATTR_UNUSED *pHostName,
446+
unsigned char LIBRELP_ATTR_UNUSED *pIP, unsigned char LIBRELP_ATTR_UNUSED *pMsg,
447+
size_t LIBRELP_ATTR_UNUSED lenMsg)
448448
{ return RELP_RET_NOT_IMPLEMENTED;
449449
}
450+
static relpRetVal relpSrvSyslogRcvDummy3(void LIBRELP_ATTR_UNUSED *pUsr,
451+
unsigned char LIBRELP_ATTR_UNUSED *pHostName,
452+
unsigned char LIBRELP_ATTR_UNUSED *pIP, unsigned char LIBRELP_ATTR_UNUSED *pPort,
453+
unsigned char LIBRELP_ATTR_UNUSED *pMsg,
454+
size_t LIBRELP_ATTR_UNUSED lenMsg)
455+
{ return RELP_RET_NOT_IMPLEMENTED; }
450456
/* set the syslog receive callback. If NULL is provided, it is set to the
451457
* not implemented dummy.
452458
*/
453459
relpRetVal PART_OF_API
454460
relpEngineSetSyslogRcv2(relpEngine_t *const pThis, relpRetVal (*pCB)(void *, unsigned char*,
455-
unsigned char*, unsigned char*, size_t))
461+
unsigned char*, unsigned char*, size_t))
456462
{
457-
ENTER_RELPFUNC;
458-
RELPOBJ_assert(pThis, Engine);
463+
ENTER_RELPFUNC;
464+
RELPOBJ_assert(pThis, Engine);
459465

460-
pThis->onSyslogRcv = NULL;
461-
pThis->onSyslogRcv2 = (pCB == NULL) ? relpSrvSyslogRcvDummy2 : pCB;
462-
LEAVE_RELPFUNC;
466+
pThis->onSyslogRcv = NULL;
467+
pThis->onSyslogRcv3 = NULL;
468+
pThis->onSyslogRcv2 = (pCB == NULL) ? relpSrvSyslogRcvDummy2 : pCB;
469+
LEAVE_RELPFUNC;
470+
}
471+
472+
/* set the syslog receive callback. If NULL is provided, it is set to the
473+
* not implemented dummy.
474+
*/
475+
relpRetVal PART_OF_API
476+
relpEngineSetSyslogRcv3(relpEngine_t *const pThis, relpRetVal (*pCB)(void *, unsigned char*,
477+
unsigned char*, unsigned char*, unsigned char*, size_t))
478+
{
479+
ENTER_RELPFUNC;
480+
RELPOBJ_assert(pThis, Engine);
481+
482+
pThis->onSyslogRcv = NULL;
483+
pThis->onSyslogRcv2 = NULL;
484+
pThis->onSyslogRcv3 = (pCB == NULL) ? relpSrvSyslogRcvDummy3 : pCB;
485+
LEAVE_RELPFUNC;
463486
}
464487

465488
/**
@@ -573,9 +596,10 @@ relpEngineSetSyslogRcv(relpEngine_t *const pThis, relpRetVal (*pCB)(unsigned cha
573596
ENTER_RELPFUNC;
574597
RELPOBJ_assert(pThis, Engine);
575598

576-
pThis->onSyslogRcv = (pCB == NULL) ? relpSrvSyslogRcvDummy : pCB;
577-
pThis->onSyslogRcv2 = NULL;
578-
LEAVE_RELPFUNC;
599+
pThis->onSyslogRcv = (pCB == NULL) ? relpSrvSyslogRcvDummy : pCB;
600+
pThis->onSyslogRcv2 = NULL;
601+
pThis->onSyslogRcv3 = NULL;
602+
LEAVE_RELPFUNC;
579603
}
580604

581605
/* Deprecated, use relpEngineListnerConstruct() family of functions.

src/relp.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* The RELP (reliable event logging protocol) core protocol library.
22
*
3-
* Copyright 2008-2020 by Rainer Gerhards and Adiscon GmbH.
3+
* Copyright 2008-2025 by Rainer Gerhards and Adiscon GmbH.
44
*
55
* This file is part of librelp.
66
*
@@ -125,14 +125,16 @@ typedef struct relpEngSessLst_s {
125125
*/
126126
struct relpEngine_s {
127127
BEGIN_RELP_OBJ;
128-
void (*dbgprint)(char *fmt, ...) LIBRELP_ATTR_FORMAT(printf, 1, 2);
129-
relpRetVal (*onSyslogRcv)(unsigned char*pHostname, unsigned char *pIP,
130-
unsigned char *pMsg, size_t lenMsg); /**< callback for "syslog" cmd */
131-
relpRetVal (*onSyslogRcv2)(void*, unsigned char*pHostname, unsigned char *pIP,
132-
unsigned char *pMsg, size_t lenMsg); /**< callback for "syslog" cmd */
133-
void (*onAuthErr)(void*pUsr, char *authinfo, char*errmsg, relpRetVal errcode);
134-
void (*onErr)(void*pUsr, char *objinfo, char*errmsg, relpRetVal errcode);
135-
void (*onGenericErr)(char *objinfo, char*errmsg, relpRetVal errcode);
128+
void (*dbgprint)(char *fmt, ...) LIBRELP_ATTR_FORMAT(printf, 1, 2);
129+
relpRetVal (*onSyslogRcv)(unsigned char*pHostname, unsigned char *pIP,
130+
unsigned char *pMsg, size_t lenMsg); /**< callback for "syslog" cmd */
131+
relpRetVal (*onSyslogRcv2)(void*, unsigned char*pHostname, unsigned char *pIP,
132+
unsigned char *pMsg, size_t lenMsg); /**< callback for "syslog" cmd */
133+
relpRetVal (*onSyslogRcv3)(void*, unsigned char*pHostname, unsigned char *pIP,
134+
unsigned char *pPort, unsigned char *pMsg, size_t lenMsg); /**< callback for "syslog" cmd */
135+
void (*onAuthErr)(void*pUsr, char *authinfo, char*errmsg, relpRetVal errcode);
136+
void (*onErr)(void*pUsr, char *objinfo, char*errmsg, relpRetVal errcode);
137+
void (*onGenericErr)(char *objinfo, char*errmsg, relpRetVal errcode);
136138
int protocolVersion; /**< version of the relp protocol supported by this engine */
137139

138140
/* Flags */

src/scsyslog.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* This command is used to transfer syslog messages.
44
*
5-
* Copyright 2008-2018 by Rainer Gerhards and Adiscon GmbH.
5+
* Copyright 2008-2025 by Rainer Gerhards and Adiscon GmbH.
66
*
77
* This file is part of librelp.
88
*
@@ -63,16 +63,20 @@ BEGINcommand(S, Syslog)
6363
* return code. I assume this should at least be optionally done.
6464
* Consider implementing this. rgerhards, 2018-04-17
6565
*/
66-
/* only highest version callback is called */
67-
if(pSess->pEngine->onSyslogRcv2 != NULL) {
68-
pSess->pEngine->onSyslogRcv2(pSess->pSrv->pUsr, pSess->pTcp->pRemHostName,
69-
pSess->pTcp->pRemHostIP, pFrame->pData, pFrame->lenData);
70-
} else if(pSess->pEngine->onSyslogRcv != NULL) {
71-
pSess->pEngine->onSyslogRcv(pSess->pTcp->pRemHostName, pSess->pTcp->pRemHostIP,
72-
pFrame->pData, pFrame->lenData);
73-
} else {
74-
pSess->pEngine->dbgprint((char*)"error: no syslog reception callback is set, nothing done\n");
75-
}
66+
/* only highest version callback is called */
67+
if(pSess->pEngine->onSyslogRcv3 != NULL) {
68+
pSess->pEngine->onSyslogRcv3(pSess->pSrv->pUsr, pSess->pTcp->pRemHostName,
69+
pSess->pTcp->pRemHostIP, pSess->pTcp->pRemHostPort,
70+
pFrame->pData, pFrame->lenData);
71+
} else if(pSess->pEngine->onSyslogRcv2 != NULL) {
72+
pSess->pEngine->onSyslogRcv2(pSess->pSrv->pUsr, pSess->pTcp->pRemHostName,
73+
pSess->pTcp->pRemHostIP, pFrame->pData, pFrame->lenData);
74+
} else if(pSess->pEngine->onSyslogRcv != NULL) {
75+
pSess->pEngine->onSyslogRcv(pSess->pTcp->pRemHostName, pSess->pTcp->pRemHostIP,
76+
pFrame->pData, pFrame->lenData);
77+
} else {
78+
pSess->pEngine->dbgprint((char*)"error: no syslog reception callback is set, nothing done\n");
79+
}
7680

7781
/* send response */
7882
iRet = relpSessSendResponse(pSess, pFrame->txnr, (unsigned char*) "200 OK", 6);

src/tcp.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* This implements the relp mapping onto TCP.
22
*
3-
* Copyright 2008-2018 by Rainer Gerhards and Adiscon GmbH.
3+
* Copyright 2008-2025 by Rainer Gerhards and Adiscon GmbH.
44
*
55
* This file is part of librelp.
66
*
@@ -819,6 +819,7 @@ relpTcpDestruct(relpTcp_t **ppThis)
819819

820820
free(pThis->pRemHostIP);
821821
free(pThis->pRemHostName);
822+
free(pThis->pRemHostPort);
822823
free(pThis->pristring);
823824
free(pThis->caCertFile);
824825
free(pThis->ownCertFile);
@@ -923,6 +924,7 @@ relpTcpSetRemHost(relpTcp_t *const pThis, struct sockaddr *pAddr)
923924
int error;
924925
unsigned char szIP[NI_MAXHOST] = "";
925926
unsigned char szHname[NI_MAXHOST+64] = ""; /* 64 extra bytes for message text */
927+
unsigned char szPort[NI_MAXSERV] = "";
926928
struct addrinfo hints, *res;
927929
size_t len;
928930

@@ -931,11 +933,12 @@ relpTcpSetRemHost(relpTcp_t *const pThis, struct sockaddr *pAddr)
931933
pEngine = pThis->pEngine;
932934
assert(pAddr != NULL);
933935

934-
error = getnameinfo(pAddr, SALEN(pAddr), (char*)szIP, sizeof(szIP), NULL, 0, NI_NUMERICHOST);
935-
if(error) {
936+
if((error = getnameinfo(pAddr, SALEN(pAddr), (char*)szIP, sizeof(szIP), (char*)szPort,
937+
sizeof(szPort), NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
936938
pThis->pEngine->dbgprint((char*)"Malformed from address %s\n", gai_strerror(error));
937939
strcpy((char*)szHname, "???");
938940
strcpy((char*)szIP, "???");
941+
strcpy((char*)szPort, "???");
939942
ABORT_FINALIZE(RELP_RET_INVALID_HNAME);
940943
}
941944

@@ -981,6 +984,16 @@ relpTcpSetRemHost(relpTcp_t *const pThis, struct sockaddr *pAddr)
981984
}
982985
memcpy(pThis->pRemHostName, szHname, len);
983986

987+
len = strlen((char*)szPort) + 1; /* +1 for \0 byte */
988+
if((pThis->pRemHostPort = malloc(len)) == NULL) {
989+
free(pThis->pRemHostIP);
990+
free(pThis->pRemHostName);
991+
pThis->pRemHostIP = NULL;
992+
pThis->pRemHostName = NULL;
993+
ABORT_FINALIZE(RELP_RET_OUT_OF_MEMORY);
994+
}
995+
memcpy(pThis->pRemHostPort, szPort, len);
996+
984997
finalize_it:
985998
LEAVE_RELPFUNC;
986999
}

src/tcp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* The mapping for relp over TCP.
22
*
3-
* Copyright 2008-2018 by Rainer Gerhards and Adiscon GmbH.
3+
* Copyright 2008-2025 by Rainer Gerhards and Adiscon GmbH.
44
*
55
* This file is part of librelp.
66
*
@@ -110,6 +110,7 @@ typedef struct relpTcp_s {
110110
relpClt_t *pClt; /**< ptr to our client; only valid if pSrv == NULL */
111111
unsigned char *pRemHostIP; /**< IP address of remote peer (currently used in server mode, only) */
112112
unsigned char *pRemHostName; /**< host name of remote peer (currently used in server mode, only) */
113+
unsigned char *pRemHostPort; /**< port of remote peer (currently used in server mode, only) */
113114
int sock; /**< the socket we use for regular, single-socket, operations */
114115
int *socks; /**< the socket(s) we use for listeners, element 0 has nbr of socks */
115116
int iSessMax; /**< maximum number of sessions permitted */

0 commit comments

Comments
 (0)