internal virtual bool ShouldIgnorePacket(SupportClass.PacketSupport packet) { bool result = false; if (Address != null) { IPAddress from = packet.Address; if (from != null) { // TODO: how to replace this? // if (from.isLinkLocalAddress() && (!Address.isLinkLocalAddress())) // { // // Ignore linklocal packets on regular interfaces, unless this is // // also a linklocal interface. This is to avoid duplicates. This is // // a terrible hack caused by the lack of an API to get the address // // of the interface on which the packet was received. // result = true; // } if (IPAddress.IsLoopback(from) && (!IPAddress.IsLoopback(Address))) { // Ignore loopback packets on a regular interface unless this is // also a loopback interface. result = true; } } } return(result); }
/// <summary> Send an outgoing multicast DNS message.</summary> internal async Task Send(DNSOutgoing out_Renamed) { out_Renamed.Finish(); if (!out_Renamed.Empty) { SupportClass.PacketSupport packet = new SupportClass.PacketSupport(SupportClass.ToByteArray(out_Renamed.data), out_Renamed.off, new IPEndPoint(group, DNSConstants.MDNS_PORT)); try { DNSIncoming msg = new DNSIncoming(packet); logger.Debug("send() JmDNS out:" + msg.Print(true)); } catch (IOException e) { logger.Error("send(DNSOutgoing) - JmDNS can not parse what it sends!!!", e); } await SupportClass.UdpClientSupport.Send(socket, packet); } }
private void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { uint dataLength = args.GetDataReader().UnconsumedBufferLength; byte[] result = new byte[dataLength]; args.GetDataReader().ReadBytes(result); SupportClass.PacketSupport packet = new SupportClass.PacketSupport(result, result.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse(args.RemoteAddress.CanonicalName), int.Parse(args.RemotePort))); try { if (Enclosing_Instance.localHost.ShouldIgnorePacket(packet)) { return; } DNSIncoming msg = new DNSIncoming(packet); logger.Debug("SocketListener.run() JmDNS in:" + msg.Print(true)); lock (Enclosing_Instance.IOLock) { if (msg.Query) { if (packet.Port != DNSConstants.MDNS_PORT) { Enclosing_Instance.HandleQuery(msg, packet.Address, packet.Port); } Enclosing_Instance.HandleQuery(msg, Enclosing_Instance.Group, DNSConstants.MDNS_PORT); } else { Enclosing_Instance.HandleResponse(msg); } } } catch (IOException e) { logger.Warn("run() exception ", e); } }
private void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { uint dataLength = args.GetDataReader().UnconsumedBufferLength; byte[] result = new byte[dataLength]; args.GetDataReader().ReadBytes(result); SupportClass.PacketSupport packet = new SupportClass.PacketSupport(result, result.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse(args.RemoteAddress.CanonicalName), int.Parse(args.RemotePort))); try { if (Enclosing_Instance.localHost.ShouldIgnorePacket(packet)) return; DNSIncoming msg = new DNSIncoming(packet); logger.Debug("SocketListener.run() JmDNS in:" + msg.Print(true)); lock (Enclosing_Instance.IOLock) { if (msg.Query) { if (packet.Port != DNSConstants.MDNS_PORT) { Enclosing_Instance.HandleQuery(msg, packet.Address, packet.Port); } Enclosing_Instance.HandleQuery(msg, Enclosing_Instance.Group, DNSConstants.MDNS_PORT); } else { Enclosing_Instance.HandleResponse(msg); } } } catch (IOException e) { logger.Warn("run() exception ", e); } }
/// <summary> Parse a message from a datagram packet.</summary> internal DNSIncoming(SupportClass.PacketSupport packet) { this.packet = packet; this.data = SupportClass.ToSByteArray(packet.Data); this.len = packet.Length; // TODO: will this always be 0 in .NET?? this.off = 0; this.questions = ArrayList.ReadOnly(new ArrayList()); this.answers = ArrayList.ReadOnly(new ArrayList()); this.receivedTime = (DateTime.Now.Ticks - 621355968000000000) / 10000; try { id = ReadUnsignedShort(); flags = ReadUnsignedShort(); numQuestions = ReadUnsignedShort(); numAnswers = ReadUnsignedShort(); numAuthorities = ReadUnsignedShort(); numAdditionals = ReadUnsignedShort(); // parse questions if (numQuestions > 0) { questions = ArrayList.Synchronized(new ArrayList(numQuestions)); for (int i = 0; i < numQuestions; i++) { DNSQuestion question = new DNSQuestion(ReadName(), ReadUnsignedShort(), ReadUnsignedShort()); questions.Add(question); } } // parse answers int n = numAnswers + numAuthorities + numAdditionals; if (n > 0) { answers = ArrayList.Synchronized(new ArrayList(n)); for (int i = 0; i < n; i++) { string domain = ReadName(); int type = ReadUnsignedShort(); int clazz = ReadUnsignedShort(); int ttl = ReadInt(); int len = ReadUnsignedShort(); int end = off + len; DNSRecord rec = null; switch (type) { case DNSConstants.TYPE_A: // IPv4 case DNSConstants.TYPE_AAAA: // IPv6 FIXME [PJYF Oct 14 2004] This has not been tested rec = new Address(domain, type, clazz, ttl, ReadBytes(off, len)); break; case DNSConstants.TYPE_CNAME: case DNSConstants.TYPE_PTR: rec = new Pointer(domain, type, clazz, ttl, ReadName()); break; case DNSConstants.TYPE_TXT: rec = new Text(domain, type, clazz, ttl, ReadBytes(off, len)); break; case DNSConstants.TYPE_SRV: rec = new Service(domain, type, clazz, ttl, ReadUnsignedShort(), ReadUnsignedShort(), ReadUnsignedShort(), ReadName()); break; case DNSConstants.TYPE_HINFO: // Maybe we should do something with those break; default: logger.Debug("DNSIncoming() unknown type:" + type); break; } if (rec != null) { // Add a record, if we were able to create one. answers.Add(rec); } else { // Addjust the numbers for the skipped record if (answers.Count < numAnswers) { numAnswers--; } else if (answers.Count < numAnswers + numAuthorities) { numAuthorities--; } else if (answers.Count < numAnswers + numAuthorities + numAdditionals) { numAdditionals--; } } off = end; } } } catch (IOException e) { logger.Warn("DNSIncoming() dump " + Print(true) + "\n exception ", e); throw e; } }