// Handle getting ID from this peer, handshake, and adding it into the map func (s *Swarm) handleIncomingConn(nconn conn.Conn) { // this handler is a child. added by caller. defer s.Children().Done() // Setup the new connection _, err := s.connSetup(nconn) if err != nil && err != ErrAlreadyOpen { s.errChan <- err nconn.Close() } }
// connSetup takes a new connection, performs the IPFS handshake (handshake3) // and then adds it to the appropriate MultiConn. func (s *Swarm) connSetup(c conn.Conn) (conn.Conn, error) { if c == nil { return nil, errors.New("Tried to start nil connection.") } log.Event(context.TODO(), "connSetupBegin", c.LocalPeer(), c.RemotePeer()) // add address of connection to Peer. Maybe it should happen in connSecure. // NOT adding this address here, because the incoming address in TCP // is an EPHEMERAL address, and not the address we want to keep around. // addresses should be figured out through the DHT. // c.Remote.AddAddress(c.Conn.RemoteMultiaddr()) // handshake3 ctxT, _ := context.WithTimeout(c.Context(), conn.HandshakeTimeout) h3result, err := conn.Handshake3(ctxT, c) if err != nil { c.Close() return nil, fmt.Errorf("Handshake3 failed: %s", err) } // check for nats. you know, just in case. if h3result.LocalObservedAddress != nil { s.checkNATWarning(h3result.LocalObservedAddress) } else { log.Warningf("Received nil observed address from %s", c.RemotePeer()) } // add to conns mc, err := s.peerMultiConn(c.RemotePeer()) if err != nil { c.Close() return nil, err } mc.Add(c) log.Event(context.TODO(), "connSetupSuccess", c.LocalPeer(), c.RemotePeer()) return c, nil }