NetworkPackets.Dns.DnsPacket.HostnameStringToMemBlock C# (CSharp) Method

HostnameStringToMemBlock() public static method

Given a Name as a string converts it into bytes given the type of query.
public static HostnameStringToMemBlock ( String name ) : MemBlock
name String The name to convert (and resolve).
return MemBlock
    public static MemBlock HostnameStringToMemBlock(String name) {
      String[] pieces = name.Split('.');
      // First Length + Data + 0 (1 + name.Length + 1)
      byte[] nameb = new byte[name.Length + 2];
      int pos = 0;
      for(int idx = 0; idx < pieces.Length; idx++) {
        nameb[pos++] = (byte) pieces[idx].Length;
        for(int jdx = 0; jdx < pieces[idx].Length; jdx++) {
          nameb[pos++] = (byte) pieces[idx][jdx];
        }
      }
      nameb[pos] = 0;
      return MemBlock.Reference(nameb);
    }

Usage Example

Exemplo n.º 1
0
        /**
         * <summary>Constructor when creating a Dns Query</summary>
         * <param name="QName">the name of resource you are looking up, IP Address
         * when QType = Ptr otherwise hostname</param>
         * <param name="QType"> the type of look up to perform</param>
         * <param name="QClass">should always be IN</param>
         */
        public Question(String QName, DnsPacket.Types QType, DnsPacket.Classes QClass)
        {
            this.QName  = QName;
            this.QType  = QType;
            this.QClass = QClass;

            if (QType == DnsPacket.Types.A || QType == DnsPacket.Types.AAAA)
            {
                QNameBlob = DnsPacket.HostnameStringToMemBlock(QName);
            }
            else if (QType == DnsPacket.Types.Ptr)
            {
                QNameBlob = DnsPacket.PtrStringToMemBlock(QName);
            }
            else
            {
                throw new Exception("Invalid QType: " + QType + "!");
            }

            // 2 for QType + 2 for QClass
            byte[] data = new byte[4];
            int    idx  = 0;

            data[idx++] = (byte)((((int)QType) >> 8) & 0xFF);
            data[idx++] = (byte)(((int)QType) & 0xFF);
            data[idx++] = (byte)((((int)QClass) >> 8) & 0xFF);
            data[idx++] = (byte)(((int)QClass) & 0xFF);
            _icpacket   = new CopyList(QNameBlob, MemBlock.Reference(data));
        }
All Usage Examples Of NetworkPackets.Dns.DnsPacket::HostnameStringToMemBlock