- return 0;
-}
-
-
-static int connect_to_host(const char *hostname, const char *port) {
- struct addrinfo gai_hints;
- struct addrinfo *gai_result;
- int gai_return;
-
- int server_socket;
- struct addrinfo *server;
-
- if (NULL == hostname || NULL == port) {
- return -1;
- }
-
- /* Get IP of hostname server. */
- memset(&gai_hints, 0, sizeof(gai_hints));
- gai_hints.ai_family = AF_UNSPEC;
- gai_hints.ai_socktype = SOCK_STREAM;
- gai_hints.ai_protocol = 0;
- gai_hints.ai_flags = AI_NUMERICSERV /* given port is numeric */
- | AI_ADDRCONFIG /* supported by this computer */
- | AI_V4MAPPED; /* support IPv4 through IPv6 */
- gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
- if (0 != gai_return) {
- perror("getaddrinfo()");
- return -1;
- }
-
- /* Now try to connect to each server returned by getaddrinfo(), use the
- * first successful connect. */
- for (server = gai_result; NULL != server; server = server->ai_next) {
- server_socket = socket(server->ai_family,
- server->ai_socktype,
- server->ai_protocol);
- if (-1 == server_socket) {
- perror("socket(), trying next");
- continue;
- }
-
- if (-1 != connect(server_socket, server->ai_addr,
- server->ai_addrlen)) {
- break;
- }
- perror("connect(), trying next");
-
- close(server_socket);
- }
- /* Make sure we free the result from getaddrinfo(). */
- freeaddrinfo(gai_result);
-
- if (NULL == server) {
- fprintf(stderr, "no server found, aborting\n");
- return -1;
- }
-
- return server_socket;
-}
-
-
-/* Parse HTTP CONNECT request string and save its parameters.
- *
- * The following format is expected: "CONNECT host:port HTTP/1.y".
- *
- * request and host must have the same size! port must be at least 6 bytes
- * long (5 + '\0').
- */
-static int parse_request(const char *request, char *host, char *port,
- int *version_minor) {
- int port_unused; /* just used to verify the port is numeric */
- char *position;
-
- /* scanf() doesn't check spaces. */
- if (0 != strncmp(request, "CONNECT ", 8)) {
- return -1;
- }
- /* Check request and extract data, "host:port" is not yet separated. */
- if (2 != sscanf(request, "CONNECT %s HTTP/1.%d",
- host, version_minor)) {
- return -1;
- }
- /* Make sure ":port" is there. */
- if (NULL == (position = strchr(host, ':'))) {
- return -1;
- }
- /* Make sure port is numeric. */
- if (1 != sscanf(position + 1, "%d", &port_unused)) {
- return -1;