Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply option local port #222

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions tests/dtls-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,11 @@ usage( const char *program, const char *version) {
#endif /* DTLS_PSK */
"\t-o file\t\toutput received data to this file\n"
"\t \t\t(use '-' for STDOUT)\n"
"\t-p port\t\tlisten on specified port (default is %d)\n"
"\t-p port\t\tlisten on specified port\n"
"\t \t\t(default is an ephemeral free port).\n"
"\t-r\t\tforce renegotiation info (RFC5746)\n"
"\t-v num\t\tverbosity level (default: 3)\n",
"\t-v num\t\tverbosity level (default: 3)\n"
"\tDefault destination port: %d\n",
DEFAULT_PORT);
}

Expand Down Expand Up @@ -412,21 +414,22 @@ main(int argc, char **argv) {
fd_set rfds, wfds;
struct timeval timeout;
unsigned short dst_port = 0;
unsigned short port = DEFAULT_PORT;
char port_str[NI_MAXSERV] = "0";
unsigned short local_port = 0;
log_t log_level = DTLS_LOG_WARN;
int fd;
ssize_t result;
int on = 1;
int opt, res;
session_t dst;
session_t listen;
char buf[200];
size_t len = 0;
int buf_ready = 0;

memset(&dst, 0, sizeof(session_t));
memset(&listen, 0, sizeof(session_t));

dtls_init();
snprintf(port_str, sizeof(port_str), "%d", port);

#ifdef DTLS_PSK
psk_id_length = strlen(PSK_DEFAULT_IDENTITY);
Expand Down Expand Up @@ -475,8 +478,7 @@ main(int argc, char **argv) {
}
break;
case 'p' :
strncpy(port_str, optarg, NI_MAXSERV-1);
port_str[NI_MAXSERV - 1] = '\0';
local_port = atoi(optarg);
break;
case 'r' :
force_renegotiation_info = 1;
Expand Down Expand Up @@ -561,6 +563,24 @@ main(int argc, char **argv) {
}
}

if (local_port) {
listen.addr = dst.addr;
listen.size = dst.size;
if (listen.addr.sa.sa_family == AF_INET6) {
listen.addr.sin6.sin6_addr = in6addr_any;
listen.addr.sin6.sin6_port = htons(local_port);
dtls_info("bind to local IPv6, port %u\n", local_port);
} else {
listen.addr.sin.sin_addr.s_addr = INADDR_ANY;
listen.addr.sin.sin_port = htons(local_port);
dtls_info("bind to local IPv4, port %u\n", local_port);
}
if (bind(fd, (struct sockaddr *)&listen.addr.sa, listen.size) < 0) {
dtls_alert("bind: %s\n", strerror(errno));
return EXIT_FAILURE;
}
}

if (signal(SIGINT, dtls_handle_signal) == SIG_ERR) {
dtls_alert("An error occurred while setting a signal handler.\n");
return EXIT_FAILURE;
Expand Down
Loading