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

IPStringToMemBlock() public static method

Takes in an ip string such as a.b.c.d and returns a MemBlock containing the IP [a, b, c, d].
public static IPStringToMemBlock ( String ip ) : MemBlock
ip String The IP in a string to convert.
return MemBlock
    public static MemBlock IPStringToMemBlock(String ip) {
      string []bytes = ip.Split('.');
      if(bytes.Length != 4 && bytes.Length != 6) {
        throw new Exception("Invalid IP");
      }
      byte[] ipb = new byte[bytes.Length];
      for(int i = 0; i < ipb.Length; i++) {
        ipb[i] = Byte.Parse(bytes[i]);
      }
      return MemBlock.Reference(ipb);
    }

Usage Example

Esempio n. 1
0
        /**
         * <summary>Creates a response from the parameter fields with RData being
         * a memory chunk.  This is for MDns which supports caching</summary>
         * <param name="Name">The name resolved.</param>
         * <param name="Type">The query type.</param>
         * <param name="Class">The network type.</param>
         * <param name="CacheFlush">Flush the dns cache in the client.</param>
         * <param name="Ttl">How long to hold the result in the local dns cache.</param>
         * <param name="RData">RData in String format.</param>
         */
        public Response(string name, DnsPacket.Types type, DnsPacket.Classes class_type,
                        bool cache_flush, int ttl, String rdata)
        {
            Name       = name;
            Class      = class_type;
            Ttl        = ttl;
            Type       = type;
            CacheFlush = cache_flush;
            RData      = rdata;

            if (Type == DnsPacket.Types.A || Type == DnsPacket.Types.AAAA)
            {
                NameBlob  = DnsPacket.HostnameStringToMemBlock(Name);
                RDataBlob = DnsPacket.IPStringToMemBlock(RData);
            }
            else if (Type == DnsPacket.Types.Ptr)
            {
                if (DnsPacket.StringIsIP(Name))
                {
                    NameBlob = DnsPacket.PtrStringToMemBlock(Name);
                }
                else
                {
                    NameBlob = DnsPacket.HostnameStringToMemBlock(Name);
                }
                RDataBlob = DnsPacket.HostnameStringToMemBlock(RData);
            }
            else
            {
                throw new Exception("Invalid Query Type: " + Type + "!");
            }

            RdLength = (short)RDataBlob.Length;
            // 2 for Type + 2 for Class + 4 for Ttl + 2 for RdLength
            byte[] data = new byte[10];
            int    idx  = 0;

            data[idx++] = (byte)((((int)Type) >> 8) & 0xFF);
            data[idx++] = (byte)(((int)Type) & 0xFF);

            byte cf = 0x80;

            if (!CacheFlush)
            {
                cf = 0x00;
            }

            data[idx++] = (byte)(((((int)Class) >> 8) & 0x7F) | cf);
            data[idx++] = (byte)(((int)Class) & 0xFF);
            data[idx++] = (byte)((Ttl >> 24) & 0xFF);
            data[idx++] = (byte)((Ttl >> 16) & 0xFF);
            data[idx++] = (byte)((Ttl >> 8) & 0xFF);
            data[idx++] = (byte)(Ttl & 0xFF);
            data[idx++] = (byte)((RdLength >> 8) & 0xFF);
            data[idx]   = (byte)(RdLength & 0xFF);

            _icpacket = new CopyList(NameBlob, MemBlock.Reference(data), RDataBlob);
        }
All Usage Examples Of NetworkPackets.Dns.DnsPacket::IPStringToMemBlock