lnsocket

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

test.c (1405B)


      1 
      2 #include "lnsocket.h"
      3 #include "endian.h"
      4 #include "typedefs.h"
      5 #include <stdio.h>
      6 #include <assert.h>
      7 #include <sodium/randombytes.h>
      8 
      9 static void print_data(unsigned char *buf, int len)
     10 {
     11 	int i;
     12 
     13 	for (i = 0; i < len; i++) {
     14 		printf("%02x", buf[i]);
     15 	}
     16 	printf("\n");
     17 }
     18 
     19 int main(int argc, const char *argv[])
     20 {
     21 	static u8 msgbuf[4096];
     22 	u8 *buf;
     23 	u16 msgtype;
     24 	struct lnsocket *ln;
     25 
     26 	static unsigned char key[32] = {0};
     27 
     28 	u16 len;
     29 	int ok = 1;
     30 
     31 	ln = lnsocket_create();
     32 	assert(ln);
     33 
     34 	if (lnsocket_setkey(ln, key)) {
     35 		// key with 0s should be an error
     36 		ok = 0;
     37 		goto done;
     38 	}
     39 
     40 	randombytes_buf(key, sizeof(key));
     41 	if (!(ok = lnsocket_setkey(ln, key)))
     42 		goto done;
     43 
     44 	const char *nodeid = "03f3c108ccd536b8526841f0a5c58212bb9e6584a1eb493080e7c1cc34f82dad71";
     45 	if (!(ok = lnsocket_connect(ln, nodeid, "24.84.152.187")))
     46 		goto done;
     47 
     48 	if (!(ok = lnsocket_perform_init(ln)))
     49 		goto done;
     50 
     51 	printf("init ok!\n");
     52 
     53 	if (!(ok = len = lnsocket_make_ping_msg(msgbuf, sizeof(msgbuf), 1, 1)))
     54 		goto done;
     55 
     56 	if (!(ok = lnsocket_write(ln, msgbuf, len)))
     57 		goto done;
     58 
     59 	printf("sent ping ");
     60 	print_data(msgbuf, len);
     61 
     62 	for (int packets = 0; packets < 3; packets++) {
     63 		if (!(ok = lnsocket_recv(ln, &msgtype, &buf, &len)))
     64 			goto done;
     65 
     66 		if (msgtype == WIRE_PONG) {
     67 			printf("got pong! ");
     68 			print_data(buf, len);
     69 			break;
     70 		}
     71 	}
     72 
     73 done:
     74 	lnsocket_print_errors(ln);
     75 	lnsocket_destroy(ln);
     76 	return !ok;
     77 }