c - C 中的 Getaddrinfo 问题

标签 c sockets

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

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

...
//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");
}
...

获取主机函数:

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的函数

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;
    }
}

如有任何帮助,我们将不胜感激。如果需要我的更多代码,请告诉我。我只包含了我认为重要的内容,因此帖子不会太长。

最佳答案

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

调用 get_host() 的代码如下:

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

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

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

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

当你调用它时:

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

关于c - C 中的 Getaddrinfo 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14553760/

相关文章:

c - main() 包装器启动 main() 程序以从命令行编译它

c - string 和 malloc...出了什么问题?

java - 制作服务器队列java的最简单和最好的方法

mysql - 无法通过套接字 '/var/lib/mysql/mysql.sock' 连接到本地 MySQL 服务器 (2)

c - sublime text 2 无法运行我的 c 程序?

c - 具有两个不同套接字的多播加入组

使用 scanf 而不是 var=0 后,C 程序停止工作

c - 如何在linux中模拟socket/tcp编程的异常情况,例如终止连接的一侧?

python - Python HTTP始终使用套接字301

python - 如何使 socket.recv(500) 不停止 while 循环