| 480 | } |
| 481 | |
| 482 | void setup_ctrl_socket() |
| 483 | { |
| 484 | int port, firstport; |
| 485 | int try_counter = 60; |
| 486 | struct sockaddr_storage ctl_sa; |
| 487 | |
| 488 | int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 489 | if (sock == -1) { |
| 490 | ERROR_NO("Unable to create remote control socket!"); |
| 491 | } |
| 492 | |
| 493 | if (control_port) { |
| 494 | port = control_port; |
| 495 | /* If the user specified the control port, then we must assume they know |
| 496 | * what they want, and should not cycle. */ |
| 497 | try_counter = 1; |
| 498 | } else { |
| 499 | /* Allow 60 control sockets on the same system */ |
| 500 | /* (several SIPp instances) */ |
| 501 | port = DEFAULT_CTRL_SOCKET_PORT; |
| 502 | } |
| 503 | firstport = port; |
| 504 | |
| 505 | memset(&ctl_sa, 0, sizeof(struct sockaddr_storage)); |
| 506 | if (control_ip[0]) { |
| 507 | if (gai_getsockaddr(&ctl_sa, control_ip, nullptr, |
| 508 | AI_PASSIVE, AF_UNSPEC) != 0) { |
| 509 | ERROR("Unknown control address '%s'.\n" |
| 510 | "Use 'sipp -h' for details", control_ip); |
| 511 | } |
| 512 | } else { |
| 513 | ((struct sockaddr_in *)&ctl_sa)->sin_family = AF_INET; |
| 514 | ((struct sockaddr_in *)&ctl_sa)->sin_addr.s_addr = INADDR_ANY; |
| 515 | } |
| 516 | |
| 517 | while (try_counter) { |
| 518 | ((struct sockaddr_in *)&ctl_sa)->sin_port = htons(port); |
| 519 | if (!::bind(sock, (struct sockaddr *)&ctl_sa, sizeof(struct sockaddr_in))) { |
| 520 | /* Bind successful */ |
| 521 | break; |
| 522 | } |
| 523 | try_counter--; |
| 524 | port++; |
| 525 | } |
| 526 | |
| 527 | if (try_counter == 0) { |
| 528 | if (control_port) { |
| 529 | ERROR_NO("Unable to bind remote control socket to UDP port %d", |
| 530 | control_port); |
| 531 | } else { |
| 532 | WARNING("Unable to bind remote control socket (tried UDP ports %d-%d): %s", |
| 533 | firstport, port - 1, strerror(errno)); |
| 534 | } |
| 535 | close(sock); |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | ctrl_socket = new SIPpSocket(0, T_UDP, sock, 0); |
no test coverage detected