コード例 #1
0
ファイル: mDNS.cs プロジェクト: pisker/mDNS
		/// <summary> Generate a possibly unique name for a host using the information we
		/// have in the cache.
		/// 
		/// </summary>
		/// <returns> returns true, if the name of the host had to be changed.
		/// </returns>
		private bool MakeHostNameUnique(Address host)
		{
			string originalName = host.Name;
			long now = (DateTime.Now.Ticks - 621355968000000000) / 10000;
			
			bool collision;
			do 
			{
				collision = false;
				
				// Check for collision in cache
				for (DNSCache.CacheNode j = cache.find(host.Name.ToLower()); j != null; j = j.Next)
				{
					DNSRecord a = (DNSRecord) j.Value;
					if (false) // TODO: huh?
					{
						host.name = IncrementName(host.Name);
						collision = true;
						break;
					}
				}
			}
			while (collision);
			
			if (originalName.Equals(host.Name))
			{
				return false;
			}
			else
			{
				return true;
			}
		}
コード例 #2
0
ファイル: HostInfo.cs プロジェクト: pisker/mDNS
		internal virtual Address GetDNSAddressRecord(Address address)
		{
			return (DNSConstants.TYPE_AAAA == address.type?DNS6AddressRecord:DNS4AddressRecord);
		}
コード例 #3
0
ファイル: Address.cs プロジェクト: pisker/mDNS
		/// <summary> Does a lexicographic comparison of the byte array representation
		/// of this record and that record.
		/// This is needed for tie-break tests according to
		/// draft-cheshire-dnsext-multicastdns-04.txt chapter 9.2.
		/// </summary>
		private int lexCompare(Address that)
		{
			sbyte[] thisBytes = this.toByteArray();
			sbyte[] thatBytes = that.toByteArray();
			for (int i = 0, n = Math.Min(thisBytes.Length, thatBytes.Length); i < n; i++)
			{
				if (thisBytes[i] > thatBytes[i])
				{
					return 1;
				}
				else if (thisBytes[i] < thatBytes[i])
				{
					return - 1;
				}
			}
			return thisBytes.Length - thatBytes.Length;
		}
コード例 #4
0
ファイル: DNSIncoming.cs プロジェクト: pisker/mDNS
		/// <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;
			}
		}