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