45201359dbbfe2f3791e065bdb524b57fe7d324c
[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         return config->general->uuid_type;
413 }
414
415 struct hepv3_capture_info *hepv3_create_capture_info(const void *payload, size_t len)
416 {
417         struct hepv3_capture_info *info;
418
419         info = ao2_alloc(sizeof(*info), capture_info_dtor);
420         if (!info) {
421                 return NULL;
422         }
423
424         info->payload = ast_malloc(len);
425         if (!info->payload) {
426                 ao2_ref(info, -1);
427                 return NULL;
428         }
429         memcpy(info->payload, payload, len);
430         info->len = len;
431
432         return info;
433 }
434
435 /*! \brief Callback function for the \ref hep_queue_tp taskprocessor */
436 static int hep_queue_cb(void *data)
437 {
438         RAII_VAR(struct module_config *, config, ao2_global_obj_ref(global_config), ao2_cleanup);
439         RAII_VAR(struct hepv3_runtime_data *, hepv3_data, ao2_global_obj_ref(global_data), ao2_cleanup);
440         RAII_VAR(struct hepv3_capture_info *, capture_info, data, ao2_cleanup);
441         struct hep_generic hg_pkt;
442         unsigned int packet_len = 0, sock_buffer_len;
443         struct hep_chunk_ip4 ipv4_src, ipv4_dst;
444         struct hep_chunk_ip6 ipv6_src, ipv6_dst;
445         struct hep_chunk auth_key, payload, uuid;
446         void *sock_buffer;
447         int res;
448
449         if (!capture_info || !config || !hepv3_data) {
450                 return 0;
451         }
452
453         if (ast_sockaddr_is_ipv4(&capture_info->src_addr) != ast_sockaddr_is_ipv4(&capture_info->dst_addr)) {
454                 ast_log(AST_LOG_NOTICE, "Unable to send packet: Address Family mismatch between source/destination\n");
455                 return -1;
456         }
457
458         packet_len = sizeof(hg_pkt);
459
460         /* Build HEPv3 header, capture info, and calculate the total packet size */
461         memcpy(hg_pkt.header.id, "\x48\x45\x50\x33", 4);
462
463         INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.ip_proto, CHUNK_TYPE_IP_PROTOCOL_ID, 0x11);
464         INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.src_port, CHUNK_TYPE_SRC_PORT, htons(ast_sockaddr_port(&capture_info->src_addr)));
465         INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.dst_port, CHUNK_TYPE_DST_PORT, htons(ast_sockaddr_port(&capture_info->dst_addr)));
466         INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.time_sec, CHUNK_TYPE_TIMESTAMP_SEC, htonl(capture_info->capture_time.tv_sec));
467         INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.time_usec, CHUNK_TYPE_TIMESTAMP_USEC, htonl(capture_info->capture_time.tv_usec));
468         INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.proto_t, CHUNK_TYPE_PROTOCOL_TYPE, capture_info->capture_type);
469         INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.capt_id, CHUNK_TYPE_CAPTURE_AGENT_ID, htonl(config->general->capture_id));
470
471         if (ast_sockaddr_is_ipv4(&capture_info->src_addr)) {
472                 INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.ip_family,
473                         CHUNK_TYPE_IP_PROTOCOL_FAMILY, AF_INET);
474
475                 INITIALIZE_GENERIC_HEP_CHUNK(&ipv4_src, CHUNK_TYPE_IPV4_SRC_ADDR);
476                 inet_pton(AF_INET, ast_sockaddr_stringify_addr(&capture_info->src_addr), &ipv4_src.data);
477
478                 INITIALIZE_GENERIC_HEP_CHUNK(&ipv4_dst, CHUNK_TYPE_IPV4_DST_ADDR);
479                 inet_pton(AF_INET, ast_sockaddr_stringify_addr(&capture_info->dst_addr), &ipv4_dst.data);
480
481                 packet_len += (sizeof(ipv4_src) + sizeof(ipv4_dst));
482         } else {
483                 INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.ip_family,
484                         CHUNK_TYPE_IP_PROTOCOL_FAMILY, AF_INET6);
485
486                 INITIALIZE_GENERIC_HEP_CHUNK(&ipv6_src, CHUNK_TYPE_IPV6_SRC_ADDR);
487                 inet_pton(AF_INET6, ast_sockaddr_stringify_addr(&capture_info->src_addr), &ipv6_src.data);
488
489                 INITIALIZE_GENERIC_HEP_CHUNK(&ipv6_dst, CHUNK_TYPE_IPV6_DST_ADDR);
490                 inet_pton(AF_INET6, ast_sockaddr_stringify_addr(&capture_info->dst_addr), &ipv6_dst.data);
491
492                 packet_len += (sizeof(ipv6_src) + sizeof(ipv6_dst));
493         }
494
495         if (!ast_strlen_zero(config->general->capture_password))  {
496                 INITIALIZE_GENERIC_HEP_IDS_VAR(&auth_key, CHUNK_TYPE_AUTH_KEY, strlen(config->general->capture_password));
497                 packet_len += (sizeof(auth_key) + strlen(config->general->capture_password));
498         }
499         INITIALIZE_GENERIC_HEP_IDS_VAR(&uuid, CHUNK_TYPE_UUID, strlen(capture_info->uuid));
500         packet_len += (sizeof(uuid) + strlen(capture_info->uuid));
501         INITIALIZE_GENERIC_HEP_IDS_VAR(&payload,
502                 capture_info->zipped ? CHUNK_TYPE_PAYLOAD_ZIP : CHUNK_TYPE_PAYLOAD, capture_info->len);
503         packet_len += (sizeof(payload) + capture_info->len);
504         hg_pkt.header.length = htons(packet_len);
505
506         /* Build the buffer to send */
507         sock_buffer = ast_malloc(packet_len);
508         if (!sock_buffer) {
509                 return -1;
510         }
511
512         /* Copy in the header */
513         memcpy(sock_buffer, &hg_pkt, sizeof(hg_pkt));
514         sock_buffer_len = sizeof(hg_pkt);
515
516         /* Addresses */
517         if (ast_sockaddr_is_ipv4(&capture_info->src_addr)) {
518                 memcpy(sock_buffer + sock_buffer_len, &ipv4_src, sizeof(ipv4_src));
519                 sock_buffer_len += sizeof(ipv4_src);
520                 memcpy(sock_buffer + sock_buffer_len, &ipv4_dst, sizeof(ipv4_dst));
521                 sock_buffer_len += sizeof(ipv4_dst);
522         } else {
523                 memcpy(sock_buffer + sock_buffer_len, &ipv6_src, sizeof(ipv6_src));
524                 sock_buffer_len += sizeof(ipv6_src);
525                 memcpy(sock_buffer + sock_buffer_len, &ipv6_dst, sizeof(ipv6_dst));
526                 sock_buffer_len += sizeof(ipv6_dst);
527         }
528
529         /* Auth Key */
530         if (!ast_strlen_zero(config->general->capture_password)) {
531                 memcpy(sock_buffer + sock_buffer_len, &auth_key, sizeof(auth_key));
532                 sock_buffer_len += sizeof(auth_key);
533                 memcpy(sock_buffer + sock_buffer_len, config->general->capture_password, strlen(config->general->capture_password));
534                 sock_buffer_len += strlen(config->general->capture_password);
535         }
536
537         /* UUID */
538         memcpy(sock_buffer + sock_buffer_len, &uuid, sizeof(uuid));
539         sock_buffer_len += sizeof(uuid);
540         memcpy(sock_buffer + sock_buffer_len, capture_info->uuid, strlen(capture_info->uuid));
541         sock_buffer_len += strlen(capture_info->uuid);
542
543         /* Packet! */
544         memcpy(sock_buffer + sock_buffer_len, &payload, sizeof(payload));
545         sock_buffer_len += sizeof(payload);
546         memcpy(sock_buffer + sock_buffer_len, capture_info->payload, capture_info->len);
547         sock_buffer_len += capture_info->len;
548
549         ast_assert(sock_buffer_len == packet_len);
550
551         res = ast_sendto(hepv3_data->sockfd, sock_buffer, sock_buffer_len, 0, &hepv3_data->remote_addr);
552         if (res < 0) {
553                 ast_log(AST_LOG_ERROR, "Error [%d] while sending packet to HEPv3 server: %s\n",
554                         errno, strerror(errno));
555         } else if (res != sock_buffer_len) {
556                 ast_log(AST_LOG_WARNING, "Failed to send complete packet to HEPv3 server: %d of %u sent\n",
557                         res, sock_buffer_len);
558                 res = -1;
559         }
560
561         ast_free(sock_buffer);
562         return res;
563 }
564
565 int hepv3_send_packet(struct hepv3_capture_info *capture_info)
566 {
567         RAII_VAR(struct module_config *, config, ao2_global_obj_ref(global_config), ao2_cleanup);
568         int res;
569
570         if (!config || !config->general->enabled) {
571                 ao2_ref(capture_info, -1);
572                 return 0;
573         }
574
575         res = ast_taskprocessor_push(hep_queue_tp, hep_queue_cb, capture_info);
576         if (res == -1) {
577                 ao2_ref(capture_info, -1);
578         }
579
580         return res;
581 }
582
583 /*!
584  * \brief Post-apply callback for the config framework.
585  *
586  * This will create the run-time information from the supplied
587  * configuration.
588 */
589 static void hepv3_config_post_apply(void)
590 {
591         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(global_config), ao2_cleanup);
592         struct hepv3_runtime_data *data;
593
594         data = hepv3_data_alloc(mod_cfg->general);
595         if (!data) {
596                 return;
597         }
598
599         ao2_global_obj_replace_unref(global_data, data);
600         ao2_ref(data, -1);
601 }
602
603 /*!
604  * \brief Reload the module
605  */
606 static int reload_module(void)
607 {
608         if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
609                 return -1;
610         }
611         return 0;
612 }
613
614 /*!
615  * \brief Unload the module
616  */
617 static int unload_module(void)
618 {
619         hep_queue_tp = ast_taskprocessor_unreference(hep_queue_tp);
620
621         ao2_global_obj_release(global_config);
622         ao2_global_obj_release(global_data);
623         aco_info_destroy(&cfg_info);
624
625         return 0;
626 }
627
628 /*!
629  * \brief Load the module
630  */
631 static int load_module(void)
632 {
633         if (aco_info_init(&cfg_info)) {
634                 goto error;
635         }
636
637         hep_queue_tp = ast_taskprocessor_get("hep_queue_tp", TPS_REF_DEFAULT);
638         if (!hep_queue_tp) {
639                 goto error;
640         }
641
642         aco_option_register(&cfg_info, "enabled", ACO_EXACT, global_options, "yes", OPT_BOOL_T, 1, FLDSET(struct hepv3_global_config, enabled));
643         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));
644         aco_option_register(&cfg_info, "capture_password", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct hepv3_global_config, capture_password));
645         aco_option_register(&cfg_info, "capture_id", ACO_EXACT, global_options, "0", OPT_UINT_T, 0, STRFLDSET(struct hepv3_global_config, capture_id));
646         aco_option_register_custom(&cfg_info, "uuid_type", ACO_EXACT, global_options, "call-id", uuid_type_handler, 0);
647
648         if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
649                 goto error;
650         }
651
652         return AST_MODULE_LOAD_SUCCESS;
653
654 error:
655         aco_info_destroy(&cfg_info);
656         return AST_MODULE_LOAD_DECLINE;
657 }
658
659 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "HEPv3 API",
660         .support_level = AST_MODULE_SUPPORT_EXTENDED,
661         .load = load_module,
662         .unload = unload_module,
663         .reload = reload_module,
664         .load_pri = AST_MODPRI_APP_DEPEND,
665 );