Skip to content

Commit 11509fd

Browse files
committed
dtls threaded examples: fix mac warnings, small cleanup.
1 parent fae5acd commit 11509fd

2 files changed

Lines changed: 35 additions & 20 deletions

File tree

dtls/client-dtls-threaded.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ typedef struct {
4747
WOLFSSL_CTX * ctx;
4848
} thread_args_t;
4949

50-
static void safer_shutdown(thread_args_t * args);
51-
static void * client_work(void * arg);
50+
static void safer_shutdown(thread_args_t * args);
51+
static void * client_work(void * arg);
5252

5353
int
5454
main(int argc,
@@ -65,13 +65,20 @@ main(int argc,
6565
memset(threads, 0, sizeof(threads));
6666
memset(args, 0, sizeof(args));
6767

68-
while ((opt = getopt(argc, argv, "vt:n::?")) != -1) {
68+
while ((opt = getopt(argc, argv, "t:?")) != -1) {
6969
switch (opt) {
7070
case 't':
7171
n_threads = atoi(optarg);
7272
break;
7373

7474
case '?':
75+
printf("usage:\n");
76+
printf(" ./client-dtls-threaded [-t n]\n");
77+
printf("\n");
78+
printf("description:\n");
79+
printf(" A simple dtls client with configurable threadpool.\n");
80+
printf(" Num allowed threads is: 1 <= n <= %d\n",
81+
DTLS_NUMTHREADS);
7582
default:
7683
return EXIT_FAILURE;
7784
}
@@ -103,7 +110,7 @@ main(int argc,
103110
ret = pthread_create(&threads[i], NULL, client_work, &args[i]);
104111

105112
if (ret == 0 ) {
106-
printf("info: spawned thread: %ld\n", threads[i]);
113+
printf("info: spawned thread: %ld\n", (long)threads[i]);
107114
}
108115
else {
109116
printf("error: pthread_create returned %d\n", ret);
@@ -115,7 +122,7 @@ main(int argc,
115122
for (size_t i = 0; i < n_threads; ++i) {
116123
if (threads[i]) {
117124
pthread_join(threads[i], NULL);
118-
printf("info: joined thread: %ld\n", threads[i]);
125+
printf("info: joined thread: %ld\n", (long)threads[i]);
119126
threads[i] = 0;
120127
}
121128
}
@@ -168,12 +175,14 @@ client_work(void * args)
168175
return NULL;
169176
}
170177

178+
printf("info: opened socket: %d\n", thread_args->activefd);
179+
171180
/* Set the file descriptor for ssl and connect with ssl variable */
172181
wolfSSL_set_fd(thread_args->ssl, thread_args->activefd);
173182
if (wolfSSL_connect(thread_args->ssl) != SSL_SUCCESS) {
174183
err1 = wolfSSL_get_error(thread_args->ssl, 0);
175184
printf("error: thread %ld: wolfSSL_connect returned = %d, %s\n",
176-
pthread_self(), err1,
185+
(long)pthread_self(), err1,
177186
wolfSSL_ERR_reason_error_string(err1));
178187
return NULL;
179188
}
@@ -185,9 +194,12 @@ client_work(void * args)
185194
const char * tid_str = NULL; /* Str to thread id in response. */
186195

187196
for (size_t i = 0; i < 4; ++i) {
197+
memset(send_msg, '\0', sizeof(send_msg));
198+
memset(recv_msg, '\0', sizeof(recv_msg));
199+
188200
char seq = '0' + (int) i;
189201
sprintf(send_msg, "msg %zu from client thread %ld\n", i,
190-
pthread_self());
202+
(long)pthread_self());
191203

192204
n_bytes = wolfSSL_write(thread_args->ssl, send_msg, strlen(send_msg));
193205

@@ -210,7 +222,7 @@ client_work(void * args)
210222
/* Check the server replied with the correct sequence record, e.g.:
211223
* "msg 2 from server thread 140146322425536" */
212224
if (recv_msg[4] != seq) {
213-
printf("error: got msg %c, expected %c\n", recv_msg[5], seq);
225+
printf("error: got msg %c, expected %c\n", recv_msg[4], seq);
214226
break;
215227
}
216228

@@ -223,12 +235,12 @@ client_work(void * args)
223235
/* Compare saved thread id. */
224236
if (server_tid != atol(tid_str)) {
225237
printf("error: got rsp from server thread %ld, expected %ld\n",
226-
server_tid, atol(tid_str));
238+
server_tid, atol(tid_str));
227239
break;
228240
}
229241
else {
230-
printf("info: got response from server thread %ld\n",
231-
server_tid);
242+
printf("info: got response from server thread %ld\n",
243+
server_tid);
232244
}
233245
}
234246

@@ -265,5 +277,3 @@ safer_shutdown(thread_args_t * args)
265277

266278
return;
267279
}
268-
269-

dtls/server-dtls-threaded.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ main(int argc,
8787
memset(threads, 0, sizeof(threads));
8888
memset(args, 0, sizeof(args));
8989

90-
while ((opt = getopt(argc, argv, "vt:n::?")) != -1) {
90+
while ((opt = getopt(argc, argv, "t:?")) != -1) {
9191
switch (opt) {
9292
case 't':
9393
n_threads = atoi(optarg);
@@ -96,6 +96,11 @@ main(int argc,
9696
case '?':
9797
printf("usage:\n");
9898
printf(" ./server-dtls-threaded [-t n]\n");
99+
printf("\n");
100+
printf("description:\n");
101+
printf(" A simple dtls server with configurable threadpool.\n");
102+
printf(" Num allowed threads is: 1 <= n <= %d\n",
103+
DTLS_NUMTHREADS);
99104
default:
100105
return EXIT_FAILURE;
101106
}
@@ -223,7 +228,7 @@ main(int argc,
223228
ret = pthread_create(&threads[i], NULL, server_work, &args[i]);
224229

225230
if (ret == 0 ) {
226-
printf("info: spawned thread: %ld\n", threads[i]);
231+
printf("info: spawned thread: %ld\n", (long)threads[i]);
227232
}
228233
else {
229234
printf("error: pthread_create returned %d\n", ret);
@@ -238,7 +243,7 @@ main(int argc,
238243
for (size_t i = 0; i < n_threads; ++i) {
239244
if (threads[i]) {
240245
pthread_join(threads[i], NULL);
241-
printf("info: joined thread: %ld\n", threads[i]);
246+
printf("info: joined thread: %ld\n", (long)threads[i]);
242247
threads[i] = 0;
243248
}
244249
}
@@ -321,7 +326,7 @@ server_work(void * args)
321326
}
322327

323328
sprintf(send_msg, "msg %zu from server thread %ld\n", i,
324-
pthread_self());
329+
(long)pthread_self());
325330

326331
n_bytes = wolfSSL_read(thread_args->ssl, recv_msg, sizeof(recv_msg) - 1);
327332

@@ -360,7 +365,7 @@ server_work(void * args)
360365
}
361366

362367
safer_shutdown(thread_args);
363-
printf("info: exiting thread %ld\n", pthread_self());
368+
printf("info: exiting thread %ld\n", (long)pthread_self());
364369
pthread_exit(NULL);
365370
}
366371

@@ -409,15 +414,15 @@ cleanup_threadpool(pthread_t * threads,
409414
for (size_t i = 0; i < n_threads; ++i) {
410415
if (threads[i]) {
411416
pthread_tryjoin_np(threads[i], NULL);
412-
printf("info: joined thread: %ld\n", threads[i]);
417+
printf("info: joined thread: %ld\n", (long)threads[i]);
413418
threads[i] = 0;
414419
}
415420
}
416421
#else
417422
for (size_t i = 0; i < n_threads; ++i) {
418423
if (threads[i] && args[i].done == 1) {
419424
pthread_join(threads[i], NULL);
420-
printf("info: joined thread: %ld\n", threads[i]);
425+
printf("info: joined thread: %ld\n", (long)threads[i]);
421426
threads[i] = 0;
422427
}
423428
}

0 commit comments

Comments
 (0)