Skip to content

Commit

Permalink
Add -bind_to_device option.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Gankevich authored and wdoekes committed Mar 1, 2024
1 parent bf3afbf commit e55f871
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/sipp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ MAYBE_EXTERN const char * tls_crl_name DEFVAL(DEFAULT_TLS_CRL);
MAYBE_EXTERN double tls_version DEFVAL(0.0);
#endif

MAYBE_EXTERN const char * bind_to_device_name DEFVAL(NULL);

MAYBE_EXTERN char* scenario_file DEFVAL(NULL);
MAYBE_EXTERN char* scenario_path DEFVAL(NULL);

Expand Down
3 changes: 3 additions & 0 deletions include/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ class SIPpSocket {
// Have we read a message from this socket?
bool message_ready() { return ss_msglen > 0; };

// Bind to specific network device.
int bind_to_device(const char* device_name);

static void pollset_process(int wait);

int ss_count; /* How many users are there of this socket? */
Expand Down
1 change: 1 addition & 0 deletions src/sipp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ struct sipp_option options_table[] = {
{"i", "Set the local IP address for 'Contact:','Via:', and 'From:' headers. Default is primary host IP address.\n", SIPP_OPTION_IP, local_ip, 1},
{"p", "Set the local port number. Default is a random free port chosen by the system.", SIPP_OPTION_INT, &user_port, 1},
{"bind_local", "Bind socket to local IP address, i.e. the local IP address is used as the source IP address. If SIPp runs in server mode it will only listen on the local IP address instead of all IP addresses.", SIPP_OPTION_SETFLAG, &bind_local, 1},
{"bind_to_device", "Bind socket to the specified network device. Requires superuser permissions.", SIPP_OPTION_STRING, &bind_to_device_name, 1},
{"ci", "Set the local control IP address", SIPP_OPTION_IP, control_ip, 1},
{"cp", "Set the local control port number. Default is 8888.", SIPP_OPTION_INT, &control_port, 1},
{"max_socket", "Set the max number of sockets to open simultaneously. This option is significant if you use one socket per call. Once this limit is reached, traffic is distributed over the sockets already opened. Default value is 50000", SIPP_OPTION_MAX_SOCKET, NULL, 1},
Expand Down
13 changes: 13 additions & 0 deletions src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,14 @@ int SIPpSocket::reconnect()
return connect();
}

int SIPpSocket::bind_to_device(const char* device_name) {
if (setsockopt(this->ss_fd, SOL_SOCKET, SO_BINDTODEVICE,
device_name, strlen(device_name)) == -1) {
ERROR_NO("setsockopt(SO_BINDTODEVICE) failed");
}
return 0;
}


/*************************** I/O functions ***************************/

Expand Down Expand Up @@ -2471,6 +2479,11 @@ int open_connections()

sipp_customize_socket(main_socket);

/* Bind to the device if any. */
if (bind_to_device_name) {
main_socket->bind_to_device(bind_to_device_name);
}

/* Trying to bind local port */
char peripaddr[256];
if (!user_port) {
Expand Down

0 comments on commit e55f871

Please sign in to comment.