| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 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 |
|
| 126 |
#define GENERIC_VENDOR_ID 0x0000 |
| 127 |
|
| 128 |
|
| 129 |
#define ASTERISK_VENDOR_ID 0x0004 |
| 130 |
|
| 131 |
|
| 132 |
enum hepv3_chunk_types { |
| 133 |
|
| 134 |
|
| 135 |
CHUNK_TYPE_IP_PROTOCOL_FAMILY = 0X0001, |
| 136 |
|
| 137 |
|
| 138 |
CHUNK_TYPE_IP_PROTOCOL_ID = 0X0002, |
| 139 |
|
| 140 |
|
| 141 |
CHUNK_TYPE_IPV4_SRC_ADDR = 0X0003, |
| 142 |
|
| 143 |
|
| 144 |
CHUNK_TYPE_IPV4_DST_ADDR = 0X0004, |
| 145 |
|
| 146 |
|
| 147 |
CHUNK_TYPE_IPV6_SRC_ADDR = 0X0005, |
| 148 |
|
| 149 |
|
| 150 |
CHUNK_TYPE_IPV6_DST_ADDR = 0X0006, |
| 151 |
|
| 152 |
|
| 153 |
CHUNK_TYPE_SRC_PORT = 0X0007, |
| 154 |
|
| 155 |
|
| 156 |
CHUNK_TYPE_DST_PORT = 0X0008, |
| 157 |
|
| 158 |
|
| 159 |
CHUNK_TYPE_TIMESTAMP_SEC = 0X0009, |
| 160 |
|
| 161 |
|
| 162 |
CHUNK_TYPE_TIMESTAMP_USEC = 0X000A, |
| 163 |
|
| 164 |
|
| 165 |
CHUNK_TYPE_PROTOCOL_TYPE = 0X000B, |
| 166 |
|
| 167 |
|
| 168 |
CHUNK_TYPE_CAPTURE_AGENT_ID = 0X000C, |
| 169 |
|
| 170 |
|
| 171 |
CHUNK_TYPE_KEEP_ALIVE_TIMER = 0X000D, |
| 172 |
|
| 173 |
|
| 174 |
CHUNK_TYPE_AUTH_KEY = 0X000E, |
| 175 |
|
| 176 |
|
| 177 |
CHUNK_TYPE_PAYLOAD = 0X000F, |
| 178 |
|
| 179 |
|
| 180 |
CHUNK_TYPE_PAYLOAD_ZIP = 0X0010, |
| 181 |
|
| 182 |
|
| 183 |
CHUNK_TYPE_UUID = 0X0011, |
| 184 |
|
| 185 |
|
| 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 |
|
| 211 |
|
| 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 |
|
| 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 |
|
| 265 |
struct hepv3_global_config { |
| 266 |
unsigned int enabled; |
| 267 |
unsigned int capture_id; |
| 268 |
enum hep_uuid_type uuid_type; |
| 269 |
AST_DECLARE_STRING_FIELDS( |
| 270 |
AST_STRING_FIELD(capture_address); |
| 271 |
AST_STRING_FIELD(capture_password); |
| 272 |
AST_STRING_FIELD(capture_name); |
| 273 |
); |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
struct module_config { |
| 278 |
struct hepv3_global_config *general; |
| 279 |
}; |
| 280 |
|
| 281 |
|
| 282 |
struct hepv3_runtime_data { |
| 283 |
struct ast_sockaddr remote_addr; |
| 284 |
int sockfd; |
| 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 |
|
| 303 |
static AO2_GLOBAL_OBJ_STATIC(global_config); |
| 304 |
|
| 305 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 476 |
info->protocol_id = IPPROTO_UDP; |
| 477 |
|
| 478 |
return info; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 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 |
|
| 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 |
|
| 557 |
sock_buffer = ast_malloc(packet_len); |
| 558 |
if (!sock_buffer) { |
| 559 |
return -1; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
memcpy(sock_buffer, &hg_pkt, sizeof(hg_pkt)); |
| 564 |
sock_buffer_len = sizeof(hg_pkt); |
| 565 |
|
| 566 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 643 |
|
| 644 |
|
| 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 |
|
| 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 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
); |