Merge branch 'ipv6' of https://github.com/stacktic/ftp into stacktic-ipv6

Conflicts:
	ftp.go
This commit is contained in:
Julien Laffaye 2015-08-18 18:52:29 +02:00
commit a6e47bb27d
2 changed files with 32 additions and 10 deletions

View File

@ -139,3 +139,23 @@ func TestMultiline(t *testing.T) {
c.Quit() c.Quit()
} }
// antioche.antioche.eu.org with IPv6
func TestConnIPv6(t *testing.T) {
c, err := Connect("[2001:660:3302:282a:204:75ff:fe9f:9e11]:21")
if err != nil {
t.Fatal(err)
}
err = c.Login("anonymous", "anonymous")
if err != nil {
t.Fatal(err)
}
_, err = c.List(".")
if err != nil {
t.Error(err)
}
c.Quit()
}

22
ftp.go
View File

@ -4,7 +4,6 @@ package ftp
import ( import (
"bufio" "bufio"
"errors" "errors"
"fmt"
"io" "io"
"net" "net"
"net/textproto" "net/textproto"
@ -67,10 +66,14 @@ func DialTimeout(addr string, timeout time.Duration) (*ServerConn, error) {
conn := textproto.NewConn(tconn) conn := textproto.NewConn(tconn)
a := strings.SplitN(addr, ":", 2) host, _, err := net.SplitHostPort(addr)
if err != nil {
conn.Close()
return nil, err
}
c := &ServerConn{ c := &ServerConn{
conn: conn, conn: conn,
host: a[0], host: host,
timeout: timeout, timeout: timeout,
features: make(map[string]string), features: make(map[string]string),
} }
@ -218,20 +221,19 @@ func (c *ServerConn) openDataConn() (net.Conn, error) {
// else -> PASV // else -> PASV
_, nat6Supported := c.features["nat6"] _, nat6Supported := c.features["nat6"]
_, epsvSupported := c.features["EPSV"] _, epsvSupported := c.features["EPSV"]
if nat6Supported || epsvSupported {
if !nat6Supported && !epsvSupported {
port, _ = c.pasv()
}
if port == 0 {
port, err = c.epsv() port, err = c.epsv()
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else {
port, err = c.pasv()
if err != nil {
return nil, err
}
} }
// Build the new net address string // Build the new net address string
addr := fmt.Sprintf("%s:%d", c.host, port) addr := net.JoinHostPort(c.host, strconv.Itoa(port))
conn, err := net.DialTimeout("tcp", addr, c.timeout) conn, err := net.DialTimeout("tcp", addr, c.timeout)
if err != nil { if err != nil {