2929#include <arpa/inet.h>
3030#include <netinet/in.h>
3131#include <unistd.h>
32+ #include <netdb.h>
3233
3334/* wolfSSL */
3435#include <wolfssl/options.h>
4748static void print_SSL_error (const char * msg , SSL * ssl )
4849{
4950 int err ;
50-
51+
5152 if (ssl != NULL ) {
5253 err = wolfSSL_get_error (ssl , 0 );
5354 fprintf (stderr , "ERROR: %s (err %d, %s)\n" , msg , err ,
@@ -67,28 +68,28 @@ static int read_SESS(const char* file, SSL* ssl)
6768 size_t sz ;
6869 WOLFSSL_SESSION * sess = NULL ;
6970 int ret = WOLFSSL_FAILURE ;
70-
71+
7172 if (((fp = fopen (file , "rb" )) == NULL ) ||
7273 (fseek (fp , 0 , SEEK_END ) != 0 ) ||
7374 ((sz = ftell (fp )) == -1 )) {
7475 fprintf (stderr , "ERROR : failed file %s operation \n" , file );
7576 goto cleanup ;
7677 }
77-
78+
7879 rewind (fp );
7980 if ((buff = (unsigned char * )malloc (sz )) == NULL ||
8081 (fread (buff , 1 , sz , fp ) != sz )) {
8182 fprintf (stderr , "ERROR : failed reading file\n" );
8283 goto cleanup ;
8384 }
84-
85+
8586 printf ("%s size = %ld\n" , SAVED_SESS , sz );
86-
87+
8788 p = buff ;
8889 if ((sess = wolfSSL_d2i_SSL_SESSION (NULL , (const unsigned char * * )& p , sz )) == NULL ) {
8990 print_SSL_error ("wolfSSL_d2i_SSL_SESSION" , NULL );
9091 }
91-
92+
9293 if (sess != NULL && (ret = wolfSSL_set_session (ssl , sess ) != WOLFSSL_SUCCESS )) {
9394 print_SSL_error ("failed SSL session" , ssl );
9495 } else {
@@ -118,7 +119,7 @@ int main(int argc, char **argv)
118119
119120 char msg [MSG_SIZE ];
120121 int ret = WOLFSSL_FAILURE ;
121-
122+
122123 (void )ipadd ;
123124
124125 /* SSL objects */
@@ -128,15 +129,15 @@ int main(int argc, char **argv)
128129 memset (& servAddr , 0 , sizeof (servAddr ));
129130
130131 /* Check for proper calling convention */
131- if (argc == 1 )
132+ if (argc == 1 )
132133 fprintf (stderr , "Send to localhost(%s)\n" , LOCALHOST );
133134 if (argc >=2 ) {
134135 host = gethostbyname (argv [1 ]);
135136 memcpy (& servAddr .sin_addr , host -> h_addr_list [0 ], host -> h_length );
136137 }
137- if (argc >= 3 )
138+ if (argc >= 3 )
138139 ca_cert = argv [2 ];
139- if (argc == 4 )
140+ if (argc == 4 )
140141 port = atoi (argv [3 ]);
141142 if (argc >= 5 ) {
142143 fprintf (stderr , "ERROR: Too many arguments.\n" );
@@ -148,23 +149,23 @@ int main(int argc, char **argv)
148149 fprintf (stderr , "ERROR: failed to initialize the library\n" );
149150 goto cleanup ;
150151 }
151-
152+
152153 /* Create and initialize an SSL context object*/
153154 if ((ctx = wolfSSL_CTX_new (SSLv23_client_method ())) == NULL ) {
154155 fprintf (stderr , "ERROR: failed to create an SSL context object\n" );
155156 goto cleanup ;
156157 }
157158
158159 /* Load client certificate into WOLFwolfSSL_CTX */
159- if ((ret = wolfSSL_CTX_use_certificate_file (ctx , CERT_FILE ,
160+ if ((ret = wolfSSL_CTX_use_certificate_file (ctx , CERT_FILE ,
160161 WOLFSSL_FILETYPE_PEM )) != WOLFSSL_SUCCESS ) {
161162 fprintf (stderr , "ERROR: failed to load %s, please check the file.\n" ,
162163 CERT_FILE );
163164 goto cleanup ;
164165 }
165166
166167 /* Load client key into WOLFwolfSSL_CTX */
167- if ((ret = wolfSSL_CTX_use_PrivateKey_file (ctx , KEY_FILE ,
168+ if ((ret = wolfSSL_CTX_use_PrivateKey_file (ctx , KEY_FILE ,
168169 WOLFSSL_FILETYPE_PEM )) != WOLFSSL_SUCCESS ) {
169170 fprintf (stderr , "ERROR: failed to load %s, please check the file.\n" ,
170171 KEY_FILE );
@@ -178,17 +179,17 @@ int main(int argc, char **argv)
178179 goto cleanup ;
179180 }
180181
181- /*
182- * Set up a TCP Socket and connect to the server
182+ /*
183+ * Set up a TCP Socket and connect to the server
183184 */
184185 if ((sockfd = socket (AF_INET , SOCK_STREAM , 0 )) == -1 ) {
185186 fprintf (stderr , "ERROR: failed to create a socket. errno %d\n" , errno );
186187 goto cleanup ;
187188 }
188-
189+
189190 servAddr .sin_family = AF_INET ; /* using IPv4 */
190191 servAddr .sin_port = htons (port ); /* on DEFAULT_PORT */
191-
192+
192193 if ((ret = connect (sockfd , (struct sockaddr * )& servAddr , sizeof (servAddr )))
193194 == -1 ) {
194195 fprintf (stderr , "ERROR: failed to connect. errno %d\n" , errno );
@@ -206,7 +207,7 @@ int main(int argc, char **argv)
206207 fprintf (stderr , "ERROR: failed to read session information\n" );
207208 goto cleanup ;
208209 }
209-
210+
210211 /* Attach the socket to the SSL */
211212 if ((ret = wolfSSL_set_fd (ssl , sockfd )) != WOLFSSL_SUCCESS ) {
212213 fprintf (stderr , "ERROR: Failed to set the file descriptor\n" );
@@ -226,7 +227,7 @@ int main(int argc, char **argv)
226227 printf ("Session is not reused. New session was negotiated.\n" );
227228 }
228229
229- /*
230+ /*
230231 * Application messaging
231232 */
232233 while (1 ) {
@@ -235,18 +236,18 @@ int main(int argc, char **argv)
235236 break ;
236237 if (strcmp (msg , "\n" ) == 0 ){ /* if empty send HTTP request */
237238 strncpy (msg , kHttpGetMsg , sizeof (msg ));
238- } else
239+ } else
239240 msg [strnlen (msg , sizeof (msg ))- 1 ] = '\0' ;
240241 /* send a message to the server */
241242 if ((ret = wolfSSL_write (ssl , msg , strnlen (msg , sizeof (msg )))) < 0 ) {
242243 print_SSL_error ("failed SSL write" , ssl );
243244 break ;
244245 }
245246
246- /*
247+ /*
247248 * closing the session, and write session information into a file
248249 * before writing session information, the file is removed if exists
249- */
250+ */
250251 if (strcmp (msg , "break" ) == 0 ) {
251252 printf ("Sending break command\n" );
252253 ret = WOLFSSL_SUCCESS ;
0 commit comments