【问题标题】:How could I make a mDNS Query with Pcap.net?如何使用 Pcap.net 进行 mDNS 查询?
【发布时间】:2014-09-08 17:43:02
【问题描述】:

问题:我正在寻找一个 mDNS 数据包,同时在堆栈流中搜索选项。我尝试了 bonjour 和一些包装器,但成功非常有限,尤其是当我第二次请求并收到套接字绑定投诉时(当然,这可能是我的代码而不是它们)。

由于 VB.net 没有我所知道的真正可编辑的 dnsquery,我在 pcapdotnet 的构建 DNS 数据包中使用了 DNS 层,并且我自己一层一层地制作数据包。我认为这是一个不错的选择,但我对如何做到这一点有点迷茫。

这是我们想要的问题:

        q_name = new QuestionName("_axis-video._tcp.local"),
        q_type = QueryConstants.Question.QuestionType.PTR,
        q_class = QueryConstants.Question.QuestionClass.IN

这是我根据他们的标准编辑的 BuildDNSPacket 函数:

Private Shared Function BuildDnsPacket(destmac As String, domainName As String) As Packet

    'get source MAC address of PC
    Dim nic = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
    Dim source As String = nic(0).GetPhysicalAddress().ToString
    Dim sourcearray As Byte() = System.Text.Encoding.ASCII.GetBytes(source)

    'format
    Dim sourceMacStr As String = ""
    For i As Integer = 0 To sourcearray.Count - 1 Step 2
        sourceMacStr += Chr(sourcearray(i)) & Chr(sourcearray(i + 1)) & ":"
    Next

    ' Will be filled automatically.
    Dim ethernetLayer As New EthernetLayer() With { _
         .Source = New MacAddress(sourceMacStr.Substring(0, 17)), _
          .Destination = New MacAddress(destmac), _
          .EtherType = EthernetType.None _
    }

    ' Will be filled automatically.
    Dim ipV4Layer As New IpV4Layer() With { _
          .Source = New IpV4Address("1.2.3.4"), _
          .CurrentDestination = New IpV4Address(destmac), _
          .Fragmentation = IpV4Fragmentation.None, _
          .HeaderChecksum = Nothing, _
          .Identification = 123, _
          .Options = IpV4Options.None, _
          .Protocol = Nothing, _
          .Ttl = 100, _
          .TypeOfService = 0 _
    }

    ' Will be filled automatically.
    Dim udpLayer As New UdpLayer() With { _
          .SourcePort = 5353, _
          .DestinationPort = 5353, _
          .Checksum = Nothing, _
          .CalculateChecksumValue = False _
    }

    Dim dnsLayer As New DnsLayer() With { _
          .Id = 0, _
          .IsResponse = False, _
          .OpCode = DnsOpCode.Query, _
          .IsAuthoritativeAnswer = False, _
          .IsTruncated = False, _
          .IsRecursionDesired = False, _
          .IsRecursionAvailable = False, _
          .FutureUse = False, _
          .IsAuthenticData = False, _
          .IsCheckingDisabled = False, _
          .ResponseCode = DnsResponseCode.NoError, _
          .Queries = {New DnsQueryResourceRecord(New DnsDomainName(domainName), DnsType.Ptr, DnsClass.Any)}, _
          .Answers = Nothing, _
          .Authorities = Nothing, _
          .Additionals = Nothing, _
          .DomainNameCompressionMode = DnsDomainNameCompressionMode.All _
    }


    Dim builder As New PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, dnsLayer)

    Return builder.Build(DateTime.Now)
End Function

主要区别是我将 DnsType 更改为 PTR 并将端口更改为 5353。

问题:我还应该添加或更改什么以使其成为 mDNS?我可以在域名中添加什么?我应该改变 dnsclass 吗?

绝对欢迎所有或任何建议。

【问题讨论】:

    标签: vb.net dns pcap.net


    【解决方案1】:

    我正在回答我的问题,以防其他需要在 vb.net 中执行 mDNS 的人需要这个:

    解决方案:我不需要向 DNS 层添加任何东西来完成这项工作。我将 DNS 层更改为以下:

       Dim dnsLayer As New DnsLayer() With { _
              .Id = 0, _
              .IsResponse = False, _
              .OpCode = DnsOpCode.Query, _
              .IsAuthoritativeAnswer = False, _
              .IsTruncated = False, _
              .IsRecursionDesired = False, _
              .IsRecursionAvailable = False, _
              .FutureUse = False, _
              .IsAuthenticData = False, _
              .IsCheckingDisabled = False, _
              .ResponseCode = DnsResponseCode.NoError, _
              .Queries = {New DnsQueryResourceRecord(New DnsDomainName(domainName), DnsType.Ptr, DnsClass.Any)}, _
              .Answers = Nothing, _
              .Authorities = Nothing, _
              .Additionals = Nothing, _
              .DomainNameCompressionMode = DnsDomainNameCompressionMode.All _
        }
    

    我把Ipv4层的输出地址设置为“224.0.0.251”的组播地址,将端口改为5353,使用上面列出的问题的域名。

    这是一个显示响应的wireshark:

    【讨论】:

      猜你喜欢
      • 2012-01-29
      • 1970-01-01
      • 2018-10-14
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      相关资源
      最近更新 更多