Skip to content

Commit aee9f10

Browse files
committed
Beginning of cleanup.
1 parent 8254f47 commit aee9f10

1 file changed

Lines changed: 148 additions & 120 deletions

File tree

dtls/client-dtls-resume.c

Lines changed: 148 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,29 @@
3939
#define MAXLINE 4096
4040
#define SERV_PORT 11111
4141

42+
static int new_udp_client_socket(WOLFSSL * ssl, const char * host);
43+
static int talk_to_server(WOLFSSL * ssl);
44+
4245
int main (int argc, char** argv)
4346
{
4447
/* standard variables used in a dtls client*/
45-
int sockfd = 0;
46-
int err1;
47-
int readErr;
48-
struct sockaddr_in servAddr;
49-
const char* host = argv[1];
50-
WOLFSSL* ssl = 0;
51-
WOLFSSL_CTX* ctx = 0;
52-
WOLFSSL* sslResume = 0;
53-
WOLFSSL_SESSION* session = 0;
54-
char* srTest = "testing session resume";
55-
char cert_array[] = "../certs/ca-cert.pem";
56-
char buffer[80];
57-
char* certs = cert_array;
58-
/* variables used in a dtls client for session reuse*/
59-
int recvlen;
60-
char sendLine[MAXLINE];
61-
char recvLine[MAXLINE - 1];
48+
int sockfd = 0;
49+
int err1;
50+
const char * host = argv[1];
51+
WOLFSSL * ssl = NULL; /* The ssl for original connection. */
52+
WOLFSSL * ssl_res = NULL; /* The ssl for resuming connection. */
53+
WOLFSSL_CTX * ctx = NULL;
54+
WOLFSSL_SESSION * session = NULL;
55+
//const char * msg = "client testing session resume";
56+
char cert_array[] = "../certs/ca-cert.pem";
57+
char buffer[80];
58+
char * certs = cert_array;
59+
int ret = 0;
6260

61+
/* variables used in a dtls client for session reuse*/
6362
if (argc != 2) {
6463
printf("usage: udpcli <IP address>\n");
65-
return 1;
64+
return EXIT_FAILURE;
6665
}
6766

6867
wolfSSL_Init();
@@ -72,163 +71,192 @@ int main (int argc, char** argv)
7271

7372
if ( (ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method())) == NULL) {
7473
fprintf(stderr, "wolfSSL_CTX_new error.\n");
75-
return 1;
74+
return EXIT_FAILURE;
7675
}
7776

7877
if (wolfSSL_CTX_load_verify_locations(ctx, certs, 0) != SSL_SUCCESS) {
7978
fprintf(stderr, "Error loading %s, please check the file.\n", certs);
80-
return 1;
79+
return EXIT_FAILURE;
8180
}
8281

8382
ssl = wolfSSL_new(ctx);
8483
if (ssl == NULL) {
85-
printf("unable to get ssl object");
86-
return 1;
87-
}
88-
89-
memset(&servAddr, 0, sizeof(servAddr));
90-
servAddr.sin_family = AF_INET;
91-
servAddr.sin_port = htons(SERV_PORT);
92-
if ( (inet_pton(AF_INET, host, &servAddr.sin_addr)) < 1) {
93-
printf("Error and/or invalid IP address");
94-
return 1;
84+
printf("error: wolfSSL_new failed\n");
85+
return EXIT_FAILURE;
9586
}
9687

97-
wolfSSL_dtls_set_peer(ssl, &servAddr, sizeof(servAddr));
88+
sockfd = new_udp_client_socket(ssl, host);
9889

99-
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
100-
printf("cannot create a socket.");
101-
return 1;
90+
if (sockfd <= 0) {
91+
printf("error: new_udp_client_socket failed\n");
92+
return EXIT_FAILURE;
10293
}
10394

104-
wolfSSL_set_fd(ssl, sockfd);
10595
if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
10696
err1 = wolfSSL_get_error(ssl, 0);
10797
memset(buffer, 0, 80);
10898
printf("err = %d, %s\n", err1, wolfSSL_ERR_error_string(err1, buffer));
10999
printf("SSL_connect failed");
110-
return 1;
100+
return EXIT_FAILURE;
111101
}
112102

113-
/*****************************************************************************/
114-
/* Code for sending datagram to server */
103+
/* Save the session */
104+
session = wolfSSL_get1_session(ssl);
115105

116-
/* Loop while the user gives input or until an EOF is read */
117-
while( fgets(sendLine, MAXLINE, stdin) != NULL ) {
106+
ret = talk_to_server(ssl);
118107

119-
/* Attempt to send sendLine to the server */
120-
if ( ( wolfSSL_write(ssl, sendLine, strlen(sendLine))) !=
121-
strlen(sendLine) ) {
122-
printf("Error: wolfSSL_write failed.\n");
123-
}
108+
if (ret) {
109+
return EXIT_FAILURE;
110+
}
124111

125-
/* Attempt to read a message from server and store it in recvLine */
126-
recvlen = wolfSSL_read(ssl, recvLine, sizeof(recvLine) - 1);
112+
/* Close the socket */
113+
wolfSSL_shutdown(ssl);
114+
wolfSSL_free(ssl);
115+
close(sockfd);
127116

128-
/* Error checking wolfSSL_read */
129-
if (recvlen < 0) {
130-
readErr = wolfSSL_get_error(ssl, 0);
131-
if (readErr != SSL_ERROR_WANT_READ) {
132-
printf("Error: wolfSSL_read failed.\n");
133-
}
134-
}
117+
ssl = NULL;
118+
sockfd = 0;
135119

136-
recvLine[recvlen] = '\0';
137-
fputs(recvLine, stdout);
120+
/* Make a new WOLFSSL. */
121+
ssl_res = wolfSSL_new(ctx);
122+
if (ssl_res == NULL) {
123+
printf("error: wolfSSL_new failed\n");
124+
return EXIT_FAILURE;
138125
}
139-
/* */
140-
/*****************************************************************************/
141126

142-
/* Keep track of the old session information */
143-
wolfSSL_write(ssl, srTest, sizeof(srTest));
144-
session = wolfSSL_get_session(ssl);
145-
sslResume = wolfSSL_new(ctx);
127+
/* Set up to resume the session */
128+
ret = wolfSSL_set_session(ssl_res, session);
129+
130+
if (ret != WOLFSSL_SUCCESS) {
131+
fprintf(stderr, "error: wolfSSL_set_session returned: %d\n", ret);
132+
return EXIT_FAILURE;
133+
}
134+
135+
/* Open a new udp socket. */
136+
sockfd = new_udp_client_socket(ssl_res, host);
137+
138+
if (sockfd <= 0) {
139+
printf("error: new_udp_client_socket failed\n");
140+
return EXIT_FAILURE;
141+
}
142+
143+
/* Test if the resume was successful */
144+
if (wolfSSL_session_reused(ssl_res)) {
145+
printf("info: session ID reused; Successful resume\n");
146+
}
147+
else {
148+
printf("info: session ID not reused\n");
149+
}
150+
151+
ret = talk_to_server(ssl_res);
152+
153+
if (ret) {
154+
return EXIT_FAILURE;
155+
}
156+
157+
/* Cleanup memory used for storing the session information */
158+
wolfSSL_shutdown(ssl_res);
159+
wolfSSL_free(ssl_res);
146160

147-
/* Cleanup the memory used by the old session & ssl object */
148-
wolfSSL_shutdown(ssl);
149-
wolfSSL_free(ssl);
150161
close(sockfd);
162+
wolfSSL_CTX_free(ctx);
163+
wolfSSL_Cleanup();
164+
165+
ssl_res = NULL;
166+
sockfd = 0;
167+
168+
return 0;
169+
}
151170

152-
/* Perform setup with new variables/old session information */
171+
/* Given an ssl structure and host, open a new udp
172+
* client socket and bind it and the server address
173+
* to the ssl.
174+
**/
175+
static int
176+
new_udp_client_socket(WOLFSSL * ssl,
177+
const char * host)
178+
{
179+
struct sockaddr_in servAddr;
180+
int sockfd = 0;
181+
int ret = 0;
182+
183+
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
184+
185+
if (sockfd <= 0) {
186+
int errsave = errno;
187+
printf("error: socket returned %d\n", errsave);
188+
return -1;
189+
}
190+
191+
/* servAddr setup */
153192
memset(&servAddr, 0, sizeof(servAddr));
154193
servAddr.sin_family = AF_INET;
155194
servAddr.sin_port = htons(SERV_PORT);
156-
if ( (inet_pton(AF_INET, host, &servAddr.sin_addr)) < 1) {
157-
printf("Error and/or invalid IP address");
158-
return 1;
195+
196+
ret = inet_pton(AF_INET, host, &servAddr.sin_addr);
197+
198+
if (ret != 1) {
199+
printf("error: inet_pton %s returned %d\n", host, ret);
200+
close(sockfd);
201+
sockfd = 0;
202+
return -1;
159203
}
160204

161-
wolfSSL_dtls_set_peer(sslResume, &servAddr, sizeof(servAddr));
205+
ret = wolfSSL_dtls_set_peer(ssl, &servAddr, sizeof(servAddr));
162206

163-
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
164-
printf("cannot create a socket.");
165-
return 1;
207+
if (ret != SSL_SUCCESS) {
208+
printf("error: wolfSSL_dtls_set_peer returned %d\n", ret);
209+
close(sockfd);
210+
sockfd = 0;
211+
return -1;
166212
}
167213

168-
wolfSSL_set_fd(sslResume, sockfd);
214+
wolfSSL_set_fd(ssl, sockfd);
169215

170-
/* New method call - specifies to the WOLFSSL object to use the *
171-
* given WOLFSSL_SESSION object */
172-
wolfSSL_set_session(sslResume, session);
216+
printf("info: opened socket: %d\n", sockfd);
173217

174-
wolfSSL_set_fd(sslResume, sockfd);
175-
if (wolfSSL_connect(sslResume) != SSL_SUCCESS) {
176-
err1 = wolfSSL_get_error(sslResume, 0);
177-
memset(buffer, 0, 80);
178-
printf("err = %d, %s\n", err1, wolfSSL_ERR_error_string(err1, buffer));
179-
printf("SSL_connect failed on session reuse\n");
180-
return 1;
181-
}
218+
return sockfd;
219+
}
182220

183-
if (wolfSSL_session_reused(sslResume)) {
184-
printf("reused session id\n");
185-
}
186-
else {
187-
printf("didn't reuse session id!!!\n");
188-
}
221+
/* Exchange user input messages with the server.
222+
**/
223+
static int
224+
talk_to_server(WOLFSSL * ssl)
225+
{
226+
int recv_len;
227+
char send_msg[MAXLINE];
228+
char recv_msg[MAXLINE];
189229

190-
/*****************************************************************************/
191-
/* Code for sending datagram to server */
192-
/* Clear out variables for reuse */
193-
recvlen = 0;
194-
memset(sendLine, 0, MAXLINE);
195-
memset(recvLine, 0, MAXLINE - 1);
230+
/* Loop while the user gives input or until user types "stop" */
231+
while( fgets(send_msg, MAXLINE, stdin) != NULL ) {
196232

197-
/* Loop while the user gives input or until an EOF is read */
198-
while( fgets(sendLine, MAXLINE, stdin) != NULL ) {
233+
if (memcmp(send_msg, "stop", strlen("stop")) == 0) {
234+
printf("info: interrupting\n");
235+
break;
236+
}
199237

200-
/* Attempt to send sendLine to the server */
201-
if ( ( wolfSSL_write(ssl, sendLine, strlen(sendLine))) !=
202-
strlen(sendLine) ) {
238+
/* Attempt to send send_msg to the server */
239+
if ( ( wolfSSL_write(ssl, send_msg, strlen(send_msg))) !=
240+
strlen(send_msg) ) {
203241
printf("Error: wolfSSL_write failed.\n");
242+
return -1;
204243
}
205244

206-
/* Attempt to read a message from server and store it in recvLine */
207-
recvlen = wolfSSL_read(ssl, recvLine, sizeof(recvLine) - 1);
245+
/* Attempt to read a message from server and store it in recv_msg */
246+
recv_len = wolfSSL_read(ssl, recv_msg, sizeof(recv_msg) - 1);
208247

209248
/* Error checking wolfSSL_read */
210-
if (recvlen < 0) {
211-
readErr = wolfSSL_get_error(ssl, 0);
249+
if (recv_len < 0) {
250+
int readErr = wolfSSL_get_error(ssl, 0);
212251
if (readErr != SSL_ERROR_WANT_READ) {
213252
printf("Error: wolfSSL_read failed.\n");
214253
}
254+
return -1;
215255
}
216256

217-
recvLine[recvlen] = '\0';
218-
fputs(recvLine, stdout);
257+
recv_msg[recv_len] = '\0';
258+
fputs(recv_msg, stdout);
219259
}
220-
/* */
221-
/*****************************************************************************/
222-
223-
wolfSSL_write(sslResume, srTest, sizeof(srTest));
224-
225-
/* Cleanup memory used for storing the session information */
226-
wolfSSL_shutdown(sslResume);
227-
wolfSSL_free(sslResume);
228-
229-
close(sockfd);
230-
wolfSSL_CTX_free(ctx);
231-
wolfSSL_Cleanup();
232260

233261
return 0;
234262
}

0 commit comments

Comments
 (0)