Multiple network interfaces support in go


Is it possible to send packets using “net” package onto a specific network interface?

I have created a test virtual PL having:
eth0 to the internet (IPv4 gw
eth1, eth2, eth3 all connected to their own LAN’s with same IP configuration (every is IPv4
Now I’d like to send a broadcast on either eth1, eth2 or eth3.
I can use net.DialUDP which broadcasts on eth0.
I can use net.DialUDB which broadcasts on eth1.
How can I broadcast on eth2 or eth3?

Example source:
package main
import "net"
func main(){
udpconn,_ := net.DialUDP(“udp4”,nil,&net.UDPAddr{IP:net.ParseIP(“”), Port:8888})

I can send ICMP like ping -i eth1 on a desired interface using command line tools.
I can listen for network packets using ex. command tcpdump -i eth1 port 8888.
I can modify IP address to choose between eth0 and eth1 but there is no way to send it on eth2 or eth3.
I digged down to “plan9” implementation of golang where I’m a bit lost.

I don*t know of an option for selecting a particular interface for sending data (including broadcasts).

Is there a specific reason for assigning the same IP address range to all three LAN’s?

And what is the purpose of restricting the broadcast to a specific network interface?

I am asking this because I feel there might be another problem behind the one you described, and there might be other options for resolving the problem.

You can use x/net/ipv4 to set low level options such as outgoing interface for multicast. I didn’t at a glance see if you can do the same for broadcasts.

Typically, using the interface broadcast address is enough to select the interface, but of course not in your case when you have multiple interfaces in the same subnet.


When sending broadcasts using golang you send them only on the first interface matching your IP.
Example setup: a laptop with internet connection over ethernet cable and wifi for sending broadcasts to your devices in LAN. Go defined broadcast IP as which would be sent to the first network (likely ethernet cable).
A workaround is to ignore go net.IP4bcast and use broadcast to the one that matches your card - it seems to work (assuming that your interface have different configuration). With DHCP you can never tell what configuration would be received.

Thanks - that should help :slight_smile:

TIL: Look into the packages more often. :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.