amura.cxg
amura.cxg

Reputation: 2427

Getaddrinfo issue in C

I'm working on a proxy application in C for a project. I have an issue with getaddrinfo() being unsuccessful when I pass it the parsed host name. If I hard code the host, for example "www.google.ca" it doesn't error but when given the URL (from a GET request that the code receives) it does produce an error (The exact error is "Unknown name or service"). I've tried debugging in NetBeans and as far as I can tell the parsed URL is no different from the one I've hard coded. Below is the code that I'm using:

Snippet of code that receives the request and tries to forward it:

...
//Message is received in the code before this
if (get_host(message, &url) == 0)
{
//Tries to open a socket to the parsed URL. This is where the issue happens
forawrd_fd = create_forward_socket(url, "80");
}
...

The get host function:

int get_host(char *request, char **host_url)
{
    char url[BUFFER_SIZE];

    if(sscanf(request, "%*s %s HTTP/1.1\r\n", url) != 1)
    {
        return -1;
    }
    else
    {
        int len = strlen(url);

        //If there is a / at the end of the URL remove it
        if(url[len-1] == '/')
        {
            printf("%c%c\n", url[len-2], url[len-1]);
            url[len-1] = '\0';
            printf("%s\n", url);
        }

        *host_url = &url;
        //If the start of the string is http:// remove it
        if(url[0] == 'h' && url[1] == 't' && url[2] == 't'&& url[3] == 'p')
        {
            *host_url += 7;
        }

        return 0;
    }
}

Function that gets the file descriptor and makes the getaddrinfo

int create_forward_socket(char* url, const char* port)
{
    //Status variable needed for some calls
    int status, socket_fd, received_data;

    //Setup address info structs
    struct addrinfo hints;
    struct addrinfo *result, *current;

    //Initialize our hints.
    memset(&hints, 0, sizeof hints);
    //IPv4 or IPv6 we don't
    hints.ai_family = AF_UNSPEC;
    //We want a stream socket not datagram
    hints.ai_socktype = SOCK_STREAM;
    //Whatever this means (I forget but again the interwebs says...)
    hints.ai_flags = AI_PASSIVE;

    //Get a linked list of address info that we will choose from
    if ((status = getaddrinfo(url, port, &hints, &result)) != 0) //Status here is -2 when called with the parsed URL
    {
        return -1;
    }

    for (current = result; current != NULL; current = current->ai_next)
    {
        if ((socket_fd = socket(current->ai_family, current->ai_socktype, current->ai_protocol)) != -1)
        {
            if (connect(socket_fd, current->ai_addr, current->ai_addrlen) != -1)
            {
                //We found a usable socket
                break;
            }
            else
            {
                close(socket_fd);
            }
        }
    }

    if (current == NULL)
    {
        return -2;
    }
    else
    {
        return socket_fd;
    }
}

Any help would be appreciated. If more of my code is needed please let me know. I included only what I thought was important so the post wasn't too long.

Upvotes: 1

Views: 2341

Answers (1)

rodrigo
rodrigo

Reputation: 98358

My guess is that you are returning a pointer to a local variable. See, url is a local variable, and the *host_url = url; line (I'm assuming that this is an output parameter) will return it to the caller. But local variables will be destroyed just when the function returns, and then, *host_url will point to the middle of nowhere.

The code calling get_host() is like:

char *host;
get_host(req, &host);
//call getaddrinfo with host

But host will not point to valid memory outside of get_host.

The solution is that the caller allocates the buffer:

int get_host(char *request, char *url)
{ ... }

And when you call it:

char host[BUFFER_SIZE];
get_host(req, host);
//call getaddrinfo with host

Upvotes: 3

Related Questions