bool LogAssertPrefix4(PrefixedIPv4Address[] Manual, PrefixedIPv4Address address) { int Count = Manual.Where(NI => ((NI.PrefixLength == address.PrefixLength) && (NI.Address == address.Address))).Count(); string AdrText = null; foreach (PrefixedIPv4Address p in Manual) { if (AdrText != null) { AdrText += ", " + MyV4String(p); } else { AdrText = MyV4String(p); } } return (LogLog("Manual address(es): " + AdrText) & LogAssert( Count > 0, "No IP address in addresses set") && LogAssert( Count == 1, "More than one equal IP address in addresses set")); }
public static PrefixedIPv4Address NextAddress(PrefixedIPv4Address Orig) { System.Net.IPAddress adr = null; System.Diagnostics.Trace.WriteLine("Parsing address" + Orig.Address); if (!System.Net.IPAddress.TryParse(Orig.Address, out adr)) { System.Diagnostics.Trace.WriteLine("Failed with parsing"); return(Orig); } long from = (adr.Address >> 24) & 0xff; long i; /* * if (!PeekARP(adr)) * { // ARP not available - so just guess * i = from + 1; * if (i >= 254) i = 1; * adr.Address = (adr.Address & 0xffffff) | (i << 24); * return new PrefixedIPv4Address() { Address = adr.ToString(), PrefixLength = Orig.PrefixLength }; * }*/ // do ARP scanning, after current for (i = from + 1; i <= 254; i++) { adr.Address = (adr.Address & 0xffffff) | (i << 24); if (!PeekARP(adr)) { break; } } if (i >= 254) { // do ARP scanning, before current for (i = 1; i < from; i++) { adr.Address = (adr.Address & 0xffffff) | (i << 24); if (!PeekARP(adr)) { break; } } // nothing found - just guess if (i >= from) { i = from + 1; if (i >= 254) { i = 1; } adr.Address = (adr.Address & 0xffffff) | (i << 24); } } return(new PrefixedIPv4Address() { Address = adr.ToString(), PrefixLength = Orig.PrefixLength }); }
void SetIPInternal(string token, string ip, int prefix) { EnableLogResponse = false; NetworkInterface[] interfaces; try { interfaces = Client.GetNetworkInterfaces(); } catch (Exception exc) { ReportException(exc, "An exception was thrown during getting network interface: "); ReportOperationCompleted(); return; } finally { EnableLogResponse = true; } foreach (NetworkInterface networkInterface in interfaces) { if (networkInterface.token == token) { PrefixedIPv4Address address = new PrefixedIPv4Address(); address.Address = ip; address.PrefixLength = prefix; NetworkInterfaceSetConfiguration nisc = new NetworkInterfaceSetConfiguration(); nisc.IPv4 = new IPv4NetworkInterfaceSetConfiguration(); nisc.IPv4.DHCP = false; nisc.IPv4.DHCPSpecified = false; nisc.IPv4.Enabled = true; nisc.IPv4.EnabledSpecified = true; nisc.IPv4.Manual = new PrefixedIPv4Address[1]; nisc.IPv4.Manual[0] = address; nisc.MTU = networkInterface.Info.MTU; nisc.MTUSpecified = true; nisc.Link = networkInterface.Link.AdminSettings; nisc.Enabled = true; nisc.EnabledSpecified = true; Client.SetNetworkInterfaces(token, nisc); return; } } throw new Exception(string.Format("Network interface matching Token \"{0}\" not found", token)); }
public static PrefixedIPv4Address GetAvailableIPv4Address(this NetworkInterface[] interfaces, out string interfaceToken) { PrefixedIPv4Address address = null; interfaceToken = null; foreach (NetworkInterface n in interfaces) { if (n.IPv4 == null) { continue; } if (!n.IPv4.Enabled) { continue; } if (n.IPv4.Config == null) { continue; } PrefixedIPv4Address adr = null; if (n.IPv4.Config.DHCP) { adr = n.IPv4.Config.FromDHCP; } else { if (n.IPv4.Config.Manual.Length > 0) { adr = n.IPv4.Config.Manual[0]; } } if (adr == null) { continue; } // TODO - introduce correct address modification string a = adr.Address; if (a.Length < 2) { continue; } address = NextAddress(adr); interfaceToken = n.token; break; } return(address); }
private bool ValidateIPAddress(PrefixedIPv4Address ip) { return(ValidateIPAddressCore(ip.Address, ip.PrefixLength, 32)); }
public static PrefixedIPv4Address GetAvailableIPv4Address(this NetworkInterface[] interfaces, out string interfaceToken) { PrefixedIPv4Address address = null; interfaceToken = null; foreach (NetworkInterface n in interfaces) { if (n.IPv4 == null) { continue; } if (!n.IPv4.Enabled) { continue; } if (n.IPv4.Config == null) { continue; } PrefixedIPv4Address adr = null; if (n.IPv4.Config.DHCP) { adr = n.IPv4.Config.FromDHCP; } else { if (n.IPv4.Config.Manual.Length > 0) { adr = n.IPv4.Config.Manual[0]; } } if (adr == null) { continue; } // TODO - introduce correct address modification string a = adr.Address; if (a.Length < 2) { continue; } #if true address = NextAddress(adr); #else char c = a[a.Length - 1]; c++; if (c > '9') { c = '1'; } //c--; if (c < '1') { c = '9'; } a = a.Substring(0, a.Length - 1) + c; address = new PrefixedIPv4Address() { Address = a, PrefixLength = adr.PrefixLength }; #endif interfaceToken = n.token; break; } return(address); }
string MyV4String(PrefixedIPv4Address address) { return(address.Address + " [" + address.PrefixLength + "]"); }