Skip to content

Commit

Permalink
chore: use EXPERIMENTAL for BOLT7 DNS ElementsProject#911
Browse files Browse the repository at this point in the history
  • Loading branch information
m-schmoock committed Oct 27, 2021
1 parent 4026214 commit d945102
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions common/json_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,12 @@ void json_add_address(struct json_stream *response, const char *fieldname,
json_add_string(response, "type", "torv3");
json_add_string(response, "address", fmt_wireaddr_without_port(tmpctx, addr));
json_add_num(response, "port", addr->port);
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
} else if (addr->type == ADDR_TYPE_DNS) {
json_add_string(response, "type", "dns");
json_add_string(response, "address", fmt_wireaddr_without_port(tmpctx, addr));
json_add_num(response, "port", addr->port);
#endif
} else if (addr->type == ADDR_TYPE_WEBSOCKET) {
json_add_string(response, "type", "websocket");
json_add_num(response, "port", addr->port);
Expand Down
4 changes: 4 additions & 0 deletions common/test/run-ip_port_parsing.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ int main(int argc, char *argv[])

common_setup(argv[0]);

#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
/* Check IP/TOR/DNS parser */
assert(is_ipaddr("192.168.1.2"));
assert(!is_ipaddr("foo.bar.1.2"));
Expand All @@ -136,6 +137,7 @@ int main(int argc, char *argv[])
assert(!is_dnsaddr("invalid.-example.com"));
assert(!is_dnsaddr("invalid.example-.com"));
assert(!is_dnsaddr("invalid..example.com"));
#endif

/* Grossly invalid. */
assert(!separate_address_and_port(tmpctx, "[", &ip, &port));
Expand Down Expand Up @@ -168,6 +170,7 @@ int main(int argc, char *argv[])
assert(streq(ip, "192.168.2.255"));
assert(port == 0);

#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
/* DNS types */
assert(separate_address_and_port(tmpctx, "example.com:42", &ip, &port));
assert(streq(ip, "example.com"));
Expand All @@ -180,6 +183,7 @@ int main(int argc, char *argv[])
assert(streq(ip, "sub.example.com"));
assert(port == 123);
port = 0;
#endif

// unusual but possibly valid case
assert(separate_address_and_port(tmpctx, "[::1]", &ip, &port));
Expand Down
16 changes: 15 additions & 1 deletion common/wireaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ bool fromwire_wireaddr(const u8 **cursor, size_t *max, struct wireaddr *addr)
case ADDR_TYPE_TOR_V3:
addr->addrlen = TOR_V3_ADDRLEN;
break;
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
case ADDR_TYPE_DNS:
addr->addrlen = fromwire_u8(cursor, max);
memset(&addr->addr, 0, sizeof(addr->addr));
addr->addr[addr->addrlen] = 0;
break;
#endif
case ADDR_TYPE_WEBSOCKET:
addr->addrlen = 0;
break;
Expand All @@ -57,8 +59,10 @@ bool fromwire_wireaddr(const u8 **cursor, size_t *max, struct wireaddr *addr)
void towire_wireaddr(u8 **pptr, const struct wireaddr *addr)
{
towire_u8(pptr, addr->type);
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
if (addr->type == ADDR_TYPE_DNS)
towire_u8(pptr, addr->addrlen);
#endif
towire(pptr, addr->addr, addr->addrlen);
towire_u16(pptr, addr->port);
}
Expand Down Expand Up @@ -218,8 +222,10 @@ bool wireaddr_is_wildcard(const struct wireaddr *addr)
return memeqzero(addr->addr, addr->addrlen);
case ADDR_TYPE_TOR_V2:
case ADDR_TYPE_TOR_V3:
case ADDR_TYPE_DNS:
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
case ADDR_TYPE_WEBSOCKET:
#endif
case ADDR_TYPE_DNS:
return false;
}
abort();
Expand Down Expand Up @@ -267,8 +273,10 @@ char *fmt_wireaddr_without_port(const tal_t * ctx, const struct wireaddr *a)
case ADDR_TYPE_TOR_V3:
return tal_fmt(ctx, "%s.onion",
b32_encode(tmpctx, a->addr, a->addrlen));
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
case ADDR_TYPE_DNS:
return tal_fmt(ctx, "%s", a->addr);
#endif
case ADDR_TYPE_WEBSOCKET:
return tal_strdup(ctx, "websocket");
}
Expand Down Expand Up @@ -332,6 +340,7 @@ bool separate_address_and_port(const tal_t *ctx, const char *arg,
return true;
}

#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
bool is_ipaddr(const char *arg)
{
struct in_addr v4;
Expand Down Expand Up @@ -406,6 +415,7 @@ bool is_dnsaddr(const char *arg)
}
return true;
}
#endif

struct wireaddr *
wireaddr_from_hostname(const tal_t *ctx,
Expand Down Expand Up @@ -780,7 +790,9 @@ struct addrinfo *wireaddr_to_addrinfo(const tal_t *ctx,
ai->ai_addrlen = sizeof(*sin6);
ai->ai_addr = (struct sockaddr *)sin6;
return ai;
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
case ADDR_TYPE_DNS:
#endif
case ADDR_TYPE_TOR_V2:
case ADDR_TYPE_TOR_V3:
case ADDR_TYPE_WEBSOCKET:
Expand Down Expand Up @@ -835,8 +847,10 @@ bool all_tor_addresses(const struct wireaddr_internal *wireaddr)
switch (wireaddr[i].u.wireaddr.type) {
case ADDR_TYPE_IPV4:
case ADDR_TYPE_IPV6:
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
case ADDR_TYPE_DNS:
return false;
#endif
case ADDR_TYPE_TOR_V2:
case ADDR_TYPE_TOR_V3:
case ADDR_TYPE_WEBSOCKET:
Expand Down
8 changes: 8 additions & 0 deletions common/wireaddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ struct sockaddr_un;

#define TOR_V2_ADDRLEN 10
#define TOR_V3_ADDRLEN 35
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
#define DNS_ADDRLEN 255
#define LARGEST_ADDRLEN DNS_ADDRLEN
#else
#define LARGEST_ADDRLEN TOR_V3_ADDRLEN
#endif
#define TOR_V3_BLOBLEN 64
#define STATIC_TOR_MAGIC_STRING "gen-default-toraddress"

Expand All @@ -54,7 +58,9 @@ enum wire_addr_type {
ADDR_TYPE_IPV6 = 2,
ADDR_TYPE_TOR_V2 = 3,
ADDR_TYPE_TOR_V3 = 4,
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
ADDR_TYPE_DNS = 5,
#endif
ADDR_TYPE_WEBSOCKET = 6
};

Expand Down Expand Up @@ -152,11 +158,13 @@ struct wireaddr_internal {
bool separate_address_and_port(const tal_t *ctx, const char *arg,
char **addr, u16 *port);

#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
bool is_ipaddr(const char *arg);

bool is_toraddr(const char *arg);

bool is_dnsaddr(const char *arg);
#endif

bool parse_wireaddr_internal(const char *arg, struct wireaddr_internal *addr,
u16 port, bool wildcard_ok, bool dns_ok,
Expand Down
10 changes: 10 additions & 0 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,8 +874,10 @@ static struct io_plan *conn_init(struct io_conn *conn,
"Can't connect to forproxy address");
break;
case ADDR_INTERNAL_WIREADDR:
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
/* DNS should have been resolved before */
assert(addr->u.wireaddr.type != ADDR_TYPE_DNS);
#endif
/* If it was a Tor address, we wouldn't be here. */
assert(!is_toraddr((char*)addr->u.wireaddr.addr));
ai = wireaddr_to_addrinfo(tmpctx, &addr->u.wireaddr);
Expand Down Expand Up @@ -928,11 +930,13 @@ static void try_connect_one_addr(struct connecting *connect)
bool use_proxy = connect->daemon->always_use_proxy;
const struct wireaddr_internal *addr = &connect->addrs[connect->addrnum];
struct io_conn *conn;
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
struct addrinfo hints, *ais, *aii;
struct wireaddr_internal addrhint;
int gai_err;
struct sockaddr_in *sa4;
struct sockaddr_in6 *sa6;
#endif

/* In case we fail without a connection, make destroy_io_conn happy */
connect->conn = NULL;
Expand Down Expand Up @@ -980,6 +984,7 @@ static void try_connect_one_addr(struct connecting *connect)
case ADDR_TYPE_IPV6:
af = AF_INET6;
break;
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
case ADDR_TYPE_DNS:
/* Resolve with getaddrinfo */
memset(&hints, 0, sizeof(hints));
Expand Down Expand Up @@ -1021,6 +1026,7 @@ static void try_connect_one_addr(struct connecting *connect)
connect->addrnum++;
try_connect_one_addr(connect);
return;
#endif
case ADDR_TYPE_WEBSOCKET:
af = -1;
break;
Expand Down Expand Up @@ -1205,7 +1211,9 @@ static bool handle_wireaddr_listen(struct daemon *daemon,
case ADDR_TYPE_WEBSOCKET:
case ADDR_TYPE_TOR_V2:
case ADDR_TYPE_TOR_V3:
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
case ADDR_TYPE_DNS:
#endif
break;
}
status_failed(STATUS_FAIL_INTERNAL_ERROR,
Expand Down Expand Up @@ -1661,8 +1669,10 @@ static void add_seed_addrs(struct wireaddr_internal **addrs,
NULL, broken_reply, NULL);
if (new_addrs) {
for (size_t j = 0; j < tal_count(new_addrs); j++) {
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
if (new_addrs[j].type == ADDR_TYPE_DNS)
continue;
#endif
struct wireaddr_internal a;
a.itype = ADDR_INTERNAL_WIREADDR;
a.u.wireaddr = new_addrs[j];
Expand Down
2 changes: 2 additions & 0 deletions connectd/netaddress.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ bool guess_address(struct wireaddr *addr)
}
case ADDR_TYPE_TOR_V2:
case ADDR_TYPE_TOR_V3:
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
case ADDR_TYPE_DNS:
#endif
case ADDR_TYPE_WEBSOCKET:
status_broken("Cannot guess address type %u", addr->type);
break;
Expand Down
2 changes: 2 additions & 0 deletions devtools/gossipwith.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,12 @@ int main(int argc, char *argv[])
case ADDR_TYPE_IPV6:
af = AF_INET6;
break;
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
case ADDR_TYPE_DNS:
/* DNS names should have been resolved
* by parse_wireaddr_internal */
assert(false);
#endif
}
ai = wireaddr_to_addrinfo(tmpctx, &addr.u.wireaddr);
}
Expand Down
6 changes: 6 additions & 0 deletions lightningd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,21 @@ static char *opt_add_addr_withtype(const char *arg,
if (!separate_address_and_port(tmpctx, arg, &address, &port))
return tal_fmt(NULL, "Unable to parse address:port '%s'", arg);

#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
if (is_ipaddr(address) || is_toraddr(address) || ala != ADDR_ANNOUNCE) {
#endif
if (!parse_wireaddr_internal(arg, &wi, ld->portnum,
wildcard_ok, dns_ok, false,
deprecated_apis, &err_msg)) {
return tal_fmt(NULL, "Unable to parse address '%s': %s", arg, err_msg);
}
tal_arr_expand(&ld->proposed_listen_announce, ala);
tal_arr_expand(&ld->proposed_wireaddr, wi);
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
}
#endif

#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
/* Add ADDR_TYPE_DNS to announce DNS hostnames */
if (is_dnsaddr(address) && ala & ADDR_ANNOUNCE) {
memset(&wi, 0, sizeof(wi));
Expand All @@ -220,6 +225,7 @@ static char *opt_add_addr_withtype(const char *arg,
tal_arr_expand(&ld->proposed_listen_announce, ADDR_ANNOUNCE);
tal_arr_expand(&ld->proposed_wireaddr, wi);
}
#endif

return NULL;

Expand Down
19 changes: 18 additions & 1 deletion tests/test_gossip.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from fixtures import TEST_NETWORK
from pyln.client import RpcError, Millisatoshi
from utils import (
DEVELOPER, wait_for, TIMEOUT, only_one, sync_blockheight, expected_node_features, COMPAT
DEVELOPER, wait_for, TIMEOUT, only_one, sync_blockheight,
expected_node_features, COMPAT, EXPERIMENTAL_FEATURES
)

import json
Expand Down Expand Up @@ -128,6 +129,13 @@ def test_announce_address(node_factory, bitcoind):
'::'],
'log-level': 'io',
'dev-allow-localhost': None}
if not EXPERIMENTAL_FEATURES: # BOLT7 DNS RFC #911
opts = {'disable-dns': None, 'announce-addr':
['4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad.onion',
'1.2.3.4:1234',
'::'],
'log-level': 'io',
'dev-allow-localhost': None}
l1, l2 = node_factory.get_nodes(2, opts=[opts, {}])

l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
Expand All @@ -137,6 +145,14 @@ def test_announce_address(node_factory, bitcoind):
l1.wait_channel_active(scid)
l2.wait_channel_active(scid)

if not EXPERIMENTAL_FEATURES: # BOLT7 DNS RFC #911
l1.daemon.wait_for_log(r"\[OUT\] 0101.*47"
"010102030404d2"
"017f000001...."
"02000000000000000000000000000000002607"
"04e00533f3e8f2aedaa8969b3d0fa03a96e857bbb28064dca5e147e934244b9ba50230032607")
return

# We should see it send node announce with all addresses (257 = 0x0101)
# Note: local ephemeral port is masked out.
# Note: Since we `disable-dns` it should not announce a resolved IPv4
Expand Down Expand Up @@ -164,6 +180,7 @@ def test_announce_address(node_factory, bitcoind):
assert addresses_dns[1]['port'] == 1236


@unittest.skipIf(not EXPERIMENTAL_FEATURES, "BOLT7 DNS RFC #911")
@pytest.mark.developer("gossip without DEVELOPER=1 is slow")
def test_announce_and_connect_via_dns(node_factory, bitcoind):
""" Test that DNS annoucements propagate and can be used when connecting.
Expand Down

0 comments on commit d945102

Please sign in to comment.