获取主机信息时出现分段错误

发布于 2024-11-08 02:20:13 字数 562 浏览 0 评论 0原文

高并发调用时出现这种情况,好像gethostbyname的结果没有被缓存?

static int proxy_getaddr(char *HostName)
{
    struct hostent*     Host;
    Host=gethostbyname(HostName);

    if(!Host)
    {
        unsigned long int addr=inet_addr(HostName);
        if(addr!=-1)
            Host=gethostbyaddr((char*)addr,sizeof(addr),AF_INET);
    }
    ...

这是核心转储:

0x00000034f40e98b1 in gethostbyaddr () from /lib64/libc.so.6
0x000000000040182c in proxy_getaddr (HostName=0x48d75be0 "stackoverflow.com")

如何解决此问题?

It happens when called at high concurrency,it seems the result of gethostbyname is not cached ?

static int proxy_getaddr(char *HostName)
{
    struct hostent*     Host;
    Host=gethostbyname(HostName);

    if(!Host)
    {
        unsigned long int addr=inet_addr(HostName);
        if(addr!=-1)
            Host=gethostbyaddr((char*)addr,sizeof(addr),AF_INET);
    }
    ...

Here's the core dump:

0x00000034f40e98b1 in gethostbyaddr () from /lib64/libc.so.6
0x000000000040182c in proxy_getaddr (HostName=0x48d75be0 "stackoverflow.com")

How can I fix this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不咬妳我踢妳 2024-11-15 02:20:13

gethostbyaddr 的第一个参数应该是指向 in_addr 结构的指针,而不是指向 long 的指针。请参阅http://pubs.opengroup.org/onlinepubs/009695399/functions/gethostbyaddr。 html

The first parameter of gethostbyaddr is supposed to be a pointer to an in_addr structure, not a pointer to a long. See http://pubs.opengroup.org/onlinepubs/009695399/functions/gethostbyaddr.html.

面犯桃花 2024-11-15 02:20:13

您的代码不正确。您正在传递 inet_addr() 的结果,将其转换为指针,但您真正想要的是它的地址:(

Host = gethostbyaddr(&addr, sizeof(addr), AF_INET);

您应该使用的其他注释in_addr_t 而不是 unsigned long int 也是正确的,但这不太可能专门导致您的问题)。

Your code is incorrect. You are passing the result of inet_addr(), cast to a pointer, but what you actually want is the address of this:

Host = gethostbyaddr(&addr, sizeof(addr), AF_INET);

(The other comments, that you should be using in_addr_t instead of unsigned long int are correct too, but it is unlikely that this specifically is causing your problem).

固执像三岁 2024-11-15 02:20:13

根据此处找到的文档,您传入了错误的价值观:

gethostbyaddr() 接受一个 struct in_addr 或 struct in6_addr 并为您提供相应的主机名(如果有),因此它有点类似于 gethostbyname()。至于参数,即使 addr 是一个 char*,您实际上想要传递一个指向 struct in_addr 的指针。 len 应为 sizeof(struct in_addr),类型应为 AF_INET

以下是引用站点中的一些示例代码:

struct hostent *he;
struct in_addr ipv4addr;
struct in6_addr ipv6addr;

inet_pton(AF_INET, "192.0.2.34", &ipv4addr);
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
printf("Host name: %s\n", he->h_name);

inet_pton(AF_INET6, "2001:db8:63b3:1::beef", &ipv6addr);
he = gethostbyaddr(&ipv6addr, sizeof ipv6addr, AF_INET6);
printf("Host name: %s\n", he->h_name);

According to the documentation found here, you are passing in the wrong values:

gethostbyaddr() takes a struct in_addr or struct in6_addr and brings you up a corresponding host name (if there is one), so it's sort of the reverse of gethostbyname(). As for parameters, even though addr is a char*, you actually want to pass in a pointer to a struct in_addr. len should be sizeof(struct in_addr), and type should be AF_INET.

Here is some example code from the quoted site:

struct hostent *he;
struct in_addr ipv4addr;
struct in6_addr ipv6addr;

inet_pton(AF_INET, "192.0.2.34", &ipv4addr);
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
printf("Host name: %s\n", he->h_name);

inet_pton(AF_INET6, "2001:db8:63b3:1::beef", &ipv6addr);
he = gethostbyaddr(&ipv6addr, sizeof ipv6addr, AF_INET6);
printf("Host name: %s\n", he->h_name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文