程序员必知:【转】adns解析库——域名解析实例(C++、linux)

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 程序员必知:【转】adns解析库——域名解析实例(C++、linux)

转自:

adns是一个开源的dns解析库

官方文档:

1. 初始化

?adns_state adns;adns_query query;adns_answer answer; 函数原型:int adns_init(adns_state newstate_r, adns_initflags flags, FILE diagfile /0=>stderr/); 举例:adns_init(adns, adns_if_noenv, 0);

2. 提交待解析的域名

?函数原型:int adns_submit(adns_state ads, const char owner, adns_rrtype type, adns_queryflags flags, void context, adns_query query_r); 举例:adns_submit(adns, argv【1】, adns_r_a, (adns_queryflags) 0, NULL, query);

3. 检测是否有域名已检测完成

?函数原型:int adns_check(adns_state ads, adns_query query_io, adns_answer answer_r, void context_r); 例:adns_check(adns, query, answer, NULL);

4.

?函数原型:int adns_wait(adns_state ads, adns_query query_io, adns_answer answer_r, void context_r); 例:adns_wait(adns, query, answer, NULL);

5. 检测是否已完成所有提交的域名的解析

?函数原型:void adns_finish(adns_state ads); 例:adns_finish(adns);

6. 范例代码(解析IPv4地址可使用adns v1.2或adns v1.4,解析IPv6地址请使用adns v1.6)

可从此处下载:

?#include "adns.h" #include #include #include #include int test_dns(char host) { adns_state ads; adns_initflags flags; flags = adns_if_nosigpipe | adns_if_noerrprint; adns_init(ads, flags, NULL); adns_query quer = NULL; adns_submit(ads, host, (adns_rrtype) adns_r_a, (adns_queryflags) 0, NULL, quer); int tryCount = -1; int adns_cname = 0; while(tryCount < 32) { tryCount += 1; adns_answer ans; int res = adns_check(ads, quer, ans, NULL); if(res == 0) { if (ans->status == adns_s_prohibitedcname) { char cname【128】; strncpy(cname, ans->cname, 127); cname【strlen(ans->cname)】 = '\0'; adns_query quer = NULL; adns_submit(ads, cname, (adns_rrtype) adns_r_addr, (adns_queryflags) 0, NULL, quer); adns_cname = 1; } else { //resolve IPv4 address /          if(adns_cname) printf("ip: %s\n", ans->status == adns_s_ok ? inet_ntoa(ans->rrs.addr->addr.inet.sin_addr) : "no"); else printf("ip: %s\n", ans->status == adns_s_ok ? inet_ntoa((ans->rrs.inaddr)) : "no"); / //resolve IPv6 address if(adns_cname){ if(ans->status == adns_s_ok){ char buf【INET6_ADDRSTRLEN】; inet_ntop(AF_INET6, ans->rrs.addr->addr.inet6.sin6_addr, buf, sizeof(buf)); printf("ip: %s\n", buf); } else{ printf("no\n"); } } else{ if(ans->status == adns_s_ok){ char buf【INET6_ADDRSTRLEN】; inet_ntop(AF_INET6, ans->rrs.in6addr, buf, sizeof(buf)); printf("ip: %s\n", buf); } else{ printf("no\n"); } }//代码效果参考:http://www.ezhiqi.com/bx/art_7661.html adns_finish(ads); break; } } else if (res == ESRCH || res == EAGAIN) { sleep(1); } else { printf("host(%s) is err!\n", host); } } return 0; } int main(int argc, char argv【】) { char host【128】; while(1) { scanf("%s", host); if(strlen(host) == 3 strcmp(host, "eof")) break; test_dns(host); }//代码效果参考:http://www.ezhiqi.com/zx/art_1206.html return 0; }

Makefile

?CFLAGS= -g TARGETS=libadns.a LIBOBJS=types.o event.o query.o reply.o general.o setup.o transmit.om parse.o poll.o check.o all: testdns testdns: testdns.c libadns.a libadns.a: $(LIBOBJS) rm -f $@ $(AR) cq $@ $(LIBOBJS) clean: rm -f $(LIBOBJS) libadns.a *~ config.status distclean: clean rm -f config.h .depend $(LIBOBJS): adns.h internal.h config.h

相关文章
|
4天前
|
编译器 API C语言
超级好用的C++实用库之跨平台实用方法
超级好用的C++实用库之跨平台实用方法
18 6
|
4天前
|
安全 C++
超级好用的C++实用库之环形内存池
超级好用的C++实用库之环形内存池
19 5
|
4天前
|
存储 自然语言处理 API
超级好用的C++实用库之字符编码转换
超级好用的C++实用库之字符编码转换
12 2
|
4天前
|
Linux API C++
超级好用的C++实用库之互斥锁
超级好用的C++实用库之互斥锁
10 2
|
4天前
|
缓存 网络协议 Linux
超级好用的C++实用库之套接字
超级好用的C++实用库之套接字
19 1
|
4天前
|
存储 算法 安全
超级好用的C++实用库之sha256算法
超级好用的C++实用库之sha256算法
10 1
|
4天前
|
存储 算法 安全
超级好用的C++实用库之国密sm4算法
超级好用的C++实用库之国密sm4算法
14 0
|
4天前
|
网络协议 Linux C++
超级好用的C++实用库之网络
超级好用的C++实用库之网络
13 0
|
4天前
|
算法 安全 Serverless
超级好用的C++实用库之国密sm3算法
超级好用的C++实用库之国密sm3算法
10 0
|
4天前
|
算法 数据安全/隐私保护 C++
超级好用的C++实用库之MD5信息摘要算法
超级好用的C++实用库之MD5信息摘要算法
11 0
下一篇
无影云桌面