新足迹

 找回密码
 注册

精华好帖回顾

· 来就来一个重磅滴,最近一个月的早餐集锦^^ 没吃早饭的慎入(Muffin和玛德琳方子在P2) (2013-2-6) feicunzic · 塔斯马尼亚10天东海岸自驾游超详细攻略 (2015-1-27) xiaofang583
· 衣衣搭配系列1 (2005-5-22) 短腿小鳄鱼 · 路考经历,3次拿到驾照(完) (2005-7-29) 大飞熊
Advertisement
Advertisement
查看: 1293|回复: 2

monit源代码 关于DNS查询 自定义DNS NAME [复制链接]

头像被屏蔽

禁止访问

发表于 2013-6-19 16:11 |显示全部楼层
此文章由 lingg 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 lingg 所有!转贴必须注明作者、出处和本声明,并保持内容完整
本帖最后由 lingg 于 2013-6-20 13:20 编辑

以下这段代码 摘自monit,check_dns 主要用于 发起DNS查询
(http://mmonit.com/monit/download/)

monit做DNS查询的时候,应该是等于做了一个: dig ns '.'

我现在想 插入查询的hostname, 改成 dig ns 'the_name_I_want_to_query'

头大,真心不是很懂下面这些代码,似乎是基于SOCKET通信的固定格式?

请问哪有这些0x11, 0x01 对应值 的文档?

还是这些是monit内部的定义,找了半天没找到
  1. */
  2. int check_dns(Socket_T socket) {
  3.   int            offset_request  = 0;
  4.   int            offset_response = 0;
  5.   int            rc;
  6.   unsigned char  buf[STRLEN];
  7.   unsigned char *response = NULL;
  8.   unsigned char  request[19] = {
  9.     0x00,          /** Request Length field for DNS via TCP */
  10.     0x11,

  11.     0x00,                                /** Transaction ID */
  12.     0x01,

  13.     0x01,                                         /** Flags */
  14.     0x00,

  15.     0x00,                                 /** Queries count */
  16.     0x01,

  17.     0x00,                 /** Answer resource records count */
  18.     0x00,

  19.     0x00,              /** Authority resource records count */
  20.     0x00,

  21.     0x00,             /** Additional resource records count */
  22.     0x00,

  23.                                                  /** Query: */

  24.     0x00,                 /** Name: DNS root (empty string) */

  25.     0x00,                                      /** Type: NS */
  26.     0x02,

  27.     0x00,                                     /** Class: IN */
  28.     0x01
  29.   };
复制代码
最后更新一下,楼下是正确方向,已解决,PATCH等会贴后面
Advertisement
Advertisement

发表于 2013-6-19 22:38 |显示全部楼层
此文章由 audreamer 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 audreamer 所有!转贴必须注明作者、出处和本声明,并保持内容完整
这些当然是标准化的数据结构啦,DNS这些通讯数据结构在RFC 1035里定义,你去参考这个官方网址(http://www.ietf.org/rfc/rfc1035.txt)就可以了。
例如,你给出的代码里,可以在里面找到相关的规定:
例1.

    0x00,                                      /** Type: NS */
    0x02,

RFC 1035里的定义是:
3.2.2. TYPE values

TYPE fields are used in resource records.  Note that these types are a
subset of QTYPEs.

TYPE            value and meaning

A               1 a host address

NS              2 an authoritative name server

MD              3 a mail destination (Obsolete - use MX)

MF              4 a mail forwarder (Obsolete - use MX)

CNAME           5 the canonical name for an alias

SOA             6 marks the start of a zone of authority

MB              7 a mailbox domain name (EXPERIMENTAL)

MG              8 a mail group member (EXPERIMENTAL)

MR              9 a mail rename domain name (EXPERIMENTAL)

NULL            10 a null RR (EXPERIMENTAL)

WKS             11 a well known service description

PTR             12 a domain name pointer

HINFO           13 host information

MINFO           14 mailbox or mail list information

MX              15 mail exchange

TXT             16 text strings



例2:

    0x00,                                     /** Class: IN */
    0x01

RFC 1035里的定义是:
3.2.4. CLASS values

CLASS fields appear in resource records.  The following CLASS mnemonics
and values are defined:

IN              1 the Internet

CS              2 the CSNET class (Obsolete - used only for examples in
                some obsolete RFCs)

CH              3 the CHAOS class

HS              4 Hesiod [Dyer 87]

评分

参与人数 1积分 +4 收起 理由
windix + 4 感谢分享

查看全部评分

头像被屏蔽

禁止访问

发表于 2013-6-20 10:30 |显示全部楼层
此文章由 lingg 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 lingg 所有!转贴必须注明作者、出处和本声明,并保持内容完整
本帖最后由 lingg 于 2013-6-20 13:29 编辑
audreamer 发表于 2013-6-19 21:38
这些当然是标准化的数据结构啦,DNS这些通讯数据结构在RFC 1035里定义,你去参考这个官方网址(http://www. ...


非常感谢,我昨天找到这个RFC以后,最后还是CHEAT了

关于NAME这一块的转换。。看了文档,还是不知道怎么把字符串转换成对应的值,
没时间钻研了,麻烦有知道的同学科普一下,谢谢了

最后走的DUMP包,WIRESHARK开包,
然后做一个带 NAME的QUERY,拆了包拿的对应的值
  1. 4.1.2. Question section format

  2.                                     1  1  1  1  1  1
  3.       0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
  4.     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  5.     |                                               |
  6.     /                     QNAME                     /
  7.     /                                               /
  8.     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  9.     |                     QTYPE                     |
  10.     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  11.     |                     QCLASS                    |
  12.     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
复制代码
下面打开包里的这个部分,对应第一贴的数据结构:
  1. diff -Naur a/src/protocols/dns.c b/src/protocols/dns.c
  2. --- a/src/protocols/dns.c       2013-06-20 13:03:55.999891664 +1000
  3. +++ b/src/protocols/dns.c       2013-06-20 13:07:22.087892847 +1000
  4. @@ -59,9 +59,9 @@
  5.    int            rc;
  6.    unsigned char  buf[STRLEN];
  7.    unsigned char *response = NULL;
  8. -  unsigned char  request[19] = {
  9. +  unsigned char  request[29] = {
  10.      0x00,          /** Request Length field for DNS via TCP */
  11. -    0x11,
  12. +    0x1b,

  13.      0x00,                                /** Transaction ID */
  14.      0x01,
  15. @@ -82,7 +82,16 @@
  16.      0x00,

  17.                                                   /** Query: */
  18. -
  19. +    0x05,              /** Name: xxxxx.net - Start     */
  20. +    0xY1,
  21. +    0xY2,
  22. +    0xY3,
  23. +    0xY4,
  24. +    0xY5,
  25. +    0x03,
  26. +    0xY6,
  27. +    0xY7,
  28. +    0xY8,      /** Name: xxxxx.net - End, overwrite default monit Name below */
  29.      0x00,                 /** Name: DNS root (empty string) */

  30.      0x00,                                      /** Type: NS */
复制代码
有2个地方要注意,根据选取的NAME不同,长度要改,具体的NAME要改。

只说一下NAME的格式,一个长度后面接一个DATA,最后以00收尾,xxxxx.net 拆成

QNAME LengthOctet: 0x05 (5)
QNAME Data: xxxxx - 请自行转换成 C ARRAY格式,WIRESHARK里面有这个功能。
QNAME LengthOctet: 0x03 (3)
QNAME Data: net  对应 PATCH中的
+    0xY6,
+    0xY7,
+    0xY8,   

再次感谢楼上的

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

发表回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Advertisement
Advertisement
返回顶部