美文网首页C语言程序园
使用C语言获取DNS nameserver并进行域名解析

使用C语言获取DNS nameserver并进行域名解析

作者: 编程小世界 | 来源:发表于2019-06-17 20:38 被阅读2次

    #include<netinet/in.h>#include<arpa/nameser.h>#include<resolv.h>intmain(){ res_init();inti =0;for(i =0;i< _res.nscount;i++)/* _res.nscount为找到的域名服务器的数量 */{structsockaddr_in addr = _res.nsaddr_list[i];/* 域名服务器的地址 */}intclass= ns_c_in;inttype = QUERY;charanswer[256]=""; res_query("www.baidu.com",class, type, answer,sizeof(answer)); res_close();printf("answer=%s\n", answer);/* answer中为域名解析的结果 */}

    这种方法由于使用到了静态全局变量 _res ,因此并不是线程安全的。为此glib提供了线程安全的对应函数 res_ninit , res_nquery , res_nclose :

    #include<netinet/in.h>#include<arpa/nameser.h>#include<resolv.h>intmain(){  res_state res;  res_ninit(res);inti =0;for(i =0;i< res->nscount;i++)/* res->nscount存储了域名服务器的个数 */{structsockaddr_in addr = res->nsaddr_list[i];/* 域名服务器的地址 */}intclass= ns_c_in;inttype = QUERY;charanswer[256]="";  res_nquery(res,"www.baidu.com",class, type, answer,sizeof(answer));  res_nclose(res);printf("answer=%s", answer);/* answer中为域名解析的结果 */}

    /* res_state: the global state used by the resolver stub.  */#defineMAXNS                  3/* max # name servers we'll track */#defineMAXDFLSRCH              3/* # default domain levels to try */#defineMAXDNSRCH              6/* max # domains in search path */#defineMAXRESOLVSORT          10/* number of net to sort on */struct__res_state {intretrans;/* retransmition time interval */intretry;/* number of times to retransmit */unsignedlongoptions;/* option flags - see below. */intnscount;/* number of name servers */structsockaddr_in    nsaddr_list[MAXNS];/* address of name server */unsignedshortid;/* current message id *//* 2 byte hole here.  */char*dnsrch[MAXDNSRCH+1];/* components of domain to search */chardefdname[256];/* default domain (deprecated) */unsignedlongpfcode;/* RES_PRF_ flags - see below. */unsignedndots:4;/* threshold for initial abs. query */unsignednsort:4;/* number of elements in sort_list[] */unsignedipv6_unavail:1;/* connecting to IPv6 server failed */unsignedunused:23;struct{structin_addr      addr;uint32_tmask;  } sort_list[MAXRESOLVSORT];/* 4 byte hole here on 64-bit architectures.  */void* __glibc_unused_qhook;void* __glibc_unused_rhook;intres_h_errno;/* last one set for this context */int_vcsock;/* PRIVATE: for res_send VC i/o */unsignedint_flags;/* PRIVATE: see below *//* 4 byte hole here on 64-bit architectures.  */union{charpad[52];/* On an i386 this means 512b total. */struct{uint16_tnscount;uint16_tnsmap[MAXNS];intnssocks[MAXNS];uint16_tnscount6;uint16_tnsinit;structsockaddr_in6      *nsaddrs[MAXNS];#ifdef_LIBCunsignedlonglongint__glibc_extension_index        __attribute__((packed));#elseunsignedint__glibc_reserved[2];#endif} _ext;  } _u;};typedefstruct__res_state *res_state;

    /*%

    * Currently defined opcodes.

    */typedefenum__ns_opcode{                          ns_o_query =0,/*%< Standard query. */ns_o_iquery =1,/*%< Inverse query (deprecated/unsupported). */ns_o_status =2,/*%< Name server status query (unsupported). *//* Opcode 3 is undefined/reserved. */ns_o_notify =4,/*%< Zone change notification. */ns_o_update =5,/*%< Zone update message. */ns_o_max =6} ns_opcode;/*%

    * Values for class field

    */typedefenum__ns_class{                        ns_c_invalid =0,/*%< Cookie. */ns_c_in =1,/*%< Internet. */ns_c_2 =2,/*%< unallocated/unsupported. */ns_c_chaos =3,/*%< MIT Chaos-net. */ns_c_hs =4,/*%< MIT Hesiod. *//* Query class values which do not appear in resource records */ns_c_none =254,/*%< for prereq. sections in update requests */ns_c_any =255,/*%< Wildcard match. */ns_c_max =65536} ns_class;

    如果有想学习c++的程序员,可来我们的C/C++学习扣qun:589348389,

    微信公众号:java大世界(现在是发布c++学习干货哦,没有java)

    免费送C++的视频教程噢!

    我每晚上8点还会在群内直播讲解C/C++知识,欢迎大家前来学习哦。

    相关文章

      网友评论

        本文标题:使用C语言获取DNS nameserver并进行域名解析

        本文链接:https://www.haomeiwen.com/subject/fmozfctx.html