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"
30 /* Maximum length of a HTTP request line. Longer request lines are aborted
31 * with an error. The standard doesn't specify a maximum line length but this
32 * should be a good limit to make processing simpler. As HTTPS is used this
33 * doesn't limit long GET requests. */
34 #define MAX_REQUEST_LINE 4096
36 /* Format string used to send HTTP/1.0 error responses to the client.
38 * %s is used 4 times, first three are the error code (no %n$s!), the last is
40 #define HTTP_RESPONSE_FORMAT "HTTP/1.0 %s\r\n\
41 Content-Type: text/html; charset=US-ASCII\r\n\
43 <!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n\
45 <head><title>%s</title></head>\n\
53 static int initialize_tls_session_client(int peer_socket,
55 gnutls_session_t *session,
56 gnutls_certificate_credentials_t *x509_cred);
57 static int initialize_tls_session_server(int peer_socket,
58 gnutls_session_t *session,
59 gnutls_certificate_credentials_t *x509_cred);
61 static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd);
62 static int read_http_request(FILE *client_fd, char *request, size_t length);
63 static void send_bad_request(FILE *client_fd);
64 static void send_forwarding_failure(FILE *client_fd);
65 static void tls_send_invalid_cert_message(gnutls_session_t session);
67 static void transfer_data(int client, int server);
68 static int read_from_write_to(int from, int to);
69 static void transfer_data_tls(int client, int server,
70 gnutls_session_t client_session,
71 gnutls_session_t server_session);
72 static int read_from_write_to_tls(gnutls_session_t from, gnutls_session_t to,
75 static int connect_to_host(const char *hostname, const char *port);
77 static int parse_request(const char *buffer, char *host, char *port,
81 void handle_connection(int client_socket) {
83 FILE *client_fd_read, *client_fd_write, *server_fd_read, *server_fd_write;
85 char buffer[MAX_REQUEST_LINE];
86 char host[MAX_REQUEST_LINE];
89 int version_minor; /* x in HTTP/1.x */
92 /* client_x509_cred is used when talking to the client (acting as a TSL
93 * server), server_x509_cred is used when talking to the server (acting as
95 gnutls_certificate_credentials_t client_x509_cred, server_x509_cred;
97 gnutls_session_t client_session, server_session;
98 /* initialize_tls_session_*() called? - used for goto out */
99 int client_session_init, server_session_init;
100 /* gnutls_handshake() called? - used for goto out */
101 int client_session_started, server_session_started;
102 /* Validation failed? If yes we need to send the special "invalid"
104 int validation_failed;
106 LOG(LOG_DEBUG, "new connection");
109 client_fd_read = NULL;
110 client_fd_write = NULL;
111 server_fd_read = NULL;
112 server_fd_write = NULL;
113 client_session_init = 0;
114 server_session_init = 0;
115 client_session_started = 0;
116 server_session_started = 0;
117 validation_failed = 0;
119 if (fdopen_read_write(client_socket, &client_fd_read,
120 &client_fd_write) != 0) {
124 /* Read request line (CONNECT ..) and headers (they are discarded). */
125 result = read_http_request(client_fd_read, buffer, sizeof(buffer));
127 LOG(LOG_WARNING, "read_http_request(): client read error");
129 } else if (result == -2) {
130 LOG(LOG_WARNING, "read_http_request(): client EOF");
131 send_bad_request(client_fd_write);
135 if (parse_request(buffer, host, port, &version_minor) != 0) {
136 LOG(LOG_WARNING, "bad request: %s", buffer);
137 send_bad_request(client_fd_write);
141 LOG(LOG_DEBUG, "target: %s:%s (HTTP 1.%d)", host, port, version_minor);
143 /* Connect to proxy server or directly to server. */
144 if (global_proxy_host != NULL && global_proxy_port != NULL) {
145 LOG(LOG_DEBUG, "connecting to %s:%s", global_proxy_host,
147 server_socket = connect_to_host(global_proxy_host, global_proxy_port);
149 LOG(LOG_DEBUG, "connecting to %s:%s", host, port);
150 server_socket = connect_to_host(host, port);
153 if (server_socket == -1) {
154 LOG(LOG_WARNING, "failed to connect to server");
155 send_forwarding_failure(client_fd_write);
158 if (fdopen_read_write(server_socket, &server_fd_read,
159 &server_fd_write) != 0) {
160 send_forwarding_failure(client_fd_write);
164 /* Connect to proxy if requested (command line option). */
165 if (global_proxy_host != NULL && global_proxy_port != NULL) {
166 fprintf(server_fd_write, "CONNECT %s:%s HTTP/1.0\r\n", host, port);
167 fprintf(server_fd_write, "\r\n");
168 fflush(server_fd_write);
170 /* Read response line from proxy server. */
171 result = read_http_request(server_fd_read, buffer, sizeof(buffer));
173 LOG(LOG_WARNING, "read_http_request(): proxy read error");
174 send_forwarding_failure(client_fd_write);
176 } else if (result == -2) {
177 LOG(LOG_WARNING, "read_http_request(): proxy EOF");
178 send_forwarding_failure(client_fd_write);
182 /* Check response of proxy server. */
183 if (strncmp(buffer, "HTTP/1.0 200", 12)) {
184 LOG(LOG_WARNING, "bad proxy response: %s", buffer);
185 send_forwarding_failure(client_fd_write);
190 LOG(LOG_DEBUG, "connection to server established");
192 /* If the -u option is used and we don't know this hostname's server
193 * certificate then just pass through the connection and let the client
194 * verify the server certificate. */
195 if (global_passthrough_unknown) {
196 char path[TLSPROXY_MAX_PATH_LENGTH];
199 if (server_certificate_file(&file, host, path, sizeof(path)) == -2) {
200 /* We've established a connection, tell the client. */
201 fprintf(client_fd_write, "HTTP/1.0 200 Connection established\r\n");
202 fprintf(client_fd_write, "\r\n");
203 fflush(client_fd_write);
205 LOG(LOG_DEBUG, "transferring data");
207 /* Proxy data between client and server until one side is done
209 transfer_data(client_socket, server_socket);
211 LOG(LOG_DEBUG, "finished transferring data");
215 /* server_certificate_file() may have opened the file, close it. */
221 /* Initialize TLS client credentials to talk to the server. */
222 result = initialize_tls_session_server(server_socket, &server_session,
225 LOG(LOG_WARNING, "initialize_tls_session_server() failed");
226 send_forwarding_failure(client_fd_write);
229 server_session_init = 1;
231 LOG(LOG_DEBUG, "starting server TLS handshake");
233 /* Try to establish TLS handshake between us and server. */
234 result = gnutls_handshake(server_session);
235 if (result != GNUTLS_E_SUCCESS) {
236 LOG(LOG_WARNING, "server TLS handshake failed: %s",
237 gnutls_strerror(result));
238 send_forwarding_failure(client_fd_write);
241 server_session_started = 1;
243 LOG(LOG_DEBUG, "server TLS handshake finished");
245 /* Make sure the server certificate is valid and known. */
246 if (verify_tls_connection(server_session, host) != 0) {
247 LOG(LOG_ERROR, "server certificate validation failed!");
248 /* We'll send the error message over our TLS connection to the client,
249 * but with an invalid certificate. No data is transfered from/to the
251 validation_failed = 1;
254 /* Initialize TLS server credentials to talk to the client. */
255 result = initialize_tls_session_client(client_socket,
256 /* use a special host if the server
257 * certificate was invalid */
258 (validation_failed) ? "invalid"
263 LOG(LOG_WARNING, "initialize_tls_session_client() failed");
264 send_forwarding_failure(client_fd_write);
267 client_session_init = 1;
269 /* We've established a connection, tell the client. */
270 fprintf(client_fd_write, "HTTP/1.0 200 Connection established\r\n");
271 fprintf(client_fd_write, "\r\n");
272 fflush(client_fd_write);
274 LOG(LOG_DEBUG, "starting client TLS handshake");
276 /* Try to establish TLS handshake between client and us. */
277 result = gnutls_handshake(client_session);
278 if (result != GNUTLS_E_SUCCESS) {
279 LOG(LOG_WARNING, "client TLS handshake failed: %s",
280 gnutls_strerror(result));
281 send_forwarding_failure(client_fd_write);
284 client_session_started = 1;
286 LOG(LOG_DEBUG, "client TLS handshake finished");
288 /* Tell the client that the verification failed. Shouldn't be necessary as
289 * the client should terminate the connection because he received the
290 * invalid certificate but better be sure. */
291 if (validation_failed) {
292 tls_send_invalid_cert_message(client_session);
296 LOG(LOG_DEBUG, "transferring TLS data");
298 /* Proxy data between client and server until one side is done (EOF or
300 transfer_data_tls(client_socket, server_socket,
301 client_session, server_session);
303 LOG(LOG_DEBUG, "finished transferring TLS data");
306 /* Close TLS sessions if necessary. Use GNUTLS_SHUT_RDWR so the data is
307 * reliable transmitted. */
308 if (server_session_started) {
309 gnutls_bye(server_session, GNUTLS_SHUT_RDWR);
311 if (client_session_started) {
312 gnutls_bye(client_session, GNUTLS_SHUT_RDWR);
314 if (server_session_init) {
315 gnutls_deinit(server_session);
316 gnutls_certificate_free_credentials(server_x509_cred);
318 if (client_session_init) {
319 gnutls_deinit(client_session);
320 gnutls_certificate_free_cas(client_x509_cred);
321 gnutls_certificate_free_keys(client_x509_cred);
322 gnutls_certificate_free_credentials(client_x509_cred);
325 /* Close connection to server/proxy. */
326 if (server_fd_read != NULL) {
327 if (server_fd_write != NULL) {
328 fclose(server_fd_write);
330 fclose(server_fd_read);
331 } else if (server_socket != -1) {
332 close(server_socket);
334 LOG(LOG_DEBUG, "connection to server closed");
335 /* Close connection to client. */
336 if (client_fd_read != NULL) {
337 if (client_fd_write != NULL) {
338 fclose(client_fd_write);
340 fclose(client_fd_read);
342 close(client_socket);
344 LOG(LOG_DEBUG, "connection to client closed");
346 LOG(LOG_DEBUG, "connection finished");
350 static int initialize_tls_session_client(int peer_socket,
351 const char *hostname,
352 gnutls_session_t *session,
353 gnutls_certificate_credentials_t *x509_cred) {
355 int use_invalid_cert;
356 char path[TLSPROXY_MAX_PATH_LENGTH];
358 /* The "invalid" hostname is special. If it's used we send an invalid
359 * certificate to let the client know something is wrong. */
360 use_invalid_cert = (!strcmp(hostname, "invalid"));
362 if (proxy_certificate_path(hostname, path, sizeof(path)) != 0) {
364 "initialize_tls_session_client(): \
365 failed to get proxy certificate path");
369 result = gnutls_certificate_allocate_credentials(x509_cred);
370 if (result != GNUTLS_E_SUCCESS) {
372 "initialize_tls_session_client(): \
373 gnutls_certificate_allocate_credentials(): %s",
374 gnutls_strerror(result));
378 /* Load proxy CA file, this CA "list" is send to the client. */
379 if (!use_invalid_cert) {
380 result = gnutls_certificate_set_x509_trust_file(*x509_cred,
382 GNUTLS_X509_FMT_PEM);
385 "initialize_tls_session_client(): can't read CA file: '%s'",
387 gnutls_certificate_free_credentials(*x509_cred);
391 /* If the invalid hostname was specified do nothing, we use a self-signed
392 * certificate in this case. */
394 /* And certificate for this website and proxy's private key. */
395 if (!use_invalid_cert) {
396 result = gnutls_certificate_set_x509_key_file(*x509_cred,
397 path, PROXY_KEY_FILE,
398 GNUTLS_X509_FMT_PEM);
399 /* If the invalid hostname was specified load our special "invalid"
402 result = gnutls_certificate_set_x509_key_file(*x509_cred,
403 PROXY_INVALID_CERT_FILE,
405 GNUTLS_X509_FMT_PEM);
407 if (result != GNUTLS_E_SUCCESS) {
409 "initialize_tls_session_client(): \
410 can't read server certificate ('%s') or key file ('%s'): %s",
411 path, PROXY_KEY_FILE, gnutls_strerror(result));
412 gnutls_certificate_free_credentials(*x509_cred);
413 /* Could be a missing certificate. */
417 gnutls_certificate_set_dh_params(*x509_cred, global_tls_dh_params);
419 result = gnutls_init(session, GNUTLS_SERVER);
420 if (result != GNUTLS_E_SUCCESS) {
422 "initialize_tls_session_client(): gnutls_init(): %s",
423 gnutls_strerror(result));
424 gnutls_certificate_free_credentials(*x509_cred);
427 result = gnutls_priority_set(*session, global_tls_priority_cache);
428 if (result != GNUTLS_E_SUCCESS) {
430 "initialize_tls_session_client(): gnutls_priority_set(): %s",
431 gnutls_strerror(result));
432 gnutls_deinit(*session);
433 gnutls_certificate_free_credentials(*x509_cred);
436 result = gnutls_credentials_set(*session,
437 GNUTLS_CRD_CERTIFICATE, *x509_cred);
438 if (result != GNUTLS_E_SUCCESS) {
440 "initialize_tls_session_client(): gnutls_credentials_set(): %s",
441 gnutls_strerror(result));
442 gnutls_deinit(*session);
443 gnutls_certificate_free_credentials(*x509_cred);
447 gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
451 static int initialize_tls_session_server(int peer_socket,
452 gnutls_session_t *session,
453 gnutls_certificate_credentials_t *x509_cred) {
456 result = gnutls_certificate_allocate_credentials(x509_cred);
457 if (result != GNUTLS_E_SUCCESS) {
459 "initialize_tls_session_server(): \
460 gnutls_certificate_allocate_credentials(): %s",
461 gnutls_strerror(result));
465 result = gnutls_init(session, GNUTLS_CLIENT);
466 if (result != GNUTLS_E_SUCCESS) {
468 "initialize_tls_session_server(): gnutls_init(): %s",
469 gnutls_strerror(result));
470 gnutls_certificate_free_credentials(*x509_cred);
473 result = gnutls_priority_set(*session, global_tls_priority_cache);
474 if (result != GNUTLS_E_SUCCESS) {
476 "initialize_tls_session_server(): gnutls_priority_set(): %s",
477 gnutls_strerror(result));
478 gnutls_deinit(*session);
479 gnutls_certificate_free_credentials(*x509_cred);
482 result = gnutls_credentials_set(*session,
483 GNUTLS_CRD_CERTIFICATE, *x509_cred);
484 if (result != GNUTLS_E_SUCCESS) {
486 "initialize_tls_session_server(): gnutls_credentials_set(): %s",
487 gnutls_strerror(result));
488 gnutls_deinit(*session);
489 gnutls_certificate_free_credentials(*x509_cred);
493 gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
499 static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd) {
500 *read_fd = fdopen(socket, "r");
501 if (*read_fd == NULL) {
502 LOG_PERROR(LOG_WARNING, "fdopen_read_write(): fdopen(\"r\") failed");
506 *write_fd = fdopen(dup(socket), "w");
507 if (*write_fd == NULL) {
508 LOG_PERROR(LOG_WARNING, "fdopen_read_write(): fdopen(\"w\") failed");
510 *read_fd = NULL; /* "tell" caller read_fd is already closed */
517 /* Read HTTP request line and headers (ignored).
519 * On success 0 is returned, -1 on client error, -2 on unexpected EOF.
521 static int read_http_request(FILE *client_fd, char *request, size_t length) {
522 char buffer[MAX_REQUEST_LINE];
524 if (fgets(request, (int)length, client_fd) == NULL) {
525 if (ferror(client_fd)) {
526 LOG_PERROR(LOG_WARNING, "read_http_request(): fgets()");
533 while (fgets(buffer, sizeof(buffer), client_fd) != NULL) {
535 if (!strcmp(buffer, "\n") || !strcmp(buffer, "\r\n")) {
539 if (ferror(client_fd)) {
540 LOG_PERROR(LOG_WARNING, "read_http_request(): fgets()");
547 static void send_bad_request(FILE *client_fd) {
548 #define RESPONSE_ERROR "400 Bad Request"
549 #define RESPONSE_MSG "Your browser sent an invalid request."
550 fprintf(client_fd, HTTP_RESPONSE_FORMAT,
551 RESPONSE_ERROR, RESPONSE_ERROR, RESPONSE_ERROR,
554 #undef RESPONSE_ERROR
557 static void send_forwarding_failure(FILE *client_fd) {
558 #define RESPONSE_ERROR "503 Forwarding failure"
559 #define RESPONSE_MSG "Failed to connect to server, check logs."
560 fprintf(client_fd, HTTP_RESPONSE_FORMAT,
561 RESPONSE_ERROR, RESPONSE_ERROR, RESPONSE_ERROR,
564 #undef RESPONSE_ERROR
567 static void tls_send_invalid_cert_message(gnutls_session_t session) {
568 #define RESPONSE_ERROR "500 Internal Server Error"
569 #define RESPONSE_MSG "Server certificate validation failed, check logs."
572 char buffer[sizeof(HTTP_RESPONSE_FORMAT) - 1 /* '\0' */
573 - 4 * 2 /* four %s */
574 + (sizeof(RESPONSE_ERROR) - 1 /* '\0' */) * 3
575 + sizeof(RESPONSE_MSG) - 1 /* '\0' */
578 result = snprintf(buffer, sizeof(buffer),
579 HTTP_RESPONSE_FORMAT,
580 RESPONSE_ERROR, RESPONSE_ERROR, RESPONSE_ERROR,
583 LOG_PERROR(LOG_ERROR,
584 "tls_send_invalid_cert_message(): snprintf failed");
586 } else if ((size_t)result >= sizeof(buffer)) {
588 "tls_send_invalid_cert_message(): snprintf buffer too short");
592 gnutls_record_send(session, buffer, sizeof(buffer) - 1);
593 /* don't send trailing '\0' */
594 #undef RESPONSE_ERROR
599 /* Transfer data between client and server sockets until one closes the
601 static void transfer_data(int client, int server) {
602 struct pollfd fds[2];
604 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
607 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
611 int result = poll(fds, 2 /* fd count */, -1 /* no timeout */);
613 LOG_PERROR(LOG_ERROR, "transfer_data(): poll()");
617 /* Data available from client. */
618 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
619 if (read_from_write_to(client, server) != 0) {
620 /* EOF (or other error) */
624 /* Data available from server. */
625 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
626 if (read_from_write_to(server, client) != 0) {
627 /* EOF (or other error) */
632 /* Client closed connection. */
633 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
636 /* Server closed connection. */
637 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
643 /* Read available data from socket from and write it to socket to. At maximum
644 * 4096 bytes are read/written. */
645 static int read_from_write_to(int from, int to) {
647 ssize_t size_written;
650 LOG(LOG_DEBUG, "read_from_write_to(): %d -> %d", from, to);
652 size_read = read(from, buffer, sizeof(buffer));
654 LOG_PERROR(LOG_WARNING, "read_from_write_to(): read()");
657 } else if (size_read == 0) {
661 size_written = write(to, buffer, (size_t)size_read);
662 if (size_written < 0) {
663 LOG_PERROR(LOG_WARNING, "read_from_write_to(): write()");
666 if (size_read != size_written) {
667 LOG(LOG_ERROR, "read_from_write_to(): only written %ld of %ld bytes!",
668 (long int)size_written, (long int)size_read);
675 /* Transfer data between client and server TLS connection until one closes the
677 static void transfer_data_tls(int client, int server,
678 gnutls_session_t client_session,
679 gnutls_session_t server_session) {
682 struct pollfd fds[2];
684 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
687 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
690 /* Get maximum possible buffer size. */
691 buffer_size = gnutls_record_get_max_size(client_session);
692 if (gnutls_record_get_max_size(server_session) < buffer_size) {
693 buffer_size = gnutls_record_get_max_size(server_session);
695 LOG(LOG_DEBUG, "transfer_data_tls(): suggested buffer size: %ld",
696 (long int)buffer_size);
699 int result = poll(fds, 2 /* fd count */, -1 /* no timeout */);
701 LOG_PERROR(LOG_ERROR, "transfer_data(): poll()");
705 /* Data available from client. */
706 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
707 if (read_from_write_to_tls(client_session, server_session,
709 /* EOF (or other error) */
713 /* Data available from server. */
714 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
715 if (read_from_write_to_tls(server_session, client_session,
717 /* EOF (or other error) */
722 /* Client closed connection. */
723 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
726 /* Server closed connection. */
727 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
733 /* Read available data from session from and write to session to. */
734 static int read_from_write_to_tls(gnutls_session_t from,
736 size_t buffer_size) {
738 ssize_t size_written;
741 if (buffer_size > sizeof(buffer)) {
742 buffer_size = sizeof(buffer);
744 LOG(LOG_DEBUG, "read_from_write_to_tls(): used buffer size: %ld",
745 (long int)buffer_size);
747 size_read = gnutls_record_recv(from, buffer, buffer_size);
749 LOG(LOG_WARNING, "read_from_write_to_tls(): gnutls_record_recv(): %s",
750 gnutls_strerror((int)size_read));
753 } else if (size_read == 0) {
757 size_written = gnutls_record_send(to, buffer, (size_t)size_read);
758 if (size_written < 0) {
759 LOG(LOG_WARNING, "read_from_write_to_tls(): gnutls_record_send(): %s",
760 gnutls_strerror((int)size_written));
763 if (size_read != size_written) {
764 LOG(LOG_ERROR, "read_from_write_to_tls(): only written %ld of %ld bytes!",
765 (long int)size_written, (long int)size_read);
773 static int connect_to_host(const char *hostname, const char *port) {
774 struct addrinfo gai_hints;
775 struct addrinfo *gai_result;
779 struct addrinfo *server;
781 if (hostname == NULL || port == NULL) {
785 /* Get IP of hostname server. */
786 memset(&gai_hints, 0, sizeof(gai_hints));
787 gai_hints.ai_family = AF_UNSPEC;
788 gai_hints.ai_socktype = SOCK_STREAM;
789 gai_hints.ai_protocol = 0;
790 gai_hints.ai_flags = AI_NUMERICSERV /* given port is numeric */
791 | AI_ADDRCONFIG /* supported by this computer */
792 | AI_V4MAPPED; /* support IPv4 through IPv6 */
793 gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
794 if (gai_return != 0) {
795 if (gai_return == EAI_SYSTEM) {
796 LOG_PERROR(LOG_WARNING, "connect_to_host(): getaddrinfo()");
798 LOG(LOG_WARNING, "connect_to_host(): getaddrinfo(): %s",
799 gai_strerror(gai_return));
804 /* Now try to connect to each server returned by getaddrinfo(), use the
805 * first successful connect. */
806 for (server = gai_result; server != NULL; server = server->ai_next) {
807 server_socket = socket(server->ai_family,
809 server->ai_protocol);
810 if (server_socket == -1) {
811 LOG_PERROR(LOG_DEBUG, "connect_to_host(): socket(), trying next");
815 if (connect(server_socket, server->ai_addr, server->ai_addrlen) != -1) {
818 LOG_PERROR(LOG_DEBUG, "connect_to_host(): connect(), trying next");
820 close(server_socket);
822 /* Make sure we free the result from getaddrinfo(). */
823 freeaddrinfo(gai_result);
825 if (server == NULL) {
826 LOG_PERROR(LOG_WARNING, "connect_to_host(): no server found, abort");
830 return server_socket;
834 /* Parse HTTP CONNECT request string and save its parameters.
836 * The following format is expected: "CONNECT host:port HTTP/1.x".
838 * request and host must have the same size! port must be at least 6 bytes
841 static int parse_request(const char *request, char *host, char *port,
842 int *version_minor) {
843 int port_unused; /* just used to verify the port is numeric */
846 /* scanf() doesn't check spaces. */
847 if (strncmp(request, "CONNECT ", 8)) {
850 /* Check request and extract data, "host:port" is not yet separated. */
851 if (sscanf(request, "CONNECT %s HTTP/1.%d", host, version_minor) != 2) {
854 /* Make sure ":port" is there. */
855 if ((position = strchr(host, ':')) == NULL) {
858 /* Make sure port is numeric. */
859 if (sscanf(position + 1, "%d", &port_unused) != 1) {
862 /* Store it in *port. */
863 strncpy(port, position + 1, 5);
865 /* And remove port from host. */