]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/blob - src/connection.c
Verify the server certificate against a stored copy.
[tlsproxy/tlsproxy.git] / src / connection.c
1 /*
2  * Handle connections.
3  *
4  * Copyright (C) 2011  Simon Ruderich
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "tlsproxy.h"
21 #include "connection.h"
22 #include "verify.h"
23
24 /* close() */
25 #include <unistd.h>
26 /* getaddrinfo() */
27 #include <netdb.h>
28 /* poll() */
29 #include <poll.h>
30 /* errno */
31 #include <errno.h>
32
33
34 /* Maximum line of a HTTP request line. Longer request lines are aborted with
35  * an error. The standard doesn't specify a maximum line length but this
36  * should be a good limit to make processing simpler. */
37 #define MAX_REQUEST_LINE 4096
38
39
40 static int initialize_tls_session_client(int peer_socket,
41         const char *hostname,
42         gnutls_session_t *session,
43         gnutls_certificate_credentials_t *x509_cred);
44 static int initialize_tls_session_server(int peer_socket,
45         gnutls_session_t *session,
46         gnutls_certificate_credentials_t *x509_cred);
47
48 static int read_http_request(FILE *client_fd, char *request, size_t length);
49 static void send_bad_request(FILE *client_fd);
50 static void send_forwarding_failure(FILE *client_fd);
51 static void tls_send_server_error(gnutls_session_t session);
52
53 #if 0
54 static void transfer_data(int client, int server);
55 static int read_from_write_to(int from, int to);
56 #endif
57 static void transfer_data_tls(int client, int server,
58                               gnutls_session_t client_session,
59                               gnutls_session_t server_session);
60 static int read_from_write_to_tls(gnutls_session_t from, gnutls_session_t to);
61
62 static int connect_to_host(const char *hostname, const char *port);
63
64 static int parse_request(const char *buffer, char *host, char *port,
65                                              int *version_minor);
66
67
68 void handle_connection(int client_socket) {
69     int server_socket;
70     FILE *client_fd, *server_fd;
71
72     char buffer[MAX_REQUEST_LINE];
73     char host[MAX_REQUEST_LINE];
74     char port[5 + 1];
75
76     int version_minor;
77     int result;
78
79     /* client_x509_cred is used when talking to the client (acting as a TSL
80      * server), server_x509_cred is used when talking to the server (acting as
81      * a TSL client). */
82     gnutls_certificate_credentials_t client_x509_cred, server_x509_cred;
83
84     gnutls_session_t client_session, server_session;
85     /* initialize_tls_session_*() called? - used for goto out */
86     int client_session_init, server_session_init;
87     /* gnutls_handshake() called? - used for goto out */
88     int client_session_started, server_session_started;
89
90     LOG(LOG_DEBUG, "new connection");
91
92     server_socket = -1;
93     client_fd = NULL;
94     server_fd = NULL;
95     client_session_init = 0;
96     server_session_init = 0;
97     client_session_started = 0;
98     server_session_started = 0;
99
100     client_fd = fdopen(client_socket, "a+");
101     if (NULL == client_fd) {
102         LOG_PERROR(LOG_WARNING, "fdopen(): client failed");
103         goto out;
104     }
105
106     /* Read request line (CONNECT ..) and headers (they are discarded). */
107     result = read_http_request(client_fd, buffer, sizeof(buffer));
108     if (-1 == result) {
109         /* Read error. */
110         LOG(LOG_WARNING, "read_http_request(): client read error");
111         goto out;
112     } else if (-2 == result) {
113         /* EOF */
114         LOG(LOG_WARNING, "read_http_request(): client EOF");
115         send_bad_request(client_fd);
116         goto out;
117     }
118
119     if (0 != parse_request(buffer, host, port, &version_minor)) {
120         LOG(LOG_WARNING, "bad request: %s", buffer);
121         send_bad_request(client_fd);
122         goto out;
123     }
124
125     LOG(LOG_DEBUG, "target: %s:%s (HTTP 1.%d)", host, port, version_minor);
126
127     /* Connect to proxy server or directly to server. */
128     if (NULL != global_proxy_host && NULL != global_proxy_port) {
129         LOG(LOG_DEBUG, "connecting to %s:%s", global_proxy_host,
130                                               global_proxy_port);
131         server_socket = connect_to_host(global_proxy_host, global_proxy_port);
132     } else {
133         LOG(LOG_DEBUG, "connecting to %s:%s", host, port);
134         server_socket = connect_to_host(host, port);
135     }
136
137     if (-1 == server_socket) {
138         LOG(LOG_WARNING, "failed to connect to server");
139         send_forwarding_failure(client_fd);
140         goto out;
141     }
142     server_fd = fdopen(server_socket, "a+");
143     if (NULL == server_fd) {
144         LOG_PERROR(LOG_WARNING, "fdopen(): server failed");
145         send_forwarding_failure(client_fd);
146         goto out;
147     }
148
149     /* Connect to proxy if requested (command line option). */
150     if (NULL != global_proxy_host && NULL != global_proxy_port) {
151         fprintf(server_fd, "CONNECT %s:%s HTTP/1.0\r\n", host, port);
152         fprintf(server_fd, "\r\n");
153
154         /* Read response line from proxy server. */
155         result = read_http_request(server_fd, buffer, sizeof(buffer));
156         if (-1 == result) {
157             /* Read error. */
158             LOG(LOG_WARNING, "read_http_request(): proxy read error");
159             send_forwarding_failure(client_fd);
160             goto out;
161         } else if (-2 == result) {
162             /* EOF */
163             LOG(LOG_WARNING, "read_http_request(): proxy EOF");
164             send_forwarding_failure(client_fd);
165             goto out;
166         }
167
168         /* Check response of proxy server. */
169         if (0 != strncmp(buffer, "HTTP/1.0 200", 12)) {
170             LOG(LOG_WARNING, "bad proxy response: %s", buffer);
171             send_forwarding_failure(client_fd);
172             goto out;
173         }
174     }
175
176     LOG(LOG_DEBUG, "connection to server established");
177
178     /* We've established a connection, tell the client. */
179     fprintf(client_fd, "HTTP/1.0 200 Connection established\r\n");
180     fprintf(client_fd, "\r\n");
181     fflush(client_fd);
182
183     /* Initialize TLS server credentials to talk to the client. */
184     result = initialize_tls_session_client(client_socket, host,
185                                            &client_session,
186                                            &client_x509_cred);
187     if (0 != result) {
188         LOG(LOG_WARNING, "initialize_tls_session_client() failed");
189         send_forwarding_failure(client_fd);
190         goto out;
191     }
192     client_session_init = 1;
193
194     LOG(LOG_DEBUG, "starting client TLS handshake");
195
196     /* Try to establish TLS handshake between client and us. */
197     result = gnutls_handshake(client_session);
198     if (GNUTLS_E_SUCCESS != result) {
199         LOG(LOG_WARNING, "client TLS handshake failed: %s",
200                          gnutls_strerror(result));
201         send_forwarding_failure(client_fd);
202         goto out;
203     }
204     client_session_started = 1;
205
206     LOG(LOG_DEBUG, "client TLS handshake finished");
207
208     result = initialize_tls_session_server(server_socket, &server_session,
209                                                           &server_x509_cred);
210     /* Initialize TLS client credentials to talk to the server. */
211     if (0 != result) {
212         LOG(LOG_WARNING, "initialize_tls_session_server() failed");
213         send_forwarding_failure(client_fd);
214         goto out;
215     }
216     server_session_init = 1;
217
218     LOG(LOG_DEBUG, "starting server TLS handshake");
219
220     /* Try to establish TLS handshake between us and server. */
221     result = gnutls_handshake(server_session);
222     if (GNUTLS_E_SUCCESS != result) {
223         LOG(LOG_WARNING, "server TLS handshake failed: %s",
224                          gnutls_strerror(result));
225         send_forwarding_failure(client_fd);
226         goto out;
227     }
228     server_session_started = 1;
229
230     LOG(LOG_DEBUG, "server TLS handshake finished, transferring data");
231
232     /* Make sure the server certificate is valid and known. */
233     if (0 != verify_tls_connection(server_session, host)) {
234         LOG(LOG_ERROR, "server certificate validation failed!");
235         tls_send_server_error(client_session);
236         goto out;
237     }
238
239     /* Proxy data between client and server until one suite is done (EOF or
240      * error). */
241     transfer_data_tls(client_socket, server_socket,
242                       client_session, server_session);
243
244     LOG(LOG_DEBUG, "finished transferring data");
245
246 out:
247     /* Close TLS sessions if necessary. Use GNUTLS_SHUT_RDWR so the data is
248      * reliable transmitted. */
249     if (0 != server_session_started) {
250         gnutls_bye(server_session, GNUTLS_SHUT_RDWR);
251     }
252     if (0 != client_session_started) {
253         gnutls_bye(client_session, GNUTLS_SHUT_RDWR);
254     }
255     if (0 != server_session_init) {
256         gnutls_deinit(server_session);
257         gnutls_certificate_free_credentials(server_x509_cred);
258     }
259     if (0 != client_session_init) {
260         gnutls_deinit(client_session);
261         gnutls_certificate_free_cas(client_x509_cred);
262         gnutls_certificate_free_keys(client_x509_cred);
263         gnutls_certificate_free_credentials(client_x509_cred);
264     }
265
266     /* Close connection to server/proxy. */
267     if (NULL != server_fd) {
268         fclose(server_fd);
269     } else if (-1 != server_socket) {
270         close(server_socket);
271     }
272     LOG(LOG_DEBUG, "connection to server closed");
273     /* Close connection to client. */
274     if (NULL != client_fd) {
275         fclose(client_fd);
276     } else {
277         close(client_socket);
278     }
279     LOG(LOG_DEBUG, "connection to client closed");
280
281     LOG(LOG_DEBUG, "connection finished");
282 }
283
284
285 static int initialize_tls_session_client(int peer_socket,
286         const char *hostname,
287         gnutls_session_t *session,
288         gnutls_certificate_credentials_t *x509_cred) {
289     int result;
290     char path[1024];
291
292     /* Hostname too long. */
293     if (sizeof(path) - strlen(PROXY_SERVER_CERT_FORMAT) <= strlen(hostname)) {
294         LOG(LOG_WARNING,
295             "initialize_tls_session_client(): hostname too long: '%s'",
296             hostname);
297         return -1;
298     }
299     /* Try to prevent path traversals in hostnames. */
300     if (NULL != strstr(hostname, "..")) {
301         LOG(LOG_WARNING,
302             "initialize_tls_session_client(): possible path traversal: '%s'",
303             hostname);
304         return -1;
305     }
306     snprintf(path, sizeof(path), PROXY_SERVER_CERT_FORMAT, hostname);
307
308     result = gnutls_certificate_allocate_credentials(x509_cred);
309     if (GNUTLS_E_SUCCESS != result) {
310         LOG(LOG_ERROR,
311             "initialize_tls_session_client(): \
312 gnutls_certificate_allocate_credentials(): %s",
313             gnutls_strerror(result));
314         return -1;
315     }
316
317     /* Load proxy CA file, this CA "list" is send to the client. */
318     result = gnutls_certificate_set_x509_trust_file(*x509_cred,
319                                                     PROXY_CA_FILE,
320                                                     GNUTLS_X509_FMT_PEM);
321     if (0 >= result) {
322         LOG(LOG_ERROR,
323             "initialize_tls_session_client(): can't read CA file: '%s'",
324             PROXY_CA_FILE);
325         return -1;
326     }
327     /* And certificate for this website and proxy's private key. */
328     result = gnutls_certificate_set_x509_key_file(*x509_cred,
329                                                   path, PROXY_KEY_FILE,
330                                                   GNUTLS_X509_FMT_PEM);
331     if (GNUTLS_E_SUCCESS != result) {
332         LOG(LOG_ERROR,
333             "initialize_tls_session_client(): \
334 can't read server certificate ('%s') or key file ('%s'): %s",
335             path, PROXY_KEY_FILE, gnutls_strerror(result));
336         /* Could be a missing certificate. */
337         return -2;
338     }
339
340     gnutls_certificate_set_dh_params(*x509_cred, tls_dh_params);
341
342     result = gnutls_init(session, GNUTLS_SERVER);
343     if (GNUTLS_E_SUCCESS != result) {
344         LOG(LOG_ERROR,
345             "initialize_tls_session_client(): gnutls_init(): %s",
346             gnutls_strerror(result));
347         return -1;
348     }
349     result = gnutls_priority_set(*session, tls_priority_cache);
350     if (GNUTLS_E_SUCCESS != result) {
351         LOG(LOG_ERROR,
352             "initialize_tls_session_client(): gnutls_priority_set(): %s",
353             gnutls_strerror(result));
354         return -1;
355     }
356     result = gnutls_credentials_set(*session,
357                                     GNUTLS_CRD_CERTIFICATE, *x509_cred);
358     if (GNUTLS_E_SUCCESS != result) {
359         LOG(LOG_ERROR,
360             "initialize_tls_session_client(): gnutls_credentials_set(): %s",
361             gnutls_strerror(result));
362         return -1;
363     }
364
365     gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
366
367     return 0;
368 }
369 static int initialize_tls_session_server(int peer_socket,
370         gnutls_session_t *session,
371         gnutls_certificate_credentials_t *x509_cred) {
372     int result;
373
374     result = gnutls_certificate_allocate_credentials(x509_cred);
375     if (GNUTLS_E_SUCCESS != result) {
376         LOG(LOG_ERROR,
377             "initialize_tls_session_server(): \
378 gnutls_certificate_allocate_credentials(): %s",
379             gnutls_strerror(result));
380         return -1;
381     }
382
383     result = gnutls_init(session, GNUTLS_CLIENT);
384     if (GNUTLS_E_SUCCESS != result) {
385         LOG(LOG_ERROR,
386             "initialize_tls_session_server(): gnutls_init(): %s",
387             gnutls_strerror(result));
388         return -1;
389     }
390     gnutls_priority_set(*session, tls_priority_cache);
391     if (GNUTLS_E_SUCCESS != result) {
392         LOG(LOG_ERROR,
393             "initialize_tls_session_server(): gnutls_priority_set(): %s",
394             gnutls_strerror(result));
395         return -1;
396     }
397     result = gnutls_credentials_set(*session,
398                                     GNUTLS_CRD_CERTIFICATE, *x509_cred);
399     if (GNUTLS_E_SUCCESS != result) {
400         LOG(LOG_ERROR,
401             "initialize_tls_session_server(): gnutls_credentials_set(): %s",
402             gnutls_strerror(result));
403         return -1;
404     }
405
406     gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
407
408     return 0;
409 }
410
411
412 /* Read HTTP request line and headers (ignored).
413  *
414  * On success 0 is returned, -1 on client error, -2 on unexpected EOF.
415  */
416 static int read_http_request(FILE *client_fd, char *request, size_t length) {
417     char buffer[MAX_REQUEST_LINE];
418
419     if (NULL == fgets(request, (int)length, client_fd)) {
420         if (ferror(client_fd)) {
421             LOG_PERROR(LOG_WARNING, "read_http_request(): fgets()");
422             return -1;
423         }
424
425         return -2;
426     }
427
428     while (NULL != fgets(buffer, MAX_REQUEST_LINE, client_fd)) {
429         /* End of header. */
430         if (0 == strcmp(buffer, "\n") || 0 == strcmp(buffer, "\r\n")) {
431             break;
432         }
433     }
434     if (ferror(client_fd)) {
435         LOG_PERROR(LOG_WARNING, "read_http_request(): fgets()");
436         return -1;
437     }
438
439     return 0;
440 }
441
442 static void send_bad_request(FILE *client_fd) {
443     fprintf(client_fd, "HTTP/1.0 400 Bad Request\r\n");
444     fprintf(client_fd, "\r\n");
445 }
446 static void send_forwarding_failure(FILE *client_fd) {
447     fprintf(client_fd, "HTTP/1.0 503 Forwarding failure\r\n");
448     fprintf(client_fd, "\r\n");
449 }
450 static void tls_send_server_error(gnutls_session_t session) {
451     gnutls_record_send(session, "HTTP/1.0 500 Internal Server Error\r\n", 36);
452     gnutls_record_send(session, "\r\n", 2);
453 }
454
455
456 #if 0
457 /* Transfer data between client and server sockets until one closes the
458  * connection. */
459 static void transfer_data(int client, int server) {
460     struct pollfd fds[2];
461     fds[0].fd      = client;
462     fds[0].events  = POLLIN | POLLPRI | POLLHUP | POLLERR;
463     fds[0].revents = 0;
464     fds[1].fd      = server;
465     fds[1].events  = POLLIN | POLLPRI | POLLHUP | POLLERR;
466     fds[1].revents = 0;
467
468     for (;;) {
469         int result = poll(fds, 2, -1 /* no timeout */);
470         if (result < 0) {
471             LOG_PERROR(LOG_ERROR, "transfer_data(): poll()");
472             return;
473         }
474
475         /* Data available from client. */
476         if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
477             if (0 != read_from_write_to(client, server)) {
478                 /* EOF (or other error) */
479                 break;
480             }
481         }
482         /* Data available from server. */
483         if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
484             if (0 != read_from_write_to(server, client)) {
485                 /* EOF (or other error) */
486                 break;
487             }
488         }
489
490         /* Client closed connection. */
491         if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
492             break;
493         }
494         /* Server closed connection. */
495         if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
496             break;
497         }
498     }
499 }
500
501 /* Read available data from socket from and write it to socket to. At maximum
502  * 4096 bytes are read/written. */
503 static int read_from_write_to(int from, int to) {
504     ssize_t size_read;
505     ssize_t size_written;
506     char buffer[4096];
507
508     size_read = read(from, buffer, sizeof(buffer));
509     if (0 > size_read) {
510         LOG_PERROR(LOG_WARNING, "read_from_write_to(): read()");
511         return -1;
512     }
513     /* EOF */
514     if (0 == size_read) {
515         return -1;
516     }
517
518     size_written = write(to, buffer, (size_t)size_read);
519     if (0 > size_written) {
520         LOG_PERROR(LOG_WARNING, "read_from_write_to(): write()");
521         return -1;
522     }
523     if (size_read != size_written) {
524         LOG(LOG_ERROR, "read_from_write_to(): only written %ld of %ld bytes!",
525                        (long int)size_written, (long int)size_read);
526         return -1;
527     }
528
529     return 0;
530 }
531 #endif
532
533 /* Transfer data between client and server TLS connection until one closes the
534  * connection. */
535 static void transfer_data_tls(int client, int server,
536                               gnutls_session_t client_session,
537                               gnutls_session_t server_session) {
538     struct pollfd fds[2];
539     fds[0].fd      = client;
540     fds[0].events  = POLLIN | POLLPRI | POLLHUP | POLLERR;
541     fds[0].revents = 0;
542     fds[1].fd      = server;
543     fds[1].events  = POLLIN | POLLPRI | POLLHUP | POLLERR;
544     fds[1].revents = 0;
545
546     for (;;) {
547         int result = poll(fds, 2, -1 /* no timeout */);
548         if (result < 0) {
549             LOG_PERROR(LOG_ERROR, "transfer_data(): poll()");
550             return;
551         }
552
553         /* Data available from client. */
554         if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
555             if (0 != read_from_write_to_tls(client_session, server_session)) {
556                 /* EOF (or other error) */
557                 break;
558             }
559         }
560         /* Data available from server. */
561         if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
562             if (0 != read_from_write_to_tls(server_session, client_session)) {
563                 /* EOF (or other error) */
564                 break;
565             }
566         }
567
568         /* Client closed connection. */
569         if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
570             break;
571         }
572         /* Server closed connection. */
573         if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
574             break;
575         }
576     }
577 }
578
579 /* Read available data from session from and write to session to. */
580 static int read_from_write_to_tls(gnutls_session_t from,
581                                   gnutls_session_t to) {
582     size_t size;
583     ssize_t size_read;
584     ssize_t size_written;
585     char buffer[16384];
586
587     /* Get maximum possible buffer size. */
588     size = gnutls_record_get_max_size(from);
589     LOG(LOG_DEBUG, "read_from_write_to_tls(): suggested buffer size: %ld",
590                    (long int)size);
591     if (size > gnutls_record_get_max_size(to)) {
592         size = gnutls_record_get_max_size(to);
593     }
594     if (size > sizeof(buffer)) {
595         size = sizeof(buffer);
596     }
597     LOG(LOG_DEBUG, "read_from_write_to_tls(): used buffer size: %ld",
598                    (long int)size);
599
600     size_read = gnutls_record_recv(from, buffer, size);
601     if (0 > size_read) {
602         LOG(LOG_WARNING, "read_from_write_to_tls(): gnutls_record_recv(): %s",
603                          gnutls_strerror((int)size_read));
604         return -1;
605     }
606     /* EOF */
607     if (0 == size_read) {
608         return -1;
609     }
610
611     size_written = gnutls_record_send(to, buffer, (size_t)size_read);
612     if (0 > size_written) {
613         LOG(LOG_WARNING, "read_from_write_to_tls(): gnutls_record_send(): %s",
614                          gnutls_strerror((int)size_written));
615         return -1;
616     }
617     if (size_read != size_written) {
618         LOG(LOG_ERROR, "read_from_write_to_tls(): only written %ld of %ld bytes!",
619                        (long int)size_written, (long int)size_read);
620         return -1;
621     }
622
623     return 0;
624 }
625
626
627 static int connect_to_host(const char *hostname, const char *port) {
628     struct addrinfo gai_hints;
629     struct addrinfo *gai_result;
630     int gai_return;
631
632     int server_socket;
633     struct addrinfo *server;
634
635     if (NULL == hostname || NULL == port) {
636         return -1;
637     }
638
639     /* Get IP of hostname server. */
640     memset(&gai_hints, 0, sizeof(gai_hints));
641     gai_hints.ai_family   = AF_UNSPEC;
642     gai_hints.ai_socktype = SOCK_STREAM;
643     gai_hints.ai_protocol = 0;
644     gai_hints.ai_flags    = AI_NUMERICSERV /* given port is numeric */
645                           | AI_ADDRCONFIG  /* supported by this computer */
646                           | AI_V4MAPPED;   /* support IPv4 through IPv6 */
647     gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
648     if (0 != gai_return) {
649         LOG_PERROR(LOG_WARNING, "connect_to_host(): getaddrinfo()");
650         return -1;
651     }
652
653     /* Now try to connect to each server returned by getaddrinfo(), use the
654      * first successful connect. */
655     for (server = gai_result; NULL != server; server = server->ai_next) {
656         server_socket = socket(server->ai_family,
657                                server->ai_socktype,
658                                server->ai_protocol);
659         if (-1 == server_socket) {
660             LOG_PERROR(LOG_DEBUG, "connect_to_host(): socket(), trying next");
661             continue;
662         }
663
664         if (-1 != connect(server_socket, server->ai_addr,
665                                          server->ai_addrlen)) {
666             break;
667         }
668         LOG_PERROR(LOG_DEBUG, "connect_to_host(): connect(), trying next");
669
670         close(server_socket);
671     }
672     /* Make sure we free the result from getaddrinfo(). */
673     freeaddrinfo(gai_result);
674
675     if (NULL == server) {
676         LOG_PERROR(LOG_WARNING, "connect_to_host(): no server found, abort");
677         return -1;
678     }
679
680     return server_socket;
681 }
682
683
684 /* Parse HTTP CONNECT request string and save its parameters.
685  *
686  * The following format is expected: "CONNECT host:port HTTP/1.x".
687  *
688  * request and host must have the same size! port must be at least 6 bytes
689  * long (5 + '\0').
690  */
691 static int parse_request(const char *request, char *host, char *port,
692                                               int *version_minor) {
693     int port_unused; /* just used to verify the port is numeric */
694     char *position;
695
696     /* scanf() doesn't check spaces. */
697     if (0 != strncmp(request, "CONNECT ", 8)) {
698         return -1;
699     }
700     /* Check request and extract data, "host:port" is not yet separated. */
701     if (2 != sscanf(request, "CONNECT %s HTTP/1.%d",
702                              host, version_minor)) {
703         return -1;
704     }
705     /* Make sure ":port" is there. */
706     if (NULL == (position = strchr(host, ':'))) {
707         return -1;
708     }
709     /* Make sure port is numeric. */
710     if (1 != sscanf(position + 1, "%d", &port_unused)) {
711         return -1;
712     }
713     /* Store it in *port. */
714     strncpy(port, position + 1, 5);
715     port[5] = '\0';
716     /* And remove port from host. */
717     *position = '\0';
718
719     return 0;
720 }