Debian Codegraph @

res/res_hep.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2014, Digium, Inc.
5 *
6 * Alexandr Dubovikov <[email protected]>
7 * Matt Jordan <[email protected]>
8 *
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
14 *
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
18 */
19
20 /*!
21 * \file
22 * \brief Routines for integration with Homer using HEPv3
23 *
24 * \author Alexandr Dubovikov <[email protected]>
25 * \author Matt Jordan <[email protected]>
26 *
27 */
28
29 /*!
30 * \li \ref res_hep.c uses the configuration file \ref hep.conf
31 * \addtogroup configuration_file Configuration Files
32 */
33
34 /*!
35 * \page hep.conf hep.conf
36 * \verbinclude hep.conf.sample
37 */
38
39 /*** MODULEINFO
40 <support_level>extended</support_level>
41 ***/
42
43 /*** DOCUMENTATION
44 <configInfo name="res_hep" language="en_US">
45 <synopsis>Resource for integration with Homer using HEPv3</synopsis>
46 <configFile name="hep.conf">
47 <configObject name="general">
48 <since>
49 <version>12.2.0</version>
50 </since>
51 <synopsis>General settings.</synopsis>
52 <description><para>
53 The <emphasis>general</emphasis> settings section contains information
54 to configure Asterisk as a Homer capture agent.
55 </para>
56 </description>
57 <configOption name="enabled" default="yes">
58 <since>
59 <version>12.2.0</version>
60 </since>
61 <synopsis>Enable or disable packet capturing.</synopsis>
62 <description>
63 <enumlist>
64 <enum name="no" />
65 <enum name="yes" />
66 </enumlist>
67 </description>
68 </configOption>
69 <configOption name="uuid_type" default="call-id">
70 <since>
71 <version>12.2.0</version>
72 </since>
73 <synopsis>The preferred type of UUID to pass to Homer.</synopsis>
74 <description>
75 <enumlist>
76 <enum name="call-id"><para>Use the PJSIP Call-Id</para></enum>
77 <enum name="channel"><para>Use the Asterisk channel name</para></enum>
78 </enumlist>
79 </description>
80 </configOption>
81 <configOption name="capture_address">
82 <since>
83 <version>13.16.0</version>
84 <version>14.5.0</version>
85 </since>
86 <synopsis>The address and port of the Homer server to send packets to.</synopsis>
87 </configOption>
88 <configOption name="capture_password">
89 <since>
90 <version>12.2.0</version>
91 </since>
92 <synopsis>If set, the authentication password to send to Homer.</synopsis>
93 </configOption>
94 <configOption name="capture_id" default="0">
95 <since>
96 <version>12.2.0</version>
97 </since>
98 <synopsis>The ID for this capture agent.</synopsis>
99 </configOption>
100 <configOption name="capture_name" default="">
101 <since>
102 <version>18.16.0</version>
103 <version>20.1.0</version>
104 </since>
105 <synopsis>The name for this capture agent.</synopsis>
106 </configOption>
107 </configObject>
108 </configFile>
109 </configInfo>
110 ***/
111
112 #include "asterisk.h"
113
114 #include "asterisk/module.h"
115 #include "asterisk/astobj2.h"
116 #include "asterisk/config_options.h"
117 #include "asterisk/taskprocessor.h"
118 #include "asterisk/res_hep.h"
119
120 #include <netinet/ip.h>
121 #include <netinet/tcp.h>
122 #include <netinet/udp.h>
123 #include <netinet/ip6.h>
124
125 /*! Generic vendor ID. Used for HEPv3 standard packets */
126 #define GENERIC_VENDOR_ID 0x0000
127
128 /*! Asterisk vendor ID. Used for custom data to send to a capture node */
129 #define ASTERISK_VENDOR_ID 0x0004
130
131 /*! Chunk types from the HEPv3 Spec */
132 enum hepv3_chunk_types {
133
134 /*! THE IP PROTOCOL FAMILY */
135 CHUNK_TYPE_IP_PROTOCOL_FAMILY = 0X0001,
136
137 /*! THE IP PROTOCOL ID (UDP, TCP, ETC.) */
138 CHUNK_TYPE_IP_PROTOCOL_ID = 0X0002,
139
140 /*! IF IPV4, THE SOURCE ADDRESS */
141 CHUNK_TYPE_IPV4_SRC_ADDR = 0X0003,
142
143 /*! IF IPV4, THE DESTINATION ADDRESS */
144 CHUNK_TYPE_IPV4_DST_ADDR = 0X0004,
145
146 /*! IF IPV6, THE SOURCE ADDRESS */
147 CHUNK_TYPE_IPV6_SRC_ADDR = 0X0005,
148
149 /*! IF IPV6, THE DESTINATION ADDRESS */
150 CHUNK_TYPE_IPV6_DST_ADDR = 0X0006,
151
152 /*! THE SOURCE PORT */
153 CHUNK_TYPE_SRC_PORT = 0X0007,
154
155 /*! THE DESTINATION PORT */
156 CHUNK_TYPE_DST_PORT = 0X0008,
157
158 /*! THE CAPTURE TIME (SECONDS) */
159 CHUNK_TYPE_TIMESTAMP_SEC = 0X0009,
160
161 /*! THE CAPTURE TIME (MICROSECONDS) */
162 CHUNK_TYPE_TIMESTAMP_USEC = 0X000A,
163
164 /*! THE PROTOCOL PACKET TYPE. SEE /REF HEPV3_CAPTURE_TYPE */
165 CHUNK_TYPE_PROTOCOL_TYPE = 0X000B,
166
167 /*! OUR CAPTURE AGENT ID */
168 CHUNK_TYPE_CAPTURE_AGENT_ID = 0X000C,
169
170 /*! A KEEP ALIVE TIMER */
171 CHUNK_TYPE_KEEP_ALIVE_TIMER = 0X000D,
172
173 /*! THE CAPTURE_PASSWORD IF DEFINED */
174 CHUNK_TYPE_AUTH_KEY = 0X000E,
175
176 /*! THE ONE AND ONLY PAYLOAD */
177 CHUNK_TYPE_PAYLOAD = 0X000F,
178
179 /*! THE ONE AND ONLY (ZIPPED) PAYLOAD */
180 CHUNK_TYPE_PAYLOAD_ZIP = 0X0010,
181
182 /*! THE UUID FOR THIS PACKET */
183 CHUNK_TYPE_UUID = 0X0011,
184
185 /*! THE CAPTURE AGENT NAME */
186 CHUNK_TYPE_CAPTURE_AGENT_NAME = 0X0013,
187 };
188
189 #define INITIALIZE_GENERIC_HEP_IDS(hep_chunk, type) do { \
190 (hep_chunk)->vendor_id = htons(GENERIC_VENDOR_ID); \
191 (hep_chunk)->type_id = htons((type)); \
192 } while (0)
193
194 #define INITIALIZE_GENERIC_HEP_IDS_VAR(hep_chunk, type, len) do { \
195 INITIALIZE_GENERIC_HEP_IDS((hep_chunk), (type)); \
196 (hep_chunk)->length = htons(sizeof(*(hep_chunk)) + len); \
197 } while (0)
198
199 #define INITIALIZE_GENERIC_HEP_CHUNK(hep_item, type) do { \
200 INITIALIZE_GENERIC_HEP_IDS(&(hep_item)->chunk, (type)); \
201 (hep_item)->chunk.length = htons(sizeof(*(hep_item))); \
202 } while (0)
203
204 #define INITIALIZE_GENERIC_HEP_CHUNK_DATA(hep_item, type, value) do { \
205 INITIALIZE_GENERIC_HEP_CHUNK((hep_item), (type)); \
206 (hep_item)->data = (value); \
207 } while (0)
208
209 /*
210 * HEPv3 Types.
211 * Note that the content in these is stored in network byte order.
212 */
213
214 struct hep_chunk {
215 u_int16_t vendor_id;
216 u_int16_t type_id;
217 u_int16_t length;
218 } __attribute__((packed));
219
220 struct hep_chunk_uint8 {
221 struct hep_chunk chunk;
222 u_int8_t data;
223 } __attribute__((packed));
224
225 struct hep_chunk_uint16 {
226 struct hep_chunk chunk;
227 u_int16_t data;
228 } __attribute__((packed));
229
230 struct hep_chunk_uint32 {
231 struct hep_chunk chunk;
232 u_int32_t data;
233 } __attribute__((packed));
234
235 struct hep_chunk_ip4 {
236 struct hep_chunk chunk;
237 struct in_addr data;
238 } __attribute__((packed));
239
240 struct hep_chunk_ip6 {
241 struct hep_chunk chunk;
242 struct in6_addr data;
243 } __attribute__((packed));
244
245 struct hep_ctrl {
246 char id[4];
247 u_int16_t length;
248 } __attribute__((packed));
249
250 /* HEP structures */
251
252 struct hep_generic {
253 struct hep_ctrl header;
254 struct hep_chunk_uint8 ip_family;
255 struct hep_chunk_uint8 ip_proto;
256 struct hep_chunk_uint16 src_port;
257 struct hep_chunk_uint16 dst_port;
258 struct hep_chunk_uint32 time_sec;
259 struct hep_chunk_uint32 time_usec;
260 struct hep_chunk_uint8 proto_t;
261 struct hep_chunk_uint32 capt_id;
262 } __attribute__((packed));
263
264 /*! \brief Global configuration for the module */
265 struct hepv3_global_config {
266 unsigned int enabled; /*!< Whether or not sending is enabled */
267 unsigned int capture_id; /*!< Capture ID for this agent */
268 enum hep_uuid_type uuid_type; /*!< The preferred type of the UUID */
269 AST_DECLARE_STRING_FIELDS(
270 AST_STRING_FIELD(capture_address); /*!< Address to send to */
271 AST_STRING_FIELD(capture_password); /*!< Password for Homer server */
272 AST_STRING_FIELD(capture_name); /*!< Capture name for this agent */
273 );
274 };
275
276 /*! \brief The actual module config */
277 struct module_config {
278 struct hepv3_global_config *general; /*!< The general config settings */
279 };
280
281 /*! \brief Run-time data derived from \ref hepv3_global_config */
282 struct hepv3_runtime_data {
283 struct ast_sockaddr remote_addr; /*!< The address to send to */
284 int sockfd; /*!< The socket file descriptor */
285 };
286
287 static struct aco_type global_option = {
288 .type = ACO_GLOBAL,
289 .name = "general",
290 .item_offset = offsetof(struct module_config, general),
291 .category_match = ACO_WHITELIST_EXACT,
292 .category = "general",
293 };
294
295 struct aco_type *global_options[] = ACO_TYPES(&global_option);
296
297 struct aco_file hepv3_conf = {
298 .filename = "hep.conf",
299 .types = ACO_TYPES(&global_option),
300 };
301
302 /*! \brief The module configuration container */
303 static AO2_GLOBAL_OBJ_STATIC(global_config);
304
305 /*! \brief Current module data */
306 static AO2_GLOBAL_OBJ_STATIC(global_data);
307
308 static struct ast_taskprocessor *hep_queue_tp;
309
310 static void *module_config_alloc(void);
311 static int hepv3_config_pre_apply(void);
312 static void hepv3_config_post_apply(void);
313
314 /*! \brief Register information about the configs being processed by this module */
315 CONFIG_INFO_STANDARD(cfg_info, global_config, module_config_alloc,
316 .files = ACO_FILES(&hepv3_conf),
317 .pre_apply_config = hepv3_config_pre_apply,
318 .post_apply_config = hepv3_config_post_apply,
319 );
320
321 static void hepv3_config_dtor(void *obj)
322 {
323 struct hepv3_global_config *config = obj;
324
325 ast_string_field_free_memory(config);
326 }
327
328 /*! \brief HEPv3 configuration object allocation */
329 static void *hepv3_config_alloc(void)
330 {
331 struct hepv3_global_config *config;
332
333 config = ao2_alloc(sizeof(*config), hepv3_config_dtor);
334 if (!config || ast_string_field_init(config, 32)) {
335 return NULL;
336 }
337
338 return config;
339 }
340
341 /*! \brief Configuration object destructor */
342 static void module_config_dtor(void *obj)
343 {
344 struct module_config *config = obj;
345
346 if (config->general) {
347 ao2_ref(config->general, -1);
348 }
349 }
350
351 /*! \brief Module config constructor */
352 static void *module_config_alloc(void)
353 {
354 struct module_config *config;
355
356 config = ao2_alloc(sizeof(*config), module_config_dtor);
357 if (!config) {
358 return NULL;
359 }
360
361 config->general = hepv3_config_alloc();
362 if (!config->general) {
363 ao2_ref(config, -1);
364 config = NULL;
365 }
366
367 return config;
368 }
369
370 /*! \brief Handler for the uuid_type attribute */
371 static int uuid_type_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
372 {
373 struct hepv3_global_config *global_config = obj;
374
375 if (strcasecmp(var->name, "uuid_type")) {
376 return -1;
377 }
378
379 if (!strcasecmp(var->value, "channel")) {
380 global_config->uuid_type = HEP_UUID_TYPE_CHANNEL;
381 } else if (!strcasecmp(var->value, "call-id")) {
382 global_config->uuid_type = HEP_UUID_TYPE_CALL_ID;
383 } else {
384 return -1;
385 }
386 return 0;
387 }
388
389 /*! \brief HEPv3 run-time data destructor */
390 static void hepv3_data_dtor(void *obj)
391 {
392 struct hepv3_runtime_data *data = obj;
393
394 if (data->sockfd > -1) {
395 close(data->sockfd);
396 data->sockfd = -1;
397 }
398 }
399
400 /*! \brief Allocate the HEPv3 run-time data */
401 static struct hepv3_runtime_data *hepv3_data_alloc(struct hepv3_global_config *config)
402 {
403 struct hepv3_runtime_data *data;
404
405 data = ao2_alloc(sizeof(*data), hepv3_data_dtor);
406 if (!data) {
407 return NULL;
408 }
409
410 data->sockfd = -1;
411
412 if (ast_sockaddr_resolve_first_af(&data->remote_addr, config->capture_address, PARSE_PORT_REQUIRE, AST_AF_UNSPEC)) {
413 ast_log(AST_LOG_WARNING, "Failed to create address from %s\n", config->capture_address);
414 ao2_ref(data, -1);
415 return NULL;
416
417 }
418
419 data->sockfd = socket(ast_sockaddr_is_ipv6(&data->remote_addr) ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
420 if (data->sockfd < 0) {
421 ast_log(AST_LOG_WARNING, "Failed to create socket for address %s: %s\n",
422 config->capture_address, strerror(errno));
423 ao2_ref(data, -1);
424 return NULL;
425 }
426
427 return data;
428 }
429
430 /*! \brief Destructor for a \ref hepv3_capture_info object */
431 static void capture_info_dtor(void *obj)
432 {
433 struct hepv3_capture_info *info = obj;
434
435 ast_free(info->uuid);
436 ast_free(info->payload);
437 }
438
439 enum hep_uuid_type hepv3_get_uuid_type(void)
440 {
441 RAII_VAR(struct module_config *, config, ao2_global_obj_ref(global_config), ao2_cleanup);
442
443 if (!config) {
444 /* Well, that's unfortunate. Return something. */
445 return HEP_UUID_TYPE_CALL_ID;
446 }
447
448 return config->general->uuid_type;
449 }
450
451 int hepv3_is_loaded(void)
452 {
453 RAII_VAR(struct module_config *, config, ao2_global_obj_ref(global_config), ao2_cleanup);
454
455 return config && config->general->enabled;
456 }
457
458 struct hepv3_capture_info *hepv3_create_capture_info(const void *payload, size_t len)
459 {
460 struct hepv3_capture_info *info;
461
462 info = ao2_alloc(sizeof(*info), capture_info_dtor);
463 if (!info) {
464 return NULL;
465 }
466
467 info->payload = ast_malloc(len);
468 if (!info->payload) {
469 ao2_ref(info, -1);
470 return NULL;
471 }
472 memcpy(info->payload, payload, len);
473 info->len = len;
474
475 /* Set a reasonable default */
476 info->protocol_id = IPPROTO_UDP;
477
478 return info;
479 }
480
481 /*! \brief Callback function for the \ref hep_queue_tp taskprocessor */
482 static int hep_queue_cb(void *data)
483 {
484 RAII_VAR(struct module_config *, config, ao2_global_obj_ref(global_config), ao2_cleanup);
485 RAII_VAR(struct hepv3_runtime_data *, hepv3_data, ao2_global_obj_ref(global_data), ao2_cleanup);
486 RAII_VAR(struct hepv3_capture_info *, capture_info, data, ao2_cleanup);
487 struct hep_generic hg_pkt;
488 unsigned int packet_len = 0, sock_buffer_len;
489 struct hep_chunk_ip4 ipv4_src, ipv4_dst;
490 struct hep_chunk_ip6 ipv6_src, ipv6_dst;
491 struct hep_chunk auth_key, payload, uuid, capturename;
492 void *sock_buffer;
493 int res;
494
495 if (!capture_info || !config || !hepv3_data) {
496 return 0;
497 }
498
499 if (ast_sockaddr_is_ipv4(&capture_info->src_addr) != ast_sockaddr_is_ipv4(&capture_info->dst_addr)) {
500 ast_log(AST_LOG_NOTICE, "Unable to send packet: Address Family mismatch between source/destination\n");
501 return -1;
502 }
503
504 packet_len = sizeof(hg_pkt);
505
506 /* Build HEPv3 header, capture info, and calculate the total packet size */
507 memcpy(hg_pkt.header.id, "\x48\x45\x50\x33", 4);
508
509 INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.ip_proto, CHUNK_TYPE_IP_PROTOCOL_ID, capture_info->protocol_id);
510 INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.src_port, CHUNK_TYPE_SRC_PORT, htons(ast_sockaddr_port(&capture_info->src_addr)));
511 INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.dst_port, CHUNK_TYPE_DST_PORT, htons(ast_sockaddr_port(&capture_info->dst_addr)));
512 INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.time_sec, CHUNK_TYPE_TIMESTAMP_SEC, htonl(capture_info->capture_time.tv_sec));
513 INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.time_usec, CHUNK_TYPE_TIMESTAMP_USEC, htonl(capture_info->capture_time.tv_usec));
514 INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.proto_t, CHUNK_TYPE_PROTOCOL_TYPE, capture_info->capture_type);
515 INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.capt_id, CHUNK_TYPE_CAPTURE_AGENT_ID, htonl(config->general->capture_id));
516
517 if (ast_sockaddr_is_ipv4(&capture_info->src_addr)) {
518 INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.ip_family,
519 CHUNK_TYPE_IP_PROTOCOL_FAMILY, AF_INET);
520
521 INITIALIZE_GENERIC_HEP_CHUNK(&ipv4_src, CHUNK_TYPE_IPV4_SRC_ADDR);
522 inet_pton(AF_INET, ast_sockaddr_stringify_addr(&capture_info->src_addr), &ipv4_src.data);
523
524 INITIALIZE_GENERIC_HEP_CHUNK(&ipv4_dst, CHUNK_TYPE_IPV4_DST_ADDR);
525 inet_pton(AF_INET, ast_sockaddr_stringify_addr(&capture_info->dst_addr), &ipv4_dst.data);
526
527 packet_len += (sizeof(ipv4_src) + sizeof(ipv4_dst));
528 } else {
529 INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.ip_family,
530 CHUNK_TYPE_IP_PROTOCOL_FAMILY, AF_INET6);
531
532 INITIALIZE_GENERIC_HEP_CHUNK(&ipv6_src, CHUNK_TYPE_IPV6_SRC_ADDR);
533 inet_pton(AF_INET6, ast_sockaddr_stringify_addr(&capture_info->src_addr), &ipv6_src.data);
534
535 INITIALIZE_GENERIC_HEP_CHUNK(&ipv6_dst, CHUNK_TYPE_IPV6_DST_ADDR);
536 inet_pton(AF_INET6, ast_sockaddr_stringify_addr(&capture_info->dst_addr), &ipv6_dst.data);
537
538 packet_len += (sizeof(ipv6_src) + sizeof(ipv6_dst));
539 }
540
541 if (!ast_strlen_zero(config->general->capture_password)) {
542 INITIALIZE_GENERIC_HEP_IDS_VAR(&auth_key, CHUNK_TYPE_AUTH_KEY, strlen(config->general->capture_password));
543 packet_len += (sizeof(auth_key) + strlen(config->general->capture_password));
544 }
545 if (!ast_strlen_zero(config->general->capture_name)) {
546 INITIALIZE_GENERIC_HEP_IDS_VAR(&capturename, CHUNK_TYPE_CAPTURE_AGENT_NAME, strlen(config->general->capture_name));
547 packet_len += (sizeof(capturename) + strlen(config->general->capture_name));
548 }
549 INITIALIZE_GENERIC_HEP_IDS_VAR(&uuid, CHUNK_TYPE_UUID, strlen(capture_info->uuid));
550 packet_len += (sizeof(uuid) + strlen(capture_info->uuid));
551 INITIALIZE_GENERIC_HEP_IDS_VAR(&payload,
552 capture_info->zipped ? CHUNK_TYPE_PAYLOAD_ZIP : CHUNK_TYPE_PAYLOAD, capture_info->len);
553 packet_len += (sizeof(payload) + capture_info->len);
554 hg_pkt.header.length = htons(packet_len);
555
556 /* Build the buffer to send */
557 sock_buffer = ast_malloc(packet_len);
558 if (!sock_buffer) {
559 return -1;
560 }
561
562 /* Copy in the header */
563 memcpy(sock_buffer, &hg_pkt, sizeof(hg_pkt));
564 sock_buffer_len = sizeof(hg_pkt);
565
566 /* Addresses */
567 if (ast_sockaddr_is_ipv4(&capture_info->src_addr)) {
568 memcpy(sock_buffer + sock_buffer_len, &ipv4_src, sizeof(ipv4_src));
569 sock_buffer_len += sizeof(ipv4_src);
570 memcpy(sock_buffer + sock_buffer_len, &ipv4_dst, sizeof(ipv4_dst));
571 sock_buffer_len += sizeof(ipv4_dst);
572 } else {
573 memcpy(sock_buffer + sock_buffer_len, &ipv6_src, sizeof(ipv6_src));
574 sock_buffer_len += sizeof(ipv6_src);
575 memcpy(sock_buffer + sock_buffer_len, &ipv6_dst, sizeof(ipv6_dst));
576 sock_buffer_len += sizeof(ipv6_dst);
577 }
578
579 /* Auth Key */
580 if (!ast_strlen_zero(config->general->capture_password)) {
581 memcpy(sock_buffer + sock_buffer_len, &auth_key, sizeof(auth_key));
582 sock_buffer_len += sizeof(auth_key);
583 memcpy(sock_buffer + sock_buffer_len, config->general->capture_password, strlen(config->general->capture_password));
584 sock_buffer_len += strlen(config->general->capture_password);
585 }
586
587 /* UUID */
588 memcpy(sock_buffer + sock_buffer_len, &uuid, sizeof(uuid));
589 sock_buffer_len += sizeof(uuid);
590 memcpy(sock_buffer + sock_buffer_len, capture_info->uuid, strlen(capture_info->uuid));
591 sock_buffer_len += strlen(capture_info->uuid);
592
593 /* Capture Agent Name */
594 if (!ast_strlen_zero(config->general->capture_name)) {
595 memcpy(sock_buffer + sock_buffer_len, &capturename, sizeof(capturename));
596 sock_buffer_len += sizeof(capturename);
597 memcpy(sock_buffer + sock_buffer_len, config->general->capture_name, strlen(config->general->capture_name));
598 sock_buffer_len += strlen(config->general->capture_name);
599 }
600
601 /* Packet! */
602 memcpy(sock_buffer + sock_buffer_len, &payload, sizeof(payload));
603 sock_buffer_len += sizeof(payload);
604 memcpy(sock_buffer + sock_buffer_len, capture_info->payload, capture_info->len);
605 sock_buffer_len += capture_info->len;
606
607 ast_assert(sock_buffer_len == packet_len);
608
609 res = ast_sendto(hepv3_data->sockfd, sock_buffer, sock_buffer_len, 0, &hepv3_data->remote_addr);
610 if (res < 0) {
611 ast_log(AST_LOG_ERROR, "Error [%d] while sending packet to HEPv3 server: %s\n",
612 errno, strerror(errno));
613 } else if (res != sock_buffer_len) {
614 ast_log(AST_LOG_WARNING, "Failed to send complete packet to HEPv3 server: %d of %u sent\n",
615 res, sock_buffer_len);
616 res = -1;
617 }
618
619 ast_free(sock_buffer);
620 return res;
621 }
622
623 int hepv3_send_packet(struct hepv3_capture_info *capture_info)
624 {
625 RAII_VAR(struct module_config *, config, ao2_global_obj_ref(global_config), ao2_cleanup);
626 int res;
627
628 if (!config || !config->general->enabled) {
629 ao2_ref(capture_info, -1);
630 return 0;
631 }
632
633 res = ast_taskprocessor_push(hep_queue_tp, hep_queue_cb, capture_info);
634 if (res == -1) {
635 ao2_ref(capture_info, -1);
636 }
637
638 return res;
639 }
640
641 /*!
642 * \brief Pre-apply callback for the config framework.
643 *
644 * This validates that required fields exist and are populated.
645 */
646 static int hepv3_config_pre_apply(void)
647 {
648 struct module_config *config = aco_pending_config(&cfg_info);
649
650 if (!config->general->enabled) {
651 /* If we're not enabled, we don't care about anything else */
652 return 0;
653 }
654
655 if (ast_strlen_zero(config->general->capture_address)) {
656 ast_log(AST_LOG_ERROR, "Missing required configuration option 'capture_address'\n");
657 return -1;
658 }
659
660 return 0;
661 }
662
663 /*!
664 * \brief Post-apply callback for the config framework.
665 *
666 * This will create the run-time information from the supplied
667 * configuration.
668 */
669 static void hepv3_config_post_apply(void)
670 {
671 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(global_config), ao2_cleanup);
672 struct hepv3_runtime_data *data;
673
674 data = hepv3_data_alloc(mod_cfg->general);
675 if (!data) {
676 return;
677 }
678
679 ao2_global_obj_replace_unref(global_data, data);
680 ao2_ref(data, -1);
681 }
682
683 /*!
684 * \brief Reload the module
685 */
686 static int reload_module(void)
687 {
688 if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
689 return -1;
690 }
691 return 0;
692 }
693
694 /*!
695 * \brief Unload the module
696 */
697 static int unload_module(void)
698 {
699 hep_queue_tp = ast_taskprocessor_unreference(hep_queue_tp);
700
701 ao2_global_obj_release(global_config);
702 ao2_global_obj_release(global_data);
703 aco_info_destroy(&cfg_info);
704
705 return 0;
706 }
707
708 /*!
709 * \brief Load the module
710 */
711 static int load_module(void)
712 {
713 if (aco_info_init(&cfg_info)) {
714 goto error;
715 }
716
717 hep_queue_tp = ast_taskprocessor_get("hep_queue_tp", TPS_REF_DEFAULT);
718 if (!hep_queue_tp) {
719 goto error;
720 }
721
722 aco_option_register(&cfg_info, "enabled", ACO_EXACT, global_options, "yes", OPT_BOOL_T, 1, FLDSET(struct hepv3_global_config, enabled));
723 aco_option_register(&cfg_info, "capture_address", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 1, STRFLDSET(struct hepv3_global_config, capture_address));
724 aco_option_register(&cfg_info, "capture_password", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct hepv3_global_config, capture_password));
725 aco_option_register(&cfg_info, "capture_id", ACO_EXACT, global_options, "0", OPT_UINT_T, 0, STRFLDSET(struct hepv3_global_config, capture_id));
726 aco_option_register(&cfg_info, "capture_name", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct hepv3_global_config, capture_name));
727 aco_option_register_custom(&cfg_info, "uuid_type", ACO_EXACT, global_options, "call-id", uuid_type_handler, 0);
728
729 if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
730 goto error;
731 }
732
733 return AST_MODULE_LOAD_SUCCESS;
734
735 error:
736 aco_info_destroy(&cfg_info);
737 return AST_MODULE_LOAD_DECLINE;
738 }
739
740 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "HEPv3 API",
741 .support_level = AST_MODULE_SUPPORT_EXTENDED,
742 .load = load_module,
743 .unload = unload_module,
744 .reload = reload_module,
745 .load_pri = AST_MODPRI_APP_DEPEND,
746 );