// Handle getting ID from this peer and adding it into the map func (s *Swarm) handleNewConn(nconn net.Conn) { p := new(peer.Peer) conn := &Conn{ Peer: p, Addr: nil, Conn: nconn, } newConnChans(conn) sin, sout, err := ident.Handshake(s.local, p, conn.Incoming.MsgChan, conn.Outgoing.MsgChan) if err != nil { u.PErr("%v\n", err.Error()) conn.Close() return } // Get address to contact remote peer from addr := <-sin maddr, err := ma.NewMultiaddr(string(addr)) if err != nil { u.PErr("Got invalid address from peer.") s.Error(err) return } p.AddAddress(maddr) conn.secIn = sin conn.secOut = sout err = s.StartConn(conn) if err != nil { s.Error(err) } }
// ConnectNew is for connecting to a peer when you dont know their ID, // Should only be used when you are sure that you arent already connected to peer in question func (s *Swarm) ConnectNew(addr *ma.Multiaddr) (*peer.Peer, error) { if addr == nil { return nil, errors.New("nil Multiaddr passed to swarm.Connect()") } npeer := new(peer.Peer) npeer.AddAddress(addr) conn, err := Dial("tcp", npeer) if err != nil { return nil, err } err = s.handleDialedCon(conn) return npeer, err }
func makePeer(addr *ma.Multiaddr) *peer.Peer { p := new(peer.Peer) p.AddAddress(addr) sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512) if err != nil { panic(err) } p.PrivKey = sk p.PubKey = pk id, err := identify.IDFromPubKey(pk) if err != nil { panic(err) } p.ID = id return p }
func setupDHTS(n int, t *testing.T) ([]*ma.Multiaddr, []*peer.Peer, []*IpfsDHT) { var addrs []*ma.Multiaddr for i := 0; i < 4; i++ { a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 5000+i)) if err != nil { t.Fatal(err) } addrs = append(addrs, a) } var peers []*peer.Peer for i := 0; i < 4; i++ { p := new(peer.Peer) p.AddAddress(addrs[i]) sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512) if err != nil { panic(err) } p.PubKey = pk p.PrivKey = sk id, err := identify.IDFromPubKey(pk) if err != nil { panic(err) } p.ID = id peers = append(peers, p) } var dhts []*IpfsDHT for i := 0; i < 4; i++ { net := swarm.NewSwarm(peers[i]) err := net.Listen() if err != nil { t.Fatal(err) } d := NewDHT(peers[i], net, ds.NewMapDatastore()) dhts = append(dhts, d) d.Start() } return addrs, peers, dhts }