lnsocket.c (19179B)
1 2 3 #include <sys/socket.h> 4 #include <sys/types.h> 5 #include <inttypes.h> 6 #include <netdb.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <assert.h> 11 #include <errno.h> 12 #include <unistd.h> 13 #include <fcntl.h> 14 #include <netinet/in.h> 15 #include <arpa/inet.h> 16 17 #include <secp256k1.h> 18 #include <secp256k1_ecdh.h> 19 #include <sodium/crypto_aead_chacha20poly1305.h> 20 #include <sodium/randombytes.h> 21 22 #include "handshake.h" 23 #include "error.h" 24 #include "crypto.h" 25 #include "endian.h" 26 #include "bigsize.h" 27 #include "compiler.h" 28 #include "lnsocket_internal.h" 29 #include "lnsocket.h" 30 31 #define array_len(x) (sizeof(x)/sizeof(x[0])) 32 33 #define MSGBUF_MEM (65536*2) 34 #define ERROR_MEM 4096 35 #define DEFAULT_TIMEOUT 3000 36 #define DEFAULT_TOR_TIMEOUT 10000 37 38 // Tor socks define 39 #define SOCKS_NOAUTH 0 40 #define SOCKS_ERROR 0xff 41 #define SOCKS_CONNECT 1 42 #define SOCKS_TYP_IPV4 1 43 #define SOCKS_DOMAIN 3 44 #define SOCKS_TYP_IPV6 4 45 #define SOCKS_V5 5 46 47 #define MAX_SIZE_OF_SOCKS5_REQ_OR_RESP 255 48 #define SIZE_OF_RESPONSE 4 49 #define SIZE_OF_REQUEST 3 50 #define SIZE_OF_IPV4_RESPONSE 6 51 #define SIZE_OF_IPV6_RESPONSE 18 52 #define SOCK_REQ_METH_LEN 3 53 #define SOCK_REQ_V5_LEN 5 54 #define SOCK_REQ_V5_HEADER_LEN 7 55 56 int push_error(struct lnsocket *lnsocket, const char *err); 57 58 static int char_to_hex(unsigned char *val, char c) 59 { 60 if (c >= '0' && c <= '9') { 61 *val = c - '0'; 62 return 1; 63 } 64 if (c >= 'a' && c <= 'f') { 65 *val = c - 'a' + 10; 66 return 1; 67 } 68 if (c >= 'A' && c <= 'F') { 69 *val = c - 'A' + 10; 70 return 1; 71 } 72 return 0; 73 } 74 75 76 static int hex_decode(const char *str, size_t slen, void *buf, size_t bufsize) 77 { 78 unsigned char v1, v2; 79 unsigned char *p = buf; 80 81 while (slen > 1) { 82 if (!char_to_hex(&v1, str[0]) || !char_to_hex(&v2, str[1])) 83 return 0; 84 if (!bufsize) 85 return 0; 86 *(p++) = (v1 << 4) | v2; 87 str += 2; 88 slen -= 2; 89 bufsize--; 90 } 91 return slen == 0 && bufsize == 0; 92 } 93 94 95 int parse_node_id(const char *str, struct node_id *dest) 96 { 97 return hex_decode(str, strlen(str), dest->k, sizeof(*dest)); 98 } 99 100 int pubkey_from_node_id(secp256k1_context *secp, struct pubkey *key, 101 const struct node_id *id) 102 { 103 return secp256k1_ec_pubkey_parse(secp, &key->pubkey, 104 memcheck(id->k, sizeof(id->k)), 105 sizeof(id->k)); 106 } 107 108 109 static int read_all(int fd, void *data, size_t size) 110 { 111 while (size) { 112 ssize_t done; 113 114 done = read(fd, data, size); 115 if (done < 0 && errno == EINTR) 116 continue; 117 if (done <= 0) 118 return 0; 119 data = (char *)data + done; 120 size -= done; 121 } 122 123 return 1; 124 } 125 126 int EXPORT lnsocket_make_default_initmsg(unsigned char *msgbuf, int buflen) 127 { 128 u8 global_features[2] = {0}; 129 u8 features[5] = {0}; 130 u16 len; 131 132 /* 133 struct tlv network_tlv; 134 u8 tlvbuf[1024]; 135 const u8 genesis_block[] = { 136 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, 0xc1, 0xa6, 0xa2, 0x46, 137 0xae, 0x63, 0xf7, 0x4f, 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, 138 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00 139 }; 140 141 const u8 *blockids[] = { genesis_block }; 142 143 if (!lnsocket_make_network_tlv(tlvbuf, sizeof(tlvbuf), blockids, 1, 144 &network_tlv)) 145 return 0; 146 147 148 const struct tlv *init_tlvs[] = { &network_tlv } ; 149 */ 150 151 const struct tlv *init_tlvs[] = { } ; 152 153 if (!lnsocket_make_init_msg(msgbuf, buflen, 154 global_features, sizeof(global_features), 155 features, sizeof(features), 156 init_tlvs, 0, 157 &len)) 158 return 0; 159 160 return (int)len; 161 } 162 163 /* 164 static void print_hex(u8 *bytes, int len) { 165 int i; 166 for (i = 0; i < len; ++i) { 167 printf("%02x", bytes[i]); 168 } 169 } 170 */ 171 172 int lnsocket_perform_init(struct lnsocket *ln) 173 { 174 u8 msgbuf[1024]; 175 u16 len; 176 u8 *buf; 177 178 // read the init message from the other side and ignore it 179 if (!lnsocket_read(ln, &buf, &len)) 180 return 0; 181 182 if (!(len = lnsocket_make_default_initmsg(msgbuf, sizeof(msgbuf)))) 183 return 0; 184 185 if (!lnsocket_write(ln, msgbuf, len)) 186 return 0; 187 188 return 1; 189 } 190 191 // simple helper that pushes a message type and payload 192 int lnsocket_send(struct lnsocket *ln, unsigned short msg_type, const unsigned char *payload, unsigned short payload_len) 193 { 194 reset_cursor(&ln->msgbuf); 195 196 if (!cursor_push_u16(&ln->msgbuf, msg_type)) 197 return note_error(&ln->errs, "could not write type to msgbuf?"); 198 199 if (!cursor_push(&ln->msgbuf, payload, payload_len)) 200 return note_error(&ln->errs, "payload too big"); 201 202 return lnsocket_write(ln, ln->msgbuf.start, ln->msgbuf.p - ln->msgbuf.start); 203 } 204 205 // simple helper that receives a message type and payload 206 int lnsocket_recv(struct lnsocket *ln, u16 *msg_type, unsigned char **payload, u16 *payload_len) 207 { 208 struct cursor cur; 209 u8 *msg; 210 u16 msglen; 211 212 if (!lnsocket_read(ln, &msg, &msglen)) 213 return 0; 214 215 make_cursor(msg, msg + msglen, &cur); 216 217 if (!cursor_pull_u16(&cur, msg_type)) 218 return note_error(&ln->errs, "could not read msgtype"); 219 220 *payload_len = msglen - 2; 221 *payload = cur.p; 222 223 if (*payload + *payload_len > cur.end) 224 return note_error(&ln->errs, "recv buffer overflow?"); 225 226 return 1; 227 } 228 229 int EXPORT lnsocket_decrypt(struct lnsocket *ln, unsigned char *packet, int size) 230 { 231 struct cursor enc, dec; 232 233 make_cursor(packet, packet + size, &enc); 234 reset_cursor(&ln->msgbuf); 235 if (!cursor_slice(&ln->msgbuf, &dec, size - 16)) 236 return note_error(&ln->errs, "out of memory: %d + %d = %d > %d", 237 ln->msgbuf.end - ln->msgbuf.p, size, 238 ln->msgbuf.end - ln->msgbuf.p + size, 239 MSGBUF_MEM 240 ); 241 242 if (!cryptomsg_decrypt_body(&ln->crypto_state, 243 enc.start, enc.end - enc.start, 244 dec.start, dec.end - dec.start)) 245 return note_error(&ln->errs, "error decrypting body"); 246 247 return dec.end - dec.start; 248 } 249 250 // this is used in js 251 int EXPORT lnsocket_decrypt_header(struct lnsocket *ln, unsigned char *hdr) 252 { 253 u16 size = 0; 254 if (!cryptomsg_decrypt_header(&ln->crypto_state, hdr, &size)) 255 return note_error(&ln->errs, 256 "Failed hdr decrypt with rn=%"PRIu64, 257 ln->crypto_state.rn-1); 258 return size; 259 } 260 261 int lnsocket_read(struct lnsocket *ln, unsigned char **buf, unsigned short *len) 262 { 263 struct cursor enc, dec; 264 u8 hdr[18]; 265 u16 size; 266 267 reset_cursor(&ln->errs.cur); 268 reset_cursor(&ln->msgbuf); 269 270 if (!read_all(ln->socket, hdr, sizeof(hdr))) 271 return note_error(&ln->errs,"Failed reading header: %s", 272 strerror(errno)); 273 274 if (!cryptomsg_decrypt_header(&ln->crypto_state, hdr, &size)) 275 return note_error(&ln->errs, 276 "Failed hdr decrypt with rn=%"PRIu64, 277 ln->crypto_state.rn-1); 278 279 if (!cursor_slice(&ln->msgbuf, &enc, size + 16)) 280 return note_error(&ln->errs, "out of memory"); 281 282 if (!cursor_slice(&ln->msgbuf, &dec, size)) 283 return note_error(&ln->errs, "out of memory: %d + %d = %d > %d", 284 ln->msgbuf.end - ln->msgbuf.p, size, 285 ln->msgbuf.end - ln->msgbuf.p + size, 286 MSGBUF_MEM 287 ); 288 289 if (!read_all(ln->socket, enc.p, enc.end - enc.start)) 290 return note_error(&ln->errs, "Failed reading body: %s", 291 strerror(errno)); 292 293 if (!cryptomsg_decrypt_body(&ln->crypto_state, 294 enc.start, enc.end - enc.start, 295 dec.start, dec.end - dec.start)) 296 return note_error(&ln->errs, "error decrypting body"); 297 298 *buf = dec.start; 299 *len = dec.end - dec.start; 300 301 return 1; 302 } 303 304 static int highest_byte(unsigned char *buf, int buflen) 305 { 306 int i, highest; 307 for (i = 0, highest = 0; i < buflen; i++) { 308 if (buf[i] != 0) 309 highest = i; 310 } 311 return highest; 312 } 313 314 #define max(a,b) ((a) > (b) ? (a) : (b)) 315 int lnsocket_set_feature_bit(unsigned char *buf, int buflen, int *newlen, unsigned int bit) 316 { 317 if (newlen == NULL) 318 return 0; 319 320 if (bit / 8 >= buflen) 321 return 0; 322 323 *newlen = max(highest_byte(buf, buflen), (bit / 8) + 1); 324 buf[*newlen - 1 - bit / 8] |= (1 << (bit % 8)); 325 326 return 1; 327 } 328 #undef max 329 330 int cursor_push_tlv(struct cursor *cur, const struct tlv *tlv) 331 { 332 /* BOLT #1: 333 * 334 * The sending node: 335 ... 336 * - MUST minimally encode `type` and `length`. 337 */ 338 return cursor_push_bigsize(cur, tlv->type) && 339 cursor_push_bigsize(cur, tlv->length) && 340 cursor_push(cur, tlv->value, tlv->length); 341 } 342 343 int cursor_push_tlvs(struct cursor *cur, const struct tlv **tlvs, int n_tlvs) 344 { 345 int i; 346 for (i = 0; i < n_tlvs; i++) { 347 if (!cursor_push_tlv(cur, tlvs[i])) 348 return 0; 349 } 350 351 return 1; 352 } 353 354 int lnsocket_make_network_tlv(unsigned char *buf, int buflen, 355 const unsigned char **blockids, int num_blockids, 356 struct tlv *tlv_out) 357 { 358 struct cursor cur; 359 360 if (!tlv_out) 361 return 0; 362 363 tlv_out->type = 1; 364 tlv_out->value = buf; 365 366 make_cursor(buf, buf + buflen, &cur); 367 368 for (size_t i = 0; i < num_blockids; i++) { 369 if (!cursor_push(&cur, memcheck(blockids[i], 32), 32)) 370 return 0; 371 } 372 373 tlv_out->length = cur.p - cur.start; 374 return 1; 375 } 376 377 int lnsocket_make_pong_msg(unsigned char *buf, int buflen, u16 num_pong_bytes) 378 { 379 struct cursor msg; 380 381 make_cursor(buf, buf + buflen, &msg); 382 383 if (!cursor_push_u16(&msg, WIRE_PONG)) 384 return 0; 385 386 // don't include itself 387 num_pong_bytes = num_pong_bytes <= 4 ? 0 : num_pong_bytes - 4; 388 389 if (!cursor_push_u16(&msg, num_pong_bytes)) 390 return 0; 391 392 if (msg.p + num_pong_bytes > msg.end) 393 return 0; 394 395 memset(msg.p, 0, num_pong_bytes); 396 msg.p += num_pong_bytes; 397 398 return msg.p - msg.start; 399 } 400 401 static int lnsocket_decode_ping_payload(const unsigned char *payload, int payload_len, u16 *pong_bytes) 402 { 403 struct cursor msg; 404 405 make_cursor((u8*)payload, (u8*)payload + payload_len, &msg); 406 407 if (!cursor_pull_u16(&msg, pong_bytes)) 408 return 0; 409 410 return 1; 411 } 412 413 int lnsocket_make_pong_from_ping(unsigned char *buf, int buflen, const unsigned char *ping, u16 ping_len) 414 { 415 u16 pong_bytes; 416 417 if (!lnsocket_decode_ping_payload(ping, ping_len, &pong_bytes)) 418 return 0; 419 420 return lnsocket_make_pong_msg(buf, buflen, pong_bytes); 421 } 422 423 int lnsocket_pong(struct lnsocket *ln, const unsigned char *ping, u16 ping_len) 424 { 425 unsigned char pong[0xFFFF]; 426 u16 len; 427 428 if (!(len = lnsocket_make_pong_from_ping(pong, sizeof(pong), ping, ping_len))) 429 return 0; 430 431 return lnsocket_write(ln, pong, len); 432 } 433 434 int lnsocket_make_ping_msg(unsigned char *buf, int buflen, u16 num_pong_bytes, u16 ignored_bytes) 435 { 436 struct cursor msg; 437 int i; 438 439 make_cursor(buf, buf + buflen, &msg); 440 441 if (!cursor_push_u16(&msg, WIRE_PING)) 442 return 0; 443 if (!cursor_push_u16(&msg, num_pong_bytes)) 444 return 0; 445 if (!cursor_push_u16(&msg, ignored_bytes)) 446 return 0; 447 for (i = 0; i < ignored_bytes; i++) { 448 if (!cursor_push_byte(&msg, 0)) 449 return 0; 450 } 451 452 return msg.p - msg.start; 453 } 454 455 int lnsocket_make_init_msg(unsigned char *buf, int buflen, 456 const unsigned char *globalfeatures, u16 gflen, 457 const unsigned char *features, u16 flen, 458 const struct tlv **tlvs, 459 unsigned short num_tlvs, 460 unsigned short *outlen) 461 { 462 struct cursor msg; 463 464 make_cursor(buf, buf + buflen, &msg); 465 466 if (!cursor_push_u16(&msg, WIRE_INIT)) 467 return 0; 468 469 if (!cursor_push_u16(&msg, gflen)) 470 return 0; 471 472 if (!cursor_push(&msg, globalfeatures, gflen)) 473 return 0; 474 475 if (!cursor_push_u16(&msg, flen)) 476 return 0; 477 478 if (!cursor_push(&msg, features, flen)) 479 return 0; 480 481 if (!cursor_push_tlvs(&msg, tlvs, num_tlvs)) 482 return 0; 483 484 *outlen = msg.p - msg.start; 485 486 return 1; 487 } 488 489 unsigned char* EXPORT lnsocket_msgbuf(struct lnsocket *ln) 490 { 491 return ln->msgbuf.start; 492 } 493 494 int EXPORT lnsocket_encrypt(struct lnsocket *ln, const u8 *msg, unsigned short msglen) 495 { 496 ssize_t outcap; 497 size_t outlen; 498 499 // this is just temporary so we don't need to move the memory cursor 500 reset_cursor(&ln->msgbuf); 501 502 u8 *out = ln->msgbuf.start; 503 outcap = ln->msgbuf.end - ln->msgbuf.start; 504 505 #ifndef __EMSCRIPTEN__ 506 if (!ln->socket) 507 return note_error(&ln->errs, "not connected"); 508 #endif 509 510 if (outcap <= 0) 511 return note_error(&ln->errs, "out of memory"); 512 513 if (!cryptomsg_encrypt_msg(&ln->crypto_state, msg, msglen, out, &outlen, (size_t)outcap)) 514 return note_error(&ln->errs, "encrypt message failed, out of memory"); 515 516 return outlen; 517 } 518 519 int lnsocket_write(struct lnsocket *ln, const u8 *msg, unsigned short msglen) 520 { 521 ssize_t writelen, outlen; 522 u8 *out = ln->msgbuf.start; 523 524 if (!(outlen = lnsocket_encrypt(ln, msg, msglen))) 525 return 0; 526 527 if ((writelen = write(ln->socket, out, outlen)) != outlen) 528 return note_error(&ln->errs, 529 "write failed. wrote %ld bytes, expected %ld %s", 530 writelen, outlen, 531 writelen < 0 ? strerror(errno) : ""); 532 533 return 1; 534 } 535 536 struct lnsocket *lnsocket_create() 537 { 538 struct cursor mem; 539 int memory = MSGBUF_MEM + ERROR_MEM + sizeof(struct lnsocket); 540 541 void *arena = malloc(memory); 542 543 if (!arena) 544 return NULL; 545 546 make_cursor(arena, arena + memory, &mem); 547 struct lnsocket *lnsocket = cursor_alloc(&mem, sizeof(*lnsocket)); 548 549 if (!lnsocket) 550 return NULL; 551 552 lnsocket->secp = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | 553 SECP256K1_CONTEXT_SIGN); 554 555 if (!cursor_slice(&mem, &lnsocket->msgbuf, MSGBUF_MEM)) 556 return NULL; 557 558 if (!cursor_slice(&mem, &lnsocket->errs.cur, ERROR_MEM)) 559 return NULL; 560 561 lnsocket->errs.enabled = 1; 562 563 lnsocket->mem = mem; 564 return lnsocket; 565 } 566 567 void lnsocket_destroy(struct lnsocket *lnsocket) 568 { 569 if (!lnsocket) 570 return; 571 572 secp256k1_context_destroy(lnsocket->secp); 573 free(lnsocket->mem.start); 574 } 575 576 int is_zero(void *vp, int size) 577 { 578 u8 *p = (u8*)vp; 579 const u8 *start = p; 580 581 for (; p < start+size; p++) { 582 if (*p != 0) 583 return 0; 584 } 585 return 1; 586 } 587 588 static int io_fd_block(int fd, int block) 589 { 590 int flags = fcntl(fd, F_GETFL); 591 592 if (flags == -1) 593 return 0; 594 595 if (block) 596 flags &= ~O_NONBLOCK; 597 else 598 flags |= O_NONBLOCK; 599 600 return fcntl(fd, F_SETFL, flags) != -1; 601 } 602 603 const char *parse_port(char *host) 604 { 605 int i, len; 606 607 len = strlen(host); 608 609 for (i = 0; i < len; i++) { 610 if (host[i] == ':') { 611 host[i] = 0; 612 return &host[i+1]; 613 } 614 } 615 616 617 return NULL; 618 } 619 620 int lnsocket_handshake_with_tor(struct lnsocket *ln, const char *host, const char *port) 621 { 622 // make the init request 623 u8 request[SIZE_OF_REQUEST] = { SOCKS_V5, 0x01, SOCKS_NOAUTH }; 624 if (write(ln->socket, request, sizeof(request)) != sizeof(request)) { 625 return note_error(&ln->errs, "Failed writing request: %s", 626 strerror(errno)); 627 } 628 629 // read proxy response 630 u8 hdr[2]; 631 if (!read(ln->socket, hdr, 2)) 632 return note_error(&ln->errs,"Failed reading header"); 633 634 if (hdr[1] == SOCKS_ERROR) 635 return note_error(&ln->errs,"Failed proxy authentication required"); 636 637 // make the V5 request 638 size_t hlen = strlen(host); 639 u16 uport = htons(atoi(port)); 640 641 u8 buffer[SOCK_REQ_V5_HEADER_LEN + hlen]; 642 buffer[0] = SOCKS_V5; 643 buffer[1] = SOCKS_CONNECT; 644 buffer[2] = 0; 645 buffer[3] = SOCKS_DOMAIN; 646 buffer[4] = hlen; 647 memcpy(buffer + SOCK_REQ_V5_LEN, host, hlen); 648 memcpy(buffer + SOCK_REQ_V5_LEN + hlen, &uport, sizeof(uport)); 649 650 if (write(ln->socket, buffer, sizeof(buffer)) != sizeof(buffer)) { 651 return note_error(&ln->errs,"Failed writing buffer: %s", strerror(errno)); 652 } 653 654 // read completion 655 u8 inbuffer[SIZE_OF_IPV4_RESPONSE + SIZE_OF_RESPONSE]; 656 if (!read_all(ln->socket, inbuffer, sizeof(inbuffer))) 657 return note_error(&ln->errs,"Failed reading inbuffer: %s", strerror(errno)); 658 if (inbuffer[1] != '\0') 659 return note_error(&ln->errs,"Failed proxy connection: %d", inbuffer[1]); 660 if (inbuffer[3] == SOCKS_TYP_IPV6) 661 return note_error(&ln->errs,"Failed ipv6 not supported yet"); 662 if (inbuffer[3] != SOCKS_TYP_IPV4) 663 return note_error(&ln->errs,"Failed only ipv4 supported"); 664 665 return 1; 666 } 667 668 int lnsocket_connect_with(struct lnsocket *ln, const char *node_id, const char *host, const char *tor_proxy, int timeout_ms) 669 { 670 int ret; 671 struct addrinfo *addrs = NULL; 672 struct pubkey their_id; 673 struct node_id their_node_id; 674 struct timeval timeout = {0}; 675 char onlyhost[strlen(host)+1]; 676 strncpy(onlyhost, host, sizeof(onlyhost)); 677 fd_set set; 678 679 timeout.tv_sec = timeout_ms / 1000; 680 timeout.tv_usec = (timeout_ms % 1000) * 1000; 681 682 FD_ZERO(&set); /* clear the set */ 683 684 if (is_zero(&ln->key, sizeof(ln->key))) 685 return note_error(&ln->errs, "key not initialized, use lnsocket_set_key() or lnsocket_genkey()"); 686 687 // convert node_id string to bytes 688 if (!parse_node_id(node_id, &their_node_id)) 689 return note_error(&ln->errs, "failed to parse node id"); 690 691 // encode node_id bytes to secp pubkey 692 if (!pubkey_from_node_id(ln->secp, &their_id, &their_node_id)) 693 return note_error(&ln->errs, "failed to convert node_id to pubkey"); 694 695 // create our network socket for comms 696 if (!(ln->socket = socket(AF_INET, SOCK_STREAM, 0))) 697 return note_error(&ln->errs, "creating socket failed"); 698 699 FD_SET(ln->socket, &set); /* add our file descriptor to the set */ 700 701 if (!io_fd_block(ln->socket, 0)) 702 return note_error(&ln->errs, "failed setting socket to non-blocking"); 703 704 if (tor_proxy != NULL) { 705 // connect to the tor proxy! 706 char proxyhost[strlen(tor_proxy)+1]; 707 strncpy(proxyhost, tor_proxy, sizeof(proxyhost)); 708 const char *port = parse_port(proxyhost); 709 struct sockaddr_in proxy_server; 710 proxy_server.sin_family = AF_INET; 711 proxy_server.sin_addr.s_addr = inet_addr(proxyhost); 712 proxy_server.sin_port = htons(port ? atoi(port) : 9050); 713 if (!connect(ln->socket, (struct sockaddr*) &proxy_server, sizeof(proxy_server))) 714 return note_error(&ln->errs, "Failed to connect"); 715 } else { 716 // parse ip into addrinfo 717 const char *port = parse_port(onlyhost); 718 if ((ret = getaddrinfo(onlyhost, port ? port : "9735", NULL, &addrs)) || !addrs) 719 return note_error(&ln->errs, "%s", gai_strerror(ret)); 720 // connect to the node! 721 connect(ln->socket, addrs->ai_addr, addrs->ai_addrlen); 722 } 723 724 if (!io_fd_block(ln->socket, 1)) 725 return note_error(&ln->errs, "failed setting socket to blocking"); 726 727 ret = select(ln->socket + 1, NULL, &set, NULL, &timeout); 728 if (ret == -1) { 729 return note_error(&ln->errs, "select error"); 730 } else if (ret == 0) { 731 return note_error(&ln->errs, "connection timeout"); 732 } 733 734 if (tor_proxy != NULL) { 735 // connect to the node through tor proxy 736 const char *port = parse_port(onlyhost); 737 if (!lnsocket_handshake_with_tor(ln, onlyhost, port ? port : "9735")) 738 return note_error(&ln->errs, "failed handshake with tor proxy"); 739 } 740 741 // prepare some data for ACT1 742 new_handshake(ln->secp, &ln->handshake, &their_id); 743 744 ln->handshake.side = INITIATOR; 745 ln->handshake.their_id = their_id; 746 747 // let's do this! 748 return act_one_initiator(ln); 749 } 750 751 int lnsocket_connect(struct lnsocket *ln, const char *node_id, const char *host) 752 { 753 return lnsocket_connect_with(ln, node_id, host, NULL, DEFAULT_TIMEOUT); 754 } 755 756 int lnsocket_connect_tor(struct lnsocket *ln, const char *node_id, const char *host, const char *tor_proxy) 757 { 758 return lnsocket_connect_with(ln, node_id, host, tor_proxy, DEFAULT_TOR_TIMEOUT); 759 } 760 761 int lnsocket_fd(struct lnsocket *ln, int *fd) 762 { 763 *fd = ln->socket; 764 return 1; 765 } 766 767 void * lnsocket_secp(struct lnsocket *ln) 768 { 769 return ln->secp; 770 } 771 772 void lnsocket_genkey(struct lnsocket *ln) 773 { 774 ln->key = generate_key(ln->secp); 775 } 776 777 int lnsocket_setkey(struct lnsocket *ln, const unsigned char key[32]) 778 { 779 struct keypair k; 780 memcpy(k.priv.secret.data, key, 32); 781 782 if (!secp256k1_ec_pubkey_create(ln->secp, &k.pub.pubkey, k.priv.secret.data)) 783 return 0; 784 785 ln->key = k; 786 787 return 1; 788 } 789 790 void lnsocket_print_errors(struct lnsocket *ln) 791 { 792 print_error_backtrace(&ln->errs); 793 } 794 795