4 * Copyright (C) 2011-2013 Simon Ruderich
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "connection.h"
29 #include <sys/socket.h>
32 #include <gnutls/x509.h>
35 /* Maximum length of a HTTP request line. Longer request lines are aborted
36 * with an error. The standard doesn't specify a maximum line length but this
37 * should be a good limit to make processing simpler. As HTTPS is used this
38 * doesn't limit long GET requests. */
39 #define MAX_REQUEST_LINE 4096
41 /* Format string used to send HTTP/1.0 error responses to the client.
43 * %s is used 5 times, first is the error code, then additional headers, next
44 * two are the error code (no %n$s which is not in C98!), the last is the
46 #define HTTP_RESPONSE_FORMAT "HTTP/1.0 %s\r\n\
47 Content-Type: text/html; charset=US-ASCII\r\n\
49 <!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n\
51 <head><title>%s</title></head>\n\
59 static int initialize_tls_session_client(int peer_socket,
61 gnutls_session_t *session,
62 gnutls_certificate_credentials_t *x509_cred);
63 static int initialize_tls_session_server(int peer_socket,
64 gnutls_session_t *session,
65 gnutls_certificate_credentials_t *x509_cred);
66 static int initialize_tls_session_both(unsigned int flags,
68 gnutls_session_t *session,
69 gnutls_certificate_credentials_t *x509_cred);
71 static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd);
72 static int read_http_request(FILE *client_fd, char *request, size_t length);
73 static void send_bad_request(FILE *client_fd);
74 static void send_authentication_required(FILE *client_fd);
75 static void send_forwarding_failure(FILE *client_fd);
76 static void tls_send_invalid_cert_message(gnutls_session_t session);
78 static void transfer_data(int client, int server);
79 static int read_from_write_to(int from, int to);
80 static void transfer_data_tls(int client, int server,
81 gnutls_session_t client_session,
82 gnutls_session_t server_session);
83 static int read_from_write_to_tls(gnutls_session_t from, gnutls_session_t to,
86 static int connect_to_host(const char *hostname, const char *port);
88 static int parse_request(const char *buffer, char *host, char *port,
91 static void log_session_information(gnutls_session_t session);
94 void handle_connection(int client_socket) {
96 FILE *client_fd_read, *client_fd_write, *server_fd_read, *server_fd_write;
98 char buffer[MAX_REQUEST_LINE];
99 char host[MAX_REQUEST_LINE];
102 int version_minor; /* x in HTTP/1.x */
105 /* client_x509_cred is used when talking to the client (acting as a TSL
106 * server), server_x509_cred is used when talking to the server (acting as
108 gnutls_certificate_credentials_t client_x509_cred, server_x509_cred;
110 gnutls_session_t client_session, server_session;
111 /* initialize_tls_session_*() called? - used for goto out */
112 int client_session_init, server_session_init;
113 /* gnutls_handshake() called? - used for goto out */
114 int client_session_started, server_session_started;
115 /* Validation failed? If yes we need to send the special "invalid"
117 int validation_failed;
119 LOG(DEBUG1, "new connection");
122 client_fd_read = NULL;
123 client_fd_write = NULL;
124 server_fd_read = NULL;
125 server_fd_write = NULL;
126 client_session_init = 0;
127 server_session_init = 0;
128 client_session_started = 0;
129 server_session_started = 0;
130 validation_failed = 0;
132 if (fdopen_read_write(client_socket, &client_fd_read,
133 &client_fd_write) != 0) {
137 /* Read request line (CONNECT ..) and headers (they are discarded). */
138 result = read_http_request(client_fd_read, buffer, sizeof(buffer));
140 LOG(WARNING, "read_http_request(): client read error");
142 } else if (result == -2) {
143 LOG(WARNING, "read_http_request(): client EOF");
144 send_bad_request(client_fd_write);
146 } else if (result == -3) {
147 LOG(WARNING, "read_http_request(): proxy authentication failed");
148 send_authentication_required(client_fd_write);
152 if (parse_request(buffer, host, port, &version_minor) != 0) {
153 LOG(WARNING, "bad request: >%s<", buffer);
154 send_bad_request(client_fd_write);
158 LOG(DEBUG2, "target: %s:%s (HTTP 1.%d)", host, port, version_minor);
160 /* Connect to proxy server or directly to server. */
161 if (global_proxy_host != NULL && global_proxy_port != NULL) {
162 LOG(DEBUG1, "connecting to %s:%s", global_proxy_host,
164 server_socket = connect_to_host(global_proxy_host, global_proxy_port);
166 LOG(DEBUG1, "connecting to %s:%s", host, port);
167 server_socket = connect_to_host(host, port);
170 if (server_socket < 0) {
171 LOG(WARNING, "failed to connect to server");
172 send_forwarding_failure(client_fd_write);
175 if (fdopen_read_write(server_socket, &server_fd_read,
176 &server_fd_write) != 0) {
177 send_forwarding_failure(client_fd_write);
181 /* Connect to proxy if requested (command line option). */
182 if (global_proxy_host != NULL && global_proxy_port != NULL) {
183 fprintf(server_fd_write, "CONNECT %s:%s HTTP/1.0\r\n", host, port);
184 fprintf(server_fd_write, "\r\n");
185 fflush(server_fd_write);
187 /* Read response line from proxy server. */
188 result = read_http_request(server_fd_read, buffer, sizeof(buffer));
190 LOG(WARNING, "read_http_request(): proxy read error");
191 send_forwarding_failure(client_fd_write);
193 } else if (result == -2) {
194 LOG(WARNING, "read_http_request(): proxy EOF");
195 send_forwarding_failure(client_fd_write);
199 /* Check response of proxy server. */
200 if (strncmp(buffer, "HTTP/1.0 200", 12)) {
201 LOG(WARNING, "bad proxy response: >%s<", buffer);
202 send_forwarding_failure(client_fd_write);
207 LOG(DEBUG1, "connection to server established");
209 /* If the -u option is used and we don't know this hostname's server
210 * certificate, then just pass through the connection and let the client
211 * verify the server certificate. */
212 if (global_passthrough_unknown) {
213 char path[TLSPROXY_MAX_PATH_LENGTH];
216 if (server_certificate_file(&file, host, path, sizeof(path)) == -2) {
217 /* We've established a connection, tell the client. */
218 fprintf(client_fd_write, "HTTP/1.0 200 Connection established\r\n");
219 fprintf(client_fd_write, "\r\n");
220 fflush(client_fd_write);
222 LOG(DEBUG1, "transferring data");
224 /* Proxy data between client and server until one side is done
226 transfer_data(client_socket, server_socket);
228 LOG(DEBUG1, "finished transferring data");
232 /* server_certificate_file() may have opened the file, close it. */
238 /* Initialize TLS client credentials to talk to the server. */
239 result = initialize_tls_session_server(server_socket, &server_session,
242 LOG(WARNING, "initialize_tls_session_server() failed");
243 send_forwarding_failure(client_fd_write);
246 server_session_init = 1;
248 LOG(DEBUG1, "starting server TLS handshake");
250 /* Try to establish TLS handshake between us and server. */
251 result = gnutls_handshake(server_session);
252 if (result != GNUTLS_E_SUCCESS) {
253 LOG(WARNING, "server TLS handshake failed: %s",
254 gnutls_strerror(result));
255 send_forwarding_failure(client_fd_write);
258 server_session_started = 1;
260 LOG(DEBUG1, "server TLS handshake finished");
262 if (global_log_level >= LOG_DEBUG2_LEVEL) {
263 log_session_information(server_session);
266 /* Make sure the server certificate is valid and known. */
267 if (verify_tls_connection(server_session, host) != 0) {
268 LOG(ERROR, "server certificate validation failed!");
269 /* We'll send the error message over our TLS connection to the client,
270 * but with an invalid certificate. No data is transfered from/to the
272 validation_failed = 1;
275 /* Initialize TLS server credentials to talk to the client. */
276 result = initialize_tls_session_client(client_socket,
277 /* use a special host if the server
278 * certificate was invalid */
279 (validation_failed) ? "invalid"
284 LOG(WARNING, "initialize_tls_session_client() failed");
285 send_forwarding_failure(client_fd_write);
288 client_session_init = 1;
290 /* We've established a connection, tell the client. */
291 fprintf(client_fd_write, "HTTP/1.0 200 Connection established\r\n");
292 fprintf(client_fd_write, "\r\n");
293 fflush(client_fd_write);
295 LOG(DEBUG1, "starting client TLS handshake");
297 /* Try to establish TLS handshake between client and us. */
298 result = gnutls_handshake(client_session);
299 if (result != GNUTLS_E_SUCCESS) {
300 LOG(WARNING, "client TLS handshake failed: %s",
301 gnutls_strerror(result));
302 send_forwarding_failure(client_fd_write);
305 client_session_started = 1;
307 LOG(DEBUG1, "client TLS handshake finished");
309 if (global_log_level >= LOG_DEBUG2_LEVEL) {
310 log_session_information(client_session);
313 /* Tell the client that the verification failed. Shouldn't be necessary as
314 * the client should terminate the connection because he received the
315 * invalid certificate but better be sure. */
316 if (validation_failed) {
317 tls_send_invalid_cert_message(client_session);
321 LOG(DEBUG1, "transferring TLS data");
323 /* Proxy data between client and server until one side is done (EOF or
325 transfer_data_tls(client_socket, server_socket,
326 client_session, server_session);
328 LOG(DEBUG1, "finished transferring TLS data");
331 /* Close TLS sessions if necessary. Use GNUTLS_SHUT_RDWR so the data is
332 * reliable transmitted. */
333 if (server_session_started) {
334 /* Recent gnutls-serv (used in the test-suite) won't terminate the
335 * connection when gnutls_bye(session, GNUTLS_SHUT_RDWR) is used
336 * before any other data was received. If the validation failed just
337 * close the connection without waiting for data, we won't read it
340 * For verified connections GNUTLS_SHUT_RDWR is important or we might
342 gnutls_bye(server_session, validation_failed ? GNUTLS_SHUT_WR
345 if (client_session_started) {
346 gnutls_bye(client_session, GNUTLS_SHUT_RDWR);
348 if (server_session_init) {
349 gnutls_deinit(server_session);
350 gnutls_certificate_free_credentials(server_x509_cred);
352 if (client_session_init) {
353 gnutls_deinit(client_session);
354 gnutls_certificate_free_credentials(client_x509_cred);
357 /* Close connection to server/proxy. */
358 if (server_fd_read != NULL) {
359 if (server_fd_write != NULL) {
360 fclose(server_fd_write);
362 fclose(server_fd_read);
363 } else if (server_socket != -1) {
364 close(server_socket);
366 LOG(DEBUG1, "connection to server closed");
367 /* Close connection to client. */
368 if (client_fd_read != NULL) {
369 if (client_fd_write != NULL) {
370 fclose(client_fd_write);
372 fclose(client_fd_read);
374 close(client_socket);
376 LOG(DEBUG1, "connection to client closed");
378 LOG(DEBUG1, "connection finished");
382 static int initialize_tls_session_client(int peer_socket,
383 const char *hostname,
384 gnutls_session_t *session,
385 gnutls_certificate_credentials_t *x509_cred) {
387 int use_invalid_cert;
388 char path[TLSPROXY_MAX_PATH_LENGTH];
390 /* The "invalid" hostname is special. If it's used we send an invalid
391 * certificate to let the client know something is wrong. */
392 use_invalid_cert = (!strcmp(hostname, "invalid"));
394 if (proxy_certificate_path(hostname, path, sizeof(path)) != 0) {
396 "initialize_tls_session_client(): "
397 "failed to get proxy certificate path");
401 result = gnutls_certificate_allocate_credentials(x509_cred);
402 if (result != GNUTLS_E_SUCCESS) {
404 "initialize_tls_session_client(): "
405 "gnutls_certificate_allocate_credentials(): %s",
406 gnutls_strerror(result));
410 /* Load proxy CA file, this CA "list" is send to the client. */
411 if (!use_invalid_cert) {
412 result = gnutls_certificate_set_x509_trust_file(*x509_cred,
414 GNUTLS_X509_FMT_PEM);
417 "initialize_tls_session_client(): can't read CA file: '%s'",
419 gnutls_certificate_free_credentials(*x509_cred);
421 } else if (result != 1) {
422 /* Must contain only one CA, our proxy CA. */
423 LOG(ERROR, "initialize_tls_session_client(): multiple CAs found");
424 gnutls_certificate_free_credentials(*x509_cred);
428 /* If the invalid hostname was specified do nothing, we use a self-signed
429 * certificate in this case. */
431 /* And certificate for this website and proxy's private key. */
432 if (!use_invalid_cert) {
433 result = gnutls_certificate_set_x509_key_file(*x509_cred,
436 GNUTLS_X509_FMT_PEM);
437 /* If the invalid hostname was specified load our special "invalid"
440 result = gnutls_certificate_set_x509_key_file(*x509_cred,
441 PROXY_INVALID_CERT_PATH,
443 GNUTLS_X509_FMT_PEM);
445 if (result != GNUTLS_E_SUCCESS) {
447 "initialize_tls_session_client(): "
448 "can't read server certificate ('%s') or key file ('%s'): %s",
449 path, PROXY_KEY_PATH, gnutls_strerror(result));
450 gnutls_certificate_free_credentials(*x509_cred);
451 /* Could be a missing certificate. */
455 gnutls_certificate_set_dh_params(*x509_cred, global_tls_dh_params);
457 return initialize_tls_session_both(GNUTLS_SERVER,
458 peer_socket, session, x509_cred);
460 static int initialize_tls_session_server(int peer_socket,
461 gnutls_session_t *session,
462 gnutls_certificate_credentials_t *x509_cred) {
465 result = gnutls_certificate_allocate_credentials(x509_cred);
466 if (result != GNUTLS_E_SUCCESS) {
468 "initialize_tls_session_server(): "
469 "gnutls_certificate_allocate_credentials(): %s",
470 gnutls_strerror(result));
474 return initialize_tls_session_both(GNUTLS_CLIENT,
475 peer_socket, session, x509_cred);
477 static int initialize_tls_session_both(unsigned int flags,
479 gnutls_session_t *session,
480 gnutls_certificate_credentials_t *x509_cred) {
485 result = gnutls_init(session, flags);
486 if (result != GNUTLS_E_SUCCESS) {
488 "initialize_tls_session_both(): gnutls_init(): %s",
489 gnutls_strerror(result));
492 result = gnutls_priority_set(*session, global_tls_priority_cache);
493 if (result != GNUTLS_E_SUCCESS) {
495 "initialize_tls_session_both(): gnutls_priority_set(): %s",
496 gnutls_strerror(result));
499 result = gnutls_credentials_set(*session,
500 GNUTLS_CRD_CERTIFICATE, *x509_cred);
501 if (result != GNUTLS_E_SUCCESS) {
503 "initialize_tls_session_both(): gnutls_credentials_set(): %s",
504 gnutls_strerror(result));
508 #ifdef HAVE_GNUTLS_TRANSPORT_SET_INT2
509 /* gnutls_transport_set_int() is a macro. */
510 gnutls_transport_set_int(*session, peer_socket);
512 gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
519 gnutls_deinit(*session);
521 gnutls_certificate_free_credentials(*x509_cred);
526 static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd) {
527 *read_fd = fdopen(socket, "r");
528 if (*read_fd == NULL) {
529 LOG_PERROR(WARNING, "fdopen_read_write(): fdopen(\"r\") failed");
533 *write_fd = fdopen(dup(socket), "w");
534 if (*write_fd == NULL) {
535 LOG_PERROR(WARNING, "fdopen_read_write(): fdopen(\"w\") failed");
537 *read_fd = NULL; /* "tell" caller read_fd is already closed */
544 /* Read HTTP request line and headers (ignored).
546 * On success 0 is returned, -1 on client error, -2 on unexpected EOF.
548 static int read_http_request(FILE *client_fd, char *request, size_t length) {
549 char buffer[MAX_REQUEST_LINE];
550 int found_proxy_authorization;
552 assert(length <= INT_MAX);
553 if (fgets(request, (int)length, client_fd) == NULL) {
554 if (ferror(client_fd)) {
555 LOG_PERROR(WARNING, "read_http_request(): fgets()");
562 found_proxy_authorization = 0;
563 while (fgets(buffer, sizeof(buffer), client_fd) != NULL) {
564 const char *authentication = "Proxy-Authorization: Basic ";
566 if (global_http_digest_authorization != NULL
567 && !strncmp(buffer, authentication, strlen(authentication))) {
568 found_proxy_authorization = 1;
570 /* Check if the passphrase matches. */
571 strtok(buffer, "\r\n");
572 if (strcmp(buffer + strlen(authentication),
573 global_http_digest_authorization)) {
579 if (!strcmp(buffer, "\n") || !strcmp(buffer, "\r\n")) {
583 if (ferror(client_fd)) {
584 LOG_PERROR(WARNING, "read_http_request(): fgets()");
586 } else if (feof(client_fd)) {
590 if (global_http_digest_authorization != NULL && !found_proxy_authorization) {
597 static void send_bad_request(FILE *client_fd) {
598 const char error[] = "400 Bad Request";
599 const char msg[] = "Your browser sent an invalid request.";
600 fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, "", error, error, msg);
603 static void send_authentication_required(FILE *client_fd) {
604 const char error[] = "407 Proxy Authentication Required";
605 const char auth[] = "Proxy-Authenticate: Basic realm=\"tlsproxy\"\r\n";
606 const char msg[] = "TODO";
607 fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, auth, error, error, msg);
610 static void send_forwarding_failure(FILE *client_fd) {
611 const char error[] = "503 Forwarding failure";
612 const char msg[] = "Failed to connect to server, check logs.";
613 fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, "", error, error, msg);
616 static void tls_send_invalid_cert_message(gnutls_session_t session) {
617 const char error[] = "500 Internal Server Error";
618 const char msg[] = "Server certificate validation failed, check logs.";
621 ssize_t size_written;
622 char buffer[sizeof(HTTP_RESPONSE_FORMAT)
623 + 3 * sizeof(error) + sizeof(msg)];
625 result = snprintf(buffer, sizeof(buffer), HTTP_RESPONSE_FORMAT,
626 error, "", error, error, msg);
627 assert(result > 0 && (size_t)result < sizeof(buffer));
629 size_written = gnutls_record_send(session, buffer, strlen(buffer));
630 if (size_written < 0) {
631 LOG(WARNING, "tls_send_invalid_cert_message(): "
632 "gnutls_record_send(): %s",
633 gnutls_strerror((int)size_written));
635 /* Just an error message, no need to check if everything was written. */
639 /* Transfer data between client and server sockets until one closes the
641 static void transfer_data(int client, int server) {
642 struct pollfd fds[2];
644 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
647 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
650 LOG(DEBUG2, "transfer_data(): %d -> %d", client, server);
653 int result = poll(fds, 2 /* fd count */, -1 /* no timeout */);
655 LOG_PERROR(ERROR, "transfer_data(): poll()");
659 /* Data available from client. */
660 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
661 if (read_from_write_to(client, server) != 0) {
662 /* EOF (or other error) */
666 /* Data available from server. */
667 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
668 if (read_from_write_to(server, client) != 0) {
669 /* EOF (or other error) */
674 /* Client closed connection. */
675 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
678 /* Server closed connection. */
679 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
685 /* Read available data from socket from and write it to socket to. At maximum
686 * 4096 bytes are read/written. */
687 static int read_from_write_to(int from, int to) {
689 ssize_t size_written;
692 size_read = read(from, buffer, sizeof(buffer));
694 LOG_PERROR(WARNING, "read_from_write_to(): read()");
697 } else if (size_read == 0) {
701 size_written = write(to, buffer, (size_t)size_read);
702 if (size_written < 0) {
703 LOG_PERROR(WARNING, "read_from_write_to(): write()");
706 if (size_read != size_written) {
707 LOG(ERROR, "read_from_write_to(): only written %zu of %zu bytes!",
708 size_written, size_read);
715 /* Transfer data between client and server TLS connection until one closes the
717 static void transfer_data_tls(int client, int server,
718 gnutls_session_t client_session,
719 gnutls_session_t server_session) {
722 struct pollfd fds[2];
724 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
727 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
730 /* Get maximum possible buffer size. */
731 buffer_size = gnutls_record_get_max_size(client_session);
732 if (gnutls_record_get_max_size(server_session) < buffer_size) {
733 buffer_size = gnutls_record_get_max_size(server_session);
735 LOG(DEBUG2, "transfer_data_tls(): suggested buffer size: %zu",
739 int result = poll(fds, 2 /* fd count */, -1 /* no timeout */);
741 LOG_PERROR(ERROR, "transfer_data(): poll()");
745 /* Data available from client. */
746 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
747 if (read_from_write_to_tls(client_session, server_session,
749 /* EOF (or other error) */
753 /* Data available from server. */
754 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
755 if (read_from_write_to_tls(server_session, client_session,
757 /* EOF (or other error) */
762 /* Client closed connection. */
763 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
766 /* Server closed connection. */
767 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
773 /* Read available data from session from and write to session to. */
774 static int read_from_write_to_tls(gnutls_session_t from,
776 size_t buffer_size) {
778 ssize_t size_written;
779 char buffer[16384]; /* GnuTLS default maximum */
781 if (buffer_size > sizeof(buffer)) {
782 LOG(WARNING, "read_from_write_to_tls(): reduced buffer size to %zu",
784 buffer_size = sizeof(buffer);
787 size_read = gnutls_record_recv(from, buffer, buffer_size);
789 /* Allow rehandshakes. As handshakes might be insecure make sure that
790 * %SAFE_RENEGOTIATION is used in GnuTLS's priority string. */
791 if (size_read == GNUTLS_E_REHANDSHAKE) {
794 LOG(DEBUG1, "server requested TLS rehandshake");
796 result = gnutls_handshake(from);
797 if (result != GNUTLS_E_SUCCESS) {
798 LOG(WARNING, "server TLS rehandshake failed: %s",
799 gnutls_strerror(result));
805 LOG(WARNING, "read_from_write_to_tls(): gnutls_record_recv(): %s",
806 gnutls_strerror((int)size_read));
809 } else if (size_read == 0) {
813 size_written = gnutls_record_send(to, buffer, (size_t)size_read);
814 if (size_written < 0) {
815 LOG(WARNING, "read_from_write_to_tls(): gnutls_record_send(): %s",
816 gnutls_strerror((int)size_written));
819 if (size_read != size_written) {
820 LOG(ERROR, "read_from_write_to_tls(): only written %zu of %zu bytes!",
821 size_written, size_read);
829 static int connect_to_host(const char *hostname, const char *port) {
830 struct addrinfo gai_hints;
831 struct addrinfo *gai_result;
835 struct addrinfo *server;
837 if (hostname == NULL || port == NULL) {
841 /* Get IP of hostname server. */
842 memset(&gai_hints, 0, sizeof(gai_hints));
843 gai_hints.ai_family = AF_UNSPEC;
844 gai_hints.ai_socktype = SOCK_STREAM;
845 gai_hints.ai_protocol = 0;
846 gai_hints.ai_flags = AI_NUMERICSERV /* given port is numeric */
848 | AI_ADDRCONFIG /* supported by this computer */
851 gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
852 if (gai_return != 0) {
853 if (gai_return == EAI_SYSTEM) {
854 LOG_PERROR(WARNING, "connect_to_host(): getaddrinfo()");
856 LOG(WARNING, "connect_to_host(): getaddrinfo(): %s",
857 gai_strerror(gai_return));
862 /* Now try to connect to each server returned by getaddrinfo(), use the
863 * first successful connect. */
864 for (server = gai_result; server != NULL; server = server->ai_next) {
865 server_socket = socket(server->ai_family,
867 server->ai_protocol);
868 if (server_socket < 0) {
869 LOG_PERROR(DEBUG1, "connect_to_host(): socket(), trying next");
873 if (connect(server_socket, server->ai_addr, server->ai_addrlen) == 0) {
876 LOG_PERROR(DEBUG1, "connect_to_host(): connect(), trying next");
878 close(server_socket);
880 /* Make sure we free the result from getaddrinfo(). */
881 freeaddrinfo(gai_result);
883 if (server == NULL) {
884 LOG_PERROR(WARNING, "connect_to_host(): no server found, abort");
888 return server_socket;
892 /* Parse HTTP CONNECT request string and save its parameters.
894 * The following format is expected: "CONNECT host:port HTTP/1.x".
896 * request and host must have the same size! port must be at least 6 bytes
899 static int parse_request(const char *request, char *host, char *port,
900 int *version_minor) {
901 int port_unused; /* just used to verify the port is numeric */
904 /* scanf() doesn't check spaces. */
905 if (strncmp(request, "CONNECT ", 8)) {
908 /* Check request and extract data, "host:port" is not yet separated. */
909 if (sscanf(request, "CONNECT %s HTTP/1.%d", host, version_minor) != 2) {
912 /* Make sure ":port" is there. */
913 if ((position = strchr(host, ':')) == NULL) {
916 /* Make sure port is numeric. */
917 if (sscanf(position + 1, "%d", &port_unused) != 1) {
920 /* Store it in *port. */
921 strncpy(port, position + 1, 5);
923 /* And remove port from host. */
930 static void log_session_information(gnutls_session_t session) {
931 /* From doc/examples/ex-session-info.c of GnuTLS 3.2.3's tarball and
932 * modified, thanks. */
935 gnutls_credentials_type_t cred;
936 gnutls_kx_algorithm_t kx;
942 /* Key exchange algorithm. */
943 kx = gnutls_kx_get(session);
944 LOG(DEBUG2, "- key exchange: %s", gnutls_kx_get_name(kx));
946 /* Authentication type. */
947 cred = gnutls_auth_get_type(session);
949 case GNUTLS_CRD_CERTIFICATE:
950 if (kx == GNUTLS_KX_DHE_RSA
951 || kx == GNUTLS_KX_DHE_DSS) {
953 #ifdef GNUTLS_KX_ECDHE_RSA
954 } else if (kx == GNUTLS_KX_ECDHE_RSA
955 || kx == GNUTLS_KX_ECDHE_ECDSA) {
964 case GNUTLS_CRD_ANON:
966 /* This shouldn't occur. */
967 LOG(WARNING, "unexpected authentication method: %d", cred);
971 /* Information about key exchange. */
973 LOG(DEBUG2, "- ephemeral DH using prime of %d bits",
974 gnutls_dh_get_prime_bits(session));
976 #ifdef GNUTLS_KX_ECDHE_RSA
977 LOG(DEBUG2, "- ephemeral ECDH using curve %s",
978 gnutls_ecc_curve_get_name(gnutls_ecc_curve_get(session)));
982 tmp = gnutls_protocol_get_name(gnutls_protocol_get_version(session));
983 LOG(DEBUG2, "- protocol: %s", tmp); /* e.g. TLS 1.0 */
985 tmp = gnutls_certificate_type_get_name(gnutls_certificate_type_get(session));
986 LOG(DEBUG2, "- certificate type: %s", tmp);
988 tmp = gnutls_compression_get_name(gnutls_compression_get(session));
989 LOG(DEBUG2, "- compression: %s", tmp);
991 tmp = gnutls_cipher_get_name(gnutls_cipher_get(session));
992 LOG(DEBUG2, "- cipher: %s", tmp);
994 tmp = gnutls_mac_get_name(gnutls_mac_get(session));
995 LOG(DEBUG2, "- MAC: %s", tmp);