handshake.go 2.9 KB
Newer Older
1 2 3 4 5 6 7
package conn

import (
	"errors"
	"fmt"

	handshake "github.com/jbenet/go-ipfs/net/handshake"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8
	hspb "github.com/jbenet/go-ipfs/net/handshake/pb"
Jeromy's avatar
Jeromy committed
9
	u "github.com/jbenet/go-ipfs/util"
10 11 12

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
Jeromy's avatar
Jeromy committed
13
	ma "github.com/jbenet/go-multiaddr"
14 15
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
// Handshake1 exchanges local and remote versions and compares them
17
// closes remote and returns an error in case of major difference
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18
func Handshake1(ctx context.Context, c Conn) error {
19 20 21
	rpeer := c.RemotePeer()
	lpeer := c.LocalPeer()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22 23
	var remoteH, localH *hspb.Handshake1
	localH = handshake.Handshake1Msg()
24 25 26 27 28 29 30

	myVerBytes, err := proto.Marshal(localH)
	if err != nil {
		return err
	}

	c.Out() <- myVerBytes
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31
	log.Debugf("Sent my version (%s) to %s", localH, rpeer)
32 33 34 35 36

	select {
	case <-ctx.Done():
		return ctx.Err()

37
	case <-c.Closing():
38 39 40 41 42 43 44
		return errors.New("remote closed connection during version exchange")

	case data, ok := <-c.In():
		if !ok {
			return fmt.Errorf("error retrieving from conn: %v", rpeer)
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
		remoteH = new(hspb.Handshake1)
46 47 48 49 50
		err = proto.Unmarshal(data, remoteH)
		if err != nil {
			return fmt.Errorf("could not decode remote version: %q", err)
		}

Jeromy's avatar
Jeromy committed
51
		log.Debugf("Received remote version (%s) from %s", remoteH, rpeer)
52 53
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54
	if err := handshake.Handshake1Compatible(localH, remoteH); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55
		log.Infof("%s (%s) incompatible version with %s (%s)", lpeer, localH, rpeer, remoteH)
56 57 58
		return err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59
	log.Debugf("%s version handshake compatible %s", lpeer, rpeer)
60 61
	return nil
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62 63 64 65 66 67 68 69

// Handshake3 exchanges local and remote service information
func Handshake3(ctx context.Context, c Conn) error {
	rpeer := c.RemotePeer()
	lpeer := c.LocalPeer()

	var remoteH, localH *hspb.Handshake3
	localH = handshake.Handshake3Msg(lpeer)
Jeromy's avatar
Jeromy committed
70 71 72 73

	rma := c.RemoteMultiaddr()
	localH.ObservedAddr = proto.String(rma.String())

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74 75 76 77 78 79
	localB, err := proto.Marshal(localH)
	if err != nil {
		return err
	}

	c.Out() <- localB
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80
	log.Debugf("Handshake1: sent to %s", rpeer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

	select {
	case <-ctx.Done():
		return ctx.Err()

	case <-c.Closing():
		return errors.New("Handshake3: error remote connection closed")

	case remoteB, ok := <-c.In():
		if !ok {
			return fmt.Errorf("Handshake3 error receiving from conn: %v", rpeer)
		}

		remoteH = new(hspb.Handshake3)
		err = proto.Unmarshal(remoteB, remoteH)
		if err != nil {
			return fmt.Errorf("Handshake3 could not decode remote msg: %q", err)
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100
		log.Debugf("Handshake3 received from %s", rpeer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
101 102 103
	}

	if err := handshake.Handshake3UpdatePeer(rpeer, remoteH); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104
		log.Errorf("Handshake3 failed to update %s", rpeer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105 106 107 108 109
		return err
	}

	return nil
}
Jeromy's avatar
Jeromy committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

func CheckNAT(obsaddr string) (bool, error) {
	oma, err := ma.NewMultiaddr(obsaddr)
	if err != nil {
		return false, err
	}
	addrs, err := u.GetLocalAddresses()
	if err != nil {
		return false, err
	}
	_ = oma
	_ = addrs

	panic("not yet implemented!")
}