robert
robert

Reputation: 811

error when trying to assign local address value

I'm trying to set the local address of an http request like so:

localAddr, err := net.ResolveIPAddr("ip6", laddr)

if err != nil {
    log.Fatal(err)
}

localTCPAddr := net.TCPAddr{
    IP: localAddr.IP,
}

client := &http.Client{
    Transport: &http.Transport{
        Proxy: http.ProxyFromEnvironment,
        DialContext: (&net.Dialer{
            LocalAddr: &localTCPAddr,
            Timeout:   30 * time.Second,
            KeepAlive: 30 * time.Second,
            DualStack: true,
        }).DialContext,
        MaxIdleConns:          100,
        IdleConnTimeout:       90 * time.Second,
        TLSHandshakeTimeout:   10 * time.Second,
        ExpectContinueTimeout: 1 * time.Second,
    },
}

For reference: the laddr variable would look like this:

 2620:13a:c020:0016:1f7b:169c:846f:218a:49152

The error occurs when trying to send the request, and this is the error: 2018/11/25 00:17:58 lookup 2620:13a:c020:0016:1f7b:169c:846f:218a:49152: no such host.

I'm not sure whats causing this error. Some details about my environment: its a Ubuntu fresh VPS, with almost nothing installed.

EDIT: Even when removing the port as pointed out below I still get this error: dial tcp [2620:13a:c020:16:1f7b:169c:846f:218a]:0 ->[remoteip]:[remoteport] bind: cannot assign requested address

Upvotes: 1

Views: 381

Answers (2)

robert
robert

Reputation: 811

Error was on my end: I didn't assign the address to an interface before. Thanks for informing me about ports and all that, much appreciated.

Upvotes: 0

ssemilla
ssemilla

Reputation: 3970

This 2620:13a:c020:0016:1f7b:169c:846f:218a:49152 is not a valid IPv6 address. You have an extra 49152 which is not even a valid IPv6 hextet. This 2620:13a:c020:0016:1f7b:169c:846f:218a is a valid IPv6 address.

Validator

Edit

Address resolution is not the same as making a connection. If you want to make a connection rather than resolving the address, that is the time that you need the port.

e.g.

net.Dial("tcp", "[2620:13a:c020:16:1f7b:169c:846f:218a]:49152")

Upvotes: 3

Related Questions