aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/discover/udp.go
blob: 4ad5d9cc42612cb8c0ae9b151720e052f95f86db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package discover

import (
    "bytes"
    "crypto/ecdsa"
    "errors"
    "fmt"
    "net"
    "time"

    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/logger"
    "github.com/ethereum/go-ethereum/rlp"
)

var log = logger.NewLogger("P2P Discovery")

// Errors
var (
    errPacketTooSmall = errors.New("too small")
    errBadHash        = errors.New("bad hash")
    errExpired        = errors.New("expired")
    errTimeout        = errors.New("RPC timeout")
    errClosed         = errors.New("socket closed")
)

// Timeouts
const (
    respTimeout = 300 * time.Millisecond
    sendTimeout = 300 * time.Millisecond
    expiration  = 3 * time.Second

    refreshInterval = 1 * time.Hour
)

// RPC packet types
const (
    pingPacket = iota + 1 // zero is 'reserved'
    pongPacket
    findnodePacket
    neighborsPacket
)

// RPC request structures
type (
    ping struct {
        IP         string // our IP
        Port       uint16 // our port
        Expiration uint64
    }

    // reply to Ping
    pong struct {
        ReplyTok   []byte
        Expiration uint64
    }

    findnode struct {
        // Id to look up. The responding node will send back nodes
        // closest to the target.
        Target     NodeID
        Expiration uint64
    }

    // reply to findnode
    neighbors struct {
        Nodes      []*Node
        Expiration uint64
    }
)

type rpcNode struct {
    IP   string
    Port uint16
    ID   NodeID
}

// udp implements the RPC protocol.
type udp struct {
    conn       *net.UDPConn
    priv       *ecdsa.PrivateKey
    addpending chan *pending
    replies    chan reply
    closing    chan struct{}

    *Table
}

// pending represents a pending reply.
//
// some implementations of the protocol wish to send more than one
// reply packet to findnode. in general, any neighbors packet cannot
// be matched up with a specific findnode packet.
//
// our implementation handles this by storing a callback function for
// each pending reply. incoming packets from a node are dispatched
// to all the callback functions for that node.
type pending struct {
    // these fields must match in the reply.
    from  NodeID
    ptype byte

    // time when the request must complete
    deadline time.Time

    // callback is called when a matching reply arrives. if it returns
    // true, the callback is removed from the pending reply queue.
    // if it returns false, the reply is considered incomplete and
    // the callback will be invoked again for the next matching reply.
    callback func(resp interface{}) (done bool)

    // errc receives nil when the callback indicates completion or an
    // error if no further reply is received within the timeout.
    errc chan<- error
}

type reply struct {
    from  NodeID
    ptype byte
    data  interface{}
}

// ListenUDP returns a new table that listens for UDP packets on laddr.
func ListenUDP(priv *ecdsa.PrivateKey, laddr string) (*Table, error) {
    net, realaddr, err := listen(priv, laddr)
    if err != nil {
        return nil, err
    }
    net.Table = newTable(net, PubkeyID(&priv.PublicKey), realaddr)
    log.Debugf("Listening, %v\n", net.self)
    return net.Table, nil
}

func listen(priv *ecdsa.PrivateKey, laddr string) (*udp, *net.UDPAddr, error) {
    addr, err := net.ResolveUDPAddr("udp", laddr)
    if err != nil {
        return nil, nil, err
    }
    conn, err := net.ListenUDP("udp", addr)
    if err != nil {
        return nil, nil, err
    }
    realaddr := conn.LocalAddr().(*net.UDPAddr)

    udp := &udp{
        conn:       conn,
        priv:       priv,
        closing:    make(chan struct{}),
        addpending: make(chan *pending),
        replies:    make(chan reply),
    }
    go udp.loop()
    go udp.readLoop()
    return udp, realaddr, nil
}

func (t *udp) close() {
    close(t.closing)
    t.conn.Close()
    // TODO: wait for the loops to end.
}

// ping sends a ping message to the given node and waits for a reply.
func (t *udp) ping(e *Node) error {
    // TODO: maybe check for ReplyTo field in callback to measure RTT
    errc := t.pending(e.ID, pongPacket, func(interface{}) bool { return true })
    t.send(e, pingPacket, ping{
        IP:         t.self.IP.String(),
        Port:       uint16(t.self.TCPPort),
        Expiration: uint64(time.Now().Add(expiration).Unix()),
    })
    return <-errc
}

// findnode sends a findnode request to the given node and waits until
// the node has sent up to k neighbors.
func (t *udp) findnode(to *Node, target NodeID) ([]*Node, error) {
    nodes := make([]*Node, 0, bucketSize)
    nreceived := 0
    errc := t.pending(to.ID, neighborsPacket, func(r interface{}) bool {
        reply := r.(*neighbors)
        for _, n := range reply.Nodes {
            nreceived++
            if n.isValid() {
                nodes = append(nodes, n)
            }
        }
        return nreceived == bucketSize
    })

    t.send(to, findnodePacket, findnode{
        Target:     target,
        Expiration: uint64(time.Now().Add(expiration).Unix()),
    })
    err := <-errc
    return nodes, err
}

// pending adds a reply callback to the pending reply queue.
// see the documentation of type pending for a detailed explanation.
func (t *udp) pending(id NodeID, ptype byte, callback func(interface{}) bool) <-chan error {
    ch := make(chan error, 1)
    p := &pending{from: id, ptype: ptype, callback: callback, errc: ch}
    select {
    case t.addpending <- p:
        // loop will handle it
    case <-t.closing:
        ch <- errClosed
    }
    return ch
}

// loop runs in its own goroutin. it keeps track of
// the refresh timer and the pending reply queue.
func (t *udp) loop() {
    var (
        pending      []*pending
        nextDeadline time.Time
        timeout      = time.NewTimer(0)
        refresh      = time.NewTicker(refreshInterval)
    )
    <-timeout.C // ignore first timeout
    defer refresh.Stop()
    defer timeout.Stop()

    rearmTimeout := func() {
        if len(pending) == 0 || nextDeadline == pending[0].deadline {
            return
        }
        nextDeadline = pending[0].deadline
        timeout.Reset(nextDeadline.Sub(time.Now()))
    }

    for {
        select {
        case <-refresh.C:
            go t.refresh()

        case <-t.closing:
            for _, p := range pending {
                p.errc <- errClosed
            }
            return

        case p := <-t.addpending:
            p.deadline = time.Now().Add(respTimeout)
            pending = append(pending, p)
            rearmTimeout()

        case reply := <-t.replies:
            // run matching callbacks, remove if they return false.
            for i, p := range pending {
                if reply.from == p.from && reply.ptype == p.ptype && p.callback(reply.data) {
                    p.errc <- nil
                    copy(pending[i:], pending[i+1:])
                    pending = pending[:len(pending)-1]
                    i--
                }
            }
            rearmTimeout()

        case now := <-timeout.C:
            // notify and remove callbacks whose deadline is in the past.
            i := 0
            for ; i < len(pending) && now.After(pending[i].deadline); i++ {
                pending[i].errc <- errTimeout
            }
            if i > 0 {
                copy(pending, pending[i:])
                pending = pending[:len(pending)-i]
            }
            rearmTimeout()
        }
    }
}

const (
    macSize  = 256 / 8
    sigSize  = 520 / 8
    headSize = macSize + sigSize // space of packet frame data
)

var headSpace = make([]byte, headSize)

func (t *udp) send(to *Node, ptype byte, req interface{}) error {
    b := new(bytes.Buffer)
    b.Write(headSpace)
    b.WriteByte(ptype)
    if err := rlp.Encode(b, req); err != nil {
        log.Errorln("error encoding packet:", err)
        return err
    }

    packet := b.Bytes()
    sig, err := crypto.Sign(crypto.Sha3(packet[headSize:]), t.priv)
    if err != nil {
        log.Errorln("could not sign packet:", err)
        return err
    }
    copy(packet[macSize:], sig)
    // add the hash to the front. Note: this doesn't protect the
    // packet in any way. Our public key will be part of this hash in
    // the future.
    copy(packet, crypto.Sha3(packet[macSize:]))

    toaddr := &net.UDPAddr{IP: to.IP, Port: to.DiscPort}
    log.DebugDetailf(">>> %v %T %v\n", toaddr, req, req)
    if _, err = t.conn.WriteToUDP(packet, toaddr); err != nil {
        log.DebugDetailln("UDP send failed:", err)
    }
    return err
}

// readLoop runs in its own goroutine. it handles incoming UDP packets.
func (t *udp) readLoop() {
    defer t.conn.Close()
    buf := make([]byte, 4096) // TODO: good buffer size
    for {
        nbytes, from, err := t.conn.ReadFromUDP(buf)
        if err != nil {
            return
        }
        if err := t.packetIn(from, buf[:nbytes]); err != nil {
            log.Debugf("Bad packet from %v: %v\n", from, err)
        }
    }
}

func (t *udp) packetIn(from *net.UDPAddr, buf []byte) error {
    if len(buf) < headSize+1 {
        return errPacketTooSmall
    }
    hash, sig, sigdata := buf[:macSize], buf[macSize:headSize], buf[headSize:]
    shouldhash := crypto.Sha3(buf[macSize:])
    if !bytes.Equal(hash, shouldhash) {
        return errBadHash
    }
    fromID, err := recoverNodeID(crypto.Sha3(buf[headSize:]), sig)
    if err != nil {
        return err
    }

    var req interface {
        handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error
    }
    switch ptype := sigdata[0]; ptype {
    case pingPacket:
        req = new(ping)
    case pongPacket:
        req = new(pong)
    case findnodePacket:
        req = new(findnode)
    case neighborsPacket:
        req = new(neighbors)
    default:
        return fmt.Errorf("unknown type: %d", ptype)
    }
    if err := rlp.Decode(bytes.NewReader(sigdata[1:]), req); err != nil {
        return err
    }
    log.DebugDetailf("<<< %v %T %v\n", from, req, req)
    return req.handle(t, from, fromID, hash)
}

func (req *ping) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
    if expired(req.Expiration) {
        return errExpired
    }
    t.mutex.Lock()
    // Note: we're ignoring the provided IP address right now
    n := t.bumpOrAdd(fromID, from)
    if req.Port != 0 {
        n.TCPPort = int(req.Port)
    }
    t.mutex.Unlock()

    t.send(n, pongPacket, pong{
        ReplyTok:   mac,
        Expiration: uint64(time.Now().Add(expiration).Unix()),
    })
    return nil
}

func (req *pong) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
    if expired(req.Expiration) {
        return errExpired
    }
    t.mutex.Lock()
    t.bump(fromID)
    t.mutex.Unlock()

    t.replies <- reply{fromID, pongPacket, req}
    return nil
}

func (req *findnode) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
    if expired(req.Expiration) {
        return errExpired
    }
    t.mutex.Lock()
    e := t.bumpOrAdd(fromID, from)
    closest := t.closest(req.Target, bucketSize).entries
    t.mutex.Unlock()

    t.send(e, neighborsPacket, neighbors{
        Nodes:      closest,
        Expiration: uint64(time.Now().Add(expiration).Unix()),
    })
    return nil
}

func (req *neighbors) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
    if expired(req.Expiration) {
        return errExpired
    }
    t.mutex.Lock()
    t.bump(fromID)
    t.add(req.Nodes)
    t.mutex.Unlock()

    t.replies <- reply{fromID, neighborsPacket, req}
    return nil
}

func expired(ts uint64) bool {
    return time.Unix(int64(ts), 0).Before(time.Now())
}