[tor-commits] [goptlib/master] Have DialOr take a string for USERADDR.

dcf at torproject.org dcf at torproject.org
Sun Dec 15 01:39:01 UTC 2013


commit b0a3458e272e8f54bb94d7ae346a3362ba018862
Author: David Fifield <david at bamsoftware.com>
Date:   Sat Dec 14 16:42:07 2013 -0800

    Have DialOr take a string for USERADDR.
    
    All we did was immediately convert the address to a string anyway. With
    an http.Request, for example, we have only RemoteAddr, which is a
    string. You would have to parse the address only for it to be turned
    into a string again.
---
 examples/dummy-server/dummy-server.go |    2 +-
 pt.go                                 |   10 +++++-----
 pt_test.go                            |   22 +++++++++++++---------
 3 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/examples/dummy-server/dummy-server.go b/examples/dummy-server/dummy-server.go
index c01876e..dd8dff7 100644
--- a/examples/dummy-server/dummy-server.go
+++ b/examples/dummy-server/dummy-server.go
@@ -52,7 +52,7 @@ func handler(conn net.Conn) error {
 		handlerChan <- -1
 	}()
 
-	or, err := pt.DialOr(&ptInfo, conn.RemoteAddr(), "dummy")
+	or, err := pt.DialOr(&ptInfo, conn.RemoteAddr().String(), "dummy")
 	if err != nil {
 		return err
 	}
diff --git a/pt.go b/pt.go
index 1e60d6c..1c3d77e 100644
--- a/pt.go
+++ b/pt.go
@@ -52,7 +52,7 @@
 // 	...
 // 	func handler(conn net.Conn) error {
 // 		defer conn.Close()
-// 		or, err := pt.DialOr(&ptInfo, conn.RemoteAddr(), "foo")
+// 		or, err := pt.DialOr(&ptInfo, conn.RemoteAddr().String(), "foo")
 // 		if err != nil {
 // 			return
 // 		}
@@ -696,8 +696,8 @@ func extOrPortSendCommand(s io.Writer, cmd uint16, body []byte) error {
 
 // Send a USERADDR command on s. See section 3.1.2.1 of
 // 196-transport-control-ports.txt.
-func extOrPortSendUserAddr(s io.Writer, addr net.Addr) error {
-	return extOrPortSendCommand(s, extOrCmdUserAddr, []byte(addr.String()))
+func extOrPortSendUserAddr(s io.Writer, addr string) error {
+	return extOrPortSendCommand(s, extOrCmdUserAddr, []byte(addr))
 }
 
 // Send a TRANSPORT command on s. See section 3.1.2.2 of
@@ -740,7 +740,7 @@ func extOrPortRecvCommand(s io.Reader) (cmd uint16, body []byte, err error) {
 // Send USERADDR and TRANSPORT commands followed by a DONE command. Wait for an
 // OKAY or DENY response command from the server. Returns nil if and only if
 // OKAY is received.
-func extOrPortSetup(s io.ReadWriter, addr net.Addr, methodName string) error {
+func extOrPortSetup(s io.ReadWriter, addr, methodName string) error {
 	var err error
 
 	err = extOrPortSendUserAddr(s, addr)
@@ -772,7 +772,7 @@ func extOrPortSetup(s io.ReadWriter, addr net.Addr, methodName string) error {
 // *net.TCPConn. If connecting to the extended OR port, extended OR port
 // authentication à la 217-ext-orport-auth.txt is done before returning; an
 // error is returned if authentication fails.
-func DialOr(info *ServerInfo, addr net.Addr, methodName string) (*net.TCPConn, error) {
+func DialOr(info *ServerInfo, addr, methodName string) (*net.TCPConn, error) {
 	if info.ExtendedOrAddr == nil || info.AuthCookie == nil {
 		return net.DialTCP("tcp", nil, info.OrAddr)
 	}
diff --git a/pt_test.go b/pt_test.go
index 856b23c..aa629c9 100644
--- a/pt_test.go
+++ b/pt_test.go
@@ -469,17 +469,17 @@ func TestExtOrSendCommand(t *testing.T) {
 }
 
 func TestExtOrSendUserAddr(t *testing.T) {
-	addrs := [...]net.TCPAddr{
-		net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 0},
-		net.TCPAddr{IP: net.ParseIP("1.2.3.4"), Port: 9999},
-		net.TCPAddr{IP: net.ParseIP("255.255.255.255"), Port: 65535},
-		net.TCPAddr{IP: net.ParseIP("::"), Port: 0},
-		net.TCPAddr{IP: net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"), Port: 65535},
+	addrs := [...]string{
+		"0.0.0.0:0",
+		"1.2.3.4:9999",
+		"255.255.255.255:65535",
+		"[::]:0",
+		"[ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]:63335",
 	}
 
 	for _, addr := range addrs {
 		var buf bytes.Buffer
-		err := extOrPortSendUserAddr(&buf, &addr)
+		err := extOrPortSendUserAddr(&buf, addr)
 		if err != nil {
 			t.Errorf("%s unexpectedly returned an error: %s", addr, err)
 		}
@@ -495,12 +495,16 @@ func TestExtOrSendUserAddr(t *testing.T) {
 			t.Errorf("%s said length %d but had at least %d", addr, length, n)
 		}
 		// test that parsing the address gives something equivalent to
-		// the original.
+		// parsing the original.
+		inputAddr, err := resolveAddr(addr)
+		if err != nil {
+			t.Fatal(err)
+		}
 		outputAddr, err := resolveAddr(string(p[:n]))
 		if err != nil {
 			t.Fatal(err)
 		}
-		if !tcpAddrsEqual(&addr, outputAddr) {
+		if !tcpAddrsEqual(inputAddr, outputAddr) {
 			t.Errorf("%s → %s", addr, outputAddr)
 		}
 	}





More information about the tor-commits mailing list