[Feature]: Support for SO_BINDTODEVICE? #532
Labels
enhancement
New feature or request
pending development
Requested PR owner to improve code and waiting for the result
proposal accepted
Proposal has been accepted
proposal
Proposal for this repo
Milestone
Description of new feature
While listening for UDP packets, it is common to listen on "0.0.0.0:123" or just ":123", which allows for the receipt of both unicast and broadcast UDP packets, however there are times where an application might want to listen on just a single network interface. Obtaining that interface's IP and then listening on "1.2.3.4:123" will only receive unicast packets, and thus the preferred mechanism is to still listen on ":123" (for v4+v6) or "0.0.0.0:123" (for v4), but to use SO_BINDTODEVICE to bind the socket to a single interface.
Once bound, tools like
ss
show it as0.0.0.0%eth0:123
rather than just0.0.0.0:123
.Scenarios for new feature
On network appliances with multiple interfaces, those interfaces are often attached to different networks - such as a network gateway that is attached to a LAN network on eth0 and the WAN network on eth1. If that device wants to listen for unicast+broadcast UDP on just the LAN interface, there is currently no way to do this with gnet (at least not that I could find).
Breaking changes or not?
No
Code snippets (optional)
For darwin:
For windows, there is example calls at https://stackoverflow.com/a/73041705 , but they'd need to be converted to go.
Alternatives for new feature
Could have separate listeners for unicast and broadcast traffic, but it gets messy.
Could listen to everything and then filter in
OnTraffic
, but it is much more efficient to have the kernel just bind to a single interface.Additional context (optional)
WithMulticastInterfaceIndex()
does something similar for multicast traffic.The text was updated successfully, but these errors were encountered: