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"
31 /* Maximum length of a HTTP request line. Longer request lines are aborted
32 * with an error. The standard doesn't specify a maximum line length but this
33 * should be a good limit to make processing simpler. As HTTPS is used this
34 * doesn't limit long GET requests. */
35 #define MAX_REQUEST_LINE 4096
37 /* Format string used to send HTTP/1.0 error responses to the client.
39 * %s is used 5 times, first is the error code, then additional headers, next
40 * two are the error code (no %n$s!), the last is the message. */
41 #define HTTP_RESPONSE_FORMAT "HTTP/1.0 %s\r\n\
42 Content-Type: text/html; charset=US-ASCII\r\n\
44 <!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n\
46 <head><title>%s</title></head>\n\
54 static int initialize_tls_session_client(int peer_socket,
56 gnutls_session_t *session,
57 gnutls_certificate_credentials_t *x509_cred);
58 static int initialize_tls_session_server(int peer_socket,
59 gnutls_session_t *session,
60 gnutls_certificate_credentials_t *x509_cred);
62 static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd);
63 static int read_http_request(FILE *client_fd, char *request, size_t length);
64 static void send_bad_request(FILE *client_fd);
65 static void send_forwarding_failure(FILE *client_fd);
66 static void tls_send_invalid_cert_message(gnutls_session_t session);
68 static void transfer_data(int client, int server);
69 static int read_from_write_to(int from, int to);
70 static void transfer_data_tls(int client, int server,
71 gnutls_session_t client_session,
72 gnutls_session_t server_session);
73 static int read_from_write_to_tls(gnutls_session_t from, gnutls_session_t to,
76 static int connect_to_host(const char *hostname, const char *port);
78 static int parse_request(const char *buffer, char *host, char *port,
82 void handle_connection(int client_socket) {
84 FILE *client_fd_read, *client_fd_write, *server_fd_read, *server_fd_write;
86 char buffer[MAX_REQUEST_LINE];
87 char host[MAX_REQUEST_LINE];
90 int version_minor; /* x in HTTP/1.x */
93 /* client_x509_cred is used when talking to the client (acting as a TSL
94 * server), server_x509_cred is used when talking to the server (acting as
96 gnutls_certificate_credentials_t client_x509_cred, server_x509_cred;
98 gnutls_session_t client_session, server_session;
99 /* initialize_tls_session_*() called? - used for goto out */
100 int client_session_init, server_session_init;
101 /* gnutls_handshake() called? - used for goto out */
102 int client_session_started, server_session_started;
103 /* Validation failed? If yes we need to send the special "invalid"
105 int validation_failed;
107 LOG(DEBUG, "new connection");
110 client_fd_read = NULL;
111 client_fd_write = NULL;
112 server_fd_read = NULL;
113 server_fd_write = NULL;
114 client_session_init = 0;
115 server_session_init = 0;
116 client_session_started = 0;
117 server_session_started = 0;
118 validation_failed = 0;
120 if (fdopen_read_write(client_socket, &client_fd_read,
121 &client_fd_write) != 0) {
125 /* Read request line (CONNECT ..) and headers (they are discarded). */
126 result = read_http_request(client_fd_read, buffer, sizeof(buffer));
128 LOG(WARNING, "read_http_request(): client read error");
130 } else if (result == -2) {
131 LOG(WARNING, "read_http_request(): client EOF");
132 send_bad_request(client_fd_write);
136 if (parse_request(buffer, host, port, &version_minor) != 0) {
137 LOG(WARNING, "bad request: %s", buffer);
138 send_bad_request(client_fd_write);
142 LOG(DEBUG, "target: %s:%s (HTTP 1.%d)", host, port, version_minor);
144 /* Connect to proxy server or directly to server. */
145 if (global_proxy_host != NULL && global_proxy_port != NULL) {
146 LOG(DEBUG, "connecting to %s:%s", global_proxy_host,
148 server_socket = connect_to_host(global_proxy_host, global_proxy_port);
150 LOG(DEBUG, "connecting to %s:%s", host, port);
151 server_socket = connect_to_host(host, port);
154 if (server_socket == -1) {
155 LOG(WARNING, "failed to connect to server");
156 send_forwarding_failure(client_fd_write);
159 if (fdopen_read_write(server_socket, &server_fd_read,
160 &server_fd_write) != 0) {
161 send_forwarding_failure(client_fd_write);
165 /* Connect to proxy if requested (command line option). */
166 if (global_proxy_host != NULL && global_proxy_port != NULL) {
167 fprintf(server_fd_write, "CONNECT %s:%s HTTP/1.0\r\n", host, port);
168 fprintf(server_fd_write, "\r\n");
169 fflush(server_fd_write);
171 /* Read response line from proxy server. */
172 result = read_http_request(server_fd_read, buffer, sizeof(buffer));
174 LOG(WARNING, "read_http_request(): proxy read error");
175 send_forwarding_failure(client_fd_write);
177 } else if (result == -2) {
178 LOG(WARNING, "read_http_request(): proxy EOF");
179 send_forwarding_failure(client_fd_write);
183 /* Check response of proxy server. */
184 if (strncmp(buffer, "HTTP/1.0 200", 12)) {
185 LOG(WARNING, "bad proxy response: %s", buffer);
186 send_forwarding_failure(client_fd_write);
191 LOG(DEBUG, "connection to server established");
193 /* If the -u option is used and we don't know this hostname's server
194 * certificate then just pass through the connection and let the client
195 * verify the server certificate. */
196 if (global_passthrough_unknown) {
197 char path[TLSPROXY_MAX_PATH_LENGTH];
200 if (server_certificate_file(&file, host, path, sizeof(path)) == -2) {
201 /* We've established a connection, tell the client. */
202 fprintf(client_fd_write, "HTTP/1.0 200 Connection established\r\n");
203 fprintf(client_fd_write, "\r\n");
204 fflush(client_fd_write);
206 LOG(DEBUG, "transferring data");
208 /* Proxy data between client and server until one side is done
210 transfer_data(client_socket, server_socket);
212 LOG(DEBUG, "finished transferring data");
216 /* server_certificate_file() may have opened the file, close it. */
222 /* Initialize TLS client credentials to talk to the server. */
223 result = initialize_tls_session_server(server_socket, &server_session,
226 LOG(WARNING, "initialize_tls_session_server() failed");
227 send_forwarding_failure(client_fd_write);
230 server_session_init = 1;
232 LOG(DEBUG, "starting server TLS handshake");
234 /* Try to establish TLS handshake between us and server. */
235 result = gnutls_handshake(server_session);
236 if (result != GNUTLS_E_SUCCESS) {
237 LOG(WARNING, "server TLS handshake failed: %s",
238 gnutls_strerror(result));
239 send_forwarding_failure(client_fd_write);
242 server_session_started = 1;
244 LOG(DEBUG, "server TLS handshake finished");
246 /* Make sure the server certificate is valid and known. */
247 if (verify_tls_connection(server_session, host) != 0) {
248 LOG(ERROR, "server certificate validation failed!");
249 /* We'll send the error message over our TLS connection to the client,
250 * but with an invalid certificate. No data is transfered from/to the
252 validation_failed = 1;
255 /* Initialize TLS server credentials to talk to the client. */
256 result = initialize_tls_session_client(client_socket,
257 /* use a special host if the server
258 * certificate was invalid */
259 (validation_failed) ? "invalid"
264 LOG(WARNING, "initialize_tls_session_client() failed");
265 send_forwarding_failure(client_fd_write);
268 client_session_init = 1;
270 /* We've established a connection, tell the client. */
271 fprintf(client_fd_write, "HTTP/1.0 200 Connection established\r\n");
272 fprintf(client_fd_write, "\r\n");
273 fflush(client_fd_write);
275 LOG(DEBUG, "starting client TLS handshake");
277 /* Try to establish TLS handshake between client and us. */
278 result = gnutls_handshake(client_session);
279 if (result != GNUTLS_E_SUCCESS) {
280 LOG(WARNING, "client TLS handshake failed: %s",
281 gnutls_strerror(result));
282 send_forwarding_failure(client_fd_write);
285 client_session_started = 1;
287 LOG(DEBUG, "client TLS handshake finished");
289 /* Tell the client that the verification failed. Shouldn't be necessary as
290 * the client should terminate the connection because he received the
291 * invalid certificate but better be sure. */
292 if (validation_failed) {
293 tls_send_invalid_cert_message(client_session);
297 LOG(DEBUG, "transferring TLS data");
299 /* Proxy data between client and server until one side is done (EOF or
301 transfer_data_tls(client_socket, server_socket,
302 client_session, server_session);
304 LOG(DEBUG, "finished transferring TLS data");
307 /* Close TLS sessions if necessary. Use GNUTLS_SHUT_RDWR so the data is
308 * reliable transmitted. */
309 if (server_session_started) {
310 gnutls_bye(server_session, GNUTLS_SHUT_RDWR);
312 if (client_session_started) {
313 gnutls_bye(client_session, GNUTLS_SHUT_RDWR);
315 if (server_session_init) {
316 gnutls_deinit(server_session);
317 gnutls_certificate_free_credentials(server_x509_cred);
319 if (client_session_init) {
320 gnutls_deinit(client_session);
321 gnutls_certificate_free_cas(client_x509_cred);
322 gnutls_certificate_free_keys(client_x509_cred);
323 gnutls_certificate_free_credentials(client_x509_cred);
326 /* Close connection to server/proxy. */
327 if (server_fd_read != NULL) {
328 if (server_fd_write != NULL) {
329 fclose(server_fd_write);
331 fclose(server_fd_read);
332 } else if (server_socket != -1) {
333 close(server_socket);
335 LOG(DEBUG, "connection to server closed");
336 /* Close connection to client. */
337 if (client_fd_read != NULL) {
338 if (client_fd_write != NULL) {
339 fclose(client_fd_write);
341 fclose(client_fd_read);
343 close(client_socket);
345 LOG(DEBUG, "connection to client closed");
347 LOG(DEBUG, "connection finished");
351 static int initialize_tls_session_client(int peer_socket,
352 const char *hostname,
353 gnutls_session_t *session,
354 gnutls_certificate_credentials_t *x509_cred) {
356 int use_invalid_cert;
357 char path[TLSPROXY_MAX_PATH_LENGTH];
359 /* The "invalid" hostname is special. If it's used we send an invalid
360 * certificate to let the client know something is wrong. */
361 use_invalid_cert = (!strcmp(hostname, "invalid"));
363 if (proxy_certificate_path(hostname, path, sizeof(path)) != 0) {
365 "initialize_tls_session_client(): \
366 failed to get proxy certificate path");
370 result = gnutls_certificate_allocate_credentials(x509_cred);
371 if (result != GNUTLS_E_SUCCESS) {
373 "initialize_tls_session_client(): \
374 gnutls_certificate_allocate_credentials(): %s",
375 gnutls_strerror(result));
379 /* Load proxy CA file, this CA "list" is send to the client. */
380 if (!use_invalid_cert) {
381 result = gnutls_certificate_set_x509_trust_file(*x509_cred,
383 GNUTLS_X509_FMT_PEM);
386 "initialize_tls_session_client(): can't read CA file: '%s'",
388 gnutls_certificate_free_credentials(*x509_cred);
392 /* If the invalid hostname was specified do nothing, we use a self-signed
393 * certificate in this case. */
395 /* And certificate for this website and proxy's private key. */
396 if (!use_invalid_cert) {
397 result = gnutls_certificate_set_x509_key_file(*x509_cred,
398 path, PROXY_KEY_FILE,
399 GNUTLS_X509_FMT_PEM);
400 /* If the invalid hostname was specified load our special "invalid"
403 result = gnutls_certificate_set_x509_key_file(*x509_cred,
404 PROXY_INVALID_CERT_FILE,
406 GNUTLS_X509_FMT_PEM);
408 if (result != GNUTLS_E_SUCCESS) {
410 "initialize_tls_session_client(): \
411 can't read server certificate ('%s') or key file ('%s'): %s",
412 path, PROXY_KEY_FILE, gnutls_strerror(result));
413 gnutls_certificate_free_credentials(*x509_cred);
414 /* Could be a missing certificate. */
418 gnutls_certificate_set_dh_params(*x509_cred, global_tls_dh_params);
420 result = gnutls_init(session, GNUTLS_SERVER);
421 if (result != GNUTLS_E_SUCCESS) {
423 "initialize_tls_session_client(): gnutls_init(): %s",
424 gnutls_strerror(result));
425 gnutls_certificate_free_credentials(*x509_cred);
428 result = gnutls_priority_set(*session, global_tls_priority_cache);
429 if (result != GNUTLS_E_SUCCESS) {
431 "initialize_tls_session_client(): gnutls_priority_set(): %s",
432 gnutls_strerror(result));
433 gnutls_deinit(*session);
434 gnutls_certificate_free_credentials(*x509_cred);
437 result = gnutls_credentials_set(*session,
438 GNUTLS_CRD_CERTIFICATE, *x509_cred);
439 if (result != GNUTLS_E_SUCCESS) {
441 "initialize_tls_session_client(): gnutls_credentials_set(): %s",
442 gnutls_strerror(result));
443 gnutls_deinit(*session);
444 gnutls_certificate_free_credentials(*x509_cred);
448 gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
452 static int initialize_tls_session_server(int peer_socket,
453 gnutls_session_t *session,
454 gnutls_certificate_credentials_t *x509_cred) {
457 result = gnutls_certificate_allocate_credentials(x509_cred);
458 if (result != GNUTLS_E_SUCCESS) {
460 "initialize_tls_session_server(): \
461 gnutls_certificate_allocate_credentials(): %s",
462 gnutls_strerror(result));
466 result = gnutls_init(session, GNUTLS_CLIENT);
467 if (result != GNUTLS_E_SUCCESS) {
469 "initialize_tls_session_server(): gnutls_init(): %s",
470 gnutls_strerror(result));
471 gnutls_certificate_free_credentials(*x509_cred);
474 result = gnutls_priority_set(*session, global_tls_priority_cache);
475 if (result != GNUTLS_E_SUCCESS) {
477 "initialize_tls_session_server(): gnutls_priority_set(): %s",
478 gnutls_strerror(result));
479 gnutls_deinit(*session);
480 gnutls_certificate_free_credentials(*x509_cred);
483 result = gnutls_credentials_set(*session,
484 GNUTLS_CRD_CERTIFICATE, *x509_cred);
485 if (result != GNUTLS_E_SUCCESS) {
487 "initialize_tls_session_server(): gnutls_credentials_set(): %s",
488 gnutls_strerror(result));
489 gnutls_deinit(*session);
490 gnutls_certificate_free_credentials(*x509_cred);
494 gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
500 static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd) {
501 *read_fd = fdopen(socket, "r");
502 if (*read_fd == NULL) {
503 LOG_PERROR(WARNING, "fdopen_read_write(): fdopen(\"r\") failed");
507 *write_fd = fdopen(dup(socket), "w");
508 if (*write_fd == NULL) {
509 LOG_PERROR(WARNING, "fdopen_read_write(): fdopen(\"w\") failed");
511 *read_fd = NULL; /* "tell" caller read_fd is already closed */
518 /* Read HTTP request line and headers (ignored).
520 * On success 0 is returned, -1 on client error, -2 on unexpected EOF.
522 static int read_http_request(FILE *client_fd, char *request, size_t length) {
523 char buffer[MAX_REQUEST_LINE];
525 if (fgets(request, (int)length, client_fd) == NULL) {
526 if (ferror(client_fd)) {
527 LOG_PERROR(WARNING, "read_http_request(): fgets()");
534 while (fgets(buffer, sizeof(buffer), client_fd) != NULL) {
536 if (!strcmp(buffer, "\n") || !strcmp(buffer, "\r\n")) {
540 if (ferror(client_fd)) {
541 LOG_PERROR(WARNING, "read_http_request(): fgets()");
548 static void send_bad_request(FILE *client_fd) {
549 const char error[] = "400 Bad Request";
550 const char msg[] = "Your browser sent an invalid request.";
551 fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, "", error, error, msg);
554 static void send_forwarding_failure(FILE *client_fd) {
555 const char error[] = "503 Forwarding failure";
556 const char msg[] = "Failed to connect to server, check logs.";
557 fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, "", error, error, msg);
560 static void tls_send_invalid_cert_message(gnutls_session_t session) {
561 const char error[] = "500 Internal Server Error";
562 const char msg[] = "Server certificate validation failed, check logs.";
565 char buffer[sizeof(HTTP_RESPONSE_FORMAT)
566 + 3 * sizeof(error) + sizeof(msg)];
568 result = snprintf(buffer, sizeof(buffer), HTTP_RESPONSE_FORMAT,
569 error, "", error, error, msg);
570 assert(result > 0 && (size_t)result < sizeof(buffer));
572 gnutls_record_send(session, buffer, strlen(buffer));
576 /* Transfer data between client and server sockets until one closes the
578 static void transfer_data(int client, int server) {
579 struct pollfd fds[2];
581 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
584 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
588 int result = poll(fds, 2 /* fd count */, -1 /* no timeout */);
590 LOG_PERROR(ERROR, "transfer_data(): poll()");
594 /* Data available from client. */
595 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
596 if (read_from_write_to(client, server) != 0) {
597 /* EOF (or other error) */
601 /* Data available from server. */
602 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
603 if (read_from_write_to(server, client) != 0) {
604 /* EOF (or other error) */
609 /* Client closed connection. */
610 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
613 /* Server closed connection. */
614 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
620 /* Read available data from socket from and write it to socket to. At maximum
621 * 4096 bytes are read/written. */
622 static int read_from_write_to(int from, int to) {
624 ssize_t size_written;
627 LOG(DEBUG, "read_from_write_to(): %d -> %d", from, to);
629 size_read = read(from, buffer, sizeof(buffer));
631 LOG_PERROR(WARNING, "read_from_write_to(): read()");
634 } else if (size_read == 0) {
638 size_written = write(to, buffer, (size_t)size_read);
639 if (size_written < 0) {
640 LOG_PERROR(WARNING, "read_from_write_to(): write()");
643 if (size_read != size_written) {
644 LOG(ERROR, "read_from_write_to(): only written %ld of %ld bytes!",
645 (long int)size_written, (long int)size_read);
652 /* Transfer data between client and server TLS connection until one closes the
654 static void transfer_data_tls(int client, int server,
655 gnutls_session_t client_session,
656 gnutls_session_t server_session) {
659 struct pollfd fds[2];
661 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
664 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
667 /* Get maximum possible buffer size. */
668 buffer_size = gnutls_record_get_max_size(client_session);
669 if (gnutls_record_get_max_size(server_session) < buffer_size) {
670 buffer_size = gnutls_record_get_max_size(server_session);
672 LOG(DEBUG, "transfer_data_tls(): suggested buffer size: %ld",
673 (long int)buffer_size);
676 int result = poll(fds, 2 /* fd count */, -1 /* no timeout */);
678 LOG_PERROR(ERROR, "transfer_data(): poll()");
682 /* Data available from client. */
683 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
684 if (read_from_write_to_tls(client_session, server_session,
686 /* EOF (or other error) */
690 /* Data available from server. */
691 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
692 if (read_from_write_to_tls(server_session, client_session,
694 /* EOF (or other error) */
699 /* Client closed connection. */
700 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
703 /* Server closed connection. */
704 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
710 /* Read available data from session from and write to session to. */
711 static int read_from_write_to_tls(gnutls_session_t from,
713 size_t buffer_size) {
715 ssize_t size_written;
718 if (buffer_size > sizeof(buffer)) {
719 buffer_size = sizeof(buffer);
721 LOG(DEBUG, "read_from_write_to_tls(): used buffer size: %ld",
722 (long int)buffer_size);
724 size_read = gnutls_record_recv(from, buffer, buffer_size);
726 LOG(WARNING, "read_from_write_to_tls(): gnutls_record_recv(): %s",
727 gnutls_strerror((int)size_read));
730 } else if (size_read == 0) {
734 size_written = gnutls_record_send(to, buffer, (size_t)size_read);
735 if (size_written < 0) {
736 LOG(WARNING, "read_from_write_to_tls(): gnutls_record_send(): %s",
737 gnutls_strerror((int)size_written));
740 if (size_read != size_written) {
741 LOG(ERROR, "read_from_write_to_tls(): only written %ld of %ld bytes!",
742 (long int)size_written, (long int)size_read);
750 static int connect_to_host(const char *hostname, const char *port) {
751 struct addrinfo gai_hints;
752 struct addrinfo *gai_result;
756 struct addrinfo *server;
758 if (hostname == NULL || port == NULL) {
762 /* Get IP of hostname server. */
763 memset(&gai_hints, 0, sizeof(gai_hints));
764 gai_hints.ai_family = AF_UNSPEC;
765 gai_hints.ai_socktype = SOCK_STREAM;
766 gai_hints.ai_protocol = 0;
767 gai_hints.ai_flags = AI_NUMERICSERV /* given port is numeric */
768 | AI_ADDRCONFIG /* supported by this computer */
769 | AI_V4MAPPED; /* support IPv4 through IPv6 */
770 gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
771 if (gai_return != 0) {
772 if (gai_return == EAI_SYSTEM) {
773 LOG_PERROR(WARNING, "connect_to_host(): getaddrinfo()");
775 LOG(WARNING, "connect_to_host(): getaddrinfo(): %s",
776 gai_strerror(gai_return));
781 /* Now try to connect to each server returned by getaddrinfo(), use the
782 * first successful connect. */
783 for (server = gai_result; server != NULL; server = server->ai_next) {
784 server_socket = socket(server->ai_family,
786 server->ai_protocol);
787 if (server_socket == -1) {
788 LOG_PERROR(DEBUG, "connect_to_host(): socket(), trying next");
792 if (connect(server_socket, server->ai_addr, server->ai_addrlen) != -1) {
795 LOG_PERROR(DEBUG, "connect_to_host(): connect(), trying next");
797 close(server_socket);
799 /* Make sure we free the result from getaddrinfo(). */
800 freeaddrinfo(gai_result);
802 if (server == NULL) {
803 LOG_PERROR(WARNING, "connect_to_host(): no server found, abort");
807 return server_socket;
811 /* Parse HTTP CONNECT request string and save its parameters.
813 * The following format is expected: "CONNECT host:port HTTP/1.x".
815 * request and host must have the same size! port must be at least 6 bytes
818 static int parse_request(const char *request, char *host, char *port,
819 int *version_minor) {
820 int port_unused; /* just used to verify the port is numeric */
823 /* scanf() doesn't check spaces. */
824 if (strncmp(request, "CONNECT ", 8)) {
827 /* Check request and extract data, "host:port" is not yet separated. */
828 if (sscanf(request, "CONNECT %s HTTP/1.%d", host, version_minor) != 2) {
831 /* Make sure ":port" is there. */
832 if ((position = strchr(host, ':')) == NULL) {
835 /* Make sure port is numeric. */
836 if (sscanf(position + 1, "%d", &port_unused) != 1) {
839 /* Store it in *port. */
840 strncpy(port, position + 1, 5);
842 /* And remove port from host. */