]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/blob - src/connection.c
src/connection.c: Move path related defines to tlsproxy.h.
[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. */
241     if (0 != server_session_started) {
242         gnutls_bye(server_session, GNUTLS_SHUT_WR);
243     }
244     if (0 != client_session_started) {
245         gnutls_bye(client_session, GNUTLS_SHUT_WR);
246     }
247     if (0 != server_session_init) {
248         gnutls_deinit(server_session);
249         gnutls_certificate_free_credentials(server_x509_cred);
250     }
251     if (0 != client_session_init) {
252         gnutls_deinit(client_session);
253         gnutls_certificate_free_cas(client_x509_cred);
254         gnutls_certificate_free_keys(client_x509_cred);
255         gnutls_certificate_free_credentials(client_x509_cred);
256     }
257
258     /* Close connection to server/proxy. */
259     if (NULL != server_fd) {
260         fclose(server_fd);
261     } else if (-1 != server_socket) {
262         close(server_socket);
263     }
264     LOG(LOG_DEBUG, "connection to server closed");
265     /* Close connection to client. */
266     if (NULL != client_fd) {
267         fclose(client_fd);
268     } else {
269         close(client_socket);
270     }
271     LOG(LOG_DEBUG, "connection to client closed");
272
273     LOG(LOG_DEBUG, "connection finished");
274 }
275
276
277 static int initialize_tls_session_client(int peer_socket,
278         const char *hostname,
279         gnutls_session_t *session,
280         gnutls_certificate_credentials_t *x509_cred) {
281     int result;
282     char path[1024];
283
284     /* Hostname too long. */
285     if (sizeof(path) - strlen(PROXY_SERVER_CERT_FORMAT) <= strlen(hostname)) {
286         LOG(LOG_WARNING,
287             "initialize_tls_session_client(): hostname too long: '%s'",
288             hostname);
289         return -1;
290     }
291     /* Try to prevent path traversals in hostnames. */
292     if (NULL != strstr(hostname, "..")) {
293         LOG(LOG_WARNING,
294             "initialize_tls_session_client(): possible path traversal: '%s'",
295             hostname);
296         return -1;
297     }
298     snprintf(path, sizeof(path), PROXY_SERVER_CERT_FORMAT, hostname);
299
300     result = gnutls_certificate_allocate_credentials(x509_cred);
301     if (GNUTLS_E_SUCCESS != result) {
302         LOG(LOG_ERROR,
303             "initialize_tls_session_client(): \
304 gnutls_certificate_allocate_credentials(): %s",
305             gnutls_strerror(result));
306         return -1;
307     }
308
309     /* Load proxy CA file, this CA "list" is send to the client. */
310     result = gnutls_certificate_set_x509_trust_file(*x509_cred,
311                                                     PROXY_CA_FILE,
312                                                     GNUTLS_X509_FMT_PEM);
313     if (0 >= result) {
314         LOG(LOG_ERROR,
315             "initialize_tls_session_client(): can't read CA file: '%s'",
316             PROXY_CA_FILE);
317         return -1;
318     }
319     /* And certificate for this website and proxy's private key. */
320     result = gnutls_certificate_set_x509_key_file(*x509_cred,
321                                                   path, PROXY_KEY_FILE,
322                                                   GNUTLS_X509_FMT_PEM);
323     if (GNUTLS_E_SUCCESS != result) {
324         LOG(LOG_ERROR,
325             "initialize_tls_session_client(): \
326 can't read server certificate ('%s') or key file ('%s'): %s",
327             path, PROXY_KEY_FILE, gnutls_strerror(result));
328         /* Could be a missing certificate. */
329         return -2;
330     }
331
332     gnutls_certificate_set_dh_params(*x509_cred, tls_dh_params);
333
334     result = gnutls_init(session, GNUTLS_SERVER);
335     if (GNUTLS_E_SUCCESS != result) {
336         LOG(LOG_ERROR,
337             "initialize_tls_session_client(): gnutls_init(): %s",
338             gnutls_strerror(result));
339         return -1;
340     }
341     result = gnutls_priority_set(*session, tls_priority_cache);
342     if (GNUTLS_E_SUCCESS != result) {
343         LOG(LOG_ERROR,
344             "initialize_tls_session_client(): gnutls_priority_set(): %s",
345             gnutls_strerror(result));
346         return -1;
347     }
348     result = gnutls_credentials_set(*session,
349                                     GNUTLS_CRD_CERTIFICATE, *x509_cred);
350     if (GNUTLS_E_SUCCESS != result) {
351         LOG(LOG_ERROR,
352             "initialize_tls_session_client(): gnutls_credentials_set(): %s",
353             gnutls_strerror(result));
354         return -1;
355     }
356
357     gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
358
359     return 0;
360 }
361 static int initialize_tls_session_server(int peer_socket,
362         gnutls_session_t *session,
363         gnutls_certificate_credentials_t *x509_cred) {
364     int result;
365
366     result = gnutls_certificate_allocate_credentials(x509_cred);
367     if (GNUTLS_E_SUCCESS != result) {
368         LOG(LOG_ERROR,
369             "initialize_tls_session_server(): \
370 gnutls_certificate_allocate_credentials(): %s",
371             gnutls_strerror(result));
372         return -1;
373     }
374
375     result = gnutls_init(session, GNUTLS_CLIENT);
376     if (GNUTLS_E_SUCCESS != result) {
377         LOG(LOG_ERROR,
378             "initialize_tls_session_server(): gnutls_init(): %s",
379             gnutls_strerror(result));
380         return -1;
381     }
382     gnutls_priority_set(*session, tls_priority_cache);
383     if (GNUTLS_E_SUCCESS != result) {
384         LOG(LOG_ERROR,
385             "initialize_tls_session_server(): gnutls_priority_set(): %s",
386             gnutls_strerror(result));
387         return -1;
388     }
389     result = gnutls_credentials_set(*session,
390                                     GNUTLS_CRD_CERTIFICATE, *x509_cred);
391     if (GNUTLS_E_SUCCESS != result) {
392         LOG(LOG_ERROR,
393             "initialize_tls_session_server(): gnutls_credentials_set(): %s",
394             gnutls_strerror(result));
395         return -1;
396     }
397
398     gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
399
400     return 0;
401 }
402
403
404 /* Read HTTP request line and headers (ignored).
405  *
406  * On success 0 is returned, -1 on client error, -2 on unexpected EOF.
407  */
408 static int read_http_request(FILE *client_fd, char *request, size_t length) {
409     char buffer[MAX_REQUEST_LINE];
410
411     if (NULL == fgets(request, (int)length, client_fd)) {
412         if (ferror(client_fd)) {
413             LOG_PERROR(LOG_WARNING, "read_http_request(): fgets()");
414             return -1;
415         }
416
417         return -2;
418     }
419
420     while (NULL != fgets(buffer, MAX_REQUEST_LINE, client_fd)) {
421         /* End of header. */
422         if (0 == strcmp(buffer, "\n") || 0 == strcmp(buffer, "\r\n")) {
423             break;
424         }
425     }
426     if (ferror(client_fd)) {
427         LOG_PERROR(LOG_WARNING, "read_http_request(): fgets()");
428         return -1;
429     }
430
431     return 0;
432 }
433
434 static void send_bad_request(FILE *client_fd) {
435     fprintf(client_fd, "HTTP/1.0 400 Bad Request\r\n");
436     fprintf(client_fd, "\r\n");
437 }
438 static void send_forwarding_failure(FILE *client_fd) {
439     fprintf(client_fd, "HTTP/1.0 503 Forwarding failure\r\n");
440     fprintf(client_fd, "\r\n");
441 }
442
443
444 #if 0
445 /* Transfer data between client and server sockets until one closes the
446  * connection. */
447 static void transfer_data(int client, int server) {
448     struct pollfd fds[2];
449     fds[0].fd      = client;
450     fds[0].events  = POLLIN | POLLPRI | POLLHUP | POLLERR;
451     fds[0].revents = 0;
452     fds[1].fd      = server;
453     fds[1].events  = POLLIN | POLLPRI | POLLHUP | POLLERR;
454     fds[1].revents = 0;
455
456     for (;;) {
457         int result = poll(fds, 2, -1 /* no timeout */);
458         if (result < 0) {
459             LOG_PERROR(LOG_ERROR, "transfer_data(): poll()");
460             return;
461         }
462
463         /* Data available from client. */
464         if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
465             if (0 != read_from_write_to(client, server)) {
466                 /* EOF (or other error) */
467                 break;
468             }
469         }
470         /* Data available from server. */
471         if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
472             if (0 != read_from_write_to(server, client)) {
473                 /* EOF (or other error) */
474                 break;
475             }
476         }
477
478         /* Client closed connection. */
479         if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
480             break;
481         }
482         /* Server closed connection. */
483         if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
484             break;
485         }
486     }
487 }
488
489 /* Read available data from socket from and write it to socket to. At maximum
490  * 4096 bytes are read/written. */
491 static int read_from_write_to(int from, int to) {
492     ssize_t size_read;
493     ssize_t size_written;
494     char buffer[4096];
495
496     size_read = read(from, buffer, sizeof(buffer));
497     if (0 > size_read) {
498         LOG_PERROR(LOG_WARNING, "read_from_write_to(): read()");
499         return -1;
500     }
501     /* EOF */
502     if (0 == size_read) {
503         return -1;
504     }
505
506     size_written = write(to, buffer, (size_t)size_read);
507     if (0 > size_written) {
508         LOG_PERROR(LOG_WARNING, "read_from_write_to(): write()");
509         return -1;
510     }
511     if (size_read != size_written) {
512         LOG(LOG_ERROR, "read_from_write_to(): only written %ld of %ld bytes!",
513                        (long int)size_written, (long int)size_read);
514         return -1;
515     }
516
517     return 0;
518 }
519 #endif
520
521 /* Transfer data between client and server TLS connection until one closes the
522  * connection. */
523 static void transfer_data_tls(int client, int server,
524                               gnutls_session_t client_session,
525                               gnutls_session_t server_session) {
526     struct pollfd fds[2];
527     fds[0].fd      = client;
528     fds[0].events  = POLLIN | POLLPRI | POLLHUP | POLLERR;
529     fds[0].revents = 0;
530     fds[1].fd      = server;
531     fds[1].events  = POLLIN | POLLPRI | POLLHUP | POLLERR;
532     fds[1].revents = 0;
533
534     for (;;) {
535         int result = poll(fds, 2, -1 /* no timeout */);
536         if (result < 0) {
537             LOG_PERROR(LOG_ERROR, "transfer_data(): poll()");
538             return;
539         }
540
541         /* Data available from client. */
542         if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
543             if (0 != read_from_write_to_tls(client_session, server_session)) {
544                 /* EOF (or other error) */
545                 break;
546             }
547         }
548         /* Data available from server. */
549         if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
550             if (0 != read_from_write_to_tls(server_session, client_session)) {
551                 /* EOF (or other error) */
552                 break;
553             }
554         }
555
556         /* Client closed connection. */
557         if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
558             break;
559         }
560         /* Server closed connection. */
561         if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
562             break;
563         }
564     }
565 }
566
567 /* Read available data from session from and write to session to. */
568 static int read_from_write_to_tls(gnutls_session_t from,
569                                   gnutls_session_t to) {
570     size_t size;
571     ssize_t size_read;
572     ssize_t size_written;
573     char buffer[16384];
574
575     /* Get maximum possible buffer size. */
576     size = gnutls_record_get_max_size(from);
577     LOG(LOG_DEBUG, "read_from_write_to_tls(): suggested buffer size: %ld",
578                    (long int)size);
579     if (size > gnutls_record_get_max_size(to)) {
580         size = gnutls_record_get_max_size(to);
581     }
582     if (size > sizeof(buffer)) {
583         size = sizeof(buffer);
584     }
585     LOG(LOG_DEBUG, "read_from_write_to_tls(): used buffer size: %ld",
586                    (long int)size);
587
588     size_read = gnutls_record_recv(from, buffer, size);
589     if (0 > size_read) {
590         LOG(LOG_WARNING, "read_from_write_to_tls(): gnutls_record_recv(): %s",
591                          gnutls_strerror((int)size_read));
592         return -1;
593     }
594     /* EOF */
595     if (0 == size_read) {
596         return -1;
597     }
598
599     size_written = gnutls_record_send(to, buffer, (size_t)size_read);
600     if (0 > size_written) {
601         LOG(LOG_WARNING, "read_from_write_to_tls(): gnutls_record_send(): %s",
602                          gnutls_strerror((int)size_written));
603         return -1;
604     }
605     if (size_read != size_written) {
606         LOG(LOG_ERROR, "read_from_write_to_tls(): only written %ld of %ld bytes!",
607                        (long int)size_written, (long int)size_read);
608         return -1;
609     }
610
611     return 0;
612 }
613
614
615 static int connect_to_host(const char *hostname, const char *port) {
616     struct addrinfo gai_hints;
617     struct addrinfo *gai_result;
618     int gai_return;
619
620     int server_socket;
621     struct addrinfo *server;
622
623     if (NULL == hostname || NULL == port) {
624         return -1;
625     }
626
627     /* Get IP of hostname server. */
628     memset(&gai_hints, 0, sizeof(gai_hints));
629     gai_hints.ai_family   = AF_UNSPEC;
630     gai_hints.ai_socktype = SOCK_STREAM;
631     gai_hints.ai_protocol = 0;
632     gai_hints.ai_flags    = AI_NUMERICSERV /* given port is numeric */
633                           | AI_ADDRCONFIG  /* supported by this computer */
634                           | AI_V4MAPPED;   /* support IPv4 through IPv6 */
635     gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
636     if (0 != gai_return) {
637         LOG_PERROR(LOG_WARNING, "connect_to_host(): getaddrinfo()");
638         return -1;
639     }
640
641     /* Now try to connect to each server returned by getaddrinfo(), use the
642      * first successful connect. */
643     for (server = gai_result; NULL != server; server = server->ai_next) {
644         server_socket = socket(server->ai_family,
645                                server->ai_socktype,
646                                server->ai_protocol);
647         if (-1 == server_socket) {
648             LOG_PERROR(LOG_DEBUG, "connect_to_host(): socket(), trying next");
649             continue;
650         }
651
652         if (-1 != connect(server_socket, server->ai_addr,
653                                          server->ai_addrlen)) {
654             break;
655         }
656         LOG_PERROR(LOG_DEBUG, "connect_to_host(): connect(), trying next");
657
658         close(server_socket);
659     }
660     /* Make sure we free the result from getaddrinfo(). */
661     freeaddrinfo(gai_result);
662
663     if (NULL == server) {
664         LOG_PERROR(LOG_WARNING, "connect_to_host(): no server found, abort");
665         return -1;
666     }
667
668     return server_socket;
669 }
670
671
672 /* Parse HTTP CONNECT request string and save its parameters.
673  *
674  * The following format is expected: "CONNECT host:port HTTP/1.x".
675  *
676  * request and host must have the same size! port must be at least 6 bytes
677  * long (5 + '\0').
678  */
679 static int parse_request(const char *request, char *host, char *port,
680                                               int *version_minor) {
681     int port_unused; /* just used to verify the port is numeric */
682     char *position;
683
684     /* scanf() doesn't check spaces. */
685     if (0 != strncmp(request, "CONNECT ", 8)) {
686         return -1;
687     }
688     /* Check request and extract data, "host:port" is not yet separated. */
689     if (2 != sscanf(request, "CONNECT %s HTTP/1.%d",
690                              host, version_minor)) {
691         return -1;
692     }
693     /* Make sure ":port" is there. */
694     if (NULL == (position = strchr(host, ':'))) {
695         return -1;
696     }
697     /* Make sure port is numeric. */
698     if (1 != sscanf(position + 1, "%d", &port_unused)) {
699         return -1;
700     }
701     /* Store it in *port. */
702     strncpy(port, position + 1, 5);
703     port[5] = '\0';
704     /* And remove port from host. */
705     *position = '\0';
706
707     return 0;
708 }