Beispiel #1
0
 /// <summary>
 /// Construct a new record
 /// </summary>
 /// <param name="type"></param>
 /// <param name="name"></param>
 /// <param name="address"></param>
 public NameRecord(NameRecordType type, string name, Reference address = null)
 {
     Name    = name;
     Id      = name;
     Address = address ?? new Reference();
     Type    = type;
 }
 /// <summary>
 /// Match record against address and type
 /// </summary>
 /// <param name="record"></param>
 /// <param name="address"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool Matches(this INameRecord record, Reference address, NameRecordType type)
 {
     if (!record.Type.HasFlag(type))
     {
         return(false);
     }
     return(address == Reference.All || record.Address.Equals(address));
 }
Beispiel #3
0
 public ResourceRecord(NameRecordType type)
 {
     Name = String.Empty;
     Type = type;
     Class = ResourceRecordClass.In;
     TTL = (uint)new TimeSpan(7, 0, 0, 0).TotalSeconds;
     Data = new byte[0];
 }
Beispiel #4
0
 public ResourceRecord(byte[] buffer, ref int offset)
 {
     Name = NetBiosUtils.DecodeName(buffer, ref offset);
     Type = (NameRecordType)BigEndianReader.ReadUInt16(buffer, ref offset);
     Class = (ResourceRecordClass)BigEndianReader.ReadUInt16(buffer, ref offset);
     TTL = BigEndianReader.ReadUInt32(buffer, ref offset);
     ushort dataLength = BigEndianReader.ReadUInt16(buffer, ref offset);
     Data = ByteReader.ReadBytes(buffer, ref offset, dataLength);
 }
 /// <summary>
 /// Match record against name and type
 /// </summary>
 /// <param name="record"></param>
 /// <param name="name"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool Matches(this INameRecord record, string name, NameRecordType type)
 {
     if (!record.Type.HasFlag(type))
     {
         return(false);
     }
     return(name == null ||
            (record.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase) ||
             record.Id.Equals(name, StringComparison.CurrentCultureIgnoreCase)));
 }
 /// <summary>
 /// Match record against address
 /// </summary>
 /// <param name="record"></param>
 /// <param name="address"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool Matches(this INameRecord record, SocketAddress address,
                            NameRecordType type)
 {
     if (address.Family == AddressFamily.Collection)
     {
         return(Matches(record, (SocketAddressCollection)address, type));
     }
     if (Matches(record, address.ToString(), type))
     {
         return(true);
     }
     if (address.Family == AddressFamily.InterNetworkV6)
     {
         return(Matches(record, Reference.FromSocketAddress((Inet6SocketAddress)address), type));
     }
     return(false);
 }
        /// <summary>
        /// Create record type expression from type
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static string CreateQueryString(NameRecordType type, bool filterAlive = false)
        {
            var sql    = new StringBuilder("SELECT * FROM devices WHERE ");
            var concat = false;

            // No support for bit queries ...
            if (0 != (type & NameRecordType.Proxy))
            {
                sql.Append("(");
                sql.Append("tags.proxy=1");
                concat = true;
            }

            if (0 != (type & NameRecordType.Host))
            {
                sql.Append(concat ? " OR " : "(");
                sql.Append("tags.host=1");
                concat = true;
            }

            if (0 != (type & NameRecordType.Link))
            {
                sql.Append(concat ? " OR " : "(");
                sql.Append("tags.link=1");
                concat = true;
            }

            if (concat)
            {
                sql.Append(")");
                if (filterAlive)
                {
                    sql.Append(" AND ");
                }
            }
            if (filterAlive)
            {
                sql.Append("properties.reported.alive = 1");
            }
            return(sql.ToString());
        }
Beispiel #8
0
        /// <summary>
        /// Lookup record by record type
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public async Task <IEnumerable <INameRecord> > LookupAsync(string name, NameRecordType type,
                                                                   CancellationToken ct)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(nameof(name));
            }
            name = name.ToLowerInvariant();
            if (NameRecordType.Host == (type & NameRecordType.Host))
            {
                // Use cache to look up a host record
                INameRecord record;
                if (_cache.TryGetValue(name, out record) && record.References.Any())
                {
                    return(new NameRecord(record).AsEnumerable());
                }
            }

            var sql = new StringBuilder("SELECT * FROM devices WHERE ");

            sql.Append("(deviceId = '");
            sql.Append(name);

            if (NameRecordType.Proxy == (type & NameRecordType.Proxy))
            {
                sql.Append("' OR tags.name = '");
                sql.Append(name);
            }

            Reference address;

            if (Reference.TryParse(name, out address))
            {
                sql.Append("' OR tags.address = '");
                sql.Append(address.ToString().ToLower());
            }

            sql.Append("') AND ");
            sql.Append(IoTHubRecord.CreateTypeQueryString(type));
            return(await LookupAsync(sql.ToString(), ct).ConfigureAwait(false));
        }
        /// <summary>
        /// Create record type expression from type
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static string CreateTypeQueryString(NameRecordType type)
        {
            var  sql    = new StringBuilder("(");
            bool concat = false;

            // No support for bit queries ...
            if (0 != (type & NameRecordType.Proxy))
            {
                sql.Append("tags.proxy=1");
                concat = true;
            }
            if (0 != (type & NameRecordType.Host))
            {
                if (concat)
                {
                    sql.Append(" OR ");
                }
                sql.Append("tags.host=1");
                concat = true;
            }
            if (0 != (type & NameRecordType.Link))
            {
                if (concat)
                {
                    sql.Append(" OR ");
                }
                sql.Append("tags.link=1");
                concat = true;
            }
            if (!concat)
            {
                return("");
            }
            sql.Append(")");
            return(sql.ToString());
        }
 /// <summary>
 /// Lookup records by address
 /// </summary>
 /// <param name="address"></param>
 /// <param name="type"></param>
 /// <param name="ct"></param>
 /// <returns></returns>
 public static Task <IEnumerable <INameRecord> > LookupAsync(this INameService service,
                                                             Reference address, NameRecordType type, CancellationToken ct) =>
 LookupAsync(service, r => r.Matches(address, type), ct);
 /// <summary>
 /// Lookup records by name
 /// </summary>
 /// <param name="name"></param>
 /// <param name="type"></param>
 /// <param name="ct"></param>
 /// <returns></returns>
 public static Task <IEnumerable <INameRecord> > LookupAsync(this INameService service,
                                                             string name, NameRecordType type, CancellationToken ct) =>
 LookupAsync(service, r => r.Matches(name, type), ct);
 /// <summary>
 /// Match record against list of addresses
 /// </summary>
 /// <param name="record"></param>
 /// <param name="addresses"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool Matches(this INameRecord record, SocketAddressCollection addresses,
                            NameRecordType type) =>
 addresses.Addresses().Any(a => record.Matches(a, type));
 /// <summary>
 /// Match record against address and type
 /// </summary>
 /// <param name="record"></param>
 /// <param name="addresses"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool Matches(this INameRecord record, IEnumerable <Reference> addresses,
                            NameRecordType type) =>
 addresses.Any(a => record.Matches(a, type));
Beispiel #14
0
 public QuestionSection(byte[] buffer, ref int offset)
 {
     Name  = NetBiosUtils.DecodeName(buffer, ref offset);
     Type  = (NameRecordType)BigEndianReader.ReadUInt16(buffer, ref offset);
     Class = (QuestionClass)BigEndianReader.ReadUInt16(buffer, ref offset);
 }
Beispiel #15
0
        /// <summary>
        /// Lookup record by address
        /// </summary>
        /// <param name="address"></param>
        /// <param name="type"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public async Task <IEnumerable <INameRecord> > LookupAsync(Reference address, NameRecordType type,
                                                                   CancellationToken ct)
        {
            if (address == null || address == Reference.Null)
            {
                throw new ArgumentException(nameof(address));
            }
            var sql = new StringBuilder("SELECT * FROM devices WHERE ");

            if (address != Reference.All)
            {
                sql.Append("tags.address = '");
                sql.Append(address.ToString().ToLower());
                sql.Append("' AND ");
            }
            sql.Append(IoTHubRecord.CreateTypeQueryString(type));
            return(await LookupAsync(sql.ToString(), ct).ConfigureAwait(false));
        }