@cryptotaxi247 / kubo / commits / 48879dd56

conn: added (*dialer).DialAddr

Juan Batiz-Benet committed Nov 20, 2014 at 09:12 UTC 48879dd56c00a9656400330fb0fd84d5e94ce754
2 files changed +99 -6
net/conn/dial.go
+29 -6
@@ -6,6 +6,7 @@ import (
6
7 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
8
9 + ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
10 manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
11
12 peer "github.com/jbenet/go-ipfs/peer"
@@ -14,22 +15,44 @@ import (
15 // Dial connects to a particular peer, over a given network
16 // Example: d.Dial(ctx, "udp", peer)
17 func (d *Dialer) Dial(ctx context.Context, network string, remote peer.Peer) (Conn, error) {
17 - laddr := d.LocalPeer.NetAddress(network)
18 - if laddr == nil {
19 - return nil, fmt.Errorf("No local address for network %s", network)
20 - }
21 -
18 raddr := remote.NetAddress(network)
19 if raddr == nil {
20 return nil, fmt.Errorf("No remote address for network %s", network)
21 }
22 + return d.DialAddr(ctx, raddr, remote)
23 +}
24 +
25 +// DialAddr connects to a peer over a particular address
26 +// Ensures raddr is part of peer.Addresses()
27 +// Example: d.DialAddr(ctx, peer.Addresses()[0], peer)
28 +func (d *Dialer) DialAddr(ctx context.Context, raddr ma.Multiaddr, remote peer.Peer) (Conn, error) {
29 +
30 + found := false
31 + for _, addr := range remote.Addresses() {
32 + if addr.Equal(raddr) {
33 + found = true
34 + }
35 + }
36 + if !found {
37 + return nil, fmt.Errorf("address %s is not in peer %s", raddr, remote)
38 + }
39 +
40 + network, _, err := manet.DialArgs(raddr)
41 + if err != nil {
42 + return nil, err
43 + }
44 +
45 + laddr := d.LocalPeer.NetAddress(network)
46 + if laddr == nil {
47 + return nil, fmt.Errorf("No local address for network %s", network)
48 + }
49
50 if strings.HasPrefix(raddr.String(), "/ip4/0.0.0.0") {
51 return nil, fmt.Errorf("Attempted to connect to zero address: %s", raddr)
52 }
53
54 remote.SetType(peer.Remote)
32 - remote, err := d.Peerstore.Add(remote)
55 + remote, err = d.Peerstore.Add(remote)
56 if err != nil {
57 log.Errorf("Error putting peer into peerstore: %s", remote)
58 }
net/conn/dial_test.go
+70
@@ -157,3 +157,73 @@ func TestDialer(t *testing.T) {
157 l.Close()
158 cancel()
159 }
160 +
161 +func TestDialAddr(t *testing.T) {
162 + // t.Skip("Skipping in favor of another test")
163 +
164 + p1, err := setupPeer("/ip4/127.0.0.1/tcp/4334")
165 + if err != nil {
166 + t.Fatal("error setting up peer", err)
167 + }
168 +
169 + p2, err := setupPeer("/ip4/127.0.0.1/tcp/4335")
170 + if err != nil {
171 + t.Fatal("error setting up peer", err)
172 + }
173 +
174 + ctx, cancel := context.WithCancel(context.Background())
175 +
176 + laddr := p1.NetAddress("tcp")
177 + if laddr == nil {
178 + t.Fatal("Listen address is nil.")
179 + }
180 +
181 + ps1 := peer.NewPeerstore()
182 + ps2 := peer.NewPeerstore()
183 + ps1.Add(p1)
184 + ps2.Add(p2)
185 +
186 + l, err := Listen(ctx, laddr, p1, ps1)
187 + if err != nil {
188 + t.Fatal(err)
189 + }
190 +
191 + go echoListen(ctx, l)
192 +
193 + d := &Dialer{
194 + Peerstore: ps2,
195 + LocalPeer: p2,
196 + }
197 +
198 + raddr := p1.NetAddress("tcp")
199 + if raddr == nil {
200 + t.Fatal("Dial address is nil.")
201 + }
202 +
203 + c, err := d.DialAddr(ctx, raddr, p1)
204 + if err != nil {
205 + t.Fatal("error dialing peer", err)
206 + }
207 +
208 + // fmt.Println("sending")
209 + c.Out() <- []byte("beep")
210 + c.Out() <- []byte("boop")
211 +
212 + out := <-c.In()
213 + // fmt.Println("recving", string(out))
214 + data := string(out)
215 + if data != "beep" {
216 + t.Error("unexpected conn output", data)
217 + }
218 +
219 + out = <-c.In()
220 + data = string(out)
221 + if string(out) != "boop" {
222 + t.Error("unexpected conn output", data)
223 + }
224 +
225 + // fmt.Println("closing")
226 + c.Close()
227 + l.Close()
228 + cancel()
229 +}