Skip to content

Commit 8d34464

Browse files
committed
dtls-client.c: accept options after arguments.
Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
1 parent 4a6a782 commit 8d34464

1 file changed

Lines changed: 36 additions & 17 deletions

File tree

tests/dtls-client.c

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ usage( const char *program, const char *version) {
359359
program = ++p;
360360

361361
fprintf(stderr, "%s v%s -- DTLS client implementation\n"
362-
"(c) 2011-2014 Olaf Bergmann <bergmann@tzi.org>\n\n"
362+
"(c) 2011-2024 Olaf Bergmann <bergmann@tzi.org>\n\n"
363363
#ifdef DTLS_PSK
364364
"usage: %s [-c cipher suites] [-e] [-i file] [-k file] [-o file]\n"
365365
" %*s [-p port] [-r] [-v num] addr [port]\n",
@@ -411,6 +411,7 @@ int
411411
main(int argc, char **argv) {
412412
fd_set rfds, wfds;
413413
struct timeval timeout;
414+
unsigned short dst_port = 0;
414415
unsigned short port = DEFAULT_PORT;
415416
char port_str[NI_MAXSERV] = "0";
416417
log_t log_level = DTLS_LOG_WARN;
@@ -423,7 +424,7 @@ main(int argc, char **argv) {
423424
size_t len = 0;
424425
int buf_ready = 0;
425426

426-
427+
memset(&dst, 0, sizeof(session_t));
427428
dtls_init();
428429
snprintf(port_str, sizeof(port_str), "%d", port);
429430

@@ -434,7 +435,8 @@ main(int argc, char **argv) {
434435
memcpy(psk_key, PSK_DEFAULT_KEY, psk_key_length);
435436
#endif /* DTLS_PSK */
436437

437-
while ((opt = getopt(argc, argv, "c:eo:p:rv:" PSK_OPTIONS)) != -1) {
438+
while (optind < argc) {
439+
opt = getopt(argc, argv, "c:eo:p:rv:z" PSK_OPTIONS);
438440
switch (opt) {
439441
#ifdef DTLS_PSK
440442
case 'i' :
@@ -482,31 +484,48 @@ main(int argc, char **argv) {
482484
case 'v' :
483485
log_level = strtol(optarg, NULL, 10);
484486
break;
487+
case -1 :
488+
/* handle arguments */
489+
if (!dst.size) {
490+
/* first argument: destination address */
491+
/* resolve destination address where server should be sent */
492+
res = resolve_address(argv[optind++], &dst.addr.sa);
493+
if (res < 0) {
494+
dtls_emerg("failed to resolve address\n");
495+
exit(-1);
496+
}
497+
dst.size = res;
498+
} else if (!dst_port) {
499+
/* second argument: destination port (optional) */
500+
dst_port = atoi(argv[optind++]);
501+
} else {
502+
dtls_warn("too many arguments!\n");
503+
usage(argv[0], dtls_package_version());
504+
exit(1);
505+
}
506+
break;
485507
default:
486508
usage(argv[0], dtls_package_version());
487509
exit(1);
488510
}
489511
}
490512

491-
dtls_set_log_level(log_level);
492-
493-
if (argc <= optind) {
513+
if (!dst.size) {
514+
dtls_warn("missing destination address!\n");
494515
usage(argv[0], dtls_package_version());
495516
exit(1);
496517
}
497-
498-
memset(&dst, 0, sizeof(session_t));
499-
/* resolve destination address where server should be sent */
500-
res = resolve_address(argv[optind++], &dst.addr.sa);
501-
if (res < 0) {
502-
dtls_emerg("failed to resolve address\n");
503-
exit(-1);
518+
if (!dst_port) {
519+
/* destination port not provided, use default */
520+
dst_port = DEFAULT_PORT;
521+
}
522+
if (dst.addr.sa.sa_family == AF_INET6) {
523+
dst.addr.sin6.sin6_port = htons(dst_port);
524+
} else {
525+
dst.addr.sin.sin_port = htons(dst_port);
504526
}
505-
dst.size = res;
506527

507-
/* use port number from command line when specified or the listen
508-
port, otherwise */
509-
dst.addr.sin.sin_port = htons(atoi(optind < argc ? argv[optind++] : port_str));
528+
dtls_set_log_level(log_level);
510529

511530
/* init socket and set it to non-blocking */
512531
fd = socket(dst.addr.sa.sa_family, SOCK_DGRAM, 0);

0 commit comments

Comments
 (0)