From 531eacd6c97c2c44e88b0e26c9d5c2acc4d7f463 Mon Sep 17 00:00:00 2001
From: Naveen Albert <asterisk@phreaknet.org>
Date: Mon, 21 Nov 2022 18:53:49 +0000
Subject: [PATCH] res_hep: Add support for named capture agents.

Adds support for the capture agent name field
of the Homer protocol to Asterisk by allowing
users to specify a name that will be sent to
the HEP server.

ASTERISK-30322 #close

Change-Id: I6136583017f9dd08daeb8be02f60fb8df4639a2b
---
 configs/samples/hep.conf.sample |  3 +++
 doc/CHANGES-staging/res_hep.txt |  5 +++++
 res/res_hep.c                   | 22 +++++++++++++++++++++-
 3 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 doc/CHANGES-staging/res_hep.txt

diff --git a/configs/samples/hep.conf.sample b/configs/samples/hep.conf.sample
index 48586445e4..db39bed26e 100644
--- a/configs/samples/hep.conf.sample
+++ b/configs/samples/hep.conf.sample
@@ -22,6 +22,9 @@ capture_password = foo             ; If specified, the authorization password
 capture_id = 1234                  ; A unique integer identifier for this
                                    ; server. This ID will be embedded sent
                                    ; with each packet from this server.
+;capture_name = asterisk           ; A unique string identifier for this
+                                   ; server. This ID will be embedded sent
+                                   ; with each packet from this server.
 uuid_type = call-id                ; Specify the preferred source for the Homer
                                    ; correlation UUID. Valid options are:
                                    ; - 'call-id' for the PJSIP or chan_sip SIP
diff --git a/doc/CHANGES-staging/res_hep.txt b/doc/CHANGES-staging/res_hep.txt
new file mode 100644
index 0000000000..fb386a11d4
--- /dev/null
+++ b/doc/CHANGES-staging/res_hep.txt
@@ -0,0 +1,5 @@
+Subject: Add support for named capture agent.
+
+A name for the capture agent can now be specified
+using the capture_name option which, if specified,
+will be sent to the HEP server.
diff --git a/res/res_hep.c b/res/res_hep.c
index 3241801b05..36f7e43ec8 100644
--- a/res/res_hep.c
+++ b/res/res_hep.c
@@ -78,6 +78,9 @@
 				<configOption name="capture_id" default="0">
 					<synopsis>The ID for this capture agent.</synopsis>
 				</configOption>
+				<configOption name="capture_name" default="">
+					<synopsis>The name for this capture agent.</synopsis>
+				</configOption>
 			</configObject>
 		</configFile>
 	</configInfo>
@@ -155,6 +158,9 @@ enum hepv3_chunk_types {
 
 	/*! THE UUID FOR THIS PACKET */
 	CHUNK_TYPE_UUID = 0X0011,
+
+	/*! THE CAPTURE AGENT NAME */
+	CHUNK_TYPE_CAPTURE_AGENT_NAME = 0X0013,
 };
 
 #define INITIALIZE_GENERIC_HEP_IDS(hep_chunk, type) do { \
@@ -240,6 +246,7 @@ struct hepv3_global_config {
 	AST_DECLARE_STRING_FIELDS(
 		AST_STRING_FIELD(capture_address);   /*!< Address to send to */
 		AST_STRING_FIELD(capture_password);  /*!< Password for Homer server */
+		AST_STRING_FIELD(capture_name);      /*!< Capture name for this agent */
 	);
 };
 
@@ -458,7 +465,7 @@ static int hep_queue_cb(void *data)
 	unsigned int packet_len = 0, sock_buffer_len;
 	struct hep_chunk_ip4 ipv4_src, ipv4_dst;
 	struct hep_chunk_ip6 ipv6_src, ipv6_dst;
-	struct hep_chunk auth_key, payload, uuid;
+	struct hep_chunk auth_key, payload, uuid, capturename;
 	void *sock_buffer;
 	int res;
 
@@ -512,6 +519,10 @@ static int hep_queue_cb(void *data)
 		INITIALIZE_GENERIC_HEP_IDS_VAR(&auth_key, CHUNK_TYPE_AUTH_KEY, strlen(config->general->capture_password));
 		packet_len += (sizeof(auth_key) + strlen(config->general->capture_password));
 	}
+	if (!ast_strlen_zero(config->general->capture_name))  {
+		INITIALIZE_GENERIC_HEP_IDS_VAR(&capturename, CHUNK_TYPE_CAPTURE_AGENT_NAME, strlen(config->general->capture_name));
+		packet_len += (sizeof(capturename) + strlen(config->general->capture_name));
+	}
 	INITIALIZE_GENERIC_HEP_IDS_VAR(&uuid, CHUNK_TYPE_UUID, strlen(capture_info->uuid));
 	packet_len += (sizeof(uuid) + strlen(capture_info->uuid));
 	INITIALIZE_GENERIC_HEP_IDS_VAR(&payload,
@@ -556,6 +567,14 @@ static int hep_queue_cb(void *data)
 	memcpy(sock_buffer + sock_buffer_len, capture_info->uuid, strlen(capture_info->uuid));
 	sock_buffer_len += strlen(capture_info->uuid);
 
+	/* Capture Agent Name */
+	if (!ast_strlen_zero(config->general->capture_name)) {
+		memcpy(sock_buffer + sock_buffer_len, &capturename, sizeof(capturename));
+		sock_buffer_len += sizeof(capturename);
+		memcpy(sock_buffer + sock_buffer_len, config->general->capture_name, strlen(config->general->capture_name));
+		sock_buffer_len += strlen(config->general->capture_name);
+	}
+
 	/* Packet! */
 	memcpy(sock_buffer + sock_buffer_len, &payload, sizeof(payload));
 	sock_buffer_len += sizeof(payload);
@@ -681,6 +700,7 @@ static int load_module(void)
 	aco_option_register(&cfg_info, "capture_address", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 1, STRFLDSET(struct hepv3_global_config, capture_address));
 	aco_option_register(&cfg_info, "capture_password", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct hepv3_global_config, capture_password));
 	aco_option_register(&cfg_info, "capture_id", ACO_EXACT, global_options, "0", OPT_UINT_T, 0, STRFLDSET(struct hepv3_global_config, capture_id));
+	aco_option_register(&cfg_info, "capture_name", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct hepv3_global_config, capture_name));
 	aco_option_register_custom(&cfg_info, "uuid_type", ACO_EXACT, global_options, "call-id", uuid_type_handler, 0);
 
 	if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
-- 
GitLab