本文整理汇总了C#中System.Net.IPAddress.GetAddressBytes方法的典型用法代码示例。如果您正苦于以下问题:C# IPAddress.GetAddressBytes方法的具体用法?C# IPAddress.GetAddressBytes怎么用?C# IPAddress.GetAddressBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.IPAddress
的用法示例。
在下文中一共展示了IPAddress.GetAddressBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetNetworkAddressAsString
public static string GetNetworkAddressAsString(IPAddress address, IPAddress mask)
{
string netIP = ""; // Network address as string
int networkLength = 0;
if (null != mask)
{
for (int i = 0; i < 4; i++)
{
byte ba = address.GetAddressBytes()[i];
byte bm = mask.GetAddressBytes()[i];
netIP += ba & bm;
if (i < 3) netIP += ".";
networkLength += 8 - (int)System.Math.Truncate(System.Math.Log(256 - bm, 2));
}
netIP += "/" + networkLength;
}
else
{
netIP = address.ToString() + "/32";
}
return netIP;
}
示例2: SocketAddress
public SocketAddress(IPAddress ipAddress)
: this(ipAddress.AddressFamily, ipAddress.AddressFamily == AddressFamily.InterNetwork ? 16 : 28)
{
this.m_buffer[2] = (byte)0;
this.m_buffer[3] = (byte)0;
if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
{
this.m_buffer[4] = (byte)0;
this.m_buffer[5] = (byte)0;
this.m_buffer[6] = (byte)0;
this.m_buffer[7] = (byte)0;
long scopeId = ipAddress.ScopeId;
this.m_buffer[24] = (byte)scopeId;
this.m_buffer[25] = (byte)(scopeId >> 8);
this.m_buffer[26] = (byte)(scopeId >> 16);
this.m_buffer[27] = (byte)(scopeId >> 24);
byte[] addressBytes = ipAddress.GetAddressBytes();
for (int index = 0; index < addressBytes.Length; ++index)
this.m_buffer[8 + index] = addressBytes[index];
}
else
{
System.Buffer.BlockCopy(ipAddress.GetAddressBytes(), 0, m_buffer, 4, 4);
}
}
示例3: FormatIp
public static string FormatIp(IPAddress ipa)
{
string ipData = string.Empty;
if (ipa.AddressFamily == AddressFamily.InterNetwork)
{
ipData = "IPv4";
foreach (byte b in ipa.GetAddressBytes())
{
if (ipData[ipData.Length - 1] != ' ')
ipData += ".";
ipData += string.Format("{0}", b);
}
}
else
{
ipData = "IPv6";
byte[] bytes = ipa.GetAddressBytes();
for (int i = 0; i < bytes.Length; i = i + 2)
{
if (ipData[ipData.Length - 1] != ' ')
ipData += ":";
byte b = bytes[i];
ipData += string.Format("{0:X2}", b);
b = bytes[i + 1];
ipData += string.Format("{0:X2}", b);
}
}
return ipData;
}
示例4: WhoisTask
public WhoisTask(IPAddress ip)
{
StringBuilder host = new StringBuilder();
switch (ip.AddressFamily)
{
// Turn the address into its in-addr.arpa form.
case System.Net.Sockets.AddressFamily.InterNetwork:
foreach (byte octet in ip.GetAddressBytes().Reverse())
{
host.Append(octet.ToString() + ".");
}
host.Append("in-addr.arpa");
break;
// Turn the address into its ip6.arpa form.
case System.Net.Sockets.AddressFamily.InterNetworkV6:
foreach (byte octet in ip.GetAddressBytes().Reverse())
{
string hex = string.Format("{0:x2}", octet);
host.Append(string.Format("{0}.{1}.", hex[1], hex[0]));
}
host.Append("ip6.arpa");
break;
}
Host = host.ToString();
}
示例5: sockaddr_in6
public sockaddr_in6(IPAddress address)
{
if (address.AddressFamily == AddressFamily.InterNetworkV6)
{
this.sin6_addr = address.GetAddressBytes();
this.sin6_scope_id = (uint) address.ScopeId;
}
else
{
byte[] addressBytes = address.GetAddressBytes();
this.sin6_addr = new byte[0x10];
for (int i = 0; i < 10; i++)
{
this.sin6_addr[i] = 0;
}
this.sin6_addr[10] = 0xff;
this.sin6_addr[11] = 0xff;
for (int j = 12; j < 0x10; j++)
{
this.sin6_addr[j] = addressBytes[j - 12];
}
this.sin6_scope_id = 0;
}
this.sin6_family = 0x17;
this.sin6_port = 0;
this.sin6_flowinfo = 0;
}
示例6: BlockSource
public unsafe void BlockSource(IPAddress groupAdress, IPAddress sourceAdress, uint interfaceIndex = 0)
{
if (groupAdress.AddressFamily != sourceAdress.AddressFamily)
throw new ArgumentException("Address family must be the same");
var adress = groupAdress.GetAddressBytes();
var sourceadress = groupAdress.GetAddressBytes();
if (groupAdress.AddressFamily == AddressFamily.InterNetwork)
{
ip_mreq_source value = new ip_mreq_source();
value.imr_interface.s_b4 = (byte)interfaceIndex;
fixed (byte* a = adress)
Unsafe.CopyBlock(value.imr_multiaddr.Address, a, (uint)adress.Length);
fixed (byte* a = sourceadress)
Unsafe.CopyBlock(value.imr_multiaddr.Address, a, (uint)sourceadress.Length);
if (SetSocketOption(IPPROTO_IP_SocketOptions.IP_BLOCK_SOURCE, (void*)&value, Marshal.SizeOf<ip_mreq_source>()) != 0)
WinSock.ThrowLastWSAError();
}
else if (groupAdress.AddressFamily == AddressFamily.InterNetworkV6)
{
//ipv6_mreq value = new ipv6_mreq();
//value.ipv6mr_interface = interfaceIndex;
//fixed (byte* a = adress)
// Unsafe.CopyBlock(value.ipv6mr_multiaddr.Address, a, (uint)adress.Length);
//if (SetSocketOption(IPPROTO_IPV6_SocketOptions., (void*)&value, Marshal.SizeOf<ipv6_mreq>()) != 0)
// WinSock.ThrowLastWSAError();
}
}
示例7: IPToLong
private static long IPToLong(IPAddress address)
{
MemoryStream ms = new MemoryStream();
ms.Write(address.GetAddressBytes(), 0, address.GetAddressBytes().Length);
while (ms.Length < 8)
ms.WriteByte(0);
return System.BitConverter.ToInt64(ms.ToArray(), 0);
}
示例8: CloneAddress
public static IPAddress CloneAddress(IPAddress source, bool maskScopeId)
{
if (maskScopeId || V4Address(source))
{
return new IPAddress(source.GetAddressBytes());
}
return new IPAddress(source.GetAddressBytes(), source.ScopeId);
}
示例9: InternetAddress
public InternetAddress(IPAddress address)
{
if (address == null || address.GetAddressBytes().Length != 4)
{
_address = null;
}
_address = address.GetAddressBytes();
}
示例10: NetAddr
public NetAddr(Services services, IPAddress address, UInt16 port)
{
this.services = services;
this.address = address;
this.port = port;
if (address.GetAddressBytes().Length != 16)
this.address = new IPAddress((new Byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF })
.Concat(address.GetAddressBytes()).ToArray());
}
示例11: IsInRange
public bool IsInRange(IPAddress address)
{
// Some people just have to be like that...
if (address.AddressFamily != Family || address.GetLength() != LowerBoundary.Length)
return false;
var bytes = address.GetAddressBytes();
var lowerBoundary = true;
var upperBoundary = true;
for (var i = 0; i < LowerBoundary.Length && (lowerBoundary || upperBoundary); i++)
{
var currentByte = bytes[i];
var lowerByte = LowerBoundary[i];
var upperByte = UpperBoundary[i];
if ((lowerBoundary && currentByte < lowerByte) || (upperBoundary && currentByte > upperByte))
return false;
lowerBoundary &= currentByte == lowerByte;
upperBoundary &= currentByte == upperByte;
}
return true;
}
示例12: GetPseudoHeader
// get IPv6 pseudo-header (adapted from: http://www.winsocketdotnetworkprogramming.com/clientserversocketnetworkcommunication8f_3.html)
public static byte[] GetPseudoHeader(IPAddress sourceIP, IPAddress destinationIP, int icmpv6Length, int nextHeader)
{
byte[] pseudoHeader, byteValue;
int offset = 0, payLoadLength;
// now build the pseudo header
pseudoHeader = new byte[40];
byteValue = sourceIP.GetAddressBytes();
Array.Copy(byteValue, 0, pseudoHeader, offset, byteValue.Length);
offset += byteValue.Length;
byteValue = destinationIP.GetAddressBytes();
Array.Copy(byteValue, 0, pseudoHeader, offset, byteValue.Length);
offset += byteValue.Length;
// Packet total length
payLoadLength = IPAddress.HostToNetworkOrder(4 + icmpv6Length);
byteValue = BitConverter.GetBytes(payLoadLength);
Array.Copy(byteValue, 0, pseudoHeader, offset, byteValue.Length);
offset += byteValue.Length;
// 3 bytes of zero padding
pseudoHeader[offset++] = (byte)0;
pseudoHeader[offset++] = (byte)0;
pseudoHeader[offset++] = (byte)0;
pseudoHeader[offset++] = (byte)nextHeader;
return pseudoHeader;
}
示例13: ParseIPv6AddressScope
private static IPAddress ParseIPv6AddressScope(Uri uri, IPAddress ipWithoutScope)
{
// Sometimes this can be escaped, sometimes not
var idnHost = WebUtility.UrlDecode(uri.DnsSafeHost); // IdnHost is preferred in .NET 4.6
var hostWithScopeParts = idnHost.Split(new[] { '%' }, 2);
// Did they provide a scope?
IPAddress ipWithScope;
if (hostWithScopeParts.Length > 1)
{
var scopeName = hostWithScopeParts[1];
// Just in case Syncthing ever starts returning proper scope IDs...
long scopeId;
if (!Int64.TryParse(scopeName, out scopeId))
{
var scopeLevel = ipWithoutScope.IsIPv6SiteLocal ? ScopeLevel.Site : ScopeLevel.Interface;
// I've seen Go produce ID and Name
var network = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(x => x.Id == scopeName || x.Name == scopeName);
if (network == null)
throw new FormatException($"Unable to find an interface with name {scopeName}");
scopeId = network.GetIPProperties().GetIPv6Properties().GetScopeId(scopeLevel);
}
ipWithScope = new IPAddress(ipWithoutScope.GetAddressBytes(), scopeId);
}
else
{
ipWithScope = ipWithoutScope;
}
return ipWithScope;
}
示例14: GetHostByAddr
public static unsafe IPHostEntry GetHostByAddr(IPAddress addr)
{
// TODO #2891: Optimize this (or decide if this legacy code can be removed):
byte[] addressBytes = addr.GetAddressBytes();
var address = new Interop.libc.in_addr { s_addr = unchecked((uint)BitConverter.ToInt32(addressBytes, 0)) };
int bufferSize = 512;
byte* stackBuffer = stackalloc byte[bufferSize];
var hostent = default(Interop.libc.hostent);
var result = (Interop.libc.hostent*)null;
if (TryGetHostByAddr(address, stackBuffer, bufferSize, &hostent, &result))
{
return CreateHostEntry(result);
}
for (; ;)
{
bufferSize *= 2;
fixed (byte* heapBuffer = new byte[bufferSize])
{
if (TryGetHostByAddr(address, heapBuffer, bufferSize, &hostent, &result))
{
return CreateHostEntry(result);
}
}
}
}
示例15: IsInRange
public bool IsInRange(IPAddress address)
{
if (address.AddressFamily != addressFamily)
{
return false;
}
var addressBytes = address.GetAddressBytes();
bool lowerBoundary = true, upperBoundary = true;
for (var i = 0; i < lowerBytes.Length &&
(lowerBoundary || upperBoundary); i++)
{
if ((lowerBoundary && addressBytes[i] < lowerBytes[i]) || (upperBoundary && addressBytes[i] > upperBytes[i]))
{
return false;
}
lowerBoundary &= (addressBytes[i] == lowerBytes[i]);
upperBoundary &= (addressBytes[i] == upperBytes[i]);
}
return true;
}