private static void GetMacAddress(System.Net.IPAddress address, Action<PhysicalAddress> callback) { new Thread(() => { try { var destAddr = BitConverter.ToInt32(address.GetAddressBytes(), 0); var srcAddr = BitConverter.ToInt32(System.Net.IPAddress.Any.GetAddressBytes(), 0); var macAddress = new byte[6]; var macAddrLen = macAddress.Length; var ret = SendArp(destAddr, srcAddr, macAddress, ref macAddrLen); if (ret != 0) { throw new System.ComponentModel.Win32Exception(ret); } var mac = new PhysicalAddress(macAddress); if (callback != null) callback(mac); } catch { //do nothing } }) { IsBackground = true }.Start(); }
public static string getReversedIpString(System.Net.IPAddress ipAddress) { byte[ ] ipForward = ipAddress.GetAddressBytes( ); byte[ ] ipReverse = new byte[ 4 ]; ipReverse[ 0 ] = ipForward[ 3 ]; ipReverse[ 1 ] = ipForward[ 2 ]; ipReverse[ 2 ] = ipForward[ 1 ]; ipReverse[ 3 ] = ipForward[ 0 ]; return new System.Net.IPAddress(ipReverse).ToString(); }
public ushort GetChecksum(ref byte[] Packet, int start, int end, ref System.Net.IPAddress SourceAddress, ref System.Net.IPAddress DestinationAddress) { byte[] PseudoPacket; PseudoPacket = new byte[12 + Packet.Length]; System.Buffer.BlockCopy(SourceAddress.GetAddressBytes(), 0, PseudoPacket, 0, 4); System.Buffer.BlockCopy(DestinationAddress.GetAddressBytes(), 0, PseudoPacket, 4, 4); PseudoPacket[8] = 0; PseudoPacket[9] = 6; System.Buffer.BlockCopy(System.BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder((short)Packet.Length)), 0, PseudoPacket, 10, 2); System.Buffer.BlockCopy(Packet, 0, PseudoPacket, 12, Packet.Length); return IPPacket.GetChecksum(ref PseudoPacket, 0, PseudoPacket.Length - 1); }
public static bool IsInRange(System.Net.IPAddress address, byte?[] range) { if(address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork) { return false; } for(int i = 0; i < range.Length; i++) { if(range[i].HasValue) { if (address.GetAddressBytes()[i] != range[i].Value) { return false; } } } return true; }
private static bool IsValidIPv4Address(System.Net.IPAddress address) { return address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && address.GetAddressBytes()[0] < (byte)224 && address.GetAddressBytes()[0] > (byte)0 && Array.IndexOf<byte>(address.GetAddressBytes(), 0xFF) == -1; }
public static System.Net.IPAddress ApplyNetmask(System.Net.IPAddress address, System.Net.IPAddress netmask) { byte[] addr = address.GetAddressBytes(); byte[] mask = netmask.GetAddressBytes(); if (addr.Length != mask.Length) throw new ArgumentOutOfRangeException("Address and netmask do not have the same address length"); for (int i = 0; i < addr.Length; ++i) { addr[i] &= mask[i]; } return new System.Net.IPAddress(addr); }
public static bool IsValidNetmask(System.Net.IPAddress netmask) { bool bits = true; byte[] bytes = netmask.GetAddressBytes(); for (int i = 0; i < bytes.Length; ++i) { if (bits && bytes[i] == 0xFF) continue; else if (!bits && bytes[i] == 0x00) continue; else if (bits && Array.IndexOf<byte>(validmaskbytes, bytes[i]) != -1) bits = false; else return false; } return true; }
private bool checkValidSubnet(System.Net.IPAddress subnet) { //No way is this the best way to do this but it gets the job done byte[] btssubnet = subnet.GetAddressBytes(); bool subnetValid = true; bool endSubnet = false; for (int i = 0; i < btssubnet.Count(); i++) { if (btssubnet[i] == 0 || btssubnet[i] == 128 || btssubnet[i] == 192 || btssubnet[i] == 224 || btssubnet[i] == 240 || btssubnet[i] == 248 || btssubnet[i] == 252 || btssubnet[i] == 254 || btssubnet[i] == 255) { if (endSubnet) { if (btssubnet[i] != 0) { subnetValid = false; break; } } if (btssubnet[i] == 0) { endSubnet = true; } } else { subnetValid = false; break; } } return subnetValid; }
/// <summary> /// Create a new IIPAddress based on the given System.Net.IPAddress. /// </summary> /// <param name="IPAddress">A System.Net.IPAddress.</param> public static IIPAddress Build(System.Net.IPAddress IPAddress) { return Build(IPAddress.GetAddressBytes()); }
/// <summary> /// Converts a Physical Address object in a XX:XX:XX:XX:XX format string corresponding to it's Mac Address. /// </summary> /// <param name="pa">The physical address to convert.</param> /// <returns>A formated string of the physical address' value.</returns> public static String PhysicalAddressToString(System.Net.NetworkInformation.PhysicalAddress pa) { return string.Join(":", (from z in pa.GetAddressBytes() select z.ToString("X2")).ToArray()); }
/// <summary> /// Return an unsigned BigInteger from an IP address /// </summary> /// <param name="ip">Ethernet MAC address</param> /// <returns>BigInteger value of the address</returns> private static BigInteger GetAddressNumber(System.Net.NetworkInformation.PhysicalAddress mac) { return GetAddressNumber(mac.GetAddressBytes()); }
public static PatriciaTrieKey FromIpAddress(System.Net.IPAddress addr) { byte[] bytes = addr.GetAddressBytes(); UInt64 mask = 0; for (int i = 0; i < bytes.Length; i++) { mask = (mask << 8) | bytes[i]; } return new PatriciaTrieKey((long)mask, bytes.Length * 8); }
/// <summary> /// Checks if the given IP falls into the specified range /// </summary> /// <param name="target">The IP to check</param> /// <returns>True if it is inside the range, otherwise false</returns> public override bool Matches(System.Net.IPAddress target) { if (target == null) throw new ArgumentNullException("target"); byte[] addressBytes = target.GetAddressBytes(); bool lowerBoundary = true, upperBoundary = true; for (int i = 0; i < StartAddress.GetAddressBytes().Length && (lowerBoundary || upperBoundary); i++) { if ((lowerBoundary && addressBytes[i] < StartAddress.GetAddressBytes()[i]) || (upperBoundary && addressBytes[i] > EndAddress.GetAddressBytes()[i])) { return false; } lowerBoundary &= (addressBytes[i] == StartAddress.GetAddressBytes()[i]); upperBoundary &= (addressBytes[i] == EndAddress.GetAddressBytes()[i]); } return true; }
// Constructors public ConfigureAlertMessageRoute(byte bRouteNumber, System.Net.IPAddress destIPAddress, int destPortNum) { byte[] buffer = { (byte)BravaCodes.Tasks.Configure, (byte)BravaCodes.Functions.Gateway, (byte)BravaCodes.GatewayConfiguration.SetMessageRoute, bRouteNumber // This number is application specific. This route is stored and referenced by this number. }; // Header m_reqStrm = new MemoryStream(16); m_reqStrm.Write(buffer, 0, 4); // Write Route Address Record. // Layout [bytes]{Array} // [RouteNumber][RouteAddressType=EthernetIP(1)][TASK][FUNCTION][DATASET][RECORDINDEX]{IPADR0..4}{PORT0..1}{UTC_TIME0..4} m_reqStrm.WriteByte(bRouteNumber); m_reqStrm.WriteByte((byte)BravaCodes.RouteAddressType.EthernetIP); // Write the Message ID that must be routed. byte[] buffer1 = { (byte)BravaCodes.Tasks.Status, (byte)BravaCodes.Functions.GenericIO, (byte)BravaCodes.GenericIORecords.DigitalInput, (byte)BravaConstants.AllRecords }; m_reqStrm.Write(buffer1, 0, 4); // Write the destination Address for the specified Message ID. // In this case it's IP address and Port Address. m_reqStrm.Write(destIPAddress.GetAddressBytes(), 0, 4); m_reqStrm.Write(BitConverter.GetBytes(destPortNum), 0, 2); // Write the UTC Time Stamp. DateTime TimeBase = DateTime.Parse("01 January 2000"); DateTime UTCTime = DateTime.UtcNow; TimeSpan UTCOffset = (TimeSpan)(UTCTime - TimeBase); int reqTimeStamp = (int)UTCOffset.TotalSeconds; System.Diagnostics.Debug.WriteLine("UTC Seconds is" + reqTimeStamp.ToString()); buffer = BitConverter.GetBytes(reqTimeStamp); m_reqStrm.Write(buffer, 0, 4); // 20 bytes long }
public static System.Net.IPAddress Not(System.Net.IPAddress mask) { byte[] mm = mask.GetAddressBytes(); for (int i = 0; i < mm.Length; i++) mm[i] = (byte)(~mm[i]); return new System.Net.IPAddress(mm); }
public static System.Net.IPAddress ReverseIpAddress(System.Net.IPAddress iPAddress) { byte[] byteArray = iPAddress.GetAddressBytes(); Array.Reverse(byteArray); return new System.Net.IPAddress(byteArray); }
/// <summary>Constructor</summary> /// <remarks>Construct the IpAddress class from supplied IPAddress value.</remarks> /// <param name="inetAddr">IP Address to use to initialize the object.</param> public IpAddress(System.Net.IPAddress inetAddr) : this(inetAddr.GetAddressBytes()) { _asnType = SnmpConstants.SMI_IPADDRESS; }
private static void CheckIPVersion(System.Net.IPAddress ipAddress, System.Net.IPAddress mask, out byte[] addressBytes, out byte[] maskBytes) { if (ipAddress == null) throw new System.ArgumentNullException("ipAddress"); if (mask == null) throw new System.ArgumentNullException("mask"); addressBytes = ipAddress.GetAddressBytes(); maskBytes = mask.GetAddressBytes(); if (addressBytes.Length != maskBytes.Length) { throw new System.ArgumentException("The address and mask don't use the same IP standard"); } }
public static In6Addr ToIn6Addr (System.Net.IPAddress address) { if (address == null) throw new ArgumentNullException ("address"); if (address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6) throw new ArgumentException ("address", "address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6"); return new In6Addr (address.GetAddressBytes ()); }
private static System.Net.IPAddress BitOperate(System.Net.IPAddress x, System.Net.IPAddress mask, Operate op) { byte[] xx = x.GetAddressBytes(); byte[] mm = mask.GetAddressBytes(); if (xx.Length != mm.Length) throw new ApplicationException("IP地址版本不一致,不能执行“或”操作"); int i = 0; for (i = 0; i < xx.Length; i++) xx[i] = op(xx[i], mm[i]); return new System.Net.IPAddress(xx); }
public static string GetMacAddressByIP(System.Net.IPAddress ipAddress) { byte[] macBytes = new byte[6]; int length = 6; SendARP(BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0), 0, macBytes, ref length); return BitConverter.ToString(macBytes, 0, 6); }
public static List<byte[]> GetPatchedIPData(int iVersion, System.Net.IPAddress theIP, uint thePort) { List<byte[]> patchedThing = new List<byte[]>(); switch (iVersion) { case 3: { byte[] ipdata = new byte[StaticData.UOKR_IPDATA_3a.Length]; StaticData.UOKR_IPDATA_3a.CopyTo(ipdata, 0); ipdata[4] = theIP.GetAddressBytes()[3]; ipdata[5] = theIP.GetAddressBytes()[2]; ipdata[6] = theIP.GetAddressBytes()[1]; ipdata[7] = theIP.GetAddressBytes()[0]; ipdata[13] = (byte)(thePort & 0xFF); ipdata[14] = (byte)((thePort & 0xFF00) >> 8); patchedThing.Add(ipdata); byte[] ipdata2 = new byte[StaticData.UOKR_IPDATA_3b.Length]; StaticData.UOKR_IPDATA_3b.CopyTo(ipdata2, 0); ipdata2[2] = theIP.GetAddressBytes()[3]; ipdata2[3] = theIP.GetAddressBytes()[2]; ipdata2[4] = theIP.GetAddressBytes()[1]; ipdata2[5] = theIP.GetAddressBytes()[0]; ipdata2[10] = (byte)(thePort & 0xFF); ipdata2[11] = (byte)((thePort & 0xFF00) >> 8); ipdata2[20] = (byte)(thePort & 0xFF); ipdata2[21] = (byte)((thePort & 0xFF00) >> 8); patchedThing.Add(ipdata2); } break; case 2: { byte[] ipdata = new byte[StaticData.UOKR_IPDATA_2a.Length]; StaticData.UOKR_IPDATA_2a.CopyTo(ipdata, 0); ipdata[4] = theIP.GetAddressBytes()[3]; ipdata[5] = theIP.GetAddressBytes()[2]; ipdata[6] = theIP.GetAddressBytes()[1]; ipdata[7] = theIP.GetAddressBytes()[0]; ipdata[13] = (byte)(thePort & 0xFF); ipdata[14] = (byte)((thePort & 0xFF00) >> 8); patchedThing.Add(ipdata); byte[] ipdata2 = new byte[StaticData.UOKR_IPDATA_2b.Length]; StaticData.UOKR_IPDATA_2b.CopyTo(ipdata2, 0); ipdata2[2] = theIP.GetAddressBytes()[3]; ipdata2[3] = theIP.GetAddressBytes()[2]; ipdata2[4] = theIP.GetAddressBytes()[1]; ipdata2[5] = theIP.GetAddressBytes()[0]; ipdata2[10] = (byte)(thePort & 0xFF); ipdata2[11] = (byte)((thePort & 0xFF00) >> 8); ipdata2[20] = (byte)(thePort & 0xFF); ipdata2[21] = (byte)((thePort & 0xFF00) >> 8); patchedThing.Add(ipdata2); } break; case 1: { byte[] ipdata = new byte[StaticData.UOKR_IPDATA_1a.Length]; StaticData.UOKR_IPDATA_1a.CopyTo(ipdata, 0); ipdata[4] = theIP.GetAddressBytes()[3]; ipdata[5] = theIP.GetAddressBytes()[2]; ipdata[6] = theIP.GetAddressBytes()[1]; ipdata[7] = theIP.GetAddressBytes()[0]; ipdata[13] = (byte)(thePort & 0xFF); ipdata[14] = (byte)((thePort & 0xFF00) >> 8); patchedThing.Add(ipdata); byte[] ipdata2 = new byte[StaticData.UOKR_IPDATA_1b.Length]; StaticData.UOKR_IPDATA_1b.CopyTo(ipdata2, 0); ipdata2[2] = theIP.GetAddressBytes()[3]; ipdata2[3] = theIP.GetAddressBytes()[2]; ipdata2[4] = theIP.GetAddressBytes()[1]; ipdata2[5] = theIP.GetAddressBytes()[0]; ipdata2[10] = (byte)(thePort & 0xFF); ipdata2[11] = (byte)((thePort & 0xFF00) >> 8); ipdata2[20] = (byte)(thePort & 0xFF); ipdata2[21] = (byte)((thePort & 0xFF00) >> 8); patchedThing.Add(ipdata2); } break; case 0: { byte[] ipdata = new byte[StaticData.UOKR_IPDATA_0.Length]; StaticData.UOKR_IPDATA_0.CopyTo(ipdata, 0); ipdata[1] = theIP.GetAddressBytes()[3]; ipdata[3] = theIP.GetAddressBytes()[1]; ipdata[8] = theIP.GetAddressBytes()[0]; ipdata[14] = theIP.GetAddressBytes()[2]; ipdata[24] = (byte)(thePort & 0xFF); ipdata[25] = (byte)((thePort & 0xFF00) >> 8); ipdata[30] = (byte)(thePort & 0xFF); ipdata[31] = (byte)((thePort & 0xFF00) >> 8); patchedThing.Add(ipdata); } break; default: { } break; } return patchedThing; }
/// <summary> /// Encodes the ip address. /// </summary> /// <param name="bytes">The bytes.</param> /// <param name="offset">The offset.</param> /// <param name="ipAddress">The ip address.</param> /// <returns>offset as int</returns> private static int EncodeIPAddress(this byte[] bytes, int offset, System.Net.IPAddress ipAddress) { offset = bytes.EncodeClassConstructType(offset, Asn1Class.Application, ConstructType.Primitive, (byte)Asn1SnmpTag.IpAddress); offset = bytes.EncodeLength(offset, 4); byte[] bytesEncoded = ipAddress.GetAddressBytes(); Array.Copy(bytesEncoded, 0, bytes, offset, bytesEncoded.Length); return offset + 4; }
static public int Distance(System.Net.IPAddress x, System.Net.IPAddress y) { return IPSeg.Distance(x.GetAddressBytes(), y.GetAddressBytes()); }
/// <summary> /// Return an unsigned BigInteger from an IP address /// </summary> /// <param name="ip">IPv4 or IPv6 address</param> /// <returns>BigInteger value of the address</returns> private static BigInteger GetAddressNumber(System.Net.IPAddress ip) { return GetAddressNumber(ip.GetAddressBytes()); }
public static void IPDatabaseAdd(System.Net.IPAddress ax) { if (ax == null) return; string left = null; Byte[] bytes = ax.GetAddressBytes(); if (bytes.Length != 4) { Console.WriteLine("IPAddress error: {0}", ax.ToString()); return; } // get first 3 octets only to mask out the whole range covered by the 4th octet left += bytes[0].ToString() + "."; left += bytes[1].ToString() + "."; left += bytes[2].ToString(); m_IPDatabase[left] = null; }
private System.Net.IPAddress getNetworkAddress(System.Net.IPAddress ip, System.Net.IPAddress subnet) { //Based on the code by knom at http://blogs.msdn.com/b/knom/archive/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks.aspx byte[] btsIP = ip.GetAddressBytes(); byte[] btsSubnet = subnet.GetAddressBytes(); byte[] btsBroadcast = new byte[btsIP.Length]; for (int i = 0; i < btsBroadcast.Length; i++) { btsBroadcast[i] = (byte)(btsIP[i] & (btsSubnet[i])); } return new System.Net.IPAddress(btsBroadcast); }
public static byte[] crearA_Logueo(Terminal ter, string[] UltimaCon, System.Net.IPAddress ipLocal) { #region //variables mensaje A // string idBody1 = "N"; string datos1 = UltimaCon[0]; // string idBody2 = "P"; string datos2 = UltimaCon[1]; // string idBody3 = "T"; string datos3 = UltimaCon[2]; //Fecha/Hora real con ceros agregados si fuese necesario string dia, mes, anio, hora, min, seg; if ((Convert.ToString(ter.FechaHora.Day).Length < 2)) { dia = "0" + Convert.ToString(ter.FechaHora.Day); } else { dia = Convert.ToString(ter.FechaHora.Day); } if ((Convert.ToString(ter.FechaHora.Month).Length < 2)) { mes = "0" + Convert.ToString(ter.FechaHora.Month); } else { mes = Convert.ToString(ter.FechaHora.Month); } if ((Convert.ToString(ter.FechaHora.Year).Length > 2)) { anio = Convert.ToString(ter.FechaHora.Year).Remove(0, 2); } else { anio = Convert.ToString(ter.FechaHora.Year); } if ((Convert.ToString(ter.FechaHora.Hour).Length < 2)) { hora = "0" + Convert.ToString(ter.FechaHora.Hour); } else { hora = Convert.ToString(ter.FechaHora.Hour); } if ((Convert.ToString(ter.FechaHora.Minute).Length < 2)) { min = "0" + Convert.ToString(ter.FechaHora.Minute); } else { min = Convert.ToString(ter.FechaHora.Minute); } if ((Convert.ToString(ter.FechaHora.Second).Length < 2)) { seg = "0" + Convert.ToString(ter.FechaHora.Second); } else { seg = Convert.ToString(ter.FechaHora.Second); } #endregion //header byte[] pacHead = DataConverter.Pack("^$8SbbII$8$8$8$8$8$8S", "A", 0, (int)TransacManager.ProtoConfig.TIPO_CXN, ter.Tipo, ter.Tarjeta, ter.NumeroTerminal, dia, mes, anio, hora, min, seg, ter.Version); byte[] pacSal1 = new byte[1024]; TARJETA = ter.Tarjeta; int lon = pacHead.Length + ter.MacTarjeta.Length; Array.Copy(pacHead, pacSal1, pacHead.Length); Array.Copy(ter.MacTarjeta, 0, pacSal1, pacHead.Length, ter.MacTarjeta.Length); string telefono = UltimaCon[2]; for (int i = 0; i < 15 - UltimaCon[2].Length; i++) { telefono = telefono + " "; } telefono = telefono.Substring(0, 15); string user = UltimaCon[0]; for (int i = 0; i < 10 - UltimaCon[0].Length; i++) { user = "******" + user; ; } user = user.Substring(0, 10); byte[] pacSal2 = DataConverter.Pack("^$8$8", telefono, user); Array.Copy(pacSal2, 0, pacSal1, lon, pacSal2.Length); byte[] pacSal3 = DataConverter.Pack("^SS", 512, Convert.ToUInt16(UltimaCon[1])); byte[] ipBytes = ipLocal.GetAddressBytes(); Array.Copy(pacSal3, 0, pacSal1, lon+pacSal2.Length, pacSal3.Length); Buffer.BlockCopy(ipBytes, 0, pacSal1, lon + pacSal2.Length + pacSal3.Length, ipBytes.Length); lon += pacSal2.Length + pacSal3.Length + ipBytes.Length; Array.Resize(ref pacSal1, lon); return pacSal1; }
public static bool IPLookup(System.Net.IPAddress ip) { if (ip == null || m_IPDatabase == null) return false; string left = null; Byte[] bytes = ip.GetAddressBytes(); if (bytes.Length != 4) { Console.WriteLine("IPAddress error: {0}", ip.ToString()); return false; } // get first 3 octets only to mask out the whole range covered by the 4th octet left += bytes[0].ToString() + "."; left += bytes[1].ToString() + "."; left += bytes[2].ToString(); return m_IPDatabase.ContainsKey(left); }
static public System.Net.IPAddress Minus(System.Net.IPAddress x, byte y) { byte[] New = Minus(x.GetAddressBytes(), y); string ni = New.Length == 4 ? string.Format("{0}.{1}.{2}.{3}", New[0], New[1], New[2], New[3]) : string.Format("{0:x2}{1:x2}:{2:x2}{3:x2}:{4:x2}{5:x2}:{6:x2}{7:x2}:{8:x2}{9:x2}:{10:x2}{11:x2}:{12:x2}{13:x2}:{14:x2}{15:x2}", New[0], New[1], New[2], New[3], New[4], New[5], New[6], New[7], New[8], New[9], New[10], New[11], New[12], New[13], New[14], New[15]); return System.Net.IPAddress.Parse(ni); }