我正在为一个项目开发 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;
}
}
任何帮助,将不胜感激。如果需要更多我的代码,请告诉我。我只包含了我认为重要的内容,因此帖子不会太长。