Skip to content

Commit 1f1bc2a

Browse files
committed
dtls-client.c: apply option for local port.
The previous version ignores the option for the local port. That may be caused by issues using the same default local port for the server and client. This enables the use of an specific local port and changes the default to an ephemeral free port, similar to quite a lot of other UDP clients. The DEFAULT_PORT is therefore only used for the destination. Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
1 parent c063d72 commit 1f1bc2a

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

tests/dtls-client.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,11 @@ usage( const char *program, const char *version) {
376376
#endif /* DTLS_PSK */
377377
"\t-o file\t\toutput received data to this file\n"
378378
"\t \t\t(use '-' for STDOUT)\n"
379-
"\t-p port\t\tlisten on specified port (default is %d)\n"
379+
"\t-p port\t\tlisten on specified port\n"
380+
"\t \t\t(default is an ephemeral free port).\n"
380381
"\t-r\t\tforce renegotiation info (RFC5746)\n"
381-
"\t-v num\t\tverbosity level (default: 3)\n",
382+
"\t-v num\t\tverbosity level (default: 3)\n"
383+
"\tDefault destination port: %d\n",
382384
DEFAULT_PORT);
383385
}
384386

@@ -412,21 +414,22 @@ main(int argc, char **argv) {
412414
fd_set rfds, wfds;
413415
struct timeval timeout;
414416
unsigned short dst_port = 0;
415-
unsigned short port = DEFAULT_PORT;
416-
char port_str[NI_MAXSERV] = "0";
417+
unsigned short local_port = 0;
417418
log_t log_level = DTLS_LOG_WARN;
418419
int fd;
419420
ssize_t result;
420421
int on = 1;
421422
int opt, res;
422423
session_t dst;
424+
session_t listen;
423425
char buf[200];
424426
size_t len = 0;
425427
int buf_ready = 0;
426428

427429
memset(&dst, 0, sizeof(session_t));
430+
memset(&listen, 0, sizeof(session_t));
431+
428432
dtls_init();
429-
snprintf(port_str, sizeof(port_str), "%d", port);
430433

431434
#ifdef DTLS_PSK
432435
psk_id_length = strlen(PSK_DEFAULT_IDENTITY);
@@ -475,8 +478,7 @@ main(int argc, char **argv) {
475478
}
476479
break;
477480
case 'p' :
478-
strncpy(port_str, optarg, NI_MAXSERV-1);
479-
port_str[NI_MAXSERV - 1] = '\0';
481+
local_port = atoi(optarg);
480482
break;
481483
case 'r' :
482484
force_renegotiation_info = 1;
@@ -561,6 +563,24 @@ main(int argc, char **argv) {
561563
}
562564
}
563565

566+
if (local_port) {
567+
listen.addr = dst.addr;
568+
listen.size = dst.size;
569+
if (listen.addr.sa.sa_family == AF_INET6) {
570+
listen.addr.sin6.sin6_addr = in6addr_any;
571+
listen.addr.sin6.sin6_port = htons(local_port);
572+
dtls_info("bind to local IPv6, port %u\n", local_port);
573+
} else {
574+
listen.addr.sin.sin_addr.s_addr = INADDR_ANY;
575+
listen.addr.sin.sin_port = htons(local_port);
576+
dtls_info("bind to local IPv4, port %u\n", local_port);
577+
}
578+
if (bind(fd, (struct sockaddr *)&listen.addr.sa, listen.size) < 0) {
579+
dtls_alert("bind: %s\n", strerror(errno));
580+
return EXIT_FAILURE;
581+
}
582+
}
583+
564584
if (signal(SIGINT, dtls_handle_signal) == SIG_ERR) {
565585
dtls_alert("An error occurred while setting a signal handler.\n");
566586
return EXIT_FAILURE;

0 commit comments

Comments
 (0)