Skip to content

Commit 8c57fd3

Browse files
committed
cleanup.
1 parent aee9f10 commit 8c57fd3

1 file changed

Lines changed: 35 additions & 33 deletions

File tree

dtls/client-dtls-resume.c

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#define SERV_PORT 11111
4141

4242
static int new_udp_client_socket(WOLFSSL * ssl, const char * host);
43-
static int talk_to_server(WOLFSSL * ssl);
43+
static int talk_to_server(WOLFSSL * ssl, const char * msg);
4444

4545
int main (int argc, char** argv)
4646
{
@@ -103,7 +103,14 @@ int main (int argc, char** argv)
103103
/* Save the session */
104104
session = wolfSSL_get1_session(ssl);
105105

106-
ret = talk_to_server(ssl);
106+
if (session == NULL) {
107+
printf("error: get session failed\n");
108+
return EXIT_FAILURE;
109+
}
110+
111+
printf("info: saved session: %p\n", session);
112+
113+
ret = talk_to_server(ssl, "first client message");
107114

108115
if (ret) {
109116
return EXIT_FAILURE;
@@ -148,7 +155,7 @@ int main (int argc, char** argv)
148155
printf("info: session ID not reused\n");
149156
}
150157

151-
ret = talk_to_server(ssl_res);
158+
ret = talk_to_server(ssl_res, "client message after resume");
152159

153160
if (ret) {
154161
return EXIT_FAILURE;
@@ -157,19 +164,21 @@ int main (int argc, char** argv)
157164
/* Cleanup memory used for storing the session information */
158165
wolfSSL_shutdown(ssl_res);
159166
wolfSSL_free(ssl_res);
167+
wolfSSL_SESSION_free(session);
160168

161169
close(sockfd);
162170
wolfSSL_CTX_free(ctx);
163171
wolfSSL_Cleanup();
164172

165173
ssl_res = NULL;
174+
session = NULL;
166175
sockfd = 0;
167176

168177
return 0;
169178
}
170179

171180
/* Given an ssl structure and host, open a new udp
172-
* client socket and bind it and the server address
181+
* client socket and set it and the server address
173182
* to the ssl.
174183
**/
175184
static int
@@ -218,45 +227,38 @@ new_udp_client_socket(WOLFSSL * ssl,
218227
return sockfd;
219228
}
220229

221-
/* Exchange user input messages with the server.
230+
/* Send a message to the server.
222231
**/
223232
static int
224-
talk_to_server(WOLFSSL * ssl)
233+
talk_to_server(WOLFSSL * ssl,
234+
const char * send_msg)
225235
{
226-
int recv_len;
227-
char send_msg[MAXLINE];
228236
char recv_msg[MAXLINE];
237+
int recv_len;
229238

230-
/* Loop while the user gives input or until user types "stop" */
231-
while( fgets(send_msg, MAXLINE, stdin) != NULL ) {
232-
233-
if (memcmp(send_msg, "stop", strlen("stop")) == 0) {
234-
printf("info: interrupting\n");
235-
break;
236-
}
239+
memset(recv_msg, 0, sizeof(recv_msg));
237240

238-
/* Attempt to send send_msg to the server */
239-
if ( ( wolfSSL_write(ssl, send_msg, strlen(send_msg))) !=
240-
strlen(send_msg) ) {
241-
printf("Error: wolfSSL_write failed.\n");
242-
return -1;
243-
}
241+
/* Attempt to send send_msg to the server */
242+
if ( ( wolfSSL_write(ssl, send_msg, strlen(send_msg))) !=
243+
strlen(send_msg) ) {
244+
printf("Error: wolfSSL_write failed.\n");
245+
return -1;
246+
}
244247

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);
248+
/* Attempt to read a message from server and store it in recv_msg */
249+
recv_len = wolfSSL_read(ssl, recv_msg, sizeof(recv_msg) - 1);
247250

248-
/* Error checking wolfSSL_read */
249-
if (recv_len < 0) {
250-
int readErr = wolfSSL_get_error(ssl, 0);
251-
if (readErr != SSL_ERROR_WANT_READ) {
252-
printf("Error: wolfSSL_read failed.\n");
253-
}
254-
return -1;
251+
/* Error checking wolfSSL_read */
252+
if (recv_len < 0) {
253+
int readErr = wolfSSL_get_error(ssl, 0);
254+
if (readErr != SSL_ERROR_WANT_READ) {
255+
printf("Error: wolfSSL_read failed.\n");
255256
}
256-
257-
recv_msg[recv_len] = '\0';
258-
fputs(recv_msg, stdout);
257+
return -1;
259258
}
260259

260+
recv_msg[recv_len] = '\0';
261+
fputs(recv_msg, stdout);
262+
261263
return 0;
262264
}

0 commit comments

Comments
 (0)