1+ /* simple_client_example.c
2+ *
3+ * Copyright (C) 2006-2023 wolfSSL Inc.
4+ *
5+ * This file is part of wolfSSL.
6+ *
7+ * wolfSSL is free software; you can redistribute it and/or modify
8+ * it under the terms of the GNU General Public License as published by
9+ * the Free Software Foundation; either version 2 of the License, or
10+ * (at your option) any later version.
11+ *
12+ * wolfSSL is distributed in the hope that it will be useful,
13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ * GNU General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU General Public License
18+ * along with this program; if not, write to the Free Software
19+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+ */
21+
122#include "simple_client_example.h"
223
324#include <stdio.h>
425#include <stdlib.h>
526#include <string.h>
627#include <sys/socket.h>
728#include <netdb.h>
29+
30+ #ifndef WOLFSSL_USER_SETTINGS
831#include <wolfssl/options.h>
32+ #endif
933#include <wolfssl/ssl.h>
1034
1135#define SERVER_HOST "www.wolfssl.com"
@@ -17,7 +41,7 @@ int simple_client_example(void)
1741 WOLFSSL * ssl ;
1842 int sockfd , ret ;
1943
20- // Resolve the server address
44+ /* Resolve the server address */
2145 struct addrinfo hints , * server_addr ;
2246 memset (& hints , 0 , sizeof (hints ));
2347 hints .ai_family = AF_UNSPEC ;
@@ -29,15 +53,15 @@ int simple_client_example(void)
2953 return 1 ;
3054 }
3155
32- // Create a TCP socket
56+ /* Create a TCP socket */
3357 sockfd = socket (server_addr -> ai_family , server_addr -> ai_socktype , server_addr -> ai_protocol );
3458 if (sockfd == -1 ) {
3559 perror ("Failed to create socket" );
3660 freeaddrinfo (server_addr );
3761 return 1 ;
3862 }
3963
40- // Connect to the server
64+ /* Connect to the server */
4165 ret = connect (sockfd , server_addr -> ai_addr , server_addr -> ai_addrlen );
4266 if (ret == -1 ) {
4367 perror ("Failed to connect to server" );
@@ -48,29 +72,29 @@ int simple_client_example(void)
4872
4973 freeaddrinfo (server_addr );
5074
51- // Initialize wolfSSL library
75+ /* Initialize wolfSSL library */
5276 wolfSSL_Init ();
5377
54- // Create a new SSL context
78+ /* Create a new SSL context */
5579 ctx = wolfSSL_CTX_new (wolfTLSv1_3_client_method ());
5680 if (ctx == NULL ) {
5781 printf ("Unable to create SSL context.\n" );
5882 close (sockfd );
5983 return 1 ;
6084 }
6185
62- // Load CA certificate into WOLFSSL_CTX
63- // NOTE: CERT_PATH macro is set relative to Xcode $(PROJECT_DIR) environment variable
64- // in the preprocessor macros section of the project build settings to avoid hardcoding
65- // a path in the source code. The CA cert is located at wolfssl/certs/wolfssl-website-ca.pem.
86+ /* Load CA certificate into WOLFSSL_CTX
87+ * NOTE: CERT_PATH macro is set relative to Xcode $(PROJECT_DIR) environment variable
88+ * in the preprocessor macros section of the project build settings to avoid hardcoding
89+ * a path in the source code. The CA cert is located at wolfssl/certs/wolfssl-website-ca.pem. */
6690 if ((ret = wolfSSL_CTX_load_verify_locations (ctx , CERT_PATH , NULL )) != WOLFSSL_SUCCESS ) {
6791 printf ("ERROR: failed to load %s, please check the file.\n" , CERT_PATH );
6892 wolfSSL_CTX_free (ctx );
6993 close (sockfd );
7094 return 1 ;
7195 }
7296
73- // Create a new SSL object
97+ /* Create a new SSL object */
7498 ssl = wolfSSL_new (ctx );
7599 if (ssl == NULL ) {
76100 printf ("Unable to create SSL object.\n" );
@@ -79,10 +103,10 @@ int simple_client_example(void)
79103 return 1 ;
80104 }
81105
82- // Attach the SSL object to the socket file descriptor
106+ /* Attach the SSL object to the socket file descriptor */
83107 wolfSSL_set_fd (ssl , sockfd );
84108
85- // Perform the SSL handshake
109+ /* Perform the SSL handshake */
86110 ret = wolfSSL_connect (ssl );
87111 if (ret != SSL_SUCCESS ) {
88112 printf ("SSL connection failed: %d\n" , wolfSSL_get_error (ssl , ret ));
@@ -92,14 +116,14 @@ int simple_client_example(void)
92116 return 1 ;
93117 }
94118
95- // Send an HTTP request
119+ /* Send an HTTP request */
96120 const char * request = "GET / HTTP/1.1\r\nHost: www.wolfssl.com\r\n\r\n" ;
97121 ret = wolfSSL_write (ssl , request , (int )strlen (request ));
98122 if (ret < 0 ) {
99123 printf ("Failed to send HTTP request.\n" );
100124 }
101125
102- // Receive and print the server's response
126+ /* Receive and print the server's response */
103127 char buffer [1024 ];
104128 ret = wolfSSL_read (ssl , buffer , sizeof (buffer ) - 1 );
105129 if (ret > 0 ) {
@@ -109,7 +133,7 @@ int simple_client_example(void)
109133 printf ("Failed to receive server response.\n" );
110134 }
111135
112- // Clean up and close the connection
136+ /* Clean up and close the connection */
113137 wolfSSL_shutdown (ssl );
114138 wolfSSL_free (ssl );
115139 wolfSSL_CTX_free (ctx );
0 commit comments