首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C语言中的Getaddrinfo问题

C语言中的Getaddrinfo问题
EN

Stack Overflow用户
提问于 2013-01-28 08:04:51
回答 1查看 1.7K关注 0票数 1

我正在为一个项目用C语言开发一个代理应用程序。当我向getaddrinfo()传递解析的主机名时,我遇到了一个不成功的问题。如果我硬编码主机,例如"www.google.ca“,它不会出错,但当给定URL (来自代码接收的GET请求)时,它确实会产生一个错误(确切的错误是”未知的名称或服务“)。我尝试过在NetBeans中调试,据我所知,解析后的网址与我硬编码的网址没有什么不同。下面是我使用的代码:

接收请求并尝试转发请求的代码片段:

代码语言:javascript
复制
...
//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");
}
...

get host函数:

代码语言:javascript
复制
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;
    }
}

函数,该函数获取文件描述符并使getaddrinfo

代码语言:javascript
复制
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;
    }
}

任何帮助都将不胜感激。如果需要更多我的代码,请让我知道。我只写了我认为重要的内容,所以这篇文章不会太长。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-28 08:18:32

我的猜测是您返回的是一个指向局部变量的指针。看,url是一个局部变量,*host_url = url;行(我假设这是一个输出参数)将把它返回给调用者。但是,当函数返回时,局部变量将被销毁,然后,*host_url将指向某个地方。

调用get_host()的代码如下:

代码语言:javascript
复制
char *host;
get_host(req, &host);
//call getaddrinfo with host

但是host不会指向get_host之外的有效内存。

解决方案是调用者分配缓冲区:

代码语言:javascript
复制
int get_host(char *request, char *url)
{ ... }

当你调用它时:

代码语言:javascript
复制
char host[BUFFER_SIZE];
get_host(req, host);
//call getaddrinfo with host
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14553760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档