-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.h
118 lines (106 loc) · 2.98 KB
/
config.h
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "openssl/bio.h"
#include "openssl/err.h"
#include "openssl/ssl.h"
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/tls.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <string.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
/* NOTE: data_size <= size of your test file
* data_size is integral multiple of 1024 bytes */
size_t data_size = 16 * 1024;
size_t round_count = 1000000;
int create_listen_socket(int port) {
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr("0.0.0.0");
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
int addr_len = sizeof(addr);
if (sockfd < 0) {
printf("Fail to create socket");
exit(-1);
}
if (bind(sockfd, (struct sockaddr *)&addr, addr_len) < 0) {
printf("Fail to bind");
exit(-1);
}
if (getsockname(sockfd, (struct sockaddr *)&addr, (socklen_t *)&addr_len) <
0) {
printf("Fail to getsockname");
exit(-1);
}
if (listen(sockfd, 1) < 0) {
printf("Unable to listen");
exit(-1);
}
printf("Serving port %d...\n", port);
return sockfd;
}
int create_server_socket(int listenfd) {
int serverfd = accept(listenfd, NULL, 0);
return serverfd;
}
int create_client_socket(char *ip, int port) {
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
int clientfd = socket(AF_INET, SOCK_STREAM, 0);
if (clientfd < 0) {
printf("Fail to create socket");
exit(-1);
}
if (connect(clientfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
printf("Fail to connect server");
exit(-1);
}
return clientfd;
}
SSL_CTX *init_ssl_ctx(const SSL_METHOD *method, int min_proto_version,
int max_proto_version) {
SSL_CTX *ctx = SSL_CTX_new(method);
if (!ctx) {
printf("Fail to create SSL context");
exit(-1);
}
if (!SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION) ||
!SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)) {
printf("set proto version error");
exit(-1);
}
return ctx;
}
void ctx_config_key(SSL_CTX *ctx, char *certfile, char *privkeyfile) {
if (SSL_CTX_use_certificate_file(ctx, certfile, SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
exit(-1);
}
if (SSL_CTX_use_PrivateKey_file(ctx, privkeyfile, SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
exit(-1);
}
if (!SSL_CTX_check_private_key(ctx)) {
ERR_print_errors_fp(stderr);
exit(-1);
}
}
SSL *create_ssl(SSL_CTX *ctx, int fd) {
SSL *ssl = SSL_new(ctx);
if (ssl == NULL) {
printf("create ssl error");
exit(-1);
}
SSL_set_fd(ssl, fd);
return ssl;
}
void init_openssl() {
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
}