golang tcp dial will complete 3-way handshake, so bindToDev() don't work. you can use syscall.Socket(), syscall.BindToDevice(fd, device) to implement. but it not join netPoll of golang , syscall.Read(fd) will create a system thread to read in blocking. i want to put fd to netPoll. try to use poll.FD:
type FD struct {
// Lock sysfd and serialize access to Read and Write methods.
fdmu fdMutex
// System file descriptor. Immutable until Close.
Sysfd int
...
}
but "internal/poll" is not-export.
golang provides (os.NewFile,net.FileConn) can make it.
file := os.NewFile(uintptr(fd), name)
conn, err := net.FileConn(file)
- ifconfig eth0 192.168.1.2/24 //here mask 255.255.255.0
- ifconfig eth1 192.168.1.3/16 //here mask 255.255.0.0
- ping 192.168.1.1, icmp packet will be send to eth0, because route mask longest match
- ping 192.168.1.1 -I eth1, will send to eth1
- ./tcpbinddev -addr 192.168.1.1:6666 -device eth1 //if tcp syn send out from eth1, it means bind device successfully