forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gossip.c
1441 lines (1201 loc) · 38.1 KB
/
gossip.c
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ccan/build_assert/build_assert.h>
#include <ccan/container_of/container_of.h>
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
#include <ccan/endian/endian.h>
#include <ccan/fdpass/fdpass.h>
#include <ccan/io/fdpass/fdpass.h>
#include <ccan/io/io.h>
#include <ccan/list/list.h>
#include <ccan/mem/mem.h>
#include <ccan/noerr/noerr.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/take/take.h>
#include <ccan/tal/str/str.h>
#include <ccan/timer/timer.h>
#include <common/cryptomsg.h>
#include <common/daemon_conn.h>
#include <common/debug.h>
#include <common/io_debug.h>
#include <common/ping.h>
#include <common/status.h>
#include <common/timeout.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <common/version.h>
#include <common/wire_error.h>
#include <common/wireaddr.h>
#include <errno.h>
#include <fcntl.h>
#include <gossipd/broadcast.h>
#include <gossipd/gen_gossip_wire.h>
#include <gossipd/handshake.h>
#include <gossipd/routing.h>
#include <hsmd/client.h>
#include <inttypes.h>
#include <lightningd/gossip_msg.h>
#include <netdb.h>
#include <netinet/in.h>
#include <secp256k1_ecdh.h>
#include <sodium/randombytes.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <wire/gen_peer_wire.h>
#include <wire/wire_io.h>
#define HSM_FD 3
struct daemon {
/* Who am I? */
struct pubkey id;
/* Peers we have directly or indirectly */
struct list_head peers;
/* Peers we are trying to reach */
struct list_head reaching;
/* Connection to main daemon. */
struct daemon_conn master;
/* Routing information */
struct routing_state *rstate;
/* Hacky list of known address hints. */
struct list_head addrhints;
struct timers timers;
u32 broadcast_interval;
/* Local and global features to offer to peers. */
u8 *localfeatures, *globalfeatures;
};
/* Peers we're trying to reach. */
struct reaching {
struct daemon *daemon;
/* daemon->reaching */
struct list_node list;
/* The ID of the peer (not necessarily unique, in transit!) */
struct pubkey id;
/* Where I'm reaching to. */
struct wireaddr addr;
/* Did we succeed? */
bool succeeded;
};
struct peer {
struct daemon *daemon;
/* daemon->peers */
struct list_node list;
/* The ID of the peer (not necessarily unique, in transit!) */
struct pubkey id;
/* Where it's connected to. */
struct wireaddr addr;
/* Feature bitmaps. */
u8 *gfeatures, *lfeatures;
/* Cryptostate */
struct peer_crypto_state pcs;
/* File descriptor corresponding to conn. */
int fd;
/* Our connection (and owner) */
struct io_conn *conn;
/* High water mark for the staggered broadcast */
u64 broadcast_index;
/* Message queue for outgoing. */
struct msg_queue peer_out;
/* Is it time to continue the staggered broadcast? */
bool gossip_sync;
/* The peer owner will use this to talk to gossipd */
struct daemon_conn owner_conn;
/* How many pongs are we expecting? */
size_t num_pings_outstanding;
/* Are we the owner of the peer? */
bool local;
/* If we die, should we reach again? */
bool reach_again;
/* Waiting to send_peer_with_fds to master? */
bool return_to_master;
/* If we're exiting due to non-gossip msg, otherwise release */
u8 *nongossip_msg;
};
struct addrhint {
/* Off ld->addrhints */
struct list_node list;
struct pubkey id;
/* FIXME: use array... */
struct wireaddr addr;
};
/* FIXME: Reorder */
static struct io_plan *peer_start_gossip(struct io_conn *conn,
struct peer *peer);
static bool send_peer_with_fds(struct peer *peer, const u8 *msg);
static void wake_pkt_out(struct peer *peer);
static void try_reach_peer(struct daemon *daemon, const struct pubkey *id);
static void destroy_peer(struct peer *peer)
{
list_del_from(&peer->daemon->peers, &peer->list);
if (peer->reach_again)
try_reach_peer(peer->daemon, &peer->id);
}
static struct peer *find_peer(struct daemon *daemon, const struct pubkey *id)
{
struct peer *peer;
list_for_each(&daemon->peers, peer, list)
if (pubkey_eq(&peer->id, id))
return peer;
return NULL;
}
static void destroy_addrhint(struct addrhint *a)
{
list_del(&a->list);
}
static struct addrhint *find_addrhint(struct daemon *daemon,
const struct pubkey *id)
{
struct addrhint *a;
list_for_each(&daemon->addrhints, a, list) {
if (pubkey_eq(&a->id, id))
return a;
}
return NULL;
}
static struct peer *new_peer(const tal_t *ctx,
struct daemon *daemon,
const struct pubkey *their_id,
const struct wireaddr *addr,
const struct crypto_state *cs)
{
struct peer *peer = tal(ctx, struct peer);
init_peer_crypto_state(peer, &peer->pcs);
peer->pcs.cs = *cs;
peer->id = *their_id;
peer->addr = *addr;
peer->daemon = daemon;
peer->local = true;
peer->reach_again = false;
peer->return_to_master = false;
peer->num_pings_outstanding = 0;
peer->broadcast_index = 0;
msg_queue_init(&peer->peer_out, peer);
return peer;
}
static void peer_finalized(struct peer *peer)
{
/* No longer tied to peer->conn's lifetime. */
tal_steal(peer->daemon, peer);
/* Now we can put this in the list of peers */
list_add_tail(&peer->daemon->peers, &peer->list);
tal_add_destructor(peer, destroy_peer);
}
static void destroy_reaching(struct reaching *reach)
{
list_del_from(&reach->daemon->reaching, &reach->list);
}
static struct reaching *find_reaching(struct daemon *daemon,
const struct pubkey *id)
{
struct reaching *r;
list_for_each(&daemon->reaching, r, list)
if (pubkey_eq(id, &r->id))
return r;
return NULL;
}
static void reached_peer(struct daemon *daemon, const struct pubkey *id,
struct io_conn *conn)
{
struct reaching *r = find_reaching(daemon, id);
if (!r)
return;
/* OK, we've reached the peer successfully, stop retrying. */
/* Don't free conn with reach. */
tal_steal(daemon, conn);
/* Don't call connect_failed */
io_set_finish(conn, NULL, NULL);
tal_free(r);
}
static void peer_error(struct peer *peer, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
status_trace("peer %s: %s",
type_to_string(trc, struct pubkey, &peer->id),
tal_vfmt(trc, fmt, ap));
va_end(ap);
/* Send error: we'll close after writing this. */
va_start(ap, fmt);
msg_enqueue(&peer->peer_out,
take(towire_errorfmtv(peer, NULL, fmt, ap)));
va_end(ap);
}
static bool is_all_channel_error(const u8 *msg)
{
struct channel_id channel_id;
u8 *data;
if (!fromwire_error(msg, msg, NULL, &channel_id, &data))
return false;
tal_free(data);
return channel_id_is_all(&channel_id);
}
static struct io_plan *peer_close_after_error(struct io_conn *conn,
struct peer *peer)
{
status_trace("%s: we sent them a fatal error, closing",
type_to_string(trc, struct pubkey, &peer->id));
return io_close(conn);
}
static struct io_plan *peer_init_received(struct io_conn *conn,
struct peer *peer,
u8 *msg)
{
if (!fromwire_init(peer, msg, NULL, &peer->gfeatures, &peer->lfeatures)){
status_trace("peer %s bad fromwire_init '%s', closing",
type_to_string(trc, struct pubkey, &peer->id),
tal_hex(trc, msg));
return io_close(conn);
}
reached_peer(peer->daemon, &peer->id, conn);
/* This is a full peer now; we keep it around until its
* gossipfd closed (forget_peer) or reconnect. */
peer_finalized(peer);
/* We will not have anything queued, since we're not duplex. */
msg = towire_gossip_peer_connected(peer, &peer->id, &peer->addr,
&peer->pcs.cs,
peer->gfeatures, peer->lfeatures);
if (!send_peer_with_fds(peer, msg))
return io_close(conn);
/* Start the gossip flowing. */
/* FIXME: This is a bit wasteful in the common case where master
* simply hands it straight back to us and we restart the peer and
* restart gossip broadcast... */
wake_pkt_out(peer);
return io_close_taken_fd(conn);
}
static struct io_plan *read_init(struct io_conn *conn, struct peer *peer)
{
/* BOLT #1:
*
* Each node MUST wait to receive `init` before sending any other
* messages.
*/
return peer_read_message(conn, &peer->pcs, peer_init_received);
}
/* This creates a temporary peer which is not in the list and is owner
* by the connection; it's placed in the list and owned by daemon once
* we have the features. */
static struct io_plan *init_new_peer(struct io_conn *conn,
const struct pubkey *their_id,
const struct wireaddr *addr,
const struct crypto_state *cs,
struct daemon *daemon)
{
struct peer *peer = new_peer(conn, daemon, their_id, addr, cs);
u8 *initmsg;
peer->fd = io_conn_fd(conn);
/* BOLT #1:
*
* Each node MUST send `init` as the first lightning message for any
* connection.
*/
initmsg = towire_init(peer,
daemon->globalfeatures, daemon->localfeatures);
return peer_write_message(conn, &peer->pcs, take(initmsg), read_init);
}
static struct io_plan *owner_msg_in(struct io_conn *conn,
struct daemon_conn *dc);
static struct io_plan *nonlocal_dump_gossip(struct io_conn *conn,
struct daemon_conn *dc);
static void handle_gossip_msg(struct routing_state *rstate, u8 *msg)
{
int t = fromwire_peektype(msg);
switch(t) {
case WIRE_CHANNEL_ANNOUNCEMENT:
handle_channel_announcement(rstate, msg, tal_count(msg));
break;
case WIRE_NODE_ANNOUNCEMENT:
handle_node_announcement(rstate, msg, tal_count(msg));
break;
case WIRE_CHANNEL_UPDATE:
handle_channel_update(rstate, msg, tal_count(msg));
break;
}
}
static void handle_ping(struct peer *peer, u8 *ping)
{
u8 *pong;
if (!check_ping_make_pong(peer, ping, &pong)) {
peer_error(peer, "Bad ping");
return;
}
if (pong)
msg_enqueue(&peer->peer_out, take(pong));
}
static void handle_pong(struct peer *peer, const u8 *pong)
{
u8 *ignored;
status_trace("Got pong!");
if (!fromwire_pong(pong, pong, NULL, &ignored)) {
peer_error(peer, "Bad pong");
return;
}
if (!peer->num_pings_outstanding) {
peer_error(peer, "Unexpected pong");
return;
}
peer->num_pings_outstanding--;
daemon_conn_send(&peer->daemon->master,
take(towire_gossip_ping_reply(pong, true,
tal_len(pong))));
}
/* If master asks us to release peer, we attach this destructor in case it
* dies while we're waiting for it to finish IO */
static void fail_release(struct peer *peer)
{
u8 *msg = towire_gossipctl_release_peer_replyfail(peer);
daemon_conn_send(&peer->daemon->master, take(msg));
}
static struct io_plan *ready_for_master(struct io_conn *conn, struct peer *peer)
{
u8 *msg;
if (peer->nongossip_msg)
msg = towire_gossip_peer_nongossip(peer, &peer->id,
&peer->addr,
&peer->pcs.cs,
peer->gfeatures,
peer->lfeatures,
peer->nongossip_msg);
else
msg = towire_gossipctl_release_peer_reply(peer,
&peer->addr,
&peer->pcs.cs,
peer->gfeatures,
peer->lfeatures);
if (send_peer_with_fds(peer, take(msg))) {
/* In case we set this earlier. */
tal_del_destructor(peer, fail_release);
peer->return_to_master = false;
return io_close_taken_fd(conn);
} else
return io_close(conn);
}
static struct io_plan *peer_msgin(struct io_conn *conn,
struct peer *peer, u8 *msg);
/* Wrapper around peer_read_message: don't read another if we want to
* pass up to master */
static struct io_plan *peer_next_in(struct io_conn *conn, struct peer *peer)
{
if (peer->return_to_master) {
assert(!peer_in_started(conn, &peer->pcs));
if (!peer_out_started(conn, &peer->pcs))
return ready_for_master(conn, peer);
return io_wait(conn, peer, peer_next_in, peer);
}
return peer_read_message(conn, &peer->pcs, peer_msgin);
}
static struct io_plan *peer_msgin(struct io_conn *conn,
struct peer *peer, u8 *msg)
{
enum wire_type t = fromwire_peektype(msg);
switch (t) {
case WIRE_ERROR:
status_trace("%s sent ERROR %s",
type_to_string(trc, struct pubkey, &peer->id),
sanitize_error(trc, msg, NULL));
return io_close(conn);
case WIRE_CHANNEL_ANNOUNCEMENT:
case WIRE_NODE_ANNOUNCEMENT:
case WIRE_CHANNEL_UPDATE:
handle_gossip_msg(peer->daemon->rstate, msg);
return peer_next_in(conn, peer);
case WIRE_PING:
handle_ping(peer, msg);
return peer_next_in(conn, peer);
case WIRE_PONG:
handle_pong(peer, msg);
return peer_next_in(conn, peer);
case WIRE_OPEN_CHANNEL:
case WIRE_CHANNEL_REESTABLISH:
case WIRE_ACCEPT_CHANNEL:
case WIRE_FUNDING_CREATED:
case WIRE_FUNDING_SIGNED:
case WIRE_FUNDING_LOCKED:
case WIRE_ANNOUNCEMENT_SIGNATURES:
case WIRE_UPDATE_FEE:
case WIRE_SHUTDOWN:
case WIRE_CLOSING_SIGNED:
case WIRE_UPDATE_ADD_HTLC:
case WIRE_UPDATE_FULFILL_HTLC:
case WIRE_UPDATE_FAIL_HTLC:
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
case WIRE_COMMITMENT_SIGNED:
case WIRE_REVOKE_AND_ACK:
case WIRE_INIT:
/* Not our place to handle this, so we punt */
peer->return_to_master = true;
peer->nongossip_msg = tal_steal(peer, msg);
/* This will wait. */
return peer_next_in(conn, peer);
}
/* BOLT #1:
*
* The type follows the _it's ok to be odd_ rule, so nodes MAY send
* odd-numbered types without ascertaining that the recipient
* understands it. */
if (t & 1) {
status_trace("Peer %s sent unknown packet %u, ignoring",
type_to_string(trc, struct pubkey, &peer->id), t);
} else
peer_error(peer, "Unknown packet %u", t);
return peer_next_in(conn, peer);
}
/* Wake up the outgoing direction of the connection and write any
* queued messages. Needed since the `io_wake` method signature does
* not allow us to specify it as the callback for `new_reltimer`, but
* it allows us to set an additional flag for the routing dump..
*/
static void wake_pkt_out(struct peer *peer)
{
peer->gossip_sync = true;
new_reltimer(&peer->daemon->timers, peer,
time_from_msec(peer->daemon->broadcast_interval),
wake_pkt_out, peer);
/* Notify the peer-write loop */
msg_wake(&peer->peer_out);
/* Notify the daemon_conn-write loop */
msg_wake(&peer->owner_conn.out);
}
static struct io_plan *peer_pkt_out(struct io_conn *conn, struct peer *peer)
{
/* First priority is queued packets, if any */
const u8 *out = msg_dequeue(&peer->peer_out);
if (out) {
if (is_all_channel_error(out))
return peer_write_message(conn, &peer->pcs, take(out),
peer_close_after_error);
return peer_write_message(conn, &peer->pcs, take(out),
peer_pkt_out);
}
/* Do we want to send this peer to the master daemon? */
if (peer->return_to_master) {
assert(!peer_out_started(conn, &peer->pcs));
if (!peer_in_started(conn, &peer->pcs))
return ready_for_master(conn, peer);
return io_out_wait(conn, peer, peer_pkt_out, peer);
}
/* If we're supposed to be sending gossip, do so now. */
if (peer->gossip_sync) {
struct queued_message *next;
next = next_broadcast_message(peer->daemon->rstate->broadcasts,
&peer->broadcast_index);
if (next)
return peer_write_message(conn, &peer->pcs,
next->payload, peer_pkt_out);
/* Gossip is drained. Wait for next timer. */
peer->gossip_sync = false;
}
return msg_queue_wait(conn, &peer->peer_out, peer_pkt_out, peer);
}
/* Now we're a fully-fledged peer. */
static struct io_plan *peer_start_gossip(struct io_conn *conn, struct peer *peer)
{
wake_pkt_out(peer);
return io_duplex(conn,
peer_next_in(conn, peer),
peer_pkt_out(conn, peer));
}
/**
* owner_msg_in - Called by the `peer->owner_conn` upon receiving a
* message
*/
static struct io_plan *owner_msg_in(struct io_conn *conn,
struct daemon_conn *dc)
{
struct peer *peer = container_of(dc, struct peer, owner_conn);
u8 *msg = dc->msg_in;
int type = fromwire_peektype(msg);
if (type == WIRE_CHANNEL_ANNOUNCEMENT || type == WIRE_CHANNEL_UPDATE ||
type == WIRE_NODE_ANNOUNCEMENT) {
handle_gossip_msg(peer->daemon->rstate, dc->msg_in);
}
return daemon_conn_read_next(conn, dc);
}
static void forget_peer(struct io_conn *conn, struct daemon_conn *dc)
{
struct peer *peer = dc->ctx;
status_trace("Forgetting %s peer %s",
peer->local ? "local" : "remote",
type_to_string(trc, struct pubkey, &peer->id));
/* Free peer. */
tal_free(dc->ctx);
}
/* When a peer is to be owned by another daemon, we create a socket
* pair to send/receive gossip from it */
static bool send_peer_with_fds(struct peer *peer, const u8 *msg)
{
int fds[2];
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0) {
status_trace("Failed to create socketpair: %s",
strerror(errno));
/* FIXME: Send error to peer? */
/* Peer will be freed when caller closes conn. */
return false;
}
/* Now we talk to socket to get to peer's owner daemon. */
peer->local = false;
daemon_conn_init(peer, &peer->owner_conn, fds[0],
owner_msg_in, forget_peer);
peer->owner_conn.msg_queue_cleared_cb = nonlocal_dump_gossip;
/* Peer stays around, even though caller will close conn. */
tal_steal(peer->daemon, peer);
daemon_conn_send(&peer->daemon->master, msg);
daemon_conn_send_fd(&peer->daemon->master, peer->fd);
daemon_conn_send_fd(&peer->daemon->master, fds[1]);
/* Don't get confused: we can't use this any more. */
peer->fd = -1;
return true;
}
/**
* nonlocal_dump_gossip - catch the nonlocal peer up with the latest gossip.
*
* Registered as `msg_queue_cleared_cb` by the `peer->owner_conn`.
*/
static struct io_plan *nonlocal_dump_gossip(struct io_conn *conn, struct daemon_conn *dc)
{
struct queued_message *next;
struct peer *peer = container_of(dc, struct peer, owner_conn);
/* Make sure we are not connected directly */
if (peer->local)
return msg_queue_wait(conn, &peer->owner_conn.out,
daemon_conn_write_next, dc);
next = next_broadcast_message(peer->daemon->rstate->broadcasts,
&peer->broadcast_index);
if (!next) {
return msg_queue_wait(conn, &peer->owner_conn.out,
daemon_conn_write_next, dc);
} else {
return io_write_wire(conn, next->payload, nonlocal_dump_gossip, dc);
}
}
static struct io_plan *new_peer_got_fd(struct io_conn *conn, struct peer *peer)
{
peer->conn = io_new_conn(conn, peer->fd, peer_start_gossip, peer);
if (!peer->conn) {
status_trace("Could not create connection for peer: %s",
strerror(errno));
tal_free(peer);
} else {
/* If conn dies, we forget peer. */
tal_steal(peer->conn, peer);
}
return daemon_conn_read_next(conn, &peer->daemon->master);
}
/* Read and close fd */
static struct io_plan *discard_peer_fd(struct io_conn *conn, int *fd)
{
struct daemon *daemon = tal_parent(fd);
close(*fd);
tal_free(fd);
return daemon_conn_read_next(conn, &daemon->master);
}
static struct io_plan *handle_peer(struct io_conn *conn, struct daemon *daemon,
const u8 *msg)
{
struct peer *peer;
struct crypto_state cs;
struct pubkey id;
struct wireaddr addr;
u8 *gfeatures, *lfeatures;
u8 *inner_msg;
if (!fromwire_gossipctl_handle_peer(msg, msg, NULL, &id, &addr, &cs,
&gfeatures, &lfeatures, &inner_msg))
master_badmsg(WIRE_GOSSIPCTL_HANDLE_PEER, msg);
/* If it already exists locally, that's probably a reconnect:
* drop this one. If it exists as remote, replace with this.*/
peer = find_peer(daemon, &id);
if (peer) {
if (peer->local) {
int *fd = tal(daemon, int);
status_trace("handle_peer %s: duplicate, dropping",
type_to_string(trc, struct pubkey, &id));
return io_recv_fd(conn, fd, discard_peer_fd, fd);
}
status_trace("handle_peer %s: found remote duplicate, dropping",
type_to_string(trc, struct pubkey, &id));
tal_free(peer);
}
status_trace("handle_peer %s: new peer",
type_to_string(trc, struct pubkey, &id));
peer = new_peer(daemon, daemon, &id, &addr, &cs);
peer->gfeatures = tal_steal(peer, gfeatures);
peer->lfeatures = tal_steal(peer, lfeatures);
peer_finalized(peer);
if (tal_len(inner_msg))
msg_enqueue(&peer->peer_out, take(inner_msg));
return io_recv_fd(conn, &peer->fd, new_peer_got_fd, peer);
}
static struct io_plan *release_peer(struct io_conn *conn, struct daemon *daemon,
const u8 *msg)
{
struct pubkey id;
struct peer *peer;
if (!fromwire_gossipctl_release_peer(msg, NULL, &id))
master_badmsg(WIRE_GOSSIPCTL_RELEASE_PEER, msg);
peer = find_peer(daemon, &id);
if (!peer || !peer->local || peer->return_to_master) {
/* This can happen with dying peers, or reconnect */
status_trace("release_peer: peer %s %s",
type_to_string(trc, struct pubkey, &id),
!peer ? "not found"
: peer->return_to_master ? "already releasing"
: "not local");
msg = towire_gossipctl_release_peer_replyfail(msg);
daemon_conn_send(&daemon->master, take(msg));
} else {
peer->return_to_master = true;
peer->nongossip_msg = NULL;
/* Wake output, in case it's idle. */
msg_wake(&peer->peer_out);
}
return daemon_conn_read_next(conn, &daemon->master);
}
static struct io_plan *getroute_req(struct io_conn *conn, struct daemon *daemon,
u8 *msg)
{
tal_t *tmpctx = tal_tmpctx(msg);
struct pubkey source, destination;
u32 msatoshi, final_cltv;
u16 riskfactor;
u8 *out;
struct route_hop *hops;
fromwire_gossip_getroute_request(msg, NULL, &source, &destination,
&msatoshi, &riskfactor, &final_cltv);
status_trace("Trying to find a route from %s to %s for %d msatoshi",
pubkey_to_hexstr(tmpctx, &source),
pubkey_to_hexstr(tmpctx, &destination), msatoshi);
hops = get_route(tmpctx, daemon->rstate, &source, &destination,
msatoshi, 1, final_cltv);
out = towire_gossip_getroute_reply(msg, hops);
tal_free(tmpctx);
daemon_conn_send(&daemon->master, out);
return daemon_conn_read_next(conn, &daemon->master);
}
static struct io_plan *getchannels_req(struct io_conn *conn, struct daemon *daemon,
u8 *msg)
{
tal_t *tmpctx = tal_tmpctx(daemon);
u8 *out;
size_t j, num_chans = 0;
struct gossip_getchannels_entry *entries;
struct node *n;
struct node_map_iter i;
entries = tal_arr(tmpctx, struct gossip_getchannels_entry, num_chans);
n = node_map_first(daemon->rstate->nodes, &i);
while (n != NULL) {
for (j=0; j<tal_count(n->out); j++){
tal_resize(&entries, num_chans + 1);
entries[num_chans].source = n->out[j]->src->id;
entries[num_chans].destination = n->out[j]->dst->id;
entries[num_chans].active = n->out[j]->active;
entries[num_chans].flags = n->out[j]->flags;
entries[num_chans].short_channel_id = n->out[j]->short_channel_id;
entries[num_chans].last_update_timestamp = n->out[j]->last_timestamp;
if (entries[num_chans].last_update_timestamp >= 0) {
entries[num_chans].base_fee_msat = n->out[j]->base_fee;
entries[num_chans].fee_per_millionth = n->out[j]->proportional_fee;
entries[num_chans].delay = n->out[j]->delay;
}
num_chans++;
}
n = node_map_next(daemon->rstate->nodes, &i);
}
out = towire_gossip_getchannels_reply(daemon, entries);
daemon_conn_send(&daemon->master, take(out));
tal_free(tmpctx);
return daemon_conn_read_next(conn, &daemon->master);
}
static struct io_plan *getnodes(struct io_conn *conn, struct daemon *daemon)
{
tal_t *tmpctx = tal_tmpctx(daemon);
u8 *out;
struct node *n;
struct node_map_iter i;
struct gossip_getnodes_entry *nodes;
size_t node_count = 0;
nodes = tal_arr(tmpctx, struct gossip_getnodes_entry, node_count);
n = node_map_first(daemon->rstate->nodes, &i);
while (n != NULL) {
tal_resize(&nodes, node_count + 1);
nodes[node_count].nodeid = n->id;
nodes[node_count].addresses = n->addresses;
node_count++;
n = node_map_next(daemon->rstate->nodes, &i);
}
out = towire_gossip_getnodes_reply(daemon, nodes);
daemon_conn_send(&daemon->master, take(out));
tal_free(tmpctx);
return daemon_conn_read_next(conn, &daemon->master);
}
static struct io_plan *ping_req(struct io_conn *conn, struct daemon *daemon,
const u8 *msg)
{
struct pubkey id;
u16 num_pong_bytes, len;
struct peer *peer;
u8 *ping;
if (!fromwire_gossip_ping(msg, NULL, &id, &num_pong_bytes, &len))
master_badmsg(WIRE_GOSSIP_PING, msg);
peer = find_peer(daemon, &id);
if (!peer) {
daemon_conn_send(&daemon->master,
take(towire_gossip_ping_reply(peer, false, 0)));
goto out;
}
ping = make_ping(peer, num_pong_bytes, len);
if (tal_len(ping) > 65535)
status_failed(STATUS_FAIL_MASTER_IO, "Oversize ping");
msg_enqueue(&peer->peer_out, take(ping));
status_trace("sending ping expecting %sresponse",
num_pong_bytes >= 65532 ? "no " : "");
/* BOLT #1:
*
* if `num_pong_bytes` is less than 65532 it MUST respond by sending a
* `pong` message with `byteslen` equal to `num_pong_bytes`, otherwise
* it MUST ignore the `ping`.
*/
if (num_pong_bytes >= 65532)
daemon_conn_send(&daemon->master,
take(towire_gossip_ping_reply(peer, true, 0)));
else
peer->num_pings_outstanding++;
out:
return daemon_conn_read_next(conn, &daemon->master);
}
static int make_listen_fd(int domain, void *addr, socklen_t len)
{
int fd = socket(domain, SOCK_STREAM, 0);
if (fd < 0) {
status_trace("Failed to create %u socket: %s",
domain, strerror(errno));
return -1;
}
if (addr) {
int on = 1;
/* Re-use, please.. */
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
status_trace("Failed setting socket reuse: %s",
strerror(errno));
if (bind(fd, addr, len) != 0) {
status_trace("Failed to bind on %u socket: %s",
domain, strerror(errno));
goto fail;
}
}
if (listen(fd, 5) != 0) {
status_trace("Failed to listen on %u socket: %s",
domain, strerror(errno));
goto fail;
}
return fd;
fail:
close_noerr(fd);
return -1;
}
static struct io_plan *connection_in(struct io_conn *conn, struct daemon *daemon)
{
struct wireaddr addr;
struct sockaddr_storage s;
socklen_t len = sizeof(s);
if (getpeername(io_conn_fd(conn), (struct sockaddr *)&s, &len) != 0) {
status_trace("Failed to get peername for incoming conn");
return io_close(conn);
}
if (s.ss_family == AF_INET6) {
struct sockaddr_in6 *s6 = (void *)&s;
addr.type = ADDR_TYPE_IPV6;
addr.addrlen = sizeof(s6->sin6_addr);
BUILD_ASSERT(sizeof(s6->sin6_addr) <= sizeof(addr.addr));
memcpy(addr.addr, &s6->sin6_addr, addr.addrlen);
addr.port = ntohs(s6->sin6_port);
} else if (s.ss_family == AF_INET) {
struct sockaddr_in *s4 = (void *)&s;
addr.type = ADDR_TYPE_IPV4;
addr.addrlen = sizeof(s4->sin_addr);
BUILD_ASSERT(sizeof(s4->sin_addr) <= sizeof(addr.addr));
memcpy(addr.addr, &s4->sin_addr, addr.addrlen);
addr.port = ntohs(s4->sin_port);
} else {
status_trace("Unknown socket type %i for incoming conn",
s.ss_family);
return io_close(conn);
}
/* FIXME: Timeout */
return responder_handshake(conn, &daemon->id, &addr,
init_new_peer, daemon);
}
static void setup_listeners(struct daemon *daemon, u16 portnum)
{
struct sockaddr_in addr;
struct sockaddr_in6 addr6;
socklen_t len;
int fd1, fd2;
if (!portnum) {
status_trace("Zero portnum, not listening for incoming");
return;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;