public static unsafe void SetAddressFamily(byte[] buffer, AddressFamily family)
{
- if ((int)(family) > (int)AddressFamily.Max)
+ if ((int)(family) > ushort.MaxValue)
{
- // For legacy values up to AddressFamily.Max, family maps directly to Winsock value.
+ // For legacy values family maps directly to Winsock value.
// Other values will need mapping if/when supported.
// Currently, that is Netlink, Packet and ControllerAreaNetwork, neither of them supported on Windows.
throw new PlatformNotSupportedException();
Irda = AddressFamily.Irda,
NetworkDesigners = AddressFamily.NetworkDesigners,
Max = 29, //AddressFamily.Max
- Netlink = AddressFamily.Netlink,
Packet = AddressFamily.Packet,
ControllerAreaNetwork = AddressFamily.ControllerAreaNetwork,
}
case AF_INET6:
*palAddressFamily = AddressFamily_AF_INET6;
return true;
-#ifdef AF_NETLINK
- case AF_NETLINK:
- *palAddressFamily = AddressFamily_AF_NETLINK;
- return true;
-#endif
#ifdef AF_PACKET
case AF_PACKET:
*palAddressFamily = AddressFamily_AF_PACKET;
*
* NOTE: these values are taken from System.Net.AddressFamily. If you add
* new entries, be sure that the values are chosen accordingly.
+ * Unix specific values have distinct offset to avoid conflict with
+ * Windows values.
*/
typedef enum
{
AddressFamily_AF_UNIX = 1, // System.Net.AddressFamily.Unix
AddressFamily_AF_INET = 2, // System.Net.AddressFamily.InterNetwork
AddressFamily_AF_INET6 = 23, // System.Net.AddressFamily.InterNetworkV6
- AddressFamily_AF_NETLINK = 30, // System.Net.AddressFamily.Netlink
- AddressFamily_AF_PACKET = 31, // System.Net.AddressFamily.Packet
- AddressFamily_AF_CAN = 32, // System.Net.AddressFamily.ControllerAreaNetwork
+ AddressFamily_AF_PACKET = 65536, // System.Net.AddressFamily.Packet
+ AddressFamily_AF_CAN = 65537, // System.Net.AddressFamily.ControllerAreaNetwork
} AddressFamily;
/*
Irda = 26,
NetworkDesigners = 28,
Max = 29,
- Netlink = 30,
- Packet = 31,
- ControllerAreaNetwork = 32,
+ Packet = 65536,
+ ControllerAreaNetwork = 65537,
}
public enum SocketError
{
Irda = 26, // IrDA
NetworkDesigners = 28, // Network Designers OSI & gateway enabled protocols
Max = 29, // Max
- Netlink = 30, // Netlink protocol
- Packet = 31, // Linux Packet
- ControllerAreaNetwork = 32, // Controller Area Network automotive bus protocol
+ // Unix specific values are past Uint16.MaxValue to avoid conflicts with Windows values.
+ // On Windows we pass values straight to OS and if we add new protocol supported by Windows,
+ // we should use actual OS value.
+ Packet = 65536, // Linux Packet
+ ControllerAreaNetwork = 65537, // Controller Area Network automotive bus protocol
}
}
}
[Theory]
- [InlineData((AddressFamily)12345, -1)]
- [InlineData((AddressFamily)12345, 16)]
+ [InlineData((AddressFamily)65539, -1)]
+ [InlineData((AddressFamily)65539, 16)]
+ [InlineData((AddressFamily)1_000_000, -1)]
public static void ToString_UnknownFamily_Throws(AddressFamily family, int size)
{
+ // Values above last known value should throw.
Assert.Throws<PlatformNotSupportedException>(() => size >= 0 ? new SocketAddress(family, size) : new SocketAddress(family));
}
[Theory]
+ [InlineData((AddressFamily)125)]
+ [InlineData((AddressFamily)65535)]
+ [PlatformSpecific(TestPlatforms.Windows)]
+ public static void ToString_LegacyUnknownFamily_Success(AddressFamily family)
+ {
+ // For legacy reasons, unknown values in ushort range don't throw on Windows.
+ var sa = new SocketAddress(family);
+ Assert.NotNull(sa.ToString());
+ }
+
+ [Theory]
[InlineData(AddressFamily.Packet)]
- [InlineData(AddressFamily.Netlink)]
[InlineData(AddressFamily.ControllerAreaNetwork)]
[PlatformSpecific(~TestPlatforms.Linux)]
public static void ToString_UnsupportedFamily_Throws(AddressFamily family)
{
foreach (AddressFamily family in Enum.GetValues(typeof(AddressFamily)))
{
- if ((int)family > (int)AddressFamily.Max)
+ if (family == AddressFamily.Packet || family == AddressFamily.ControllerAreaNetwork)
{
// Skip Linux specific protocols.
continue;
Irda = 26,
NetworkDesigners = 28,
Max = 29,
- Netlink = 30,
- Packet = 31,
- ControllerAreaNetwork = 32,
+ Packet = 65536,
+ ControllerAreaNetwork = 65537,
}
public enum ProtocolType
{
public partial class CreateSocket
{
[Theory]
- [InlineData(AddressFamily.Netlink)]
[InlineData(AddressFamily.Packet)]
[InlineData(AddressFamily.ControllerAreaNetwork)]
[PlatformSpecific(~TestPlatforms.Linux)]
}
[Theory]
- [InlineData(AddressFamily.Netlink)]
[InlineData(AddressFamily.Packet)]
[InlineData(AddressFamily.ControllerAreaNetwork)]
[PlatformSpecific(TestPlatforms.Linux)]