forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgossipwith.c
292 lines (248 loc) · 7.31 KB
/
gossipwith.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
/* Simple tool to route gossip from a peer. */
#include <ccan/array_size/array_size.h>
#include <ccan/err/err.h>
#include <ccan/io/io.h>
#include <ccan/opt/opt.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/str/hex/hex.h>
#include <common/crypto_sync.h>
#include <common/dev_disconnect.h>
#include <common/peer_failed.h>
#include <common/per_peer_state.h>
#include <common/status.h>
#include <netdb.h>
#include <poll.h>
#include <secp256k1_ecdh.h>
#include <wire/peer_wire.h>
#define io_write_ simple_write
#define io_read_ simple_read
#define io_close simple_close
static bool stream_stdin = false;
static bool no_init = false;
static struct io_plan *simple_write(struct io_conn *conn,
const void *data, size_t len,
struct io_plan *(*next)(struct io_conn *, void *),
void *arg);
static struct io_plan *simple_read(struct io_conn *conn,
void *data, size_t len,
struct io_plan *(*next)(struct io_conn *, void *),
void *next_arg);
static struct io_plan *simple_close(struct io_conn *conn)
{
return NULL;
}
#include "../connectd/handshake.c"
/* This makes the handshake prototypes work. */
struct io_conn {
int fd;
};
static struct secret notsosecret;
static bool initial_sync = false;
static unsigned long max_messages = -1UL;
/* Empty stubs to make us compile */
void status_peer_io(enum log_level iodir, const u8 *p)
{
}
void status_fmt(enum log_level level, const char *fmt, ...)
{
}
#if DEVELOPER
void dev_sabotage_fd(int fd)
{
abort();
}
void dev_blackhole_fd(int fd)
{
abort();
}
enum dev_disconnect dev_disconnect(int pkt_type)
{
return DEV_DISCONNECT_NORMAL;
}
#endif
void peer_failed_connection_lost(void)
{
exit(0);
}
struct secret *hsm_do_ecdh(const tal_t *ctx, const struct pubkey *point)
{
struct secret *ss = tal(ctx, struct secret);
if (secp256k1_ecdh(secp256k1_ctx, ss->data, &point->pubkey,
notsosecret.data, NULL, NULL) != 1)
return tal_free(ss);
return ss;
}
/* We don't want to discard *any* messages. */
bool is_unknown_msg_discardable(const u8 *cursor)
{
return false;
}
static struct io_plan *simple_write(struct io_conn *conn,
const void *data, size_t len,
struct io_plan *(*next)(struct io_conn *, void *),
void *arg)
{
if (!write_all(conn->fd, data, len))
err(1, "Writing data");
return next(conn, arg);
}
static struct io_plan *simple_read(struct io_conn *conn,
void *data, size_t len,
struct io_plan *(*next)(struct io_conn *, void *),
void *next_arg)
{
if (!read_all(conn->fd, data, len))
err(1, "Reading data");
return next(conn, next_arg);
}
static struct io_plan *handshake_success(struct io_conn *conn,
const struct pubkey *them,
const struct wireaddr_internal *addr,
const struct crypto_state *orig_cs,
char **args)
{
u8 *msg;
struct per_peer_state *pps = new_per_peer_state(conn, orig_cs);
u8 *localfeatures;
struct pollfd pollfd[2];
pps->peer_fd = io_conn_fd(conn);
if (initial_sync) {
localfeatures = tal(conn, u8);
localfeatures[0] = (1 << 3);
} else
localfeatures = NULL;
if (!no_init) {
msg = towire_init(NULL, NULL, localfeatures);
sync_crypto_write(pps, take(msg));
/* Ignore their init message. */
tal_free(sync_crypto_read(NULL, pps));
}
if (stream_stdin)
pollfd[0].fd = STDIN_FILENO;
else
pollfd[0].fd = -1;
pollfd[0].events = POLLIN;
pollfd[1].fd = pps->peer_fd;
pollfd[1].events = POLLIN;
while (*args) {
u8 *m = tal_hexdata(NULL, *args, strlen(*args));
if (!m)
errx(1, "Invalid hexdata '%s'", *args);
sync_crypto_write(pps, take(m));
args++;
}
while (max_messages != 0 || pollfd[0].fd != -1) {
beint16_t belen;
u8 *msg;
poll(pollfd, ARRAY_SIZE(pollfd), -1);
/* We always to stdin first if we can */
if (pollfd[0].revents & POLLIN) {
if (!read_all(STDIN_FILENO, &belen, sizeof(belen)))
pollfd[0].fd = -1;
else {
msg = tal_arr(NULL, u8, be16_to_cpu(belen));
if (!read_all(STDIN_FILENO, msg, tal_bytelen(msg)))
err(1, "Only read partial message");
sync_crypto_write(pps, take(msg));
}
} else if (pollfd[1].revents & POLLIN) {
msg = sync_crypto_read(NULL, pps);
if (!msg)
err(1, "Reading msg");
belen = cpu_to_be16(tal_bytelen(msg));
if (!write_all(STDOUT_FILENO, &belen, sizeof(belen))
|| !write_all(STDOUT_FILENO, msg, tal_bytelen(msg)))
err(1, "Writing out msg");
tal_free(msg);
--max_messages;
}
}
exit(0);
}
static char *opt_set_secret(const char *arg, struct secret *s)
{
if (!hex_decode(arg, strlen(arg), s->data, sizeof(s->data)))
return "secret must be 64 hex digits";
return NULL;
}
static void opt_show_secret(char buf[OPT_SHOW_LEN], const struct secret *s)
{
hex_encode(s->data, sizeof(s->data), buf, OPT_SHOW_LEN);
}
int main(int argc, char *argv[])
{
struct io_conn *conn = tal(NULL, struct io_conn);
struct wireaddr_internal addr;
int af = -1;
struct pubkey us, them;
const char *err_msg;
const char *at;
struct addrinfo *ai = NULL;
setup_locale();
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY |
SECP256K1_CONTEXT_SIGN);
memset(¬sosecret, 0x42, sizeof(notsosecret));
opt_register_noarg("--initial-sync", opt_set_bool, &initial_sync,
"Stream complete gossip history at start");
opt_register_arg("--max-messages", opt_set_ulongval, opt_show_ulongval,
&max_messages,
"Terminate after reading this many messages (> 0)");
opt_register_noarg("--stdin", opt_set_bool, &stream_stdin,
"Stream gossip messages from stdin.");
opt_register_noarg("--no-init", opt_set_bool, &no_init,
"Don't send or swallow init messages.");
opt_register_arg("--privkey", opt_set_secret, opt_show_secret,
¬sosecret,
"Secret key to use for our identity");
opt_register_noarg("--help|-h", opt_usage_and_exit,
"id@addr[:port] [hex-msg-tosend...]\n"
"Connect to a lightning peer and relay gossip messages from it",
"Print this message.");
opt_parse(&argc, argv, opt_log_stderr_exit);
if (argc < 2)
opt_usage_exit_fail("Need an id@addr to connect to");
at = strchr(argv[1], '@');
if (!at)
opt_usage_exit_fail("Need id@addr");
if (!pubkey_from_hexstr(argv[1], at - argv[1], &them))
opt_usage_exit_fail("Invalid id %.*s",
(int)(at - argv[1]), argv[1]);
if (!parse_wireaddr_internal(at+1, &addr, DEFAULT_PORT, NULL,
true, false, &err_msg))
opt_usage_exit_fail("%s '%s'", err_msg, argv[1]);
switch (addr.itype) {
case ADDR_INTERNAL_SOCKNAME:
af = AF_LOCAL;
ai = wireaddr_internal_to_addrinfo(conn, &addr);
break;
case ADDR_INTERNAL_ALLPROTO:
case ADDR_INTERNAL_AUTOTOR:
case ADDR_INTERNAL_FORPROXY:
opt_usage_exit_fail("Don't support proxy use");
case ADDR_INTERNAL_WIREADDR:
switch (addr.u.wireaddr.type) {
case ADDR_TYPE_TOR_V2:
case ADDR_TYPE_TOR_V3:
opt_usage_exit_fail("Don't support proxy use");
break;
case ADDR_TYPE_IPV4:
af = AF_INET;
break;
case ADDR_TYPE_IPV6:
af = AF_INET6;
break;
}
ai = wireaddr_to_addrinfo(tmpctx, &addr.u.wireaddr);
}
if (af == -1 || ai == NULL)
err(1, "Initializing socket");
conn->fd = socket(af, SOCK_STREAM, 0);
if (conn->fd < 0)
err(1, "Creating socket");
if (!pubkey_from_secret(¬sosecret, &us))
errx(1, "Creating pubkey");
if (connect(conn->fd, ai->ai_addr, ai->ai_addrlen) != 0)
err(1, "Connecting to %s", at+1);
initiator_handshake(conn, &us, &them, &addr, handshake_success, argv+2);
exit(0);
}