Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:17:24+00:00 2026-06-17T23:17:24+00:00

I’m working on a proxy application in C for a project. I have an

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:17:25+00:00Added an answer on June 17, 2026 at 11:17 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.