Пример #1
0
func (gw *Gateway) ListenAll(maddrs [][]byte) error {
	retErr := multierr.New()
	addErr := func(err error) {
		if retErr.Errors == nil {
			retErr.Errors = make([]error, 0)
		}
		retErr.Errors = append(retErr.Errors, err)
	}

	// listen on every address
	for _, bts := range maddrs {
		m, err := ma.NewMultiaddrBytes(bts)
		if err != nil {
			addErr(err)
			continue
		}

		err = gw.listen(m)
		if err != nil {
			addErr(err)
		}
	}

	if retErr.Errors != nil {
		return retErr
	}
	return nil
}
Пример #2
0
func (ids *IDService) consumeObservedAddress(observed []byte, c inet.Conn) {
	if observed == nil {
		return
	}

	maddr, err := ma.NewMultiaddrBytes(observed)
	if err != nil {
		log.Debugf("error parsing received observed addr for %s: %s", c, err)
		return
	}

	// we should only use ObservedAddr when our connection's LocalAddr is one
	// of our ListenAddrs. If we Dial out using an ephemeral addr, knowing that
	// address's external mapping is not very useful because the port will not be
	// the same as the listen addr.
	ifaceaddrs, err := ids.Host.Network().InterfaceListenAddresses()
	if err != nil {
		log.Infof("failed to get interface listen addrs", err)
		return
	}

	log.Debugf("identify identifying observed multiaddr: %s %s", c.LocalMultiaddr(), ifaceaddrs)
	if !addrInAddrs(c.LocalMultiaddr(), ifaceaddrs) {
		// not in our list
		return
	}

	// ok! we have the observed version of one of our ListenAddresses!
	log.Debugf("added own observed listen addr: %s --> %s", c.LocalMultiaddr(), maddr)
	ids.observedAddrs.Add(maddr, c.RemoteMultiaddr())
}
Пример #3
0
func (gw *Gateway) Dial(dest pnet.Peer, proto string) (io.ReadWriteCloser, error) {
	// See if we already have a connection with this peer.
	s, err := gw.conns.NewStreamWithGroup(dest.Id())
	if err != nil {
		// We don't, let's create a new connection.

		//TODO: Dial many/all addrs, use earliest. Like here:
		//https://github.com/ipfs/go-ipfs/blob/master/p2p/net/swarm/swarm_dial.go
		p, ok := dest.(*PeerInfo)
		if !ok {
			return nil, errors.New("Unknown pnet.Peer type")
		}

		m, err := ma.NewMultiaddrBytes(p.MAddrs[0])
		if err != nil {
			return nil, err
		}

		nc, err := manet.Dial(m)
		if err != nil {
			return nil, err
		}

		c, err := gw.conns.AddConn(nc)
		if err != nil {
			nc.Close()
			return nil, err
		}
		gw.conns.AddConnToGroup(c, p.Id())

		s, err = gw.conns.NewStreamWithGroup(p.Id())
		if err != nil {
			return nil, err
		}
	}

	// Select the protocol on the new stream.
	err = ms.SelectProtoOrFail(proto, s)
	if err != nil {
		s.Close()
	}

	return s, err
}
Пример #4
0
func (ids *IDService) consumeMessage(mes *pb.Identify, c inet.Conn) {
	p := c.RemotePeer()

	// mes.Protocols

	// mes.ObservedAddr
	ids.consumeObservedAddress(mes.GetObservedAddr(), c)

	// mes.ListenAddrs
	laddrs := mes.GetListenAddrs()
	lmaddrs := make([]ma.Multiaddr, 0, len(laddrs))
	for _, addr := range laddrs {
		maddr, err := ma.NewMultiaddrBytes(addr)
		if err != nil {
			log.Debugf("%s failed to parse multiaddr from %s %s", ID,
				p, c.RemoteMultiaddr())
			continue
		}
		lmaddrs = append(lmaddrs, maddr)
	}

	// update our peerstore with the addresses. here, we SET the addresses, clearing old ones.
	// We are receiving from the peer itself. this is current address ground truth.
	ids.Host.Peerstore().SetAddrs(p, lmaddrs, peer.ConnectedAddrTTL)
	log.Debugf("%s received listen addrs for %s: %s", c.LocalPeer(), c.RemotePeer(), lmaddrs)

	// get protocol versions
	pv := mes.GetProtocolVersion()
	av := mes.GetAgentVersion()

	// version check. if we shouldn't talk, bail.
	// TODO: at this point, we've already exchanged information.
	// move this into a first handshake before the connection can open streams.
	if !protocolVersionsAreCompatible(pv, LibP2PVersion) {
		logProtocolMismatchDisconnect(c, pv, av)
		c.Close()
		return
	}

	ids.Host.Peerstore().Put(p, "ProtocolVersion", pv)
	ids.Host.Peerstore().Put(p, "AgentVersion", av)
}