ping.go 4.11 KB
Newer Older
1
2
3
4
5
6
7
8
package main

import (
	"bufio"
	"context"
	"fmt"
	"log"

Aviv Eyal's avatar
Aviv Eyal committed
9
	uuid "github.com/google/uuid"
Steven Allen's avatar
Steven Allen committed
10
11
	"github.com/libp2p/go-libp2p-host"
	inet "github.com/libp2p/go-libp2p-net"
Steven Allen's avatar
Steven Allen committed
12
	p2p "github.com/libp2p/go-libp2p/examples/multipro/pb"
13
14
15
16
17
18
19
20
21
	protobufCodec "github.com/multiformats/go-multicodec/protobuf"
)

// pattern: /protocol-name/request-or-response-message/version
const pingRequest = "/ping/pingreq/0.0.1"
const pingResponse = "/ping/pingresp/0.0.1"

// PingProtocol type
type PingProtocol struct {
Aviv Eyal's avatar
Aviv Eyal committed
22
	node     *Node                       // local host
23
	requests map[string]*p2p.PingRequest // used to access request data from response handlers
Aviv Eyal's avatar
Aviv Eyal committed
24
	done     chan bool                   // only for demo purposes to stop main from terminating
25
26
}

Aviv Eyal's avatar
Aviv Eyal committed
27
28
29
30
func NewPingProtocol(node *Node, done chan bool) *PingProtocol {
	p := &PingProtocol{node: node, requests: make(map[string]*p2p.PingRequest), done: done}
	node.SetStreamHandler(pingRequest, p.onPingRequest)
	node.SetStreamHandler(pingResponse, p.onPingResponse)
Aviv Eyal's avatar
Aviv Eyal committed
31
	return p
32
33
34
}

// remote peer requests handler
35
func (p *PingProtocol) onPingRequest(s inet.Stream) {
36
37
38
39
40
41

	// get request data
	data := &p2p.PingRequest{}
	decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
	err := decoder.Decode(data)
	if err != nil {
Aviv Eyal's avatar
Aviv Eyal committed
42
		log.Println(err)
43
44
45
46
47
		return
	}

	log.Printf("%s: Received ping request from %s. Message: %s", s.Conn().LocalPeer(), s.Conn().RemotePeer(), data.Message)

48
49
50
	valid := p.node.authenticateMessage(data, data.MessageData)

	if !valid {
Aviv Eyal's avatar
Aviv Eyal committed
51
		log.Println("Failed to authenticate message")
52
53
54
		return
	}

Aviv Eyal's avatar
Aviv Eyal committed
55
	// generate response message
56
	log.Printf("%s: Sending ping response to %s. Message id: %s...", s.Conn().LocalPeer(), s.Conn().RemotePeer(), data.MessageData.Id)
57

Aviv Eyal's avatar
Aviv Eyal committed
58
	resp := &p2p.PingResponse{MessageData: p.node.NewMessageData(data.MessageData.Id, false),
Aviv Eyal's avatar
Aviv Eyal committed
59
		Message: fmt.Sprintf("Ping response from %s", p.node.ID())}
60

Aviv Eyal's avatar
Aviv Eyal committed
61
62
63
	// sign the data
	signature, err := p.node.signProtoMessage(resp)
	if err != nil {
Aviv Eyal's avatar
Aviv Eyal committed
64
		log.Println("failed to sign response")
Aviv Eyal's avatar
Aviv Eyal committed
65
66
67
68
69
70
71
		return
	}

	// add the signature to the message
	resp.MessageData.Sign = string(signature)

	// send the response
Aviv Eyal's avatar
Aviv Eyal committed
72
	s, respErr := p.node.NewStream(context.Background(), s.Conn().RemotePeer(), pingResponse)
73
	if respErr != nil {
Aviv Eyal's avatar
Aviv Eyal committed
74
		log.Println(respErr)
75
76
77
		return
	}

Aviv Eyal's avatar
Aviv Eyal committed
78
	ok := p.node.sendProtoMessage(resp, s)
79
80
81
82
83
84
85

	if ok {
		log.Printf("%s: Ping response to %s sent.", s.Conn().LocalPeer().String(), s.Conn().RemotePeer().String())
	}
}

// remote ping response handler
86
func (p *PingProtocol) onPingResponse(s inet.Stream) {
87
88
89
90
91
92
93
	data := &p2p.PingResponse{}
	decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
	err := decoder.Decode(data)
	if err != nil {
		return
	}

94
95
96
	valid := p.node.authenticateMessage(data, data.MessageData)

	if !valid {
Aviv Eyal's avatar
Aviv Eyal committed
97
		log.Println("Failed to authenticate message")
98
99
100
		return
	}

101
102
103
104
105
106
	// locate request data and remove it if found
	_, ok := p.requests[data.MessageData.Id]
	if ok {
		// remove request from map as we have processed it here
		delete(p.requests, data.MessageData.Id)
	} else {
Aviv Eyal's avatar
Aviv Eyal committed
107
		log.Println("Failed to locate request data boject for response")
108
109
110
111
112
113
114
		return
	}

	log.Printf("%s: Received ping response from %s. Message id:%s. Message: %s.", s.Conn().LocalPeer(), s.Conn().RemotePeer(), data.MessageData.Id, data.Message)
	p.done <- true
}

115
func (p *PingProtocol) Ping(host host.Host) bool {
Aviv Eyal's avatar
Aviv Eyal committed
116
	log.Printf("%s: Sending ping to: %s....", p.node.ID(), host.ID())
117
118

	// create message data
Aviv Eyal's avatar
Aviv Eyal committed
119
	req := &p2p.PingRequest{MessageData: p.node.NewMessageData(uuid.New().String(), false),
Aviv Eyal's avatar
Aviv Eyal committed
120
		Message: fmt.Sprintf("Ping from %s", p.node.ID())}
121

Aviv Eyal's avatar
Aviv Eyal committed
122
123
124
	// sign the data
	signature, err := p.node.signProtoMessage(req)
	if err != nil {
Aviv Eyal's avatar
Aviv Eyal committed
125
		log.Println("failed to sign pb data")
Aviv Eyal's avatar
Aviv Eyal committed
126
127
128
129
130
131
132
		return false
	}

	// add the signature to the message
	req.MessageData.Sign = string(signature)

	s, err := p.node.NewStream(context.Background(), host.ID(), pingRequest)
133
	if err != nil {
Aviv Eyal's avatar
Aviv Eyal committed
134
		log.Println(err)
135
136
137
		return false
	}

Aviv Eyal's avatar
Aviv Eyal committed
138
	ok := p.node.sendProtoMessage(req, s)
139
140
141
142
143

	if !ok {
		return false
	}

Aviv Eyal's avatar
Aviv Eyal committed
144
	// store ref request so response handler has access to it
145
	p.requests[req.MessageData.Id] = req
Aviv Eyal's avatar
Aviv Eyal committed
146
	log.Printf("%s: Ping to: %s was sent. Message Id: %s, Message: %s", p.node.ID(), host.ID(), req.MessageData.Id, req.Message)
147
148
	return true
}