lnsocket

A minimal C library for connecting to the lightning network
git clone git://jb55.com/lnsocket
Log | Files | Refs | Submodules | README | LICENSE

commit cd612be67e9c79e2d66d879dd6c0a65944694896
parent 3575f1a0cbfeb6942f9f33187d9a93cab61b71e8
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 22 Jan 2022 09:08:03 -0800

simplify init in example

Diffstat:
Mlnsocket.c | 39+++++++++++++++++++++++++++++++++++++++
Mlnsocket.h | 2++
Mtest.c | 36+++---------------------------------
3 files changed, 44 insertions(+), 33 deletions(-)

diff --git a/lnsocket.c b/lnsocket.c @@ -100,6 +100,45 @@ static int read_all(int fd, void *data, size_t size) return 1; } +int lnsocket_perform_init(struct lnsocket *ln) +{ + u8 tlvbuf[1024]; + u8 msgbuf[1024]; + u8 global_features[2] = {0}; + u8 features[5] = {0}; + struct tlv network_tlv; + int len; + u8 *buf; + + if (!lnsocket_read(ln, &buf, &len)) + return 0; + + const u8 genesis_block[] = { + 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, 0xc1, 0xa6, 0xa2, 0x46, + 0xae, 0x63, 0xf7, 0x4f, 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, + 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + const u8 *blockids[] = { genesis_block }; + + if (!lnsocket_make_network_tlv(tlvbuf, sizeof(tlvbuf), blockids, 1, + &network_tlv)) + return 0; + + const struct tlv *init_tlvs[] = { &network_tlv } ; + + if (!lnsocket_make_init_msg(msgbuf, sizeof(msgbuf), + global_features, sizeof(global_features), + features, sizeof(features), + init_tlvs, 1, + &len)) + return 0; + + if (!lnsocket_write(ln, msgbuf, len)) + return 0; + + return 1; +} int lnsocket_read(struct lnsocket *ln, unsigned char **buf, int *len) { diff --git a/lnsocket.h b/lnsocket.h @@ -66,6 +66,8 @@ int lnsocket_make_network_tlv(unsigned char *buf, int buflen, const unsigned cha int lnsocket_make_ping_msg(unsigned char *buf, int buflen, unsigned short num_pong_bytes, unsigned short ignored_bytes, int *outlen); int lnsocket_make_init_msg(unsigned char *buf, int buflen, const unsigned char *globalfeatures, unsigned short gflen, const unsigned char *features, unsigned short flen, const struct tlv **tlvs, unsigned short num_tlvs, int *outlen); +int lnsocket_perform_init(struct lnsocket *ln); + int lnsocket_connect(struct lnsocket *, const char *node_id, const char *host); int lnsocket_write(struct lnsocket *, const unsigned char *msg, int msg_len); int lnsocket_read(struct lnsocket *, unsigned char **buf, int *len); diff --git a/test.c b/test.c @@ -17,15 +17,11 @@ static void print_data(unsigned char *buf, int len) int main(int argc, const char *argv[]) { - static u8 tlvbuf[1024]; static u8 msgbuf[4096]; u8 *buf; - u8 global_features[2] = {0}; - u8 features[5] = {0}; - struct tlv network_tlv; struct lnsocket *ln; - int len; + int len; int ok = 1; ln = lnsocket_create(); @@ -37,36 +33,10 @@ int main(int argc, const char *argv[]) if (!(ok = lnsocket_connect(ln, nodeid, "24.84.152.187"))) goto done; - if (!(ok = lnsocket_read(ln, &buf, &len))) - goto done; - - printf("got "); print_data(buf, len); - - const u8 genesis_block[] = { - 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, 0xc1, 0xa6, 0xa2, 0x46, - 0xae, 0x63, 0xf7, 0x4f, 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, - 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00 - }; - - const u8 *blockids[] = { genesis_block }; - - if (!(ok = lnsocket_make_network_tlv(tlvbuf, sizeof(tlvbuf), blockids, 1, &network_tlv))) + if (!(ok = lnsocket_perform_init(ln))) goto done; - const struct tlv *init_tlvs[] = { &network_tlv } ; - - if (!(ok = lnsocket_make_init_msg(msgbuf, sizeof(msgbuf), - global_features, sizeof(global_features), - features, sizeof(features), - init_tlvs, 1, - &len))) - goto done; - - if (!(ok = lnsocket_write(ln, msgbuf, len))) - goto done; - - printf("sent init "); - print_data(msgbuf, len); + printf("init ok!\n"); if (!(ok = lnsocket_make_ping_msg(msgbuf, sizeof(msgbuf), 1, 1, &len))) goto done;