| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/module.h" |
| 33 |
#include "asterisk/sched.h" |
| 34 |
#include "asterisk/config.h" |
| 35 |
#include "asterisk/stun.h" |
| 36 |
#include "asterisk/netsock2.h" |
| 37 |
#include "asterisk/lock.h" |
| 38 |
#include "asterisk/acl.h" |
| 39 |
#include "asterisk/cli.h" |
| 40 |
#include "asterisk/json.h" |
| 41 |
#include "asterisk/stasis.h" |
| 42 |
#include "asterisk/stasis_system.h" |
| 43 |
#include "asterisk/astobj2.h" |
| 44 |
|
| 45 |
#include <fcntl.h> |
| 46 |
|
| 47 |
#define DEFAULT_MONITOR_REFRESH 30 |
| 48 |
#define DEFAULT_RETRIES 3 |
| 49 |
|
| 50 |
|
| 51 |
static const char stun_conf_file[] = "res_stun_monitor.conf"; |
| 52 |
static struct ast_sched_context *sched; |
| 53 |
|
| 54 |
static struct { |
| 55 |
|
| 56 |
ast_mutex_t lock; |
| 57 |
|
| 58 |
struct sockaddr_in external_addr; |
| 59 |
|
| 60 |
const char *server_hostname; |
| 61 |
|
| 62 |
unsigned int stun_port; |
| 63 |
|
| 64 |
unsigned int refresh; |
| 65 |
|
| 66 |
int stun_sock; |
| 67 |
|
| 68 |
unsigned int monitor_enabled:1; |
| 69 |
|
| 70 |
unsigned int external_addr_known:1; |
| 71 |
|
| 72 |
unsigned int stun_poll_failed_gripe:1; |
| 73 |
} args; |
| 74 |
|
| 75 |
static void stun_close_sock(void) |
| 76 |
{ |
| 77 |
if (0 <= args.stun_sock) { |
| 78 |
close(args.stun_sock); |
| 79 |
args.stun_sock = -1; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
static int stun_monitor_request(const void *blarg) |
| 85 |
{ |
| 86 |
int res; |
| 87 |
struct sockaddr_in answer; |
| 88 |
static const struct sockaddr_in no_addr = { 0, }; |
| 89 |
|
| 90 |
ast_mutex_lock(&args.lock); |
| 91 |
if (!args.monitor_enabled) { |
| 92 |
goto monitor_request_cleanup; |
| 93 |
} |
| 94 |
|
| 95 |
if (args.stun_sock < 0) { |
| 96 |
struct ast_sockaddr stun_addr; |
| 97 |
|
| 98 |
|
| 99 |
if (!args.server_hostname) { |
| 100 |
|
| 101 |
goto monitor_request_cleanup; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
memset(&stun_addr, 0, sizeof(stun_addr)); |
| 106 |
stun_addr.ss.ss_family = AF_INET; |
| 107 |
if (ast_get_ip(&stun_addr, args.server_hostname)) { |
| 108 |
|
| 109 |
ast_log(LOG_WARNING, "Unable to lookup STUN server '%s'\n", |
| 110 |
args.server_hostname); |
| 111 |
goto monitor_request_cleanup; |
| 112 |
} |
| 113 |
ast_sockaddr_set_port(&stun_addr, args.stun_port); |
| 114 |
|
| 115 |
|
| 116 |
args.stun_sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 117 |
if (args.stun_sock < 0) { |
| 118 |
ast_log(LOG_WARNING, "Unable to create STUN socket: %s\n", strerror(errno)); |
| 119 |
goto monitor_request_cleanup; |
| 120 |
} |
| 121 |
if (ast_connect(args.stun_sock, &stun_addr)) { |
| 122 |
ast_log(LOG_WARNING, "STUN Failed to connect to %s: %s\n", |
| 123 |
ast_sockaddr_stringify(&stun_addr), strerror(errno)); |
| 124 |
stun_close_sock(); |
| 125 |
goto monitor_request_cleanup; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
res = ast_stun_request(args.stun_sock, NULL, NULL, &answer); |
| 130 |
if (res) { |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
if (!args.stun_poll_failed_gripe) { |
| 137 |
args.stun_poll_failed_gripe = 1; |
| 138 |
ast_log(LOG_WARNING, "STUN poll %s. Re-evaluating STUN server address.\n", |
| 139 |
res < 0 ? "failed" : "got no response"); |
| 140 |
} |
| 141 |
stun_close_sock(); |
| 142 |
} else { |
| 143 |
args.stun_poll_failed_gripe = 0; |
| 144 |
if (memcmp(&no_addr, &answer, sizeof(no_addr)) |
| 145 |
&& memcmp(&args.external_addr, &answer, sizeof(args.external_addr))) { |
| 146 |
const char *newaddr = ast_strdupa(ast_inet_ntoa(answer.sin_addr)); |
| 147 |
int newport = ntohs(answer.sin_port); |
| 148 |
|
| 149 |
ast_log(LOG_NOTICE, "Old external address/port %s:%d now seen as %s:%d.\n", |
| 150 |
ast_inet_ntoa(args.external_addr.sin_addr), |
| 151 |
ntohs(args.external_addr.sin_port), newaddr, newport); |
| 152 |
|
| 153 |
args.external_addr = answer; |
| 154 |
|
| 155 |
if (args.external_addr_known) { |
| 156 |
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); |
| 157 |
RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup); |
| 158 |
RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref); |
| 159 |
|
| 160 |
if (!ast_network_change_type()) { |
| 161 |
goto publish_failure; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
json_object = ast_json_object_create(); |
| 167 |
if (!json_object) { |
| 168 |
goto publish_failure; |
| 169 |
} |
| 170 |
|
| 171 |
if (!(json_payload = ast_json_payload_create(json_object))) { |
| 172 |
goto publish_failure; |
| 173 |
} |
| 174 |
|
| 175 |
msg = stasis_message_create(ast_network_change_type(), json_payload); |
| 176 |
|
| 177 |
publish_failure: |
| 178 |
if (msg) { |
| 179 |
stasis_publish(ast_system_topic(), msg); |
| 180 |
} else { |
| 181 |
ast_log(LOG_ERROR, "Failed to issue network change message.\n"); |
| 182 |
} |
| 183 |
} else { |
| 184 |
|
| 185 |
|
| 186 |
args.external_addr_known = 1; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
monitor_request_cleanup: |
| 192 |
|
| 193 |
|
| 194 |
res = args.refresh * 1000; |
| 195 |
ast_mutex_unlock(&args.lock); |
| 196 |
|
| 197 |
return res; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
static void stun_stop_monitor(void) |
| 207 |
{ |
| 208 |
ast_mutex_lock(&args.lock); |
| 209 |
args.monitor_enabled = 0; |
| 210 |
ast_free((char *) args.server_hostname); |
| 211 |
args.server_hostname = NULL; |
| 212 |
stun_close_sock(); |
| 213 |
ast_mutex_unlock(&args.lock); |
| 214 |
|
| 215 |
if (sched) { |
| 216 |
ast_sched_context_destroy(sched); |
| 217 |
sched = NULL; |
| 218 |
ast_log(LOG_NOTICE, "STUN monitor stopped\n"); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
static int stun_start_monitor(void) |
| 229 |
{ |
| 230 |
|
| 231 |
if (sched) { |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
if (!(sched = ast_sched_context_create())) { |
| 236 |
ast_log(LOG_ERROR, "Failed to create stun monitor scheduler context\n"); |
| 237 |
return -1; |
| 238 |
} |
| 239 |
|
| 240 |
if (ast_sched_start_thread(sched)) { |
| 241 |
ast_sched_context_destroy(sched); |
| 242 |
sched = NULL; |
| 243 |
stun_close_sock(); |
| 244 |
return -1; |
| 245 |
} |
| 246 |
|
| 247 |
if (ast_sched_add_variable(sched, (args.refresh * 1000), stun_monitor_request, NULL, 1) < 0) { |
| 248 |
ast_log(LOG_ERROR, "Unable to schedule STUN network monitor \n"); |
| 249 |
ast_sched_context_destroy(sched); |
| 250 |
sched = NULL; |
| 251 |
return -1; |
| 252 |
} |
| 253 |
|
| 254 |
ast_log(LOG_NOTICE, "STUN monitor started\n"); |
| 255 |
|
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
static int setup_stunaddr(const char *value, int reload) |
| 270 |
{ |
| 271 |
char *val; |
| 272 |
char *host_str; |
| 273 |
char *port_str; |
| 274 |
unsigned int port; |
| 275 |
struct ast_sockaddr stun_addr; |
| 276 |
|
| 277 |
if (ast_strlen_zero(value)) { |
| 278 |
|
| 279 |
args.monitor_enabled = 0; |
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
val = ast_strdupa(value); |
| 284 |
if (!ast_sockaddr_split_hostport(val, &host_str, &port_str, 0) |
| 285 |
|| ast_strlen_zero(host_str)) { |
| 286 |
return -1; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
if (ast_strlen_zero(port_str) |
| 291 |
|| 1 != sscanf(port_str, "%30u", &port)) { |
| 292 |
port = STANDARD_STUN_PORT; |
| 293 |
} |
| 294 |
|
| 295 |
host_str = ast_strdup(host_str); |
| 296 |
if (!host_str) { |
| 297 |
return -1; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
memset(&stun_addr, 0, sizeof(stun_addr)); |
| 302 |
stun_addr.ss.ss_family = AF_INET; |
| 303 |
if (ast_get_ip(&stun_addr, host_str)) { |
| 304 |
ast_log(LOG_WARNING, "Unable to lookup STUN server '%s'\n", host_str); |
| 305 |
|
| 306 |
|
| 307 |
if (reload) { |
| 308 |
ast_free(host_str); |
| 309 |
return -1; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
ast_free((char *) args.server_hostname); |
| 315 |
args.server_hostname = host_str; |
| 316 |
args.stun_port = port; |
| 317 |
|
| 318 |
|
| 319 |
args.monitor_enabled = 1; |
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
static int load_config(int startup) |
| 324 |
{ |
| 325 |
struct ast_flags config_flags = { 0, }; |
| 326 |
struct ast_config *cfg; |
| 327 |
struct ast_variable *v; |
| 328 |
|
| 329 |
if (!startup) { |
| 330 |
ast_set_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED); |
| 331 |
} |
| 332 |
|
| 333 |
cfg = ast_config_load2(stun_conf_file, "res_stun_monitor", config_flags); |
| 334 |
if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { |
| 335 |
ast_log(LOG_WARNING, "Unable to load config %s\n", stun_conf_file); |
| 336 |
return -1; |
| 337 |
} |
| 338 |
if (cfg == CONFIG_STATUS_FILEUNCHANGED) { |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
stun_close_sock(); |
| 344 |
args.stun_poll_failed_gripe = 0; |
| 345 |
|
| 346 |
|
| 347 |
args.monitor_enabled = 0; |
| 348 |
args.refresh = DEFAULT_MONITOR_REFRESH; |
| 349 |
|
| 350 |
for (v = ast_variable_browse(cfg, "general"); v; v = v->next) { |
| 351 |
if (!strcasecmp(v->name, "stunaddr")) { |
| 352 |
if (setup_stunaddr(v->value, !startup)) { |
| 353 |
ast_log(LOG_WARNING, "Invalid STUN server address: %s at line %d\n", |
| 354 |
v->value, v->lineno); |
| 355 |
} |
| 356 |
} else if (!strcasecmp(v->name, "stunrefresh")) { |
| 357 |
if ((sscanf(v->value, "%30u", &args.refresh) != 1) || !args.refresh) { |
| 358 |
ast_log(LOG_WARNING, "Invalid stunrefresh value '%s', must be an integer > 0 at line %d\n", v->value, v->lineno); |
| 359 |
args.refresh = DEFAULT_MONITOR_REFRESH; |
| 360 |
} |
| 361 |
} else { |
| 362 |
ast_log(LOG_WARNING, "Invalid config option %s at line %d\n", |
| 363 |
v->value, v->lineno); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
ast_config_destroy(cfg); |
| 368 |
|
| 369 |
return 0; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
static void _stun_show_status(int fd) |
| 374 |
{ |
| 375 |
const char *status; |
| 376 |
|
| 377 |
#define DATALN "%-25s %-5u %-7u %-8d %-7s %-16s %-d\n" |
| 378 |
#define HEADER "%-25s %-5s %-7s %-8s %-7s %-16s %-s\n" |
| 379 |
|
| 380 |
|
| 381 |
ast_cli(fd, HEADER, "Hostname", "Port", "Period", "Retries", "Status", "ExternAddr", "ExternPort"); |
| 382 |
|
| 383 |
if (args.stun_poll_failed_gripe) { |
| 384 |
status = "FAIL"; |
| 385 |
} else if (args.external_addr_known) { |
| 386 |
status = "OK"; |
| 387 |
} else { |
| 388 |
status = "INIT"; |
| 389 |
} |
| 390 |
ast_cli( fd, DATALN, |
| 391 |
args.server_hostname, |
| 392 |
args.stun_port, |
| 393 |
args.refresh, |
| 394 |
DEFAULT_RETRIES, |
| 395 |
status, |
| 396 |
ast_inet_ntoa(args.external_addr.sin_addr), |
| 397 |
ntohs(args.external_addr.sin_port) |
| 398 |
); |
| 399 |
|
| 400 |
#undef HEADER |
| 401 |
#undef DATALN |
| 402 |
} |
| 403 |
|
| 404 |
static char *handle_cli_stun_show_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 405 |
{ |
| 406 |
switch (cmd) { |
| 407 |
case CLI_INIT: |
| 408 |
e->command = "stun show status"; |
| 409 |
e->usage = |
| 410 |
"Usage: stun show status\n" |
| 411 |
" List all known STUN servers and statuses.\n"; |
| 412 |
return NULL; |
| 413 |
case CLI_GENERATE: |
| 414 |
return NULL; |
| 415 |
} |
| 416 |
|
| 417 |
if (a->argc != 3) { |
| 418 |
return CLI_SHOWUSAGE; |
| 419 |
} |
| 420 |
|
| 421 |
_stun_show_status(a->fd); |
| 422 |
return CLI_SUCCESS; |
| 423 |
} |
| 424 |
|
| 425 |
static struct ast_cli_entry cli_stun[] = { |
| 426 |
AST_CLI_DEFINE(handle_cli_stun_show_status, "Show STUN servers and statuses"), |
| 427 |
}; |
| 428 |
|
| 429 |
static int __reload(int startup) |
| 430 |
{ |
| 431 |
int res; |
| 432 |
|
| 433 |
ast_mutex_lock(&args.lock); |
| 434 |
if (!(res = load_config(startup)) && args.monitor_enabled) { |
| 435 |
res = stun_start_monitor(); |
| 436 |
} |
| 437 |
ast_mutex_unlock(&args.lock); |
| 438 |
|
| 439 |
if (res < 0 || !args.monitor_enabled) { |
| 440 |
stun_stop_monitor(); |
| 441 |
} |
| 442 |
|
| 443 |
return res; |
| 444 |
} |
| 445 |
|
| 446 |
static int reload(void) |
| 447 |
{ |
| 448 |
return __reload(0); |
| 449 |
} |
| 450 |
|
| 451 |
static int unload_module(void) |
| 452 |
{ |
| 453 |
stun_stop_monitor(); |
| 454 |
ast_mutex_destroy(&args.lock); |
| 455 |
|
| 456 |
|
| 457 |
ast_cli_unregister_multiple(cli_stun, ARRAY_LEN(cli_stun)); |
| 458 |
|
| 459 |
return 0; |
| 460 |
} |
| 461 |
|
| 462 |
static int load_module(void) |
| 463 |
{ |
| 464 |
ast_mutex_init(&args.lock); |
| 465 |
args.stun_sock = -1; |
| 466 |
if (__reload(1)) { |
| 467 |
ast_mutex_destroy(&args.lock); |
| 468 |
return AST_MODULE_LOAD_DECLINE; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
ast_cli_register_multiple(cli_stun, sizeof(cli_stun) / sizeof(struct ast_cli_entry)); |
| 473 |
|
| 474 |
return AST_MODULE_LOAD_SUCCESS; |
| 475 |
} |
| 476 |
|
| 477 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "STUN Network Monitor", |
| 478 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 479 |
.load = load_module, |
| 480 |
.unload = unload_module, |
| 481 |
.reload = reload, |
| 482 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND |
| 483 |
); |