-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
84 lines (68 loc) · 1.66 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
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
#include "client.h"
int
connect_to_domain(char *domain)
{
int client;
struct addrinfo hints = {0};
struct addrinfo *remote_addr;
struct addrinfo *p;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(domain, GEMINI_PORT, &hints, &remote_addr)) {
fprintf(stderr, "getaddrinfo() failed.\n");
exit(EXIT_FAILURE);
}
for (p = remote_addr; p != NULL; p = p->ai_next) {
client = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (client == -1) {
continue;
}
if (connect(client, p->ai_addr, p->ai_addrlen) == -1) {
close(client);
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "connect() failed.\n");
exit(EXIT_FAILURE);
}
freeaddrinfo(remote_addr);
return client;
}
void
secure_connection(int client, SSL_CTX **ctx, SSL **ssl, char *domain)
{
X509 *cert;
*ctx = SSL_CTX_new(TLS_client_method());
if (ctx == NULL) {
fprintf(stderr, "SSL_CTX_new() failed.\n");
exit(EXIT_FAILURE);
}
*ssl = SSL_new(*ctx);
if (ssl == NULL) {
fprintf(stderr, "SSL_new() failed.\n");
exit(EXIT_FAILURE);
}
if (SSL_set_tlsext_host_name(*ssl, domain) == 0) {
fprintf(stderr, "SSL_set_tlsext_host_name() failed.\n");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
if (SSL_set_fd(*ssl, client) == 0) {
fprintf(stderr, "SSL_set_fd() failed.\n");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
if (SSL_connect(*ssl) == -1) {
fprintf(stderr, "SSL_connect() failed.\n");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
cert = SSL_get_peer_certificate(*ssl);
if (cert == NULL) {
fprintf(stderr, "SSL_get_peer_certificate() failed.\n");
exit(EXIT_FAILURE);
}
X509_free(cert);
}