Esempio n. 1
0
 /// <summary> Construct a service description for registrating with JmDNS.</summary>
 /// <param name="type">fully qualified service type name, such as <code>_http._tcp.local.</code>.
 /// </param>
 /// <param name="name">unqualified service instance name, such as <code>foobar</code>
 /// </param>
 /// <param name="port">the local port on which the service runs
 /// </param>
 /// <param name="weight">weight of the service
 /// </param>
 /// <param name="priority">priority of the service
 /// </param>
 /// <param name="text">string describing the service
 /// </param>
 public ServiceInfo(string type, string name, int port, int weight, int priority, string text) : this(type, name, port, weight, priority, (sbyte[])null)
 {
     try
     {
         MemoryStream out_Renamed = new MemoryStream(text.Length);
         WriteUTF(out_Renamed, text);
         this.text = SupportClass.ToSByteArray(out_Renamed.ToArray());
     }
     catch (IOException e)
     {
         throw new Exception("unexpected exception: " + e);
     }
 }
Esempio n. 2
0
 internal override void Write(DNSOutgoing out_Renamed)
 {
     if (addr != null)
     {
         sbyte[] buffer = SupportClass.ToSByteArray(addr.GetAddressBytes());
         if (DNSConstants.TYPE_A == type)
         {
             // If we have a type A records we should answer with a IPv4 address
             if (addr.AddressFamily == AddressFamily.InterNetwork)
             {
                 // All is good
             }
             else
             {
                 // Get the last four bytes
                 sbyte[] tempbuffer = buffer;
                 buffer = new sbyte[4];
                 Array.Copy(tempbuffer, 12, buffer, 0, 4);
             }
         }
         else
         {
             // If we have a type AAAA records we should answer with a IPv6 address
             if (addr.AddressFamily == AddressFamily.InterNetwork)
             {
                 sbyte[] tempbuffer = buffer;
                 buffer = new sbyte[16];
                 for (int i = 0; i < 16; i++)
                 {
                     if (i < 11)
                     {
                         buffer[i] = tempbuffer[i - 12];
                     }
                     else
                     {
                         buffer[i] = 0;
                     }
                 }
             }
         }
         int length = buffer.Length;
         out_Renamed.WriteBytes(buffer, 0, length);
     }
 }
Esempio n. 3
0
 /// <summary> Construct a service description for registrating with JmDNS. The properties hashtable must
 /// map property names to either Strings or byte arrays describing the property values.
 /// </summary>
 /// <param name="type">fully qualified service type name, such as <code>_http._tcp.local.</code>.
 /// </param>
 /// <param name="name">unqualified service instance name, such as <code>foobar</code>
 /// </param>
 /// <param name="port">the local port on which the service runs
 /// </param>
 /// <param name="weight">weight of the service
 /// </param>
 /// <param name="priority">priority of the service
 /// </param>
 /// <param name="props">properties describing the service
 /// </param>
 public ServiceInfo(string type, string name, int port, int weight, int priority, Hashtable props) : this(type, name, port, weight, priority, new sbyte[0])
 {
     if (props != null)
     {
         try
         {
             MemoryStream out_Renamed = new MemoryStream(256);
             foreach (string key in props.Keys)
             {
                 object       val  = props[key];
                 MemoryStream out2 = new MemoryStream(100);
                 WriteUTF(out2, key);
                 if (val is string)
                 {
                     out2.WriteByte((byte)'=');
                     WriteUTF(out2, (string)val);
                 }
                 else if (val is sbyte[])
                 {
                     out2.WriteByte((byte)'=');
                     sbyte[] bval = (sbyte[])val;
                     out2.Write(SupportClass.ToByteArray(bval), 0, bval.Length);
                 }
                 else if (val != NO_VALUE)
                 {
                     throw new ArgumentException("invalid property value: " + val);
                 }
                 sbyte[] data = SupportClass.ToSByteArray(out2.ToArray());
                 out_Renamed.WriteByte((byte)data.Length);
                 out_Renamed.Write(SupportClass.ToByteArray(data), 0, data.Length);
             }
             this.text = SupportClass.ToSByteArray(out_Renamed.ToArray());
         }
         catch (IOException e)
         {
             throw new Exception("unexpected exception: " + e);
         }
     }
 }
Esempio n. 4
0
 private sbyte[] toByteArray()
 {
     try
     {
         // TODO: check this
         MemoryStream bout = new MemoryStream();
         BinaryWriter dout = new BinaryWriter(bout);
         dout.Write(SupportClass.ToByteArray(SupportClass.ToSByteArray(Encoding.GetEncoding("UTF8").GetBytes(name))));
         dout.Write((Int16)type);
         dout.Write((Int16)clazz);
         //dout.writeInt(len);
         dout.Write((Int16)priority);
         dout.Write((Int16)weight);
         dout.Write((Int16)port);
         dout.Write(SupportClass.ToByteArray(SupportClass.ToSByteArray(Encoding.GetEncoding("UTF8").GetBytes(server))));
         dout.Dispose();
         return(SupportClass.ToSByteArray(bout.ToArray()));
     }
     catch
     {
         throw new Exception();
     }
 }
Esempio n. 5
0
 /// <summary> Creates a byte array representation of this record.
 /// This is needed for tie-break tests according to
 /// draft-cheshire-dnsext-multicastdns-04.txt chapter 9.2.
 /// </summary>
 private sbyte[] toByteArray()
 {
     try
     {
         // TODO: check this
         MemoryStream bout = new MemoryStream();
         BinaryWriter dout = new BinaryWriter(bout);
         dout.Write(SupportClass.ToByteArray(SupportClass.ToSByteArray(Encoding.UTF8.GetBytes(name))));
         dout.Write((Int16)type);
         dout.Write((Int16)clazz);
         //dout.writeInt(len);
         sbyte[] buffer = SupportClass.ToSByteArray(addr.GetAddressBytes());
         for (int i = 0; i < buffer.Length; i++)
         {
             dout.Write((byte)buffer[i]);
         }
         dout.Dispose();
         return(SupportClass.ToSByteArray(bout.ToArray()));
     }
     catch
     {
         throw new Exception();
     }
 }
Esempio n. 6
0
        /// <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;
            }
        }