Add macOS IP_BOUND_IF/IPV6_BOUND_IF socket binding for interface-specific UDP listeners#2040
Conversation
d2731be to
d2867c6
Compare
|
Thanks for the PR. Can you check if the network index value can be read using |
|
I looked into using There are also a couple of practical issues with the .NET approach:
The P/Invoke is safe on non-macOS platforms — .NET resolves Given that both paths end up at the same |
|
Thanks for looking this up.
I tend to avoid P/Invoke if there is equivalent option available with dotnet. It helps in long term, like if something changes, the runtime will have it updated and the app keeps working normally without additional maintenance.
This wont come up in this case since the interface will have at least one IP address as the code is attempting to bind on one of them.
This is fine with me since the loop is not on the hot path and will happen only when the DNS server starts. The
I try to avoid P/Invoke for all platforms unless there is no other way to achieve it. Best to let the runtime do it so there is nothing to worry about it even a decade later.
I would really prefer to have it done with managed code. |
On macOS, the equivalent of Linux's SO_BINDTODEVICE for binding UDP sockets to a specific network interface is the IP_BOUND_IF (IPv4) and IPV6_BOUND_IF (IPv6) socket options. These take an interface index instead of an interface name. Changes: - Add macOS socket option constants: IPPROTO_IP, IP_BOUND_IF, IPPROTO_IPV6, IPV6_BOUND_IF - Add P/Invoke for if_nametoindex() to resolve interface names to system interface indices on macOS - Extend UDP and UDP-proxy listener binding to support macOS via RuntimeInformation.IsOSPlatform(OSPlatform.OSX) Ref: TechnitiumSoftware#2023
d2867c6 to
c472e05
Compare
No problem at all - done. |
|
Thanks for the quick update. |
On macOS, the equivalent of Linux's
SO_BINDTODEVICEfor binding UDPsockets to a specific network interface is the
IP_BOUND_IF(IPv4) andIPV6_BOUND_IF(IPv6) socket options. These take an interface indexinstead of an interface name.
Changes
IPPROTO_IP=0,IP_BOUND_IF=25,IPPROTO_IPV6=41,IPV6_BOUND_IF=125if_nametoindex(): resolves interface names (e.g.en0)to system interface indices on macOS
binding logic now supports macOS via
RuntimeInformation.IsOSPlatform(OSPlatform.OSX), using theappropriate protocol level (
IPPROTO_IP/IPPROTO_IPV6) based onAddressFamilyRef: #2023