Skip to content

Commit 32caf98

Browse files
committed
Fix build errors with error: implicit declaration of function ‘gethostbyname’.
1 parent 1099e57 commit 32caf98

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

tls-options/client-tls-resume.c

+23-22
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
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>
@@ -47,7 +48,7 @@
4748
static 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;

tls-options/client-tls-session.c

+19-18
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
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>
@@ -47,7 +48,7 @@
4748
static 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,
@@ -75,7 +76,7 @@ static int write_SESS(WOLFSSL_SESSION* sess, const char* file)
7576
print_SSL_error("wolfSSL_i2d_SSL_SESSION", NULL);
7677
goto cleanup;
7778
}
78-
79+
7980
if ((fwrite(buff, 1, sz, fp)) != sz) {
8081
fprintf(stderr, "ERROR : failed fwrite\n");
8182
goto cleanup;
@@ -86,7 +87,7 @@ static int write_SESS(WOLFSSL_SESSION* sess, const char* file)
8687
fclose(fp);
8788
if (buff)
8889
free(buff);
89-
90+
9091
return ret;
9192
}
9293

@@ -102,7 +103,7 @@ int main(int argc, char **argv)
102103

103104
char msg[MSG_SIZE];
104105
int ret = WOLFSSL_FAILURE;
105-
106+
106107
(void)ipadd;
107108

108109
/* SSL objects */
@@ -113,17 +114,17 @@ int main(int argc, char **argv)
113114

114115
/* SSL SESSION object */
115116
WOLFSSL_SESSION* session= NULL;
116-
117+
117118
/* Check for proper calling convention */
118-
if (argc == 1)
119+
if (argc == 1)
119120
fprintf(stderr, "Send to localhost(%s)\n", LOCALHOST);
120121
if (argc >=2) {
121122
host = gethostbyname(argv[1]);
122123
memcpy(&servAddr.sin_addr, host->h_addr_list[0], host->h_length);
123124
}
124-
if (argc >= 3)
125+
if (argc >= 3)
125126
ca_cert = argv[2];
126-
if (argc == 4)
127+
if (argc == 4)
127128
port = atoi(argv[3]);
128129
if (argc >= 5) {
129130
fprintf(stderr, "ERROR: Too many arguments.\n");
@@ -135,23 +136,23 @@ int main(int argc, char **argv)
135136
fprintf(stderr, "ERROR: failed to initialize the library\n");
136137
goto cleanup;
137138
}
138-
139+
139140
/* Create and initialize an SSL context object*/
140141
if ((ctx = wolfSSL_CTX_new(SSLv23_client_method())) == NULL) {
141142
fprintf(stderr, "ERROR: failed to create an SSL context object\n");
142143
goto cleanup;
143144
}
144145

145146
/* Load client certificate into WOLFSSL_CTX */
146-
if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE,
147+
if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE,
147148
WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) {
148149
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
149150
CERT_FILE);
150151
goto cleanup;
151152
}
152153

153154
/* Load client key into WOLFSSL_CTX */
154-
if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE,
155+
if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE,
155156
WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) {
156157
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
157158
KEY_FILE);
@@ -165,17 +166,17 @@ int main(int argc, char **argv)
165166
goto cleanup;
166167
}
167168

168-
/*
169-
* Set up a TCP Socket and connect to the server
169+
/*
170+
* Set up a TCP Socket and connect to the server
170171
*/
171172
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
172173
fprintf(stderr, "ERROR: failed to create a socket. errno %d\n", errno);
173174
goto cleanup;
174175
}
175-
176+
176177
servAddr.sin_family = AF_INET; /* using IPv4 */
177178
servAddr.sin_port = htons(port); /* on DEFAULT_PORT */
178-
179+
179180
if ((ret = connect(sockfd, (struct sockaddr *)&servAddr, sizeof(servAddr)))
180181
== -1) {
181182
fprintf(stderr, "ERROR: failed to connect. errno %d\n", errno);
@@ -199,7 +200,7 @@ int main(int argc, char **argv)
199200
goto cleanup;
200201
}
201202

202-
/*
203+
/*
203204
* Application messaging
204205
*/
205206
while (1) {
@@ -217,10 +218,10 @@ int main(int argc, char **argv)
217218
break;
218219
}
219220

220-
/*
221+
/*
221222
* closing the session, and write session information into a file
222223
* before writing session information
223-
*/
224+
*/
224225
if (strcmp(msg, "break") == 0) {
225226
session = wolfSSL_get_session(ssl);
226227
ret = write_SESS(session, SAVED_SESS);

0 commit comments

Comments
 (0)