Dial with a specific address / interface ? Golang

huangapple go评论83阅读模式
英文:

Dial with a specific address / interface ? Golang

问题

我在我的计算机上有两个网络接口(eth0和eth1),我正在尝试使用特定的接口(eth1)拨号连接。根据Go是一种系统语言的说法,我假设可以通过当前的标准库实现,但实际上可以吗?

到目前为止,我已经通过名称[InterfaceByName][1](eth1)获取了接口,然后我遍历[Addrs][2]方法并提取了第一个地址[0],它似乎是eth1接口的源地址(例如xxx.xxx.xxx/24);另一个是IPv6地址。
我创建了一个新的[Dialer][3]并设置了[Dialer.LocalAddr][4]为提取的地址。然而,我遇到了这个错误mismatched local address type,似乎与[dial.go][5]中的dialSingle函数有关。

编辑
一些代码:

package main
import (
        "net"
        "log"
)

func main(){
        ief, err := net.InterfaceByName("eth1")
        if err !=nil{
                log.Fatal(err)
        }
        addrs, err := ief.Addrs()
        if err !=nil{
                log.Fatal(err)
        }
        d := net.Dialer{LocalAddr: addrs[0]}
        _, err = d.Dial("tcp", "google.com:80")
        if err != nil {
                log.Fatal(err)
        }
}

输出:
2014/12/10 17:11:48 dial tcp 216.58.208.32:80: mismatched local address type ip+net
[1]: http://golang.org/pkg/net/#InterfaceByName
[2]: http://golang.org/pkg/net/#Interface.Addrs
[3]: http://golang.org/pkg/net/#Dialer
[4]: http://golang.org/src/pkg/net/dial.go?s=1060:1271#L7
[5]: http://golang.org/src/pkg/net/dial.go

英文:

I have two network interfaces on my computer ( eth0 and eth1) and I'm trying to Dial a connection using a specific one (eth1). Given the statement that Go is a system language I assumed so but is it really possible with the current standard library?

So far I've got to get the interface by name [InterfaceByName][1] (eth1) then I range over the [Addrs][2] method and extracted the first address [0] which seems to be the source address of eth1 interface (e.g. xxx.xxx.xxx/24); the other one is the ipv6 address.
I've created a new [Dialer][3] and set [Dialer.LocalAddr][4] with the address extracted. However I get this error mismatched local address type wich seems related to dialSingle function from [dial.go][5]

Edit
Some code:

package main
import (
        "net"
        "log"
)

func main(){
        ief, err := net.InterfaceByName("eth1")
        if err !=nil{
                log.Fatal(err)
        }
        addrs, err := ief.Addrs()
        if err !=nil{
                log.Fatal(err)
        }
        d := net.Dialer{LocalAddr: addrs[0]}
        _, err = d.Dial("tcp", "google.com:80")
        if err != nil {
                log.Fatal(err)
        }
}

Output:
2014/12/10 17:11:48 dial tcp 216.58.208.32:80: mismatched local address type ip+net
[1]: http://golang.org/pkg/net/#InterfaceByName
[2]: http://golang.org/pkg/net/#Interface.Addrs
[3]: http://golang.org/pkg/net/#Dialer
[4]: http://golang.org/src/pkg/net/dial.go?s=1060:1271#L7
[5]: http://golang.org/src/pkg/net/dial.go

答案1

得分: 14

当你从一个接口中获取地址时,它的类型是*net.IPNet,包装在一个net.Addr接口中,其中包含一个地址和子网掩码,而不是地址和端口。你可以使用IP地址,但是在将其断言为*net.IPNet之后,你需要创建一个新的TCPAddr

ief, err := net.InterfaceByName("eth1")
if err != nil {
    log.Fatal(err)
}
addrs, err := ief.Addrs()
if err != nil {
    log.Fatal(err)
}

tcpAddr := &net.TCPAddr{
    IP: addrs[0].(*net.IPNet).IP,
}
英文:

When you pull the address from an interface, it's of type *net.IPnet wrapped in a net.Addr interface, which contains an address and netmask NOT an address and port. You can use the IP address, however, you have to create a new TCPAddr after asserting it as a *net.IPnet

    ief, err := net.InterfaceByName("eth1")
    if err !=nil{
            log.Fatal(err)
    }
    addrs, err := ief.Addrs()
    if err !=nil{
            log.Fatal(err)
    }

	tcpAddr := &net.TCPAddr{
		IP: addrs[0].(*net.IPNet).IP,
	}

huangapple
  • 本文由 发表于 2014年12月11日 05:03:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/27410764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定