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"
32 /* Maximum length of a HTTP request line. Longer request lines are aborted
33 * with an error. The standard doesn't specify a maximum line length but this
34 * should be a good limit to make processing simpler. As HTTPS is used this
35 * doesn't limit long GET requests. */
36 #define MAX_REQUEST_LINE 4096
38 /* Format string used to send HTTP/1.0 error responses to the client.
40 * %s is used 5 times, first is the error code, then additional headers, next
41 * two are the error code (no %n$s!), the last is the message. */
42 #define HTTP_RESPONSE_FORMAT "HTTP/1.0 %s\r\n\
43 Content-Type: text/html; charset=US-ASCII\r\n\
45 <!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n\
47 <head><title>%s</title></head>\n\
55 static int initialize_tls_session_client(int peer_socket,
57 gnutls_session_t *session,
58 gnutls_certificate_credentials_t *x509_cred);
59 static int initialize_tls_session_server(int peer_socket,
60 gnutls_session_t *session,
61 gnutls_certificate_credentials_t *x509_cred);
62 static int initialize_tls_session_both(int flags,
64 gnutls_session_t *session,
65 gnutls_certificate_credentials_t *x509_cred);
67 static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd);
68 static int read_http_request(FILE *client_fd, char *request, size_t length);
69 static void send_bad_request(FILE *client_fd);
70 static void send_authentication_required(FILE *client_fd);
71 static void send_forwarding_failure(FILE *client_fd);
72 static void tls_send_invalid_cert_message(gnutls_session_t session);
74 static void transfer_data(int client, int server);
75 static int read_from_write_to(int from, int to);
76 static void transfer_data_tls(int client, int server,
77 gnutls_session_t client_session,
78 gnutls_session_t server_session);
79 static int read_from_write_to_tls(gnutls_session_t from, gnutls_session_t to,
82 static int connect_to_host(const char *hostname, const char *port);
84 static int parse_request(const char *buffer, char *host, char *port,
88 void handle_connection(int client_socket) {
90 FILE *client_fd_read, *client_fd_write, *server_fd_read, *server_fd_write;
92 char buffer[MAX_REQUEST_LINE];
93 char host[MAX_REQUEST_LINE];
96 int version_minor; /* x in HTTP/1.x */
99 /* client_x509_cred is used when talking to the client (acting as a TSL
100 * server), server_x509_cred is used when talking to the server (acting as
102 gnutls_certificate_credentials_t client_x509_cred, server_x509_cred;
104 gnutls_session_t client_session, server_session;
105 /* initialize_tls_session_*() called? - used for goto out */
106 int client_session_init, server_session_init;
107 /* gnutls_handshake() called? - used for goto out */
108 int client_session_started, server_session_started;
109 /* Validation failed? If yes we need to send the special "invalid"
111 int validation_failed;
113 LOG(DEBUG1, "new connection");
116 client_fd_read = NULL;
117 client_fd_write = NULL;
118 server_fd_read = NULL;
119 server_fd_write = NULL;
120 client_session_init = 0;
121 server_session_init = 0;
122 client_session_started = 0;
123 server_session_started = 0;
124 validation_failed = 0;
126 if (fdopen_read_write(client_socket, &client_fd_read,
127 &client_fd_write) != 0) {
131 /* Read request line (CONNECT ..) and headers (they are discarded). */
132 result = read_http_request(client_fd_read, buffer, sizeof(buffer));
134 LOG(WARNING, "read_http_request(): client read error");
136 } else if (result == -2) {
137 LOG(WARNING, "read_http_request(): client EOF");
138 send_bad_request(client_fd_write);
140 } else if (result == -3) {
141 LOG(DEBUG1, "read_http_request(): proxy authentication failed");
142 send_authentication_required(client_fd_write);
146 if (parse_request(buffer, host, port, &version_minor) != 0) {
147 LOG(WARNING, "bad request: %s", buffer);
148 send_bad_request(client_fd_write);
152 LOG(DEBUG1, "target: %s:%s (HTTP 1.%d)", host, port, version_minor);
154 /* Connect to proxy server or directly to server. */
155 if (global_proxy_host != NULL && global_proxy_port != NULL) {
156 LOG(DEBUG1, "connecting to %s:%s", global_proxy_host,
158 server_socket = connect_to_host(global_proxy_host, global_proxy_port);
160 LOG(DEBUG1, "connecting to %s:%s", host, port);
161 server_socket = connect_to_host(host, port);
164 if (server_socket < 0) {
165 LOG(WARNING, "failed to connect to server");
166 send_forwarding_failure(client_fd_write);
169 if (fdopen_read_write(server_socket, &server_fd_read,
170 &server_fd_write) != 0) {
171 send_forwarding_failure(client_fd_write);
175 /* Connect to proxy if requested (command line option). */
176 if (global_proxy_host != NULL && global_proxy_port != NULL) {
177 fprintf(server_fd_write, "CONNECT %s:%s HTTP/1.0\r\n", host, port);
178 fprintf(server_fd_write, "\r\n");
179 fflush(server_fd_write);
181 /* Read response line from proxy server. */
182 result = read_http_request(server_fd_read, buffer, sizeof(buffer));
184 LOG(WARNING, "read_http_request(): proxy read error");
185 send_forwarding_failure(client_fd_write);
187 } else if (result == -2) {
188 LOG(WARNING, "read_http_request(): proxy EOF");
189 send_forwarding_failure(client_fd_write);
193 /* Check response of proxy server. */
194 if (strncmp(buffer, "HTTP/1.0 200", 12)) {
195 LOG(WARNING, "bad proxy response: %s", buffer);
196 send_forwarding_failure(client_fd_write);
201 LOG(DEBUG1, "connection to server established");
203 /* If the -u option is used and we don't know this hostname's server
204 * certificate then just pass through the connection and let the client
205 * verify the server certificate. */
206 if (global_passthrough_unknown) {
207 char path[TLSPROXY_MAX_PATH_LENGTH];
210 if (server_certificate_file(&file, host, path, sizeof(path)) == -2) {
211 /* We've established a connection, tell the client. */
212 fprintf(client_fd_write, "HTTP/1.0 200 Connection established\r\n");
213 fprintf(client_fd_write, "\r\n");
214 fflush(client_fd_write);
216 LOG(DEBUG1, "transferring data");
218 /* Proxy data between client and server until one side is done
220 transfer_data(client_socket, server_socket);
222 LOG(DEBUG1, "finished transferring data");
226 /* server_certificate_file() may have opened the file, close it. */
232 /* Initialize TLS client credentials to talk to the server. */
233 result = initialize_tls_session_server(server_socket, &server_session,
236 LOG(WARNING, "initialize_tls_session_server() failed");
237 send_forwarding_failure(client_fd_write);
240 server_session_init = 1;
242 LOG(DEBUG1, "starting server TLS handshake");
244 /* Try to establish TLS handshake between us and server. */
245 result = gnutls_handshake(server_session);
246 if (result != GNUTLS_E_SUCCESS) {
247 LOG(WARNING, "server TLS handshake failed: %s",
248 gnutls_strerror(result));
249 send_forwarding_failure(client_fd_write);
252 server_session_started = 1;
254 LOG(DEBUG1, "server TLS handshake finished");
256 /* Make sure the server certificate is valid and known. */
257 if (verify_tls_connection(server_session, host) != 0) {
258 LOG(ERROR, "server certificate validation failed!");
259 /* We'll send the error message over our TLS connection to the client,
260 * but with an invalid certificate. No data is transfered from/to the
262 validation_failed = 1;
265 /* Initialize TLS server credentials to talk to the client. */
266 result = initialize_tls_session_client(client_socket,
267 /* use a special host if the server
268 * certificate was invalid */
269 (validation_failed) ? "invalid"
274 LOG(WARNING, "initialize_tls_session_client() failed");
275 send_forwarding_failure(client_fd_write);
278 client_session_init = 1;
280 /* We've established a connection, tell the client. */
281 fprintf(client_fd_write, "HTTP/1.0 200 Connection established\r\n");
282 fprintf(client_fd_write, "\r\n");
283 fflush(client_fd_write);
285 LOG(DEBUG1, "starting client TLS handshake");
287 /* Try to establish TLS handshake between client and us. */
288 result = gnutls_handshake(client_session);
289 if (result != GNUTLS_E_SUCCESS) {
290 LOG(WARNING, "client TLS handshake failed: %s",
291 gnutls_strerror(result));
292 send_forwarding_failure(client_fd_write);
295 client_session_started = 1;
297 LOG(DEBUG1, "client TLS handshake finished");
299 /* Tell the client that the verification failed. Shouldn't be necessary as
300 * the client should terminate the connection because he received the
301 * invalid certificate but better be sure. */
302 if (validation_failed) {
303 tls_send_invalid_cert_message(client_session);
307 LOG(DEBUG1, "transferring TLS data");
309 /* Proxy data between client and server until one side is done (EOF or
311 transfer_data_tls(client_socket, server_socket,
312 client_session, server_session);
314 LOG(DEBUG1, "finished transferring TLS data");
317 /* Close TLS sessions if necessary. Use GNUTLS_SHUT_RDWR so the data is
318 * reliable transmitted. */
319 if (server_session_started) {
320 /* Recent gnutls-serv (used in the test-suite) won't terminate the
321 * connection when gnutls_bye(session, GNUTLS_SHUT_RDWR) is used
322 * before any other data was received. If the validation failed just
323 * close the connection without waiting for data, we won't read it
326 * For verified connections GNUTLS_SHUT_RDWR is important or we might
328 gnutls_bye(server_session, validation_failed ? GNUTLS_SHUT_WR
331 if (client_session_started) {
332 gnutls_bye(client_session, GNUTLS_SHUT_RDWR);
334 if (server_session_init) {
335 gnutls_deinit(server_session);
336 gnutls_certificate_free_credentials(server_x509_cred);
338 if (client_session_init) {
339 gnutls_deinit(client_session);
340 gnutls_certificate_free_cas(client_x509_cred);
341 gnutls_certificate_free_keys(client_x509_cred);
342 gnutls_certificate_free_credentials(client_x509_cred);
345 /* Close connection to server/proxy. */
346 if (server_fd_read != NULL) {
347 if (server_fd_write != NULL) {
348 fclose(server_fd_write);
350 fclose(server_fd_read);
351 } else if (server_socket != -1) {
352 close(server_socket);
354 LOG(DEBUG1, "connection to server closed");
355 /* Close connection to client. */
356 if (client_fd_read != NULL) {
357 if (client_fd_write != NULL) {
358 fclose(client_fd_write);
360 fclose(client_fd_read);
362 close(client_socket);
364 LOG(DEBUG1, "connection to client closed");
366 LOG(DEBUG1, "connection finished");
370 static int initialize_tls_session_client(int peer_socket,
371 const char *hostname,
372 gnutls_session_t *session,
373 gnutls_certificate_credentials_t *x509_cred) {
375 int use_invalid_cert;
376 char path[TLSPROXY_MAX_PATH_LENGTH];
378 /* The "invalid" hostname is special. If it's used we send an invalid
379 * certificate to let the client know something is wrong. */
380 use_invalid_cert = (!strcmp(hostname, "invalid"));
382 if (proxy_certificate_path(hostname, path, sizeof(path)) != 0) {
384 "initialize_tls_session_client(): "
385 "failed to get proxy certificate path");
389 result = gnutls_certificate_allocate_credentials(x509_cred);
390 if (result != GNUTLS_E_SUCCESS) {
392 "initialize_tls_session_client(): "
393 "gnutls_certificate_allocate_credentials(): %s",
394 gnutls_strerror(result));
398 /* Load proxy CA file, this CA "list" is send to the client. */
399 if (!use_invalid_cert) {
400 result = gnutls_certificate_set_x509_trust_file(*x509_cred,
402 GNUTLS_X509_FMT_PEM);
405 "initialize_tls_session_client(): can't read CA file: '%s'",
407 gnutls_certificate_free_credentials(*x509_cred);
411 /* If the invalid hostname was specified do nothing, we use a self-signed
412 * certificate in this case. */
414 /* And certificate for this website and proxy's private key. */
415 if (!use_invalid_cert) {
416 result = gnutls_certificate_set_x509_key_file(*x509_cred,
419 GNUTLS_X509_FMT_PEM);
420 /* If the invalid hostname was specified load our special "invalid"
423 result = gnutls_certificate_set_x509_key_file(*x509_cred,
424 PROXY_INVALID_CERT_PATH,
426 GNUTLS_X509_FMT_PEM);
428 if (result != GNUTLS_E_SUCCESS) {
430 "initialize_tls_session_client(): "
431 "can't read server certificate ('%s') or key file ('%s'): %s",
432 path, PROXY_KEY_PATH, gnutls_strerror(result));
433 gnutls_certificate_free_credentials(*x509_cred);
434 /* Could be a missing certificate. */
438 gnutls_certificate_set_dh_params(*x509_cred, global_tls_dh_params);
440 return initialize_tls_session_both(GNUTLS_SERVER,
441 peer_socket, session, x509_cred);
443 static int initialize_tls_session_server(int peer_socket,
444 gnutls_session_t *session,
445 gnutls_certificate_credentials_t *x509_cred) {
448 result = gnutls_certificate_allocate_credentials(x509_cred);
449 if (result != GNUTLS_E_SUCCESS) {
451 "initialize_tls_session_server(): "
452 "gnutls_certificate_allocate_credentials(): %s",
453 gnutls_strerror(result));
457 return initialize_tls_session_both(GNUTLS_CLIENT,
458 peer_socket, session, x509_cred);
460 static int initialize_tls_session_both(int flags,
462 gnutls_session_t *session,
463 gnutls_certificate_credentials_t *x509_cred) {
466 result = gnutls_init(session, flags);
467 if (result != GNUTLS_E_SUCCESS) {
469 "initialize_tls_session_both(): gnutls_init(): %s",
470 gnutls_strerror(result));
471 gnutls_certificate_free_credentials(*x509_cred);
474 result = gnutls_priority_set(*session, global_tls_priority_cache);
475 if (result != GNUTLS_E_SUCCESS) {
477 "initialize_tls_session_both(): gnutls_priority_set(): %s",
478 gnutls_strerror(result));
479 gnutls_deinit(*session);
480 gnutls_certificate_free_credentials(*x509_cred);
483 result = gnutls_credentials_set(*session,
484 GNUTLS_CRD_CERTIFICATE, *x509_cred);
485 if (result != GNUTLS_E_SUCCESS) {
487 "initialize_tls_session_both(): gnutls_credentials_set(): %s",
488 gnutls_strerror(result));
489 gnutls_deinit(*session);
490 gnutls_certificate_free_credentials(*x509_cred);
494 #ifdef HAVE_GNUTLS_TRANSPORT_SET_INT2
495 /* gnutls_transport_set_int() is a macro. */
496 gnutls_transport_set_int(*session, peer_socket);
498 gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
505 static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd) {
506 *read_fd = fdopen(socket, "r");
507 if (*read_fd == NULL) {
508 LOG_PERROR(WARNING, "fdopen_read_write(): fdopen(\"r\") failed");
512 *write_fd = fdopen(dup(socket), "w");
513 if (*write_fd == NULL) {
514 LOG_PERROR(WARNING, "fdopen_read_write(): fdopen(\"w\") failed");
516 *read_fd = NULL; /* "tell" caller read_fd is already closed */
523 /* Read HTTP request line and headers (ignored).
525 * On success 0 is returned, -1 on client error, -2 on unexpected EOF.
527 static int read_http_request(FILE *client_fd, char *request, size_t length) {
528 char buffer[MAX_REQUEST_LINE];
529 int found_proxy_authorization;
531 assert(length <= INT_MAX);
532 if (fgets(request, (int)length, client_fd) == NULL) {
533 if (ferror(client_fd)) {
534 LOG_PERROR(WARNING, "read_http_request(): fgets()");
541 found_proxy_authorization = 0;
542 while (fgets(buffer, sizeof(buffer), client_fd) != NULL) {
543 const char *authentication = "Proxy-Authorization: Basic ";
545 if (global_http_digest_authorization != NULL
546 && !strncmp(buffer, authentication, strlen(authentication))) {
547 found_proxy_authorization = 1;
549 /* Check if the passphrase matches. */
550 strtok(buffer, "\r\n");
551 if (strcmp(buffer + strlen(authentication),
552 global_http_digest_authorization)) {
558 if (!strcmp(buffer, "\n") || !strcmp(buffer, "\r\n")) {
562 if (ferror(client_fd)) {
563 LOG_PERROR(WARNING, "read_http_request(): fgets()");
567 if (global_http_digest_authorization != NULL && !found_proxy_authorization) {
574 static void send_bad_request(FILE *client_fd) {
575 const char error[] = "400 Bad Request";
576 const char msg[] = "Your browser sent an invalid request.";
577 fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, "", error, error, msg);
580 static void send_authentication_required(FILE *client_fd) {
581 const char error[] = "407 Proxy Authentication Required";
582 const char auth[] = "Proxy-Authenticate: Basic realm=\"tlsproxy\"\r\n";
583 const char msg[] = "TODO";
584 fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, auth, error, error, msg);
587 static void send_forwarding_failure(FILE *client_fd) {
588 const char error[] = "503 Forwarding failure";
589 const char msg[] = "Failed to connect to server, check logs.";
590 fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, "", error, error, msg);
593 static void tls_send_invalid_cert_message(gnutls_session_t session) {
594 const char error[] = "500 Internal Server Error";
595 const char msg[] = "Server certificate validation failed, check logs.";
598 char buffer[sizeof(HTTP_RESPONSE_FORMAT)
599 + 3 * sizeof(error) + sizeof(msg)];
601 result = snprintf(buffer, sizeof(buffer), HTTP_RESPONSE_FORMAT,
602 error, "", error, error, msg);
603 assert(result > 0 && (size_t)result < sizeof(buffer));
605 gnutls_record_send(session, buffer, strlen(buffer));
609 /* Transfer data between client and server sockets until one closes the
611 static void transfer_data(int client, int server) {
612 struct pollfd fds[2];
614 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
617 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
620 LOG(DEBUG1, "transfer_data(): %d -> %d", client, server);
623 int result = poll(fds, 2 /* fd count */, -1 /* no timeout */);
625 LOG_PERROR(ERROR, "transfer_data(): poll()");
629 /* Data available from client. */
630 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
631 if (read_from_write_to(client, server) != 0) {
632 /* EOF (or other error) */
636 /* Data available from server. */
637 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
638 if (read_from_write_to(server, client) != 0) {
639 /* EOF (or other error) */
644 /* Client closed connection. */
645 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
648 /* Server closed connection. */
649 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
655 /* Read available data from socket from and write it to socket to. At maximum
656 * 4096 bytes are read/written. */
657 static int read_from_write_to(int from, int to) {
659 ssize_t size_written;
662 size_read = read(from, buffer, sizeof(buffer));
664 LOG_PERROR(WARNING, "read_from_write_to(): read()");
667 } else if (size_read == 0) {
671 size_written = write(to, buffer, (size_t)size_read);
672 if (size_written < 0) {
673 LOG_PERROR(WARNING, "read_from_write_to(): write()");
676 if (size_read != size_written) {
677 LOG(ERROR, "read_from_write_to(): only written %ld of %ld bytes!",
678 (long int)size_written, (long int)size_read);
685 /* Transfer data between client and server TLS connection until one closes the
687 static void transfer_data_tls(int client, int server,
688 gnutls_session_t client_session,
689 gnutls_session_t server_session) {
692 struct pollfd fds[2];
694 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
697 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
700 /* Get maximum possible buffer size. */
701 buffer_size = gnutls_record_get_max_size(client_session);
702 if (gnutls_record_get_max_size(server_session) < buffer_size) {
703 buffer_size = gnutls_record_get_max_size(server_session);
705 LOG(DEBUG1, "transfer_data_tls(): suggested buffer size: %ld",
706 (long int)buffer_size);
709 int result = poll(fds, 2 /* fd count */, -1 /* no timeout */);
711 LOG_PERROR(ERROR, "transfer_data(): poll()");
715 /* Data available from client. */
716 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
717 if (read_from_write_to_tls(client_session, server_session,
719 /* EOF (or other error) */
723 /* Data available from server. */
724 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
725 if (read_from_write_to_tls(server_session, client_session,
727 /* EOF (or other error) */
732 /* Client closed connection. */
733 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
736 /* Server closed connection. */
737 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
743 /* Read available data from session from and write to session to. */
744 static int read_from_write_to_tls(gnutls_session_t from,
746 size_t buffer_size) {
748 ssize_t size_written;
751 if (buffer_size > sizeof(buffer)) {
752 LOG(WARNING, "read_from_write_to_tls(): reduced buffer size to %ld",
753 (long int)(sizeof(buffer)));
754 buffer_size = sizeof(buffer);
757 size_read = gnutls_record_recv(from, buffer, buffer_size);
759 LOG(WARNING, "read_from_write_to_tls(): gnutls_record_recv(): %s",
760 gnutls_strerror((int)size_read));
763 } else if (size_read == 0) {
767 size_written = gnutls_record_send(to, buffer, (size_t)size_read);
768 if (size_written < 0) {
769 LOG(WARNING, "read_from_write_to_tls(): gnutls_record_send(): %s",
770 gnutls_strerror((int)size_written));
773 if (size_read != size_written) {
774 LOG(ERROR, "read_from_write_to_tls(): only written %ld of %ld bytes!",
775 (long int)size_written, (long int)size_read);
783 static int connect_to_host(const char *hostname, const char *port) {
784 struct addrinfo gai_hints;
785 struct addrinfo *gai_result;
789 struct addrinfo *server;
791 if (hostname == NULL || port == NULL) {
795 /* Get IP of hostname server. */
796 memset(&gai_hints, 0, sizeof(gai_hints));
797 gai_hints.ai_family = AF_UNSPEC;
798 gai_hints.ai_socktype = SOCK_STREAM;
799 gai_hints.ai_protocol = 0;
800 gai_hints.ai_flags = AI_NUMERICSERV /* given port is numeric */
801 | AI_ADDRCONFIG /* supported by this computer */
802 | AI_V4MAPPED; /* support IPv4 through IPv6 */
803 gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
804 if (gai_return != 0) {
805 if (gai_return == EAI_SYSTEM) {
806 LOG_PERROR(WARNING, "connect_to_host(): getaddrinfo()");
808 LOG(WARNING, "connect_to_host(): getaddrinfo(): %s",
809 gai_strerror(gai_return));
814 /* Now try to connect to each server returned by getaddrinfo(), use the
815 * first successful connect. */
816 for (server = gai_result; server != NULL; server = server->ai_next) {
817 server_socket = socket(server->ai_family,
819 server->ai_protocol);
820 if (server_socket < 0) {
821 LOG_PERROR(DEBUG1, "connect_to_host(): socket(), trying next");
825 if (connect(server_socket, server->ai_addr, server->ai_addrlen) == 0) {
828 LOG_PERROR(DEBUG1, "connect_to_host(): connect(), trying next");
830 close(server_socket);
832 /* Make sure we free the result from getaddrinfo(). */
833 freeaddrinfo(gai_result);
835 if (server == NULL) {
836 LOG_PERROR(WARNING, "connect_to_host(): no server found, abort");
840 return server_socket;
844 /* Parse HTTP CONNECT request string and save its parameters.
846 * The following format is expected: "CONNECT host:port HTTP/1.x".
848 * request and host must have the same size! port must be at least 6 bytes
851 static int parse_request(const char *request, char *host, char *port,
852 int *version_minor) {
853 int port_unused; /* just used to verify the port is numeric */
856 /* scanf() doesn't check spaces. */
857 if (strncmp(request, "CONNECT ", 8)) {
860 /* Check request and extract data, "host:port" is not yet separated. */
861 if (sscanf(request, "CONNECT %s HTTP/1.%d", host, version_minor) != 2) {
864 /* Make sure ":port" is there. */
865 if ((position = strchr(host, ':')) == NULL) {
868 /* Make sure port is numeric. */
869 if (sscanf(position + 1, "%d", &port_unused) != 1) {
872 /* Store it in *port. */
873 strncpy(port, position + 1, 5);
875 /* And remove port from host. */