2017-10-23 06:17:38 +02:00
|
|
|
#ifndef LIGHTNING_COMMON_WIREADDR_H
|
|
|
|
#define LIGHTNING_COMMON_WIREADDR_H
|
|
|
|
#include "config.h"
|
|
|
|
#include <ccan/short_types/short_types.h>
|
2017-12-18 11:10:43 +01:00
|
|
|
#include <ccan/tal/tal.h>
|
2018-05-09 02:04:02 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2017-10-23 06:17:38 +02:00
|
|
|
|
2018-05-07 06:29:21 +02:00
|
|
|
struct in6_addr;
|
|
|
|
struct in_addr;
|
|
|
|
struct sockaddr_in6;
|
|
|
|
struct sockaddr_in;
|
2018-05-07 06:29:21 +02:00
|
|
|
struct sockaddr_un;
|
2018-05-07 06:29:21 +02:00
|
|
|
|
2018-05-10 01:18:24 +02:00
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* The default TCP port is 9735. This corresponds to hexadecimal
|
2018-06-17 12:13:44 +02:00
|
|
|
* `0x2607`: the Unicode code point for LIGHTNING.
|
2018-05-10 01:18:24 +02:00
|
|
|
*/
|
|
|
|
#define DEFAULT_PORT 9735
|
|
|
|
|
|
|
|
|
2017-10-23 06:17:38 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* The following `address descriptor` types are defined:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* * `1`: ipv4; data = `[4:ipv4_addr][2:port]` (length 6)
|
|
|
|
* * `2`: ipv6; data = `[16:ipv6_addr][2:port]` (length 18)
|
|
|
|
* * `3`: Tor v2 onion service; data = `[10:onion_addr][2:port]` (length 12)
|
|
|
|
* * version 2 onion service addresses; Encodes an 80-bit, truncated `SHA-1` hash
|
|
|
|
* of a 1024-bit `RSA` public key for the onion service (a.k.a. Tor
|
|
|
|
* hidden service).
|
|
|
|
* * `4`: Tor v3 onion service; data = `[35:onion_addr][2:port]` (length 37)
|
|
|
|
* * version 3 ([prop224](https://gitweb.torproject.org/torspec.git/tree/proposals/224-rend-spec-ng.txt))
|
|
|
|
* onion service addresses; Encodes:
|
|
|
|
* `[32:32_byte_ed25519_pubkey] || [2:checksum] || [1:version]`,
|
2017-10-23 06:17:38 +02:00
|
|
|
* where `checksum = sha3(".onion checksum" | pubkey || version)[:2]`
|
|
|
|
*/
|
|
|
|
|
2021-10-15 07:48:44 +02:00
|
|
|
/* BOLT-websockets #7:
|
2021-10-25 22:08:11 +02:00
|
|
|
* * `6`: WebSocket port; data = `[2:port]` (length 2)
|
2021-10-15 07:48:44 +02:00
|
|
|
*/
|
|
|
|
|
2018-05-10 01:18:24 +02:00
|
|
|
#define TOR_V2_ADDRLEN 10
|
|
|
|
#define TOR_V3_ADDRLEN 35
|
|
|
|
#define LARGEST_ADDRLEN TOR_V3_ADDRLEN
|
2019-11-15 09:44:22 +01:00
|
|
|
#define TOR_V3_BLOBLEN 64
|
|
|
|
#define STATIC_TOR_MAGIC_STRING "gen-default-toraddress"
|
2018-05-10 01:18:19 +02:00
|
|
|
|
2017-10-23 06:17:38 +02:00
|
|
|
enum wire_addr_type {
|
|
|
|
ADDR_TYPE_IPV4 = 1,
|
|
|
|
ADDR_TYPE_IPV6 = 2,
|
2018-05-10 01:18:19 +02:00
|
|
|
ADDR_TYPE_TOR_V2 = 3,
|
2021-10-15 07:48:44 +02:00
|
|
|
ADDR_TYPE_TOR_V3 = 4,
|
2021-10-25 22:08:11 +02:00
|
|
|
ADDR_TYPE_WEBSOCKET = 6,
|
2017-10-23 06:17:38 +02:00
|
|
|
};
|
|
|
|
|
2018-05-10 01:18:19 +02:00
|
|
|
/* Structure now fit for tor support */
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr {
|
|
|
|
enum wire_addr_type type;
|
|
|
|
u8 addrlen;
|
2018-05-10 01:18:24 +02:00
|
|
|
u8 addr[LARGEST_ADDRLEN];
|
2017-10-23 06:17:38 +02:00
|
|
|
u16 port;
|
|
|
|
};
|
|
|
|
|
2018-09-24 03:41:14 +02:00
|
|
|
bool wireaddr_eq(const struct wireaddr *a, const struct wireaddr *b);
|
|
|
|
|
2018-05-07 06:28:12 +02:00
|
|
|
/* We use wireaddr to tell gossipd both what to listen on, and what to
|
|
|
|
* announce */
|
|
|
|
enum addr_listen_announce {
|
|
|
|
ADDR_LISTEN = (1 << 0),
|
|
|
|
ADDR_ANNOUNCE = (1 << 1),
|
|
|
|
ADDR_LISTEN_AND_ANNOUNCE = ADDR_LISTEN|ADDR_ANNOUNCE
|
|
|
|
};
|
|
|
|
|
2017-10-23 06:17:38 +02:00
|
|
|
void towire_wireaddr(u8 **pptr, const struct wireaddr *addr);
|
|
|
|
bool fromwire_wireaddr(const u8 **cursor, size_t *max, struct wireaddr *addr);
|
2017-12-18 11:10:43 +01:00
|
|
|
|
2018-05-07 06:28:12 +02:00
|
|
|
enum addr_listen_announce fromwire_addr_listen_announce(const u8 **cursor,
|
|
|
|
size_t *max);
|
|
|
|
void towire_addr_listen_announce(u8 **pptr, enum addr_listen_announce ala);
|
|
|
|
|
2018-05-10 04:55:15 +02:00
|
|
|
/* If no_dns is non-NULL, we will set it to true and return false if
|
|
|
|
* we wanted to do a DNS lookup. */
|
2018-05-10 01:18:24 +02:00
|
|
|
bool parse_wireaddr(const char *arg, struct wireaddr *addr, u16 port,
|
2018-05-10 04:55:15 +02:00
|
|
|
bool *no_dns, const char **err_msg);
|
2017-12-18 11:10:43 +01:00
|
|
|
|
2017-12-18 11:25:16 +01:00
|
|
|
char *fmt_wireaddr(const tal_t *ctx, const struct wireaddr *a);
|
2018-05-10 01:18:19 +02:00
|
|
|
char *fmt_wireaddr_without_port(const tal_t *ctx, const struct wireaddr *a);
|
2017-12-18 11:25:16 +01:00
|
|
|
|
2019-09-21 16:48:03 +02:00
|
|
|
/* If no_dns is non-NULL, we will set it to true and return NULL if
|
2018-05-10 04:55:15 +02:00
|
|
|
* we wanted to do a DNS lookup. */
|
2019-09-21 16:48:03 +02:00
|
|
|
struct wireaddr *
|
|
|
|
wireaddr_from_hostname(const tal_t *ctx,
|
2020-02-04 01:03:22 +01:00
|
|
|
const char *hostname,
|
2019-09-21 16:48:03 +02:00
|
|
|
const u16 port, bool *no_dns,
|
|
|
|
struct sockaddr *broken_reply,
|
|
|
|
const char **err_msg);
|
2018-04-23 16:34:34 +02:00
|
|
|
|
2018-05-07 06:29:21 +02:00
|
|
|
void wireaddr_from_ipv4(struct wireaddr *addr,
|
|
|
|
const struct in_addr *ip4,
|
|
|
|
const u16 port);
|
|
|
|
void wireaddr_from_ipv6(struct wireaddr *addr,
|
|
|
|
const struct in6_addr *ip6,
|
|
|
|
const u16 port);
|
2021-10-15 07:48:44 +02:00
|
|
|
void wireaddr_from_websocket(struct wireaddr *addr, const u16 port);
|
2018-05-07 06:29:21 +02:00
|
|
|
bool wireaddr_to_ipv4(const struct wireaddr *addr, struct sockaddr_in *s4);
|
|
|
|
bool wireaddr_to_ipv6(const struct wireaddr *addr, struct sockaddr_in6 *s6);
|
2021-10-15 07:48:44 +02:00
|
|
|
bool wireaddr_to_websocket(const struct wireaddr *addr, u16 *port);
|
2018-05-07 06:29:21 +02:00
|
|
|
|
2018-05-07 06:29:22 +02:00
|
|
|
bool wireaddr_is_wildcard(const struct wireaddr *addr);
|
|
|
|
|
2018-05-07 06:29:21 +02:00
|
|
|
enum wireaddr_internal_type {
|
|
|
|
ADDR_INTERNAL_SOCKNAME,
|
2018-05-07 06:29:22 +02:00
|
|
|
ADDR_INTERNAL_ALLPROTO,
|
2018-05-10 01:18:24 +02:00
|
|
|
ADDR_INTERNAL_AUTOTOR,
|
2018-05-10 05:44:23 +02:00
|
|
|
ADDR_INTERNAL_FORPROXY,
|
2018-05-07 06:29:21 +02:00
|
|
|
ADDR_INTERNAL_WIREADDR,
|
2019-11-15 09:44:22 +01:00
|
|
|
ADDR_INTERNAL_STATICTOR,
|
2018-05-07 06:29:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* For internal use, where we can also supply a local socket, wildcard. */
|
|
|
|
struct wireaddr_internal {
|
|
|
|
enum wireaddr_internal_type itype;
|
|
|
|
union {
|
2018-05-09 02:04:02 +02:00
|
|
|
/* ADDR_INTERNAL_WIREADDR */
|
2018-05-07 06:29:21 +02:00
|
|
|
struct wireaddr wireaddr;
|
2018-05-07 06:29:22 +02:00
|
|
|
/* ADDR_INTERNAL_ALLPROTO */
|
|
|
|
u16 port;
|
2019-11-15 09:44:22 +01:00
|
|
|
/* ADDR_INTERNAL_AUTOTOR
|
|
|
|
* ADDR_INTERNAL_STATICTOR */
|
|
|
|
struct torservice {
|
2021-06-16 07:45:55 +02:00
|
|
|
/* Where to connect to Tor proxy */
|
2019-11-15 09:44:22 +01:00
|
|
|
struct wireaddr address;
|
2021-06-16 07:45:55 +02:00
|
|
|
/* Tor port to use */
|
2019-11-15 09:44:22 +01:00
|
|
|
u16 port;
|
2021-06-16 07:45:55 +02:00
|
|
|
/* Blob to use to create tor service */
|
2019-11-15 09:44:22 +01:00
|
|
|
u8 blob[TOR_V3_BLOBLEN + 1];
|
|
|
|
} torservice;
|
2018-05-10 05:44:23 +02:00
|
|
|
/* ADDR_INTERNAL_FORPROXY */
|
|
|
|
struct unresolved {
|
|
|
|
char name[256];
|
|
|
|
u16 port;
|
|
|
|
} unresolved;
|
2018-05-09 02:04:02 +02:00
|
|
|
/* ADDR_INTERNAL_SOCKNAME */
|
|
|
|
char sockname[sizeof(((struct sockaddr_un *)0)->sun_path)];
|
2018-05-07 06:29:21 +02:00
|
|
|
} u;
|
|
|
|
};
|
2018-05-10 01:18:24 +02:00
|
|
|
bool parse_wireaddr_internal(const char *arg, struct wireaddr_internal *addr,
|
|
|
|
u16 port, bool wildcard_ok, bool dns_ok,
|
2021-05-20 17:45:27 +02:00
|
|
|
bool unresolved_ok, bool allow_deprecated,
|
|
|
|
const char **err_msg);
|
2018-05-07 06:29:21 +02:00
|
|
|
|
|
|
|
void towire_wireaddr_internal(u8 **pptr,
|
|
|
|
const struct wireaddr_internal *addr);
|
|
|
|
bool fromwire_wireaddr_internal(const u8 **cursor, size_t *max,
|
|
|
|
struct wireaddr_internal *addr);
|
|
|
|
char *fmt_wireaddr_internal(const tal_t *ctx,
|
|
|
|
const struct wireaddr_internal *a);
|
|
|
|
|
2018-05-10 05:44:23 +02:00
|
|
|
bool wireaddr_from_unresolved(struct wireaddr_internal *addr,
|
|
|
|
const char *name, u16 port);
|
|
|
|
|
2018-05-07 06:29:21 +02:00
|
|
|
void wireaddr_from_sockname(struct wireaddr_internal *addr,
|
|
|
|
const char *sockname);
|
|
|
|
bool wireaddr_to_sockname(const struct wireaddr_internal *addr,
|
|
|
|
struct sockaddr_un *sun);
|
2018-05-10 01:18:24 +02:00
|
|
|
|
|
|
|
struct addrinfo *wireaddr_to_addrinfo(const tal_t *ctx,
|
|
|
|
const struct wireaddr *wireaddr);
|
|
|
|
struct addrinfo *wireaddr_internal_to_addrinfo(const tal_t *ctx,
|
|
|
|
const struct wireaddr_internal *wireaddr);
|
2018-05-10 01:18:24 +02:00
|
|
|
|
|
|
|
bool all_tor_addresses(const struct wireaddr_internal *wireaddr);
|
|
|
|
|
2021-06-14 23:07:38 +02:00
|
|
|
/* Decode an array of serialized addresses from node_announcement */
|
|
|
|
struct wireaddr *fromwire_wireaddr_array(const tal_t *ctx, const u8 *ser);
|
|
|
|
|
2017-10-23 06:17:38 +02:00
|
|
|
#endif /* LIGHTNING_COMMON_WIREADDR_H */
|