-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhttps_client.c
205 lines (177 loc) · 4.83 KB
/
https_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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/fcntl.h>
#include <netdb.h>
#include <errno.h>
#include <sys/poll.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#define TCP_PROTOCOL_NAME "tcp"
#define REQUEST_SIZE 1024
#define RESPONSE_SIZE 4096
void print_usage(const char *program_name)
{
fprintf(stderr, "Usage: %s <hostname> <port>\n", program_name);
}
void report_error(const char* message)
{
fprintf(stderr, "Error: %s\n", message);
}
void print_sockaddr_info(struct sockaddr *sa)
{
char ip[INET6_ADDRSTRLEN];
memset(ip, 0, INET6_ADDRSTRLEN);
void *addr;
int port;
if (sa->sa_family == AF_INET)
{
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
addr = &(sin->sin_addr);
port = ntohs(sin->sin_port);
}
else if (sa->sa_family == AF_INET6)
{
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
addr = &(sin6->sin6_addr);
port = ntohs(sin6->sin6_port);
}
else
{
report_error("Unknown address family");
return;
}
if (inet_ntop(sa->sa_family, addr, ip, sizeof(ip)) == NULL)
{
report_error("inet_ntop() failed");
return;
}
printf("%s:%d\n", ip, port);
}
void perform_https_request(char* hostname, char* port)
{
int rc;
struct protoent* tcp_proto = getprotobyname(TCP_PROTOCOL_NAME);
if (tcp_proto == NULL)
{
report_error("TCP protocol is not supported");
return;
}
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = tcp_proto->p_proto;
struct addrinfo* addr_server;
rc = getaddrinfo(hostname, port, &hints, &addr_server);
if (rc != 0)
{
report_error("Resolve address failed");
return;
}
int sock_client = socket(addr_server->ai_family, addr_server->ai_socktype, addr_server->ai_protocol);
if (sock_client < 0)
{
report_error("socket() failed");
return;
}
for (struct addrinfo* p_server = addr_server; p_server != NULL; p_server = p_server->ai_next)
{
print_sockaddr_info(p_server->ai_addr);
rc = connect(sock_client, p_server->ai_addr, p_server->ai_addrlen);
if (rc == 0)
{
break;
}
}
if (rc != 0)
{
report_error("connect() failed");
return;
}
SSL_load_error_strings();
SSL_library_init();
const SSL_METHOD* ssl_method = TLS_client_method();
SSL_CTX* ssl_context = SSL_CTX_new(ssl_method);
if (ssl_context == NULL)
{
report_error("Unable to create SSL context");
freeaddrinfo(addr_server);
close(sock_client);
return;
}
SSL* ssl = SSL_new(ssl_context);
if (ssl == NULL)
{
report_error("SSL_new() failed");
freeaddrinfo(addr_server);
close(sock_client);
SSL_CTX_free(ssl_context);
return;
}
SSL_set_fd(ssl, sock_client);
rc = SSL_connect(ssl);
if (rc <= 0)
{
report_error("SSL_connect() failed");
ERR_print_errors_fp(stderr);
freeaddrinfo(addr_server);
close(sock_client);
SSL_CTX_free(ssl_context);
SSL_shutdown(ssl);
SSL_free(ssl);
return;
}
printf("SSL connection is done with cipher suite %s\n", SSL_get_cipher(ssl));
char http_request[REQUEST_SIZE];
memset(http_request, 0, REQUEST_SIZE);
sprintf(http_request, "GET / HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", hostname);
rc = SSL_write(ssl, http_request, strlen(http_request));
if (rc <= 0)
{
report_error("SSL_write() failed");
ERR_print_errors_fp(stderr);
freeaddrinfo(addr_server);
close(sock_client);
SSL_CTX_free(ssl_context);
SSL_shutdown(ssl);
SSL_free(ssl);
return;
}
char http_response[RESPONSE_SIZE + 1];
memset(http_response, 0, RESPONSE_SIZE);
int received_bytes = 0;
int total_received_bytes = 0;
do
{
received_bytes = SSL_read(ssl, http_response + total_received_bytes, RESPONSE_SIZE - total_received_bytes);
if (received_bytes > 0)
{
printf("Received %d bytes\n", received_bytes);
total_received_bytes += received_bytes;
}
} while (received_bytes > 0);
http_response[total_received_bytes] = 0;
printf("%s\n", http_response);
freeaddrinfo(addr_server);
close(sock_client);
SSL_CTX_free(ssl_context);
SSL_shutdown(ssl);
SSL_free(ssl);
}
int main(int argc, char** argv)
{
if (argc != 3)
{
print_usage(argv[0]);
return -1;
}
perform_https_request(argv[1], argv[2]);
return 0;
}