Multiple network interfaces support in go

Hi,

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 192.168.0.2/255.255.0.0 gw 192.168.0.1)
eth1, eth2, eth3 all connected to their own LAN’s with same IP configuration (every is IPv4 10.0.0.1/255.0.0.0)
Now I’d like to send a broadcast on either eth1, eth2 or eth3.
I can use net.DialUDP 255.255.255.255 which broadcasts on eth0.
I can use net.DialUDB 10.255.255.255 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(“10.255.255.255”), Port:8888})
udpconn.Write([]byte(“HelloWorld”))
}

I can send ICMP like ping 10.0.0.1 -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.

2 Likes

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 255.255.255.255 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 golang.org/x/ packages more often. :slight_smile:

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