Skip to content

Commit 3bb9656

Browse files
committed
DTLS threaded read-write with same SSL object
Add server and client DTLS example that have read and write threads that use the same SSL objects. Server also has encryption threads when wolfSSL is compiled for that.
1 parent de246ca commit 3bb9656

3 files changed

Lines changed: 678 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ android/wolfssljni-ndk-sample/proguard-project.txt
5151
/dtls/client-dtls-ipv6
5252
/dtls/client-dtls-nonblocking
5353
/dtls/client-dtls-resume
54+
/dtls/client-dtls-rw-threads
5455
/dtls/client-dtls-shared
5556
/dtls/client-dtls
5657
/dtls/client-udp
5758
/dtls/server-dtls-callback
5859
/dtls/server-dtls-ipv6
5960
/dtls/server-dtls-nonblocking
61+
/dtls/server-dtls-rw-threads
6062
/dtls/server-dtls-threaded
6163
/dtls/server-dtls
6264
/dtls/server-udp

dtls/client-dtls-rw-threads.c

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/*
2+
* client-dtls.c
3+
*
4+
* Copyright (C) 2006-2020 wolfSSL Inc.
5+
*
6+
* This file is part of wolfSSL. (formerly known as CyaSSL)
7+
*
8+
* wolfSSL is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation; either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* wolfSSL is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21+
*
22+
*=============================================================================
23+
*
24+
* Bare-bones example of a DTLS client for instructional/learning purposes.
25+
*/
26+
27+
#include <wolfssl/options.h>
28+
#include <unistd.h>
29+
#include <wolfssl/ssl.h>
30+
#include <netdb.h>
31+
#include <signal.h>
32+
#include <sys/socket.h>
33+
#include <arpa/inet.h>
34+
#include <netinet/in.h>
35+
#include <stdio.h>
36+
#include <stdlib.h>
37+
#include <string.h>
38+
#include <pthread.h>
39+
40+
#include <sys/time.h>
41+
42+
/* WARNING: This function is not portable. */
43+
static WC_INLINE double current_time(int reset)
44+
{
45+
struct timeval tv;
46+
gettimeofday(&tv, 0);
47+
(void)reset;
48+
49+
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
50+
}
51+
52+
#define SERV_PORT 11111
53+
54+
typedef struct {
55+
int size; /* Size of buffer to read. */
56+
WOLFSSL* ssl; /* SSL object to use. */
57+
} threadArgs;
58+
59+
static int cleanup; /* To handle shutdown */
60+
static int minRunTime = 1; /* Minimum run time. Default: 1 second */
61+
62+
/* Reader thread.
63+
*
64+
* Only started after handshake complete.
65+
*/
66+
void* Reader(void* openSock)
67+
{
68+
threadArgs* args = (threadArgs*)openSock;
69+
int recvLen = 0; /* length of message */
70+
int msgLen = args->size; /* the size of message */
71+
unsigned char buff[msgLen]; /* the incoming message */
72+
WOLFSSL* ssl = args->ssl;
73+
int result;
74+
fd_set recvfds, errfds;
75+
struct timeval timeout;
76+
int currTimeout;
77+
int nb_sockfd;
78+
int nfds;
79+
80+
nb_sockfd = (int) wolfSSL_get_fd(ssl);
81+
82+
printf("Reading\n");
83+
84+
while (!cleanup) {
85+
currTimeout = wolfSSL_dtls_get_current_timeout(ssl);
86+
nfds = nb_sockfd + 1;
87+
timeout = (struct timeval) { (currTimeout > 0) ? currTimeout : 0, 0};
88+
89+
/* Setup file descriptor list to monitor. */
90+
FD_ZERO(&recvfds);
91+
FD_SET(nb_sockfd, &recvfds);
92+
FD_ZERO(&errfds);
93+
FD_SET(nb_sockfd, &errfds);
94+
95+
result = select(nfds, &recvfds, NULL, &errfds, &timeout);
96+
if ((result > 0) && FD_ISSET(nb_sockfd, &errfds)) {
97+
/* Tell other thread to cleanup. */
98+
cleanup = 1;
99+
/* Finished reading. */
100+
break;
101+
}
102+
103+
/* If data waiting on receive file descriptor, read it. */
104+
if (result > 0 && FD_ISSET(nb_sockfd, &recvfds)) {
105+
/* Read application data. */
106+
if ((recvLen = wolfSSL_read(ssl, buff, msgLen-1)) < 0) {
107+
/* Handle errors. */
108+
int readErr = wolfSSL_get_error(ssl, 0);
109+
if (readErr != SSL_ERROR_WANT_READ) {
110+
printf("SSL_read failed.\n");
111+
/* Tell other threads to cleanup. */
112+
cleanup = 1;
113+
break;
114+
}
115+
}
116+
}
117+
}
118+
119+
return NULL;
120+
}
121+
122+
void* Writer(void* openSock)
123+
{
124+
threadArgs* args = (threadArgs*)openSock;
125+
char ack[] = "I hear you fashizzle!\n";
126+
WOLFSSL* ssl = args->ssl;
127+
double start = current_time(0);
128+
129+
printf("Writing\n");
130+
131+
/* Keep writing while not in cleanup. */
132+
while (!cleanup) {
133+
/* Write message and check for error. */
134+
if (wolfSSL_write(ssl, ack, sizeof(ack)) < 0) {
135+
printf("wolfSSL_write fail.\n");
136+
/* Tell other threads to cleanup. */
137+
cleanup = 1;
138+
break;
139+
}
140+
141+
/* Stop writing after a minimum run time. */
142+
if (current_time(0) > start + minRunTime) {
143+
printf("wolfSSL_write done.\n");
144+
cleanup = 1;
145+
break;
146+
}
147+
}
148+
149+
return NULL;
150+
}
151+
152+
int main (int argc, char** argv)
153+
{
154+
/* standard variables used in a dtls client*/
155+
int n = 0;
156+
int sockfd = 0;
157+
int err1;
158+
struct sockaddr_in servAddr;
159+
WOLFSSL* ssl = 0;
160+
WOLFSSL_CTX* ctx = 0;
161+
char cert_array[] = "certs/ca-cert.pem";
162+
char* certs = cert_array;
163+
threadArgs args;
164+
pthread_t threadidReader;
165+
pthread_t threadidWriter;
166+
167+
/* Program argument checking */
168+
if (argc < 2 || argc > 3) {
169+
printf("usage: udpcli <IP address> [<min time>]\n");
170+
return 1;
171+
}
172+
173+
if (argc == 3) {
174+
minRunTime = atoi(argv[2]);
175+
}
176+
177+
/* Initialize wolfSSL before assigning ctx */
178+
wolfSSL_Init();
179+
wolfSSL_Debugging_ON();
180+
181+
/* wolfSSL_Debugging_ON(); */
182+
183+
if ( (ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method())) == NULL) {
184+
fprintf(stderr, "wolfSSL_CTX_new error.\n");
185+
return 1;
186+
}
187+
188+
/* Load certificates into ctx variable */
189+
if (wolfSSL_CTX_load_verify_locations(ctx, certs, 0)
190+
!= SSL_SUCCESS) {
191+
fprintf(stderr, "Error loading %s, please check the file.\n", certs);
192+
return 1;
193+
}
194+
195+
/* Assign ssl variable */
196+
ssl = wolfSSL_new(ctx);
197+
if (ssl == NULL) {
198+
printf("unable to get ssl object");
199+
return 1;
200+
}
201+
202+
/* servAddr setup */
203+
memset(&servAddr, 0, sizeof(servAddr));
204+
servAddr.sin_family = AF_INET;
205+
servAddr.sin_port = htons(SERV_PORT);
206+
if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) < 1) {
207+
printf("Error and/or invalid IP address\n");
208+
return 1;
209+
}
210+
211+
wolfSSL_dtls_set_peer(ssl, &servAddr, sizeof(servAddr));
212+
213+
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
214+
printf("cannot create a socket.\n");
215+
return 1;
216+
}
217+
218+
/* Set the file descriptor for ssl and connect with ssl variable */
219+
wolfSSL_set_fd(ssl, sockfd);
220+
wolfSSL_dtls_set_using_nonblock(ssl, 1);
221+
fcntl(sockfd, F_SETFL, O_NONBLOCK);
222+
223+
while (wolfSSL_connect(ssl) != SSL_SUCCESS) {
224+
err1 = wolfSSL_get_error(ssl, 0);
225+
if (err1 != SSL_ERROR_WANT_READ && err1 != SSL_ERROR_WANT_WRITE) {
226+
printf("err = %d, %s\n", err1,
227+
wolfSSL_ERR_reason_error_string(err1));
228+
printf("SSL_connect failed\n");
229+
return 1;
230+
}
231+
}
232+
233+
cleanup = 0;
234+
args.size = 256;
235+
args.ssl = ssl;
236+
237+
fcntl(wolfSSL_get_fd(ssl), F_SETFL, O_NONBLOCK);
238+
239+
/* Create reader and writer threads. */
240+
pthread_create(&threadidReader, NULL, Reader, &args);
241+
pthread_create(&threadidWriter, NULL, Writer, &args);
242+
243+
/* Wait for read/write threads to be done. */
244+
pthread_join(threadidReader, NULL);
245+
pthread_join(threadidWriter, NULL);
246+
247+
/* Housekeeping */
248+
wolfSSL_shutdown(ssl);
249+
wolfSSL_free(ssl);
250+
close(sockfd);
251+
wolfSSL_CTX_free(ctx);
252+
wolfSSL_Cleanup();
253+
254+
return 0;
255+
}

0 commit comments

Comments
 (0)