/// <summary> /// Initializes a new instance of the <see cref="SsdpRootDevice"/> class. /// </summary> /// <param name="cacheLifetime">Cache lifetime.</param> /// <param name="location">Location.</param> /// <param name="address">IP Address.</param> /// <param name="uuid">UDN.</param> /// <param name="deviceType">The type of the device.</param> public SsdpRootDevice(TimeSpan cacheLifetime, Uri location, IPNetAddress address, Guid uuid, string deviceType) : base(uuid, deviceType, "device") { CacheLifetime = cacheLifetime; Location = location?.ToString() ?? throw new ArgumentNullException(nameof(location)); NetAddress = address ?? throw new ArgumentNullException(nameof(address)); }
/// <summary> /// Sets up the proxy configuration based on the addresses in <paramref name="allowedProxies"/>. /// </summary> /// <param name="config">The <see cref="NetworkConfiguration"/> containing the config settings.</param> /// <param name="allowedProxies">The string array to parse.</param> /// <param name="options">The <see cref="ForwardedHeadersOptions"/> instance.</param> internal static void AddProxyAddresses(NetworkConfiguration config, string[] allowedProxies, ForwardedHeadersOptions options) { for (var i = 0; i < allowedProxies.Length; i++) { if (IPNetAddress.TryParse(allowedProxies[i], out var addr)) { AddIpAddress(config, options, addr.Address, addr.PrefixLength); } else if (IPHost.TryParse(allowedProxies[i], out var host)) { foreach (var address in host.GetAddresses()) { AddIpAddress(config, options, address, address.AddressFamily == AddressFamily.InterNetwork ? 32 : 128); } } } }
/// <summary> /// Tries to identify the string and return an object of that class. /// </summary> /// <param name="addr">String to parse.</param> /// <param name="result">IPObject to return.</param> /// <returns>True if the value parsed successfully.</returns> private static bool TryParse(string addr, out IPObject result) { if (!string.IsNullOrEmpty(addr)) { // Is it an IP address if (IPNetAddress.TryParse(addr, out IPNetAddress nw)) { result = nw; return(true); } if (IPHost.TryParse(addr, out IPHost h)) { result = h; return(true); } } result = IPNetAddress.None; return(false); }
public void TestEquals(string source, string dest) { Assert.True(IPNetAddress.Parse(source).Equals(IPNetAddress.Parse(dest))); Assert.True(IPNetAddress.Parse(dest).Equals(IPNetAddress.Parse(source))); }
public void IpV6SubnetMaskDoesNotMatchInvalidIpAddress(string netMask, string ipAddress) { var ipAddressObj = IPNetAddress.Parse(netMask); Assert.False(ipAddressObj.Contains(IPAddress.Parse(ipAddress))); }
public void IpV6SubnetMaskMatchesValidIpAddress(string netMask, string ipAddress) { var ipAddressObj = IPNetAddress.Parse(netMask); Assert.True(ipAddressObj.Contains(IPAddress.Parse(ipAddress))); }
public void ValidIPStrings(string address) { Assert.True(IPNetAddress.TryParse(address, out _)); }
public void TestSubnetContains(string network, string ip) { Assert.True(IPNetAddress.TryParse(network, out var networkObj)); Assert.True(IPNetAddress.TryParse(ip, out var ipObj)); Assert.True(networkObj.Contains(ipObj)); }
public void InvalidAddressString(string address) { Assert.False(IPNetAddress.TryParse(address, out _)); Assert.False(IPHost.TryParse(address, out _)); }
private void ProcessSearchRequest( string mx, string searchTarget, IPEndPoint remoteEndPoint, IPAddress receivedOnlocalIpAddress, CancellationToken cancellationToken) { if (String.IsNullOrEmpty(searchTarget)) { WriteTrace(String.Format(CultureInfo.InvariantCulture, "Invalid search request received From {0}, Target is null/empty.", remoteEndPoint.ToString())); return; } // WriteTrace(String.Format("Search Request Received From {0}, Target = {1}", remoteEndPoint.ToString(), searchTarget)); if (IsDuplicateSearchRequest(searchTarget, remoteEndPoint)) { // WriteTrace("Search Request is Duplicate, ignoring."); return; } // Wait on random interval up to MX, as per SSDP spec. // Also, as per UPnP 1.1/SSDP spec ignore missing/bank MX header. If over 120, assume random value between 0 and 120. // Using 16 as minimum as that's often the minimum system clock frequency anyway. int maxWaitInterval = 0; if (String.IsNullOrEmpty(mx)) { // Windows Explorer is poorly behaved and doesn't supply an MX header value. // if (this.SupportPnpRootDevice) mx = "1"; // else // return; } if (!Int32.TryParse(mx, out maxWaitInterval) || maxWaitInterval <= 0) { return; } if (maxWaitInterval > 120) { maxWaitInterval = _Random.Next(0, 120); } // Do not block synchronously as that may tie up a threadpool thread for several seconds. Task.Delay(_Random.Next(16, (maxWaitInterval * 1000)), CancellationToken.None).ContinueWith((parentTask) => { // Copying devices to local array here to avoid threading issues/enumerator exceptions. IEnumerable <SsdpDevice> devices = null; lock (_Devices) { if (String.Compare(SsdpConstants.SsdpDiscoverAllSTHeader, searchTarget, StringComparison.OrdinalIgnoreCase) == 0) { devices = GetAllDevicesAsFlatEnumerable().ToArray(); } else if (String.Compare(SsdpConstants.UpnpDeviceTypeRootDevice, searchTarget, StringComparison.OrdinalIgnoreCase) == 0 || (this.SupportPnpRootDevice && String.Compare(SsdpConstants.PnpDeviceTypeRootDevice, searchTarget, StringComparison.OrdinalIgnoreCase) == 0)) { devices = _Devices.ToArray(); } else if (searchTarget.Trim().StartsWith("uuid:", StringComparison.OrdinalIgnoreCase)) { devices = (from device in GetAllDevicesAsFlatEnumerable() where String.Compare(device.Uuid, searchTarget.Substring(5), StringComparison.OrdinalIgnoreCase) == 0 select device).ToArray(); } else if (searchTarget.StartsWith("urn:", StringComparison.OrdinalIgnoreCase)) { devices = (from device in GetAllDevicesAsFlatEnumerable() where String.Compare(device.FullDeviceType, searchTarget, StringComparison.OrdinalIgnoreCase) == 0 select device).ToArray(); } } if (devices != null) { var deviceList = devices.ToList(); // WriteTrace(String.Format("Sending {0} search responses", deviceList.Count)); foreach (var device in deviceList) { var root = device.ToRootDevice(); var source = new IPNetAddress(root.Address, root.PrefixLength); var destination = new IPNetAddress(remoteEndPoint.Address, root.PrefixLength); if (!_sendOnlyMatchedHost || source.NetworkAddress.Equals(destination.NetworkAddress)) { SendDeviceSearchResponses(device, remoteEndPoint, receivedOnlocalIpAddress, cancellationToken); } } } }, CancellationToken.None); }
public static void TryParse_InvalidAddressString_False(string address) => Assert.False(IPNetAddress.TryParse(address, out _));
public static Property TryParse_IPv6Address_True(IPv6Address address) => IPNetAddress.TryParse(address.Item.ToString(), out _).ToProperty();
public static void TryParse_ValidIPStrings_True(string address) => Assert.True(IPNetAddress.TryParse(address, out _));