Reputation: 19
I've bound several vpn on the server which is ppp0 ~ ppp4.
Now comes the problem.I want to start 5 progresses each one use the difference network interface.
proc0 -> ppp0
proc1 -> ppp1
proc2 -> ppp2
proc3 -> ppp3
proc4 -> ppp4
I now how to do this with c language.Just use setsockopt with parameter SO_BINDTODEVICE.
But how to do this with net package in golang?
Upvotes: 0
Views: 3533
Reputation: 1
The dialer's LocalAddr doesn't work for ppp interfaces, we should set the SOL_SOCKET
flag to socket using setsockopt
, for go 1.11+ you can create a dialer like this
dialer := net.Dialer{
Control: func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
err := syscall.SetsockoptString(int(fd), syscall.SOL_SOCKET, 25, "ppp0")
if err != nil {
log.Printf("control: %s", err)
return
}
})
},
}
// use the dialer
Upvotes: 0
Reputation: 19418
You probably want net.Interfaces()
to get a slice of net.Interface()
representing the network interfaces on your system, or, as you know the interface names, you can use net.InterfaceByName()
to get a particular net.Interface
.
ppp0, err := net.InterfaceByName("ppp0")
You can then call Interface.Addrs()
on the returned net.Interface
and get the interface's IP address(es).
addrs, err := ppp0.Addrs()
You can then use the desired address in the rest of your code (probably using net.ListenIP
or net.DialIP
.
addr, err := net.ResolveIPAddr("ip4", addrs[0])
// check err
conn, err := net.ListenIP("ip4:pptp", addr)
// check err, then do stuff with conn
Regarding doing this for more than one interface concurrently, you can launch a goroutine for each listener / interface, and move the traffic over channels, but this really depends on what you want to do. (are you just logging the data, sending the data, modifying the data etc.)
Upvotes: 1