-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.c
51 lines (47 loc) · 1.3 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "config.h"
int main(int argc, char *argv[]) {
// init arguments
init_openssl();
if (argc != 3) {
printf("usage: ./client [IP] [Port]\n");
exit(-1);
}
char *ip = argv[1];
int port = atoi(argv[2]);
// init and config SSL
int fd = create_client_socket(ip, port);
SSL_CTX *ctx =
init_ssl_ctx(TLS_client_method(), TLS1_2_VERSION, TLS1_2_VERSION);
if (SSL_CTX_set_cipher_list(ctx, "AES128-GCM-SHA256") < 0) {
printf("set cipher error");
exit(-1);
}
SSL *ssl = create_ssl(ctx, fd);
if (SSL_connect(ssl) < 0) {
printf("ssl connect error");
ERR_print_errors_fp(stderr);
exit(-1);
}
// receive data from server until read 0 bytes
char buf[data_size];
size_t buf_len = sizeof(buf);
size_t count = 0;
size_t sum_bytes = 0;
int bytes = 0;
do {
bytes = SSL_read(ssl, buf, buf_len);
if (bytes < 0) {
printf("SSL Read error: %d\n", bytes);
ERR_print_errors_fp(stderr);
exit(-1);
}
sum_bytes += bytes;
count++;
} while (bytes > 0);
printf("Round counts: %ld\nMsg recv: %ld Bytes\n", count - 1, sum_bytes);
// clean up resource
SSL_free(ssl);
SSL_CTX_free(ctx);
close(fd);
return 0;
}