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