public InterfaceAddress[] GetInterfaceAddresses() { List <InterfaceAddress> result = new List <InterfaceAddress>(); IntPtr ifap; if (getifaddrs(out ifap) != 0) { throw new SystemException("getifaddrs() failed"); } try { IntPtr next = ifap; while (next != IntPtr.Zero) { ifaddrs addr = (ifaddrs)Marshal.PtrToStructure(next, typeof(ifaddrs)); string name = addr.ifa_name; if (addr.ifa_addr != IntPtr.Zero) { int index = if_nametoindex(name); sockaddr_in sockaddr = (sockaddr_in)Marshal.PtrToStructure(addr.ifa_addr, typeof(sockaddr_in)); if (sockaddr.sin_family == AF_INET6) { sockaddr_in6 nmask = (sockaddr_in6)Marshal.PtrToStructure(addr.ifa_netmask, typeof(sockaddr_in6)); int prefixLength = GetIPv6PrefixLength(nmask.sin6_addr.u6_addr8); sockaddr_in6 sockaddr6 = (sockaddr_in6)Marshal.PtrToStructure(addr.ifa_addr, typeof(sockaddr_in6)); IPAddress address = new IPAddress(sockaddr6.sin6_addr.u6_addr8, sockaddr6.sin6_scope_id); InterfaceAddress info = new InterfaceAddress(index, name, address, prefixLength); result.Add(info); } else if (sockaddr.sin_family == AF_INET) { sockaddr_in netmaskaddr = (sockaddr_in)Marshal.PtrToStructure(addr.ifa_netmask, typeof(sockaddr_in)); IPAddress netmask = new IPAddress(netmaskaddr.in_addr); IPAddress address = new IPAddress(sockaddr.in_addr); InterfaceAddress info = new InterfaceAddress(index, name, address, netmask); result.Add(info); } } next = addr.ifa_next; } } finally { freeifaddrs(ifap); } return(result.ToArray()); }
static void Bind(TcpListener listener, string ipAddress, int port, bool ip6 = false, bool dualstack = false) { if (ip6) { sockaddr_in6 address = CreateSockaddrIp6(ipAddress.ToString(), port); UVException.ThrowIfError(UVInterop.uv_tcp_bind(listener.Handle, ref address, (uint)(dualstack ? 0 : 1))); } else { sockaddr_in address = CreateSockaddr(ipAddress.ToString(), port); UVException.ThrowIfError(UVInterop.uv_tcp_bind(listener.Handle, ref address, 0)); } }
public override string ToString() { int StrLength = 256; StringBuilder sb = new StringBuilder(StrLength); sockaddr_in6 saddr = new sockaddr_in6(); saddr.sin6_family = (int)AddressFamily.InterNetworkV6; saddr.sin6_scope_id = m_ScopeIdentifier; saddr.sin6_addr = m_Address; int errorCode = WSAAddressToString(ref saddr, 28, IntPtr.Zero, sb, ref StrLength); return(sb.ToString()); }
public static IPv6Address Parse(string address) { if (address == null) { throw new ArgumentNullException("Bad string address"); } TryAgain: int Size = 28; sockaddr_in6 SockAddrIN6 = new sockaddr_in6(); int ret = WSAStringToAddress(address, AddressFamily.InterNetworkV6, IntPtr.Zero, ref SockAddrIN6, ref Size); if (ret != 0) { int Err = Marshal.GetLastWin32Error(); if (Err == 10093) { WSAData wsaData = new WSAData(); ret = WSAStartup((short)0x0202, out wsaData); if (ret != 0) { throw new ArgumentException("WSAStartup failed"); } goto TryAgain; } throw new ArgumentException("WSAStringToAddress failed for " + address + " error " + Err.ToString()); } IPv6Address instance = new IPv6Address(SockAddrIN6.sin6_addr, SockAddrIN6.sin6_scope_id); return(instance); }
//Removed, replace this functionality by using bind(IPAddress.Any, ... or IPAddress.IPv6Any); public static IPAddress GetLocalRoutingInterface(IPAddress ipAddress) { Socket sock = null; IntPtr ptrInAddr = IntPtr.Zero; IntPtr ptrOutAddr = IntPtr.Zero; if ( ipAddress.AddressFamily == AddressFamily.InterNetwork ) { sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sockaddr_in inAddr = new sockaddr_in(); sockaddr_in outAddr = new sockaddr_in(); try { ipAddress.GetAddressBytes().CopyTo(inAddr.sin_addr, 0); // create a sockaddr_in function for our destination IP address inAddr.sin_port = IPAddress.HostToNetworkOrder(port); // create an block of unmanaged memory for use by Marshal.StructureToPtr. We seem to need to do this, even though // StructureToPtr will go ahead and release/reallocate the memory ptrInAddr = Marshal.AllocCoTaskMem(Marshal.SizeOf(inAddr)); // Copy inAddr from managed to unmanaged, reallocating the unmanaged memory Marshal.StructureToPtr(inAddr, ptrInAddr, true); // Create a managed byte array to hold the structure, but in byte array form byte[] byteInAddr = new byte[Marshal.SizeOf(inAddr)]; // Copy the structure from unmanaged ptr into managed byte array Marshal.Copy(ptrInAddr, byteInAddr, 0, byteInAddr.Length); // Create a second managed byte array to hold the output sockaddr_in structure byte[] byteOutAddr = new byte[Marshal.SizeOf(inAddr)]; // Make the call to IOControl, asking for the Interface we should use if we want to route a packet to inAddr sock.IOControl( SIO_ROUTING_INTERFACE_QUERY, byteInAddr, byteOutAddr ); // create the memory placeholder for our local interface // Copy the results from the byteOutAddr into an unmanaged pointer ptrOutAddr = Marshal.AllocCoTaskMem(Marshal.SizeOf(outAddr)); Marshal.Copy(byteOutAddr, 0, ptrOutAddr, byteOutAddr.Length); // Copy the data from the unmanaged pointer to the ourAddr structure Marshal.PtrToStructure(ptrOutAddr, outAddr); } catch(SocketException se) { // Perhaps there were no interfaces present, AKA No Network Adapters enabled/installed and connected to media (wired or wireless) EventLog.WriteEntry("RtpListener", se.ToString(), EventLogEntryType.Warning, 99); } finally { // Release the socket sock = null; Marshal.FreeCoTaskMem(ptrInAddr); Marshal.FreeCoTaskMem(ptrOutAddr); } // Return an IPAddress structure that is initialized with the value of the IP address contained in the outAddr structure if (outAddr != null) { int len = outAddr.sin_addr.Length; // Have to convert the byte[] to a uint. It turns out that the IPAddress ctor won't create an IPv4 address from four bytes, it only uses the byte[] ctor for 16 byte IPv6 construction?!? uint ipAsInt = 0; for (int i = len; i > 0; i--) { ipAsInt = ipAsInt * 256 + outAddr.sin_addr[i - 1]; } externalInterface = new IPAddress(ipAsInt); return(new IPAddress(ipAsInt)); } else { return null; } } if ( Socket.OSSupportsIPv6 && ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ) { sock = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); sockaddr_in6 inAddr = new sockaddr_in6(); sockaddr_in6 outAddr = new sockaddr_in6(); try { ipAddress.GetAddressBytes().CopyTo(inAddr.sin_addr, 0); // create a sockaddr_in function for our destination IP address inAddr.sin_port = IPAddress.HostToNetworkOrder(port); // create an block of unmanaged memory for use by Marshal.StructureToPtr. We seem to need to do this, even though // StructureToPtr will go ahead and release/reallocate the memory ptrInAddr = Marshal.AllocCoTaskMem(Marshal.SizeOf(inAddr)); // Copy inAddr from managed to unmanaged, reallocating the unmanaged memory Marshal.StructureToPtr(inAddr, ptrInAddr, true); // Create a managed byte array to hold the structure, but in byte array form byte[] byteInAddr = new byte[Marshal.SizeOf(inAddr)]; // Copy the structure from unmanaged ptr into managed byte array Marshal.Copy(ptrInAddr, byteInAddr, 0, byteInAddr.Length); // Create a second managed byte array to hold the output sockaddr_in structure byte[] byteOutAddr = new byte[Marshal.SizeOf(inAddr)]; // Make the call to IOControl, asking for the Interface we should use if we want to route a packet to inAddr sock.IOControl( SIO_ROUTING_INTERFACE_QUERY, byteInAddr, byteOutAddr ); // create the memory placeholder for our local interface // Copy the results from the byteOutAddr into an unmanaged pointer ptrOutAddr = Marshal.AllocCoTaskMem(Marshal.SizeOf(outAddr)); Marshal.Copy(byteOutAddr, 0, ptrOutAddr, byteOutAddr.Length); // Copy the data from the unmanaged pointer to the ourAddr structure Marshal.PtrToStructure(ptrOutAddr, outAddr); } catch (SocketException se) { // Perhaps there were no interfaces present, AKA No Network Adapters enabled/installed and connected to media (wired or wireless) EventLog.WriteEntry("RtpListener", se.ToString(), EventLogEntryType.Warning, 99); } finally { // Release the socket sock = null; Marshal.FreeCoTaskMem(ptrInAddr); Marshal.FreeCoTaskMem(ptrOutAddr); } // Return an IPAddress structure that is initialized with the value of the IP address contained in the outAddr structure if (outAddr != null) { externalInterface = new IPAddress(outAddr.sin_addr, outAddr.sin6_scope_id); return (new IPAddress(outAddr.sin_addr, outAddr.sin6_scope_id)); } else { return null; } } return null; }
//Removed, replace this functionality by using bind(IPAddress.Any, ... or IPAddress.IPv6Any); public static IPAddress GetLocalRoutingInterface(IPAddress ipAddress) { Socket sock = null; IntPtr ptrInAddr = IntPtr.Zero; IntPtr ptrOutAddr = IntPtr.Zero; if (ipAddress.AddressFamily == AddressFamily.InterNetwork) { sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sockaddr_in inAddr = new sockaddr_in(); sockaddr_in outAddr = new sockaddr_in(); try { ipAddress.GetAddressBytes().CopyTo(inAddr.sin_addr, 0); // create a sockaddr_in function for our destination IP address inAddr.sin_port = IPAddress.HostToNetworkOrder(port); // create an block of unmanaged memory for use by Marshal.StructureToPtr. We seem to need to do this, even though // StructureToPtr will go ahead and release/reallocate the memory ptrInAddr = Marshal.AllocCoTaskMem(Marshal.SizeOf(inAddr)); // Copy inAddr from managed to unmanaged Marshal.StructureToPtr(inAddr, ptrInAddr, false); // Create a managed byte array to hold the structure, but in byte array form byte[] byteInAddr = new byte[Marshal.SizeOf(inAddr)]; // Copy the structure from unmanaged ptr into managed byte array Marshal.Copy(ptrInAddr, byteInAddr, 0, byteInAddr.Length); // Create a second managed byte array to hold the output sockaddr_in structure byte[] byteOutAddr = new byte[Marshal.SizeOf(inAddr)]; // Make the call to IOControl, asking for the Interface we should use if we want to route a packet to inAddr sock.IOControl( SIO_ROUTING_INTERFACE_QUERY, byteInAddr, byteOutAddr ); // create the memory placeholder for our local interface // Copy the results from the byteOutAddr into an unmanaged pointer ptrOutAddr = Marshal.AllocCoTaskMem(Marshal.SizeOf(outAddr)); Marshal.Copy(byteOutAddr, 0, ptrOutAddr, byteOutAddr.Length); // Copy the data from the unmanaged pointer to the ourAddr structure Marshal.PtrToStructure(ptrOutAddr, outAddr); } catch (SocketException se) { // Perhaps there were no interfaces present, AKA No Network Adapters enabled/installed and connected to media (wired or wireless) EventLog.WriteEntry("RtpSession", se.ToString(), EventLogEntryType.Warning, 99); } finally { // Release the socket sock = null; Marshal.FreeCoTaskMem(ptrInAddr); Marshal.FreeCoTaskMem(ptrOutAddr); } // Return an IPAddress structure that is initialized with the value of the IP address contained in the outAddr structure if (outAddr != null) { int len = outAddr.sin_addr.Length; // Have to convert the byte[] to a uint. It turns out that the IPAddress ctor won't create an IPv4 address from four bytes, it only uses the byte[] ctor for 16 byte IPv6 construction?!? uint ipAsInt = 0; for (int i = len; i > 0; i--) { ipAsInt = ipAsInt * 256 + outAddr.sin_addr[i - 1]; } externalInterface = new IPAddress(ipAsInt); return(new IPAddress(ipAsInt)); } else { return(null); } } if (Socket.OSSupportsIPv6 && ipAddress.AddressFamily == AddressFamily.InterNetworkV6) { sock = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); sockaddr_in6 inAddr = new sockaddr_in6(); sockaddr_in6 outAddr = new sockaddr_in6(); try { ipAddress.GetAddressBytes().CopyTo(inAddr.sin_addr, 0); // create a sockaddr_in function for our destination IP address inAddr.sin_port = IPAddress.HostToNetworkOrder(port); // create an block of unmanaged memory for use by Marshal.StructureToPtr. We seem to need to do this, even though // StructureToPtr will go ahead and release/reallocate the memory ptrInAddr = Marshal.AllocCoTaskMem(Marshal.SizeOf(inAddr)); // Copy inAddr from managed to unmanaged Marshal.StructureToPtr(inAddr, ptrInAddr, false); // Create a managed byte array to hold the structure, but in byte array form byte[] byteInAddr = new byte[Marshal.SizeOf(inAddr)]; // Copy the structure from unmanaged ptr into managed byte array Marshal.Copy(ptrInAddr, byteInAddr, 0, byteInAddr.Length); // Create a second managed byte array to hold the output sockaddr_in structure byte[] byteOutAddr = new byte[Marshal.SizeOf(inAddr)]; // Make the call to IOControl, asking for the Interface we should use if we want to route a packet to inAddr sock.IOControl( SIO_ROUTING_INTERFACE_QUERY, byteInAddr, byteOutAddr ); // create the memory placeholder for our local interface // Copy the results from the byteOutAddr into an unmanaged pointer ptrOutAddr = Marshal.AllocCoTaskMem(Marshal.SizeOf(outAddr)); Marshal.Copy(byteOutAddr, 0, ptrOutAddr, byteOutAddr.Length); // Copy the data from the unmanaged pointer to the ourAddr structure Marshal.PtrToStructure(ptrOutAddr, outAddr); } catch (SocketException se) { // Perhaps there were no interfaces present, AKA No Network Adapters enabled/installed and connected to media (wired or wireless) EventLog.WriteEntry("RtpSession", se.ToString(), EventLogEntryType.Warning, 99); } finally { // Release the socket sock = null; Marshal.FreeCoTaskMem(ptrInAddr); Marshal.FreeCoTaskMem(ptrOutAddr); } // Return an IPAddress structure that is initialized with the value of the IP address contained in the outAddr structure if (outAddr != null) { externalInterface = new IPAddress(outAddr.sin_addr, outAddr.sin6_scope_id); return(new IPAddress(outAddr.sin_addr, outAddr.sin6_scope_id)); } else { return(null); } } return(null); }
private void IPEndPointToNative(IPEndPoint endPoint, out rtl.SOCKADDR_IN lIPv4, out sockaddr_in6 lIPv6, out void *ipPointer, out int ipSize) { switch (endPoint.AddressFamily) { case AddressFamily.InterNetworkV6: lIPv6.sin6_family = AddressFamily.InterNetworkV6; lIPv6.sin6_port = rtl.htons(endPoint.Port); lIPv6.sin6_scope_id = endPoint.Address.ScopeId; var lBytes = endPoint.Address.GetAddressBytes(); for (int i = 0; i < 16; i++) { lIPv6.sin6_addr.u.Byte[i] = lBytes[i]; } ipPointer = &lIPv6; ipSize = sizeof(sockaddr_in6); break; default: lIPv4.sin_family = AddressFamily.InterNetwork; lIPv4.sin_port = rtl.htons(endPoint.Port); lIPv4.sin_addr.S_un.S_addr = endPoint.Address.Address; ipSize = sizeof(rtl.SOCKADDR_IN); ipPointer = &lIPv4; break; } }
private void IPEndPointToNative(IPEndPoint endPoint, out rtl.SOCKADDR_IN lIPv4, out sockaddr_in6 lIPv6, out void *ipPointer, out int ipSize) #endif { switch (endPoint.AddressFamily) { case AddressFamily.InterNetworkV6: lIPv6.sin6_family = AddressFamily.InterNetworkV6; #if toffee lIPv6.sin6_port = htons(endPoint.Port); #else lIPv6.sin6_port = rtl.htons(endPoint.Port); #endif lIPv6.sin6_scope_id = endPoint.Address.ScopeId; var lBytes = endPoint.Address.GetAddressBytes(); for (int i = 0; i < 16; i++) #if toffee { lIPv6.sin6_addr.__u6_addr.__u6_addr8[i] = lBytes[i]; } #elif posix { lIPv6.sin6_addr.__in6_u.__u6_addr8[i] = lBytes[i]; } #else { lIPv6.sin6_addr.u.Byte[i] = lBytes[i]; } #endif ipPointer = &lIPv6; #if posix || toffee ipSize = sizeof(rtl.__struct_sockaddr_in6); #else ipSize = sizeof(sockaddr_in6); #endif break; default: lIPv4.sin_family = AddressFamily.InterNetwork; #if toffee lIPv4.sin_port = htons(endPoint.Port); #else lIPv4.sin_port = rtl.htons(endPoint.Port); #endif #if posix || toffee lIPv4.sin_addr.s_addr = endPoint.Address.Address; ipSize = sizeof(rtl.__struct_sockaddr_in); #else lIPv4.sin_addr.S_un.S_addr = endPoint.Address.Address; ipSize = sizeof(rtl.SOCKADDR_IN); #endif ipPointer = &lIPv4; break; } }
public static extern int connect(IntPtr s, sockaddr_in6 addr, int addrsize);
internal static extern int WSAAddressToString( [In] ref sockaddr_in6 socketAddress, [In] int AddressLength, [In] IntPtr ProtocolInfo, [Out] StringBuilder AddressString, [In, Out] ref int AddressStringLength);
internal static extern int WSAStringToAddress( [In] string addressString, [In] AddressFamily addressFamily, [In] IntPtr lpProtocolInfo, // always passing in a 0 [In, Out] ref sockaddr_in6 socketAddress, [In, Out] ref int socketAddressSize);