2023-04-08 19:20:14 +08:00
|
|
|
//go:build !linux && !darwin && !windows
|
2020-10-22 00:11:49 +08:00
|
|
|
|
|
|
|
package dialer
|
|
|
|
|
2021-09-06 23:07:34 +08:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func bindIfaceToDialer(ifaceName string, dialer *net.Dialer, network string, destination net.IP) error {
|
|
|
|
if !destination.IsGlobalUnicast() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:09:29 +08:00
|
|
|
local := uint64(0)
|
2021-09-06 23:07:34 +08:00
|
|
|
if dialer.LocalAddr != nil {
|
|
|
|
_, port, err := net.SplitHostPort(dialer.LocalAddr.String())
|
|
|
|
if err == nil {
|
2022-01-02 01:09:29 +08:00
|
|
|
local, _ = strconv.ParseUint(port, 10, 16)
|
2021-09-06 23:07:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 21:24:39 +08:00
|
|
|
addr, err := lookupLocalAddr(ifaceName, network, destination, int(local))
|
2021-09-06 23:07:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dialer.LocalAddr = addr
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func bindIfaceToListenConfig(ifaceName string, _ *net.ListenConfig, network, address string) (string, error) {
|
|
|
|
_, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
port = "0"
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:09:29 +08:00
|
|
|
local, _ := strconv.ParseUint(port, 10, 16)
|
2021-09-06 23:07:34 +08:00
|
|
|
|
2021-11-08 21:24:39 +08:00
|
|
|
addr, err := lookupLocalAddr(ifaceName, network, nil, int(local))
|
2021-09-06 23:07:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr.String(), nil
|
2020-10-22 00:11:49 +08:00
|
|
|
}
|