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);
63 static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd);
64 static int read_http_request(FILE *client_fd, char *request, size_t length);
65 static void send_bad_request(FILE *client_fd);
66 static void send_authentication_required(FILE *client_fd);
67 static void send_forwarding_failure(FILE *client_fd);
68 static void tls_send_invalid_cert_message(gnutls_session_t session);
70 static void transfer_data(int client, int server);
71 static int read_from_write_to(int from, int to);
72 static void transfer_data_tls(int client, int server,
73 gnutls_session_t client_session,
74 gnutls_session_t server_session);
75 static int read_from_write_to_tls(gnutls_session_t from, gnutls_session_t to,
78 static int connect_to_host(const char *hostname, const char *port);
80 static int parse_request(const char *buffer, char *host, char *port,
84 void handle_connection(int client_socket) {
86 FILE *client_fd_read, *client_fd_write, *server_fd_read, *server_fd_write;
88 char buffer[MAX_REQUEST_LINE];
89 char host[MAX_REQUEST_LINE];
92 int version_minor; /* x in HTTP/1.x */
95 /* client_x509_cred is used when talking to the client (acting as a TSL
96 * server), server_x509_cred is used when talking to the server (acting as
98 gnutls_certificate_credentials_t client_x509_cred, server_x509_cred;
100 gnutls_session_t client_session, server_session;
101 /* initialize_tls_session_*() called? - used for goto out */
102 int client_session_init, server_session_init;
103 /* gnutls_handshake() called? - used for goto out */
104 int client_session_started, server_session_started;
105 /* Validation failed? If yes we need to send the special "invalid"
107 int validation_failed;
109 LOG(DEBUG, "new connection");
112 client_fd_read = NULL;
113 client_fd_write = NULL;
114 server_fd_read = NULL;
115 server_fd_write = NULL;
116 client_session_init = 0;
117 server_session_init = 0;
118 client_session_started = 0;
119 server_session_started = 0;
120 validation_failed = 0;
122 if (fdopen_read_write(client_socket, &client_fd_read,
123 &client_fd_write) != 0) {
127 /* Read request line (CONNECT ..) and headers (they are discarded). */
128 result = read_http_request(client_fd_read, buffer, sizeof(buffer));
130 LOG(WARNING, "read_http_request(): client read error");
132 } else if (result == -2) {
133 LOG(WARNING, "read_http_request(): client EOF");
134 send_bad_request(client_fd_write);
136 } else if (result == -3) {
137 LOG(DEBUG, "read_http_request(): proxy authentication failed");
138 send_authentication_required(client_fd_write);
142 if (parse_request(buffer, host, port, &version_minor) != 0) {
143 LOG(WARNING, "bad request: %s", buffer);
144 send_bad_request(client_fd_write);
148 LOG(DEBUG, "target: %s:%s (HTTP 1.%d)", host, port, version_minor);
150 /* Connect to proxy server or directly to server. */
151 if (global_proxy_host != NULL && global_proxy_port != NULL) {
152 LOG(DEBUG, "connecting to %s:%s", global_proxy_host,
154 server_socket = connect_to_host(global_proxy_host, global_proxy_port);
156 LOG(DEBUG, "connecting to %s:%s", host, port);
157 server_socket = connect_to_host(host, port);
160 if (server_socket < 0) {
161 LOG(WARNING, "failed to connect to server");
162 send_forwarding_failure(client_fd_write);
165 if (fdopen_read_write(server_socket, &server_fd_read,
166 &server_fd_write) != 0) {
167 send_forwarding_failure(client_fd_write);
171 /* Connect to proxy if requested (command line option). */
172 if (global_proxy_host != NULL && global_proxy_port != NULL) {
173 fprintf(server_fd_write, "CONNECT %s:%s HTTP/1.0\r\n", host, port);
174 fprintf(server_fd_write, "\r\n");
175 fflush(server_fd_write);
177 /* Read response line from proxy server. */
178 result = read_http_request(server_fd_read, buffer, sizeof(buffer));
180 LOG(WARNING, "read_http_request(): proxy read error");
181 send_forwarding_failure(client_fd_write);
183 } else if (result == -2) {
184 LOG(WARNING, "read_http_request(): proxy EOF");
185 send_forwarding_failure(client_fd_write);
189 /* Check response of proxy server. */
190 if (strncmp(buffer, "HTTP/1.0 200", 12)) {
191 LOG(WARNING, "bad proxy response: %s", buffer);
192 send_forwarding_failure(client_fd_write);
197 LOG(DEBUG, "connection to server established");
199 /* If the -u option is used and we don't know this hostname's server
200 * certificate then just pass through the connection and let the client
201 * verify the server certificate. */
202 if (global_passthrough_unknown) {
203 char path[TLSPROXY_MAX_PATH_LENGTH];
206 if (server_certificate_file(&file, host, path, sizeof(path)) == -2) {
207 /* We've established a connection, tell the client. */
208 fprintf(client_fd_write, "HTTP/1.0 200 Connection established\r\n");
209 fprintf(client_fd_write, "\r\n");
210 fflush(client_fd_write);
212 LOG(DEBUG, "transferring data");
214 /* Proxy data between client and server until one side is done
216 transfer_data(client_socket, server_socket);
218 LOG(DEBUG, "finished transferring data");
222 /* server_certificate_file() may have opened the file, close it. */
228 /* Initialize TLS client credentials to talk to the server. */
229 result = initialize_tls_session_server(server_socket, &server_session,
232 LOG(WARNING, "initialize_tls_session_server() failed");
233 send_forwarding_failure(client_fd_write);
236 server_session_init = 1;
238 LOG(DEBUG, "starting server TLS handshake");
240 /* Try to establish TLS handshake between us and server. */
241 result = gnutls_handshake(server_session);
242 if (result != GNUTLS_E_SUCCESS) {
243 LOG(WARNING, "server TLS handshake failed: %s",
244 gnutls_strerror(result));
245 send_forwarding_failure(client_fd_write);
248 server_session_started = 1;
250 LOG(DEBUG, "server TLS handshake finished");
252 /* Make sure the server certificate is valid and known. */
253 if (verify_tls_connection(server_session, host) != 0) {
254 LOG(ERROR, "server certificate validation failed!");
255 /* We'll send the error message over our TLS connection to the client,
256 * but with an invalid certificate. No data is transfered from/to the
258 validation_failed = 1;
261 /* Initialize TLS server credentials to talk to the client. */
262 result = initialize_tls_session_client(client_socket,
263 /* use a special host if the server
264 * certificate was invalid */
265 (validation_failed) ? "invalid"
270 LOG(WARNING, "initialize_tls_session_client() failed");
271 send_forwarding_failure(client_fd_write);
274 client_session_init = 1;
276 /* We've established a connection, tell the client. */
277 fprintf(client_fd_write, "HTTP/1.0 200 Connection established\r\n");
278 fprintf(client_fd_write, "\r\n");
279 fflush(client_fd_write);
281 LOG(DEBUG, "starting client TLS handshake");
283 /* Try to establish TLS handshake between client and us. */
284 result = gnutls_handshake(client_session);
285 if (result != GNUTLS_E_SUCCESS) {
286 LOG(WARNING, "client TLS handshake failed: %s",
287 gnutls_strerror(result));
288 send_forwarding_failure(client_fd_write);
291 client_session_started = 1;
293 LOG(DEBUG, "client TLS handshake finished");
295 /* Tell the client that the verification failed. Shouldn't be necessary as
296 * the client should terminate the connection because he received the
297 * invalid certificate but better be sure. */
298 if (validation_failed) {
299 tls_send_invalid_cert_message(client_session);
303 LOG(DEBUG, "transferring TLS data");
305 /* Proxy data between client and server until one side is done (EOF or
307 transfer_data_tls(client_socket, server_socket,
308 client_session, server_session);
310 LOG(DEBUG, "finished transferring TLS data");
313 /* Close TLS sessions if necessary. Use GNUTLS_SHUT_RDWR so the data is
314 * reliable transmitted. */
315 if (server_session_started) {
316 gnutls_bye(server_session, GNUTLS_SHUT_RDWR);
318 if (client_session_started) {
319 gnutls_bye(client_session, GNUTLS_SHUT_RDWR);
321 if (server_session_init) {
322 gnutls_deinit(server_session);
323 gnutls_certificate_free_credentials(server_x509_cred);
325 if (client_session_init) {
326 gnutls_deinit(client_session);
327 gnutls_certificate_free_cas(client_x509_cred);
328 gnutls_certificate_free_keys(client_x509_cred);
329 gnutls_certificate_free_credentials(client_x509_cred);
332 /* Close connection to server/proxy. */
333 if (server_fd_read != NULL) {
334 if (server_fd_write != NULL) {
335 fclose(server_fd_write);
337 fclose(server_fd_read);
338 } else if (server_socket != -1) {
339 close(server_socket);
341 LOG(DEBUG, "connection to server closed");
342 /* Close connection to client. */
343 if (client_fd_read != NULL) {
344 if (client_fd_write != NULL) {
345 fclose(client_fd_write);
347 fclose(client_fd_read);
349 close(client_socket);
351 LOG(DEBUG, "connection to client closed");
353 LOG(DEBUG, "connection finished");
357 static int initialize_tls_session_client(int peer_socket,
358 const char *hostname,
359 gnutls_session_t *session,
360 gnutls_certificate_credentials_t *x509_cred) {
362 int use_invalid_cert;
363 char path[TLSPROXY_MAX_PATH_LENGTH];
365 /* The "invalid" hostname is special. If it's used we send an invalid
366 * certificate to let the client know something is wrong. */
367 use_invalid_cert = (!strcmp(hostname, "invalid"));
369 if (proxy_certificate_path(hostname, path, sizeof(path)) != 0) {
371 "initialize_tls_session_client(): "
372 "failed to get proxy certificate path");
376 result = gnutls_certificate_allocate_credentials(x509_cred);
377 if (result != GNUTLS_E_SUCCESS) {
379 "initialize_tls_session_client(): "
380 "gnutls_certificate_allocate_credentials(): %s",
381 gnutls_strerror(result));
385 /* Load proxy CA file, this CA "list" is send to the client. */
386 if (!use_invalid_cert) {
387 result = gnutls_certificate_set_x509_trust_file(*x509_cred,
389 GNUTLS_X509_FMT_PEM);
392 "initialize_tls_session_client(): can't read CA file: '%s'",
394 gnutls_certificate_free_credentials(*x509_cred);
398 /* If the invalid hostname was specified do nothing, we use a self-signed
399 * certificate in this case. */
401 /* And certificate for this website and proxy's private key. */
402 if (!use_invalid_cert) {
403 result = gnutls_certificate_set_x509_key_file(*x509_cred,
404 path, PROXY_KEY_FILE,
405 GNUTLS_X509_FMT_PEM);
406 /* If the invalid hostname was specified load our special "invalid"
409 result = gnutls_certificate_set_x509_key_file(*x509_cred,
410 PROXY_INVALID_CERT_FILE,
412 GNUTLS_X509_FMT_PEM);
414 if (result != GNUTLS_E_SUCCESS) {
416 "initialize_tls_session_client(): "
417 "can't read server certificate ('%s') or key file ('%s'): %s",
418 path, PROXY_KEY_FILE, gnutls_strerror(result));
419 gnutls_certificate_free_credentials(*x509_cred);
420 /* Could be a missing certificate. */
424 gnutls_certificate_set_dh_params(*x509_cred, global_tls_dh_params);
426 result = gnutls_init(session, GNUTLS_SERVER);
427 if (result != GNUTLS_E_SUCCESS) {
429 "initialize_tls_session_client(): gnutls_init(): %s",
430 gnutls_strerror(result));
431 gnutls_certificate_free_credentials(*x509_cred);
434 result = gnutls_priority_set(*session, global_tls_priority_cache);
435 if (result != GNUTLS_E_SUCCESS) {
437 "initialize_tls_session_client(): gnutls_priority_set(): %s",
438 gnutls_strerror(result));
439 gnutls_deinit(*session);
440 gnutls_certificate_free_credentials(*x509_cred);
443 result = gnutls_credentials_set(*session,
444 GNUTLS_CRD_CERTIFICATE, *x509_cred);
445 if (result != GNUTLS_E_SUCCESS) {
447 "initialize_tls_session_client(): gnutls_credentials_set(): %s",
448 gnutls_strerror(result));
449 gnutls_deinit(*session);
450 gnutls_certificate_free_credentials(*x509_cred);
454 gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
458 static int initialize_tls_session_server(int peer_socket,
459 gnutls_session_t *session,
460 gnutls_certificate_credentials_t *x509_cred) {
463 result = gnutls_certificate_allocate_credentials(x509_cred);
464 if (result != GNUTLS_E_SUCCESS) {
466 "initialize_tls_session_server(): "
467 "gnutls_certificate_allocate_credentials(): %s",
468 gnutls_strerror(result));
472 result = gnutls_init(session, GNUTLS_CLIENT);
473 if (result != GNUTLS_E_SUCCESS) {
475 "initialize_tls_session_server(): gnutls_init(): %s",
476 gnutls_strerror(result));
477 gnutls_certificate_free_credentials(*x509_cred);
480 result = gnutls_priority_set(*session, global_tls_priority_cache);
481 if (result != GNUTLS_E_SUCCESS) {
483 "initialize_tls_session_server(): gnutls_priority_set(): %s",
484 gnutls_strerror(result));
485 gnutls_deinit(*session);
486 gnutls_certificate_free_credentials(*x509_cred);
489 result = gnutls_credentials_set(*session,
490 GNUTLS_CRD_CERTIFICATE, *x509_cred);
491 if (result != GNUTLS_E_SUCCESS) {
493 "initialize_tls_session_server(): gnutls_credentials_set(): %s",
494 gnutls_strerror(result));
495 gnutls_deinit(*session);
496 gnutls_certificate_free_credentials(*x509_cred);
500 gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
506 static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd) {
507 *read_fd = fdopen(socket, "r");
508 if (*read_fd == NULL) {
509 LOG_PERROR(WARNING, "fdopen_read_write(): fdopen(\"r\") failed");
513 *write_fd = fdopen(dup(socket), "w");
514 if (*write_fd == NULL) {
515 LOG_PERROR(WARNING, "fdopen_read_write(): fdopen(\"w\") failed");
517 *read_fd = NULL; /* "tell" caller read_fd is already closed */
524 /* Read HTTP request line and headers (ignored).
526 * On success 0 is returned, -1 on client error, -2 on unexpected EOF.
528 static int read_http_request(FILE *client_fd, char *request, size_t length) {
529 char buffer[MAX_REQUEST_LINE];
530 int found_proxy_authorization;
532 assert(length <= INT_MAX);
533 if (fgets(request, (int)length, client_fd) == NULL) {
534 if (ferror(client_fd)) {
535 LOG_PERROR(WARNING, "read_http_request(): fgets()");
542 found_proxy_authorization = 0;
543 while (fgets(buffer, sizeof(buffer), client_fd) != NULL) {
544 const char *authentication = "Proxy-Authorization: Basic ";
546 if (http_digest_authorization != NULL
547 && !strncmp(buffer, authentication, strlen(authentication))) {
548 found_proxy_authorization = 1;
550 /* Check if the passphrase matches. */
551 strtok(buffer, "\r\n");
552 if (strcmp(buffer + strlen(authentication),
553 http_digest_authorization)) {
559 if (!strcmp(buffer, "\n") || !strcmp(buffer, "\r\n")) {
563 if (ferror(client_fd)) {
564 LOG_PERROR(WARNING, "read_http_request(): fgets()");
568 if (http_digest_authorization != NULL && !found_proxy_authorization) {
575 static void send_bad_request(FILE *client_fd) {
576 const char error[] = "400 Bad Request";
577 const char msg[] = "Your browser sent an invalid request.";
578 fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, "", error, error, msg);
581 static void send_authentication_required(FILE *client_fd) {
582 const char error[] = "407 Proxy Authentication Required";
583 const char auth[] = "Proxy-Authenticate: Basic realm=\"tlsproxy\"\r\n";
584 const char msg[] = "TODO";
585 fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, auth, error, error, msg);
588 static void send_forwarding_failure(FILE *client_fd) {
589 const char error[] = "503 Forwarding failure";
590 const char msg[] = "Failed to connect to server, check logs.";
591 fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, "", error, error, msg);
594 static void tls_send_invalid_cert_message(gnutls_session_t session) {
595 const char error[] = "500 Internal Server Error";
596 const char msg[] = "Server certificate validation failed, check logs.";
599 char buffer[sizeof(HTTP_RESPONSE_FORMAT)
600 + 3 * sizeof(error) + sizeof(msg)];
602 result = snprintf(buffer, sizeof(buffer), HTTP_RESPONSE_FORMAT,
603 error, "", error, error, msg);
604 assert(result > 0 && (size_t)result < sizeof(buffer));
606 gnutls_record_send(session, buffer, strlen(buffer));
610 /* Transfer data between client and server sockets until one closes the
612 static void transfer_data(int client, int server) {
613 struct pollfd fds[2];
615 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
618 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
622 int result = poll(fds, 2 /* fd count */, -1 /* no timeout */);
624 LOG_PERROR(ERROR, "transfer_data(): poll()");
628 /* Data available from client. */
629 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
630 if (read_from_write_to(client, server) != 0) {
631 /* EOF (or other error) */
635 /* Data available from server. */
636 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
637 if (read_from_write_to(server, client) != 0) {
638 /* EOF (or other error) */
643 /* Client closed connection. */
644 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
647 /* Server closed connection. */
648 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
654 /* Read available data from socket from and write it to socket to. At maximum
655 * 4096 bytes are read/written. */
656 static int read_from_write_to(int from, int to) {
658 ssize_t size_written;
661 LOG(DEBUG, "read_from_write_to(): %d -> %d", from, to);
663 size_read = read(from, buffer, sizeof(buffer));
665 LOG_PERROR(WARNING, "read_from_write_to(): read()");
668 } else if (size_read == 0) {
672 size_written = write(to, buffer, (size_t)size_read);
673 if (size_written < 0) {
674 LOG_PERROR(WARNING, "read_from_write_to(): write()");
677 if (size_read != size_written) {
678 LOG(ERROR, "read_from_write_to(): only written %ld of %ld bytes!",
679 (long int)size_written, (long int)size_read);
686 /* Transfer data between client and server TLS connection until one closes the
688 static void transfer_data_tls(int client, int server,
689 gnutls_session_t client_session,
690 gnutls_session_t server_session) {
693 struct pollfd fds[2];
695 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
698 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
701 /* Get maximum possible buffer size. */
702 buffer_size = gnutls_record_get_max_size(client_session);
703 if (gnutls_record_get_max_size(server_session) < buffer_size) {
704 buffer_size = gnutls_record_get_max_size(server_session);
706 LOG(DEBUG, "transfer_data_tls(): suggested buffer size: %ld",
707 (long int)buffer_size);
710 int result = poll(fds, 2 /* fd count */, -1 /* no timeout */);
712 LOG_PERROR(ERROR, "transfer_data(): poll()");
716 /* Data available from client. */
717 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
718 if (read_from_write_to_tls(client_session, server_session,
720 /* EOF (or other error) */
724 /* Data available from server. */
725 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
726 if (read_from_write_to_tls(server_session, client_session,
728 /* EOF (or other error) */
733 /* Client closed connection. */
734 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
737 /* Server closed connection. */
738 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
744 /* Read available data from session from and write to session to. */
745 static int read_from_write_to_tls(gnutls_session_t from,
747 size_t buffer_size) {
749 ssize_t size_written;
752 if (buffer_size > sizeof(buffer)) {
753 buffer_size = sizeof(buffer);
755 LOG(DEBUG, "read_from_write_to_tls(): used buffer size: %ld",
756 (long int)buffer_size);
758 size_read = gnutls_record_recv(from, buffer, buffer_size);
760 LOG(WARNING, "read_from_write_to_tls(): gnutls_record_recv(): %s",
761 gnutls_strerror((int)size_read));
764 } else if (size_read == 0) {
768 size_written = gnutls_record_send(to, buffer, (size_t)size_read);
769 if (size_written < 0) {
770 LOG(WARNING, "read_from_write_to_tls(): gnutls_record_send(): %s",
771 gnutls_strerror((int)size_written));
774 if (size_read != size_written) {
775 LOG(ERROR, "read_from_write_to_tls(): only written %ld of %ld bytes!",
776 (long int)size_written, (long int)size_read);
784 static int connect_to_host(const char *hostname, const char *port) {
785 struct addrinfo gai_hints;
786 struct addrinfo *gai_result;
790 struct addrinfo *server;
792 if (hostname == NULL || port == NULL) {
796 /* Get IP of hostname server. */
797 memset(&gai_hints, 0, sizeof(gai_hints));
798 gai_hints.ai_family = AF_UNSPEC;
799 gai_hints.ai_socktype = SOCK_STREAM;
800 gai_hints.ai_protocol = 0;
801 gai_hints.ai_flags = AI_NUMERICSERV /* given port is numeric */
802 | AI_ADDRCONFIG /* supported by this computer */
803 | AI_V4MAPPED; /* support IPv4 through IPv6 */
804 gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
805 if (gai_return != 0) {
806 if (gai_return == EAI_SYSTEM) {
807 LOG_PERROR(WARNING, "connect_to_host(): getaddrinfo()");
809 LOG(WARNING, "connect_to_host(): getaddrinfo(): %s",
810 gai_strerror(gai_return));
815 /* Now try to connect to each server returned by getaddrinfo(), use the
816 * first successful connect. */
817 for (server = gai_result; server != NULL; server = server->ai_next) {
818 server_socket = socket(server->ai_family,
820 server->ai_protocol);
821 if (server_socket < 0) {
822 LOG_PERROR(DEBUG, "connect_to_host(): socket(), trying next");
826 if (connect(server_socket, server->ai_addr, server->ai_addrlen) == 0) {
829 LOG_PERROR(DEBUG, "connect_to_host(): connect(), trying next");
831 close(server_socket);
833 /* Make sure we free the result from getaddrinfo(). */
834 freeaddrinfo(gai_result);
836 if (server == NULL) {
837 LOG_PERROR(WARNING, "connect_to_host(): no server found, abort");
841 return server_socket;
845 /* Parse HTTP CONNECT request string and save its parameters.
847 * The following format is expected: "CONNECT host:port HTTP/1.x".
849 * request and host must have the same size! port must be at least 6 bytes
852 static int parse_request(const char *request, char *host, char *port,
853 int *version_minor) {
854 int port_unused; /* just used to verify the port is numeric */
857 /* scanf() doesn't check spaces. */
858 if (strncmp(request, "CONNECT ", 8)) {
861 /* Check request and extract data, "host:port" is not yet separated. */
862 if (sscanf(request, "CONNECT %s HTTP/1.%d", host, version_minor) != 2) {
865 /* Make sure ":port" is there. */
866 if ((position = strchr(host, ':')) == NULL) {
869 /* Make sure port is numeric. */
870 if (sscanf(position + 1, "%d", &port_unused) != 1) {
873 /* Store it in *port. */
874 strncpy(port, position + 1, 5);
876 /* And remove port from host. */