Skip to content

Commit

Permalink
Add TPTAG_CAPT_SRC to optionally set specify IP to report to HEP/EEP
Browse files Browse the repository at this point in the history
  • Loading branch information
crienzo committed Sep 15, 2022
1 parent 5830a32 commit 1285866
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
6 changes: 6 additions & 0 deletions libsofia-sip-ua/tport/sofia-sip/tport_tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ TPORT_DLL extern tag_typedef_t tptag_capt;
TPORT_DLL extern tag_typedef_t tptag_capt_ref;
#define TPTAG_CAPT_REF(x) tptag_capt_ref, tag_str_vr(&(x))

TPORT_DLL extern tag_typedef_t tptag_capt_src;
#define TPTAG_CAPT_SRC(x) tptag_capt_src, tag_str_v((x))

TPORT_DLL extern tag_typedef_t tptag_capt_src_ref;
#define TPTAG_CAPT_SRC_REF(x) tptag_capt_src_ref, tag_str_vr(&(x))

SOFIA_END_DECLS

#endif /* !defined TPORT_TAG_H */
5 changes: 5 additions & 0 deletions libsofia-sip-ua/tport/tport.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,11 @@ void tport_destroy(tport_t *self)
if (mr->mr_timer)
su_timer_destroy(mr->mr_timer), mr->mr_timer = NULL;

if (mr->mr_capt_src_addr) {
su_freeaddrinfo(mr->mr_capt_src_addr);
mr->mr_capt_src_addr = NULL;
}

su_home_zap(mr->mr_home);
}

Expand Down
3 changes: 2 additions & 1 deletion libsofia-sip-ua/tport/tport_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ struct tport_master {
char *mr_dump; /**< Filename for dumping received/sent data */
/** SOCK to dump received and sent data */
su_socket_t mr_capt_sock;
char *mr_capt_name; /**< Servername for capturing received/sent data */
char *mr_capt_name; /**< Servername for capturing received/sent data */
su_addrinfo_t *mr_capt_src_addr;
tport_primary_t *mr_primaries; /**< List of primary contacts */
unsigned mr_prot_ver; /* hep version */
unsigned mr_agent_id; /* agent version */
Expand Down
45 changes: 36 additions & 9 deletions libsofia-sip-ua/tport/tport_logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,12 @@ extern char const TPORT_DUMP[]; /* dummy declaration for Doxygen */
* environment variable. This can be used to save message traces into database and help
* hairy debugging tasks.
*
* @sa TPORT_LOG, TPORT_DEBUG, TPORT_CAPT, tport_log
* @sa TPORT_LOG, TPORT_DEBUG, TPORT_CAPT, TPORT_CAPT_SRC, tport_log
*/
#ifdef DOXYGEN
extern char const TPORT_CAPT[]; /* dummy declaration for Doxygen */
#endif


/**@var TPORT_DEBUG
*
* Environment variable determining the debug log level for @b tport module.
Expand Down Expand Up @@ -120,13 +119,15 @@ int tport_open_log(tport_master_t *mr, tagi_t *tags)
int log_msg = mr->mr_log != 0;
char const *dump = NULL;
char const *capt = NULL;;

char const *capt_src = NULL;

if(mr->mr_capt_name) capt = mr->mr_capt_name;

n = tl_gets(tags,
TPTAG_LOG_REF(log_msg),
TPTAG_DUMP_REF(dump),
TPTAG_CAPT_REF(capt),
TPTAG_CAPT_SRC_REF(capt_src),
TAG_END());

if (getenv("MSG_STREAM_LOG") != NULL || getenv("TPORT_LOG") != NULL)
Expand All @@ -140,6 +141,18 @@ int tport_open_log(tport_master_t *mr, tagi_t *tags)
if (getenv("TPORT_DUMP"))
dump = getenv("TPORT_DUMP");

/* Overriding src address for HEP */
if (capt_src && !mr->mr_capt_src_addr) {
su_addrinfo_t *ai = NULL, hints[1] = {{ 0 }};
hints->ai_flags = AI_NUMERICSERV;
hints->ai_family = AF_UNSPEC;
if (su_getaddrinfo(capt_src, "", hints, &ai)) {
su_perror("capture_src: su_getaddrinfo()");
} else {
mr->mr_capt_src_addr = ai;
}
}

if(capt) {

char *captname, *p, *host_s;
Expand Down Expand Up @@ -456,7 +469,7 @@ int tport_capt_msg_hepv2 (tport_t const *self, msg_t *msg, size_t n,
{

int buflen = 0;
su_sockaddr_t const *su, *su_self;
su_sockaddr_t const *su, *su_self, *su_self_ext;
struct hep_hdr hep_header;
struct hep_timehdr hep_time = {0};
su_time_t now;
Expand All @@ -478,6 +491,13 @@ int tport_capt_msg_hepv2 (tport_t const *self, msg_t *msg, size_t n,
su_self = self->tp_pri->pri_primary->tp_addr;

mr = self->tp_master;
if (mr->mr_capt_src_addr) {
/* override SRC address with configuration */
su_self_ext = (void *)mr->mr_capt_src_addr->ai_addr;
} else {
/* use the SRC address from the socket */
su_self_ext = su_self;
}

/* If we don't have socket, go out */
if (!mr->mr_capt_sock) {
Expand Down Expand Up @@ -509,13 +529,13 @@ int tport_capt_msg_hepv2 (tport_t const *self, msg_t *msg, size_t n,
if(su->su_family == AF_INET) {

memcpy(dst ? &hep_ipheader.hp_src : &hep_ipheader.hp_dst, &su->su_sin.sin_addr.s_addr, sizeof(su->su_sin.sin_addr.s_addr));
memcpy(dst ? &hep_ipheader.hp_dst : &hep_ipheader.hp_src, &su_self->su_sin.sin_addr.s_addr, sizeof(su_self->su_sin.sin_addr.s_addr));
memcpy(dst ? &hep_ipheader.hp_dst : &hep_ipheader.hp_src, &su_self_ext->su_sin.sin_addr.s_addr, sizeof(su_self_ext->su_sin.sin_addr.s_addr));
hep_header.hp_l += sizeof(struct hep_iphdr);
}
#if SU_HAVE_IN6
else {
memcpy(dst ? &hep_ip6header.hp6_src : &hep_ip6header.hp6_dst, &su->su_sin.sin_addr.s_addr, sizeof(su->su_sin.sin_addr.s_addr));
memcpy(dst ? &hep_ip6header.hp6_dst : &hep_ip6header.hp6_src, &su_self->su_sin.sin_addr.s_addr, sizeof(su_self->su_sin.sin_addr.s_addr));
memcpy(dst ? &hep_ip6header.hp6_dst : &hep_ip6header.hp6_src, &su_self_ext->su_sin.sin_addr.s_addr, sizeof(su_self_ext->su_sin.sin_addr.s_addr));
hep_header.hp_l += sizeof(struct hep_ip6hdr);
}
#endif
Expand Down Expand Up @@ -594,7 +614,7 @@ int tport_capt_msg_hepv3 (tport_t const *self, msg_t *msg, size_t n,
su_iovec_t const iov[], size_t iovused, char const *what, char **buffer)
{

su_sockaddr_t const *su, *su_self;
su_sockaddr_t const *su, *su_self, *su_self_ext;
struct hep_generic *hg=NULL;
unsigned int buflen=0, iplen=0,tlen=0, payload_len = 0;
su_time_t now;
Expand All @@ -616,6 +636,13 @@ int tport_capt_msg_hepv3 (tport_t const *self, msg_t *msg, size_t n,
su_self = self->tp_pri->pri_primary->tp_addr;

mr = self->tp_master;
if (mr->mr_capt_src_addr) {
/* override SRC address with configuration */
su_self_ext = (void *)mr->mr_capt_src_addr->ai_addr;
} else {
/* use the SRC address from the socket */
su_self_ext = su_self;
}

/* If we don't have socket, go out */
if (!mr->mr_capt_sock) {
Expand Down Expand Up @@ -665,7 +692,7 @@ int tport_capt_msg_hepv3 (tport_t const *self, msg_t *msg, size_t n,
/* DST IP */
dst_ip4.chunk.vendor_id = htons(0x0000);
dst_ip4.chunk.type_id = htons(0x0004);
memcpy(dst ? &dst_ip4.data : &src_ip4.data, &su_self->su_sin.sin_addr.s_addr, sizeof(su_self->su_sin.sin_addr.s_addr));
memcpy(dst ? &dst_ip4.data : &src_ip4.data, &su_self_ext->su_sin.sin_addr.s_addr, sizeof(su_self_ext->su_sin.sin_addr.s_addr));
dst_ip4.chunk.length = htons(sizeof(dst_ip4));

iplen = sizeof(dst_ip4) + sizeof(src_ip4);
Expand All @@ -682,7 +709,7 @@ int tport_capt_msg_hepv3 (tport_t const *self, msg_t *msg, size_t n,
/* DST IPv6 */
dst_ip6.chunk.vendor_id = htons(0x0000);
dst_ip6.chunk.type_id = htons(0x0006);
memcpy(dst ? &dst_ip6.data : &src_ip6.data, &su_self->su_sin.sin_addr.s_addr, sizeof(su_self->su_sin.sin_addr.s_addr));
memcpy(dst ? &dst_ip6.data : &src_ip6.data, &su_self_ext->su_sin.sin_addr.s_addr, sizeof(su_self_ext->su_sin.sin_addr.s_addr));
dst_ip6.chunk.length = htons(sizeof(dst_ip6));

iplen = sizeof(dst_ip6) + sizeof(src_ip6);
Expand Down
11 changes: 11 additions & 0 deletions libsofia-sip-ua/tport/tport_tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,17 @@ tag_typedef_t tptag_dump = STRTAG_TYPEDEF(dump);
*/
tag_typedef_t tptag_capt = STRTAG_TYPEDEF(capt);

/**@def TPTAG_CAPT_SRC(x)
*
* SRC address to report to capture agent.
*
* Use with tport_tcreate(), nta_agent_create(), nua_create(),
* nth_engine_create(), or initial nth_site_create().
*
* @sa #TPORT_CAPT_SRC environment variable, TPTAG_LOG().
*
*/
tag_typedef_t tptag_capt_src = STRTAG_TYPEDEF(capt);

/** Mark transport as trusted.
*
Expand Down

0 comments on commit 1285866

Please sign in to comment.