Chinaunix首页 | 论坛 | 博客
  • 博客访问: 245023
  • 博文数量: 20
  • 博客积分: 1530
  • 博客等级: 上尉
  • 技术积分: 525
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-31 16:26
文章分类

全部博文(20)

文章存档

2018年(2)

2012年(1)

2011年(4)

2010年(5)

2009年(2)

2008年(6)

我的朋友

分类: LINUX

2012-02-15 15:13:44

Linux下常用的地址解析函数有:

    int res_query(const char *dname, int class, int type,
              unsigned char *answer, int anslen);

    int res_search(const char *dname, int class, int type,
              unsigned char *answer, int anslen);

一般编程中,有这两个就足够了,今天这里给大家共享一下libevent库中提供的另外一个函数:evdns_resolve_ipv4,如果大家在编程中常用libevent,那么下面的实例会很有用。
#cat dns_lookup.c

#include
#include
#include
#include
#include

#include
#include
#include

#include
#include

void usage(void)
{
    printf("USAGE: dns-lookup \n");
    exit(0);
}

void lookup_cb(int result, char type, int count, int ttl, void *addresses, void *arg)
{
    struct in_addr *addrs = addresses;
    int i;

    if (result != DNS_ERR_NONE)
    {
        printf("Error looking up address.\n");
    }
    else
    {
        for (i = 0; i < count; i++)
        {
            printf("[%i][%s]=[%s]\n", i, arg, inet_ntoa(addrs[i]));
        }
        printf("DNS lookup done.\n");
    }
    exit(0);
}

int main(int argc, char **argv)
{
    if (argc != 2)
        usage();

    event_init();
    evdns_init();
    evdns_resolve_ipv4(argv[1], 0, lookup_cb, argv[1]);
    event_dispatch();

    return (0);
}

#gcc dns_lookup.c -o dns_lookup -l event
#./dns_lookup
[0][]=[74.125.224.50]
[1][]=[74.125.224.48]
[2][]=[74.125.224.51]
[3][]=[74.125.224.52]
[4][]=[74.125.224.49]
DNS lookup done.

阅读(2415) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~