From b2c762969cb0de5e6c45ef902ce1e27db64d8d16 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 10 Nov 2021 10:57:42 +1030 Subject: [PATCH] wireaddr: clean up tor parsing. blob[] is really a string from the commandline; leave it as a char. And parsing is much simpler than this code makes it seem! Signed-off-by: Rusty Russell --- common/base64.c | 2 +- common/base64.h | 2 +- common/wireaddr.c | 64 ++++++++++++++++---------------------- common/wireaddr.h | 4 +-- connectd/connectd.c | 2 +- connectd/tor_autoservice.c | 4 +-- connectd/tor_autoservice.h | 2 +- 7 files changed, 34 insertions(+), 46 deletions(-) diff --git a/common/base64.c b/common/base64.c index f7e7f7ef5..be8e08a10 100644 --- a/common/base64.c +++ b/common/base64.c @@ -5,7 +5,7 @@ * We import base64 from libsodium to generate tor V3 ED25519-V3 onions from blobs */ -char *b64_encode(const tal_t *ctx, const u8 *data, size_t len) +char *b64_encode(const tal_t *ctx, const void *data, size_t len) { char *str = tal_arr(ctx, char, sodium_base64_encoded_len(len, sodium_base64_VARIANT_ORIGINAL) + 1); diff --git a/common/base64.h b/common/base64.h index dee4510cd..f963541db 100644 --- a/common/base64.h +++ b/common/base64.h @@ -4,6 +4,6 @@ #include #include -char *b64_encode(const tal_t *ctx, const u8 *data, size_t len); +char *b64_encode(const tal_t *ctx, const void *data, size_t len); #endif /* LIGHTNING_COMMON_BASE64_H */ diff --git a/common/wireaddr.c b/common/wireaddr.c index 4f4deac83..6638f2e6e 100644 --- a/common/wireaddr.c +++ b/common/wireaddr.c @@ -520,21 +520,18 @@ bool parse_wireaddr_internal(const char *arg, struct wireaddr_internal *addr, char **parts = tal_strsplit(tmpctx, arg, "/", STR_EMPTY_OK); for (size_t i = 1; i < tal_count(parts)-1; i++) { - if (tal_strreg(tmpctx, parts[i], "torport")) { + if (strstarts(parts[i], "torport=")) { char *endp = NULL; - char **parts_2 = tal_strsplit(tmpctx, parts[i], "=", STR_EMPTY_OK); - if (tal_count(parts_2) == 3) { - addr->u.torservice.port = strtol((const char *)parts_2[1], &endp, 10); - if (addr->u.torservice.port <= 0 || *endp != '\0') { - if (err_msg) - *err_msg = "Bad :torport: number"; - return false; - } - } else { + addr->u.torservice.port = strtol(parts[i]+strlen("torport="), &endp, 10); + if (addr->u.torservice.port <= 0 || *endp != '\0') { if (err_msg) - *err_msg = "Bad :torport: format"; + *err_msg = "Bad :torport: number"; return false; } + } else { + if (err_msg) + *err_msg = tal_fmt(tmpctx, "unknown tor arg %s", parts[i]); + return false; } } @@ -551,47 +548,38 @@ bool parse_wireaddr_internal(const char *arg, struct wireaddr_internal *addr, bool use_magic_blob = true; addr->itype = ADDR_INTERNAL_STATICTOR; addr->u.torservice.port = DEFAULT_PORT; - memset(&(addr->u.torservice.blob[0]), 0, sizeof(addr->u.torservice.blob)); + memset(addr->u.torservice.blob, 0, sizeof(addr->u.torservice.blob)); /* Format is separated by slash. */ char **parts = tal_strsplit(tmpctx, arg, "/", STR_EMPTY_OK); for (size_t i = 1; i < tal_count(parts)-1; i++) { - if (tal_strreg(tmpctx, parts[i], "torport")) { + if (strstarts(parts[i], "torport=")) { char *endp = NULL; - char **parts_eq = tal_strsplit(tmpctx, parts[i], "=", STR_EMPTY_OK); - if (tal_count(parts_eq) == 3) { - addr->u.torservice.port = strtol((const char *)parts_eq[1], &endp, 10); - if (addr->u.torservice.port <= 0 || *endp != '\0') { - if (err_msg) - *err_msg = "Bad :torport: number"; - return false; - } - } else { + addr->u.torservice.port = strtol(parts[i]+strlen("torport="), &endp, 10); + if (addr->u.torservice.port <= 0 || *endp != '\0') { if (err_msg) - *err_msg = "Bad :torport: format"; + *err_msg = "Bad :torport: number"; return false; } - } - if (tal_strreg(tmpctx, parts[i], "torblob")) { - char **parts_eq = tal_strsplit(tmpctx, parts[i], "=", STR_EMPTY_OK); - if (tal_count(parts_eq) == 3) { - if (strlen((char *)parts_eq[1]) == 0) { - if (err_msg) - *err_msg = "Blob too short"; - return false; - } - strncpy((char *)&(addr->u.torservice.blob[0]), - (const char *)parts_eq[1], TOR_V3_BLOBLEN); - use_magic_blob = false; + } else if (strstarts(parts[i], "torblob=")) { + const char *blobdata = parts[i] + strlen("torblob="); + if (strlen(blobdata) > TOR_V3_BLOBLEN) { + if (err_msg) + *err_msg = "torblob too long"; + return false; } + strcpy(addr->u.torservice.blob, blobdata); + use_magic_blob = false; + } else { + if (err_msg) + *err_msg = tal_fmt(tmpctx, "unknown tor arg %s", parts[i]); + return false; } } if (use_magic_blob) { /* when statictor called just with the service address and or port generate the unique onion */ - strncpy((char *)&(addr->u.torservice.blob[0]), - tal_fmt(tmpctx, STATIC_TOR_MAGIC_STRING), - strlen(STATIC_TOR_MAGIC_STRING)); + strcpy(addr->u.torservice.blob, STATIC_TOR_MAGIC_STRING); } service_addr = tal_fmt(tmpctx, "%s", parts[0] + strlen("statictor:")); diff --git a/common/wireaddr.h b/common/wireaddr.h index cf16914d7..3f9cddc62 100644 --- a/common/wireaddr.h +++ b/common/wireaddr.h @@ -134,8 +134,8 @@ struct wireaddr_internal { struct wireaddr address; /* Tor port to use */ u16 port; - /* Blob to use to create tor service */ - u8 blob[TOR_V3_BLOBLEN + 1]; + /* Nul-terminated blob to use to create tor service */ + char blob[TOR_V3_BLOBLEN + 1]; } torservice; /* ADDR_INTERNAL_FORPROXY */ struct unresolved { diff --git a/connectd/connectd.c b/connectd/connectd.c index 4cd25fbc4..1150f6ee7 100644 --- a/connectd/connectd.c +++ b/connectd/connectd.c @@ -1232,7 +1232,7 @@ static struct wireaddr_internal *setup_listeners(const tal_t *ctx, struct sockaddr_un addrun; int fd; struct wireaddr_internal *binding; - const u8 *blob = NULL; + const char *blob = NULL; struct secret random; struct pubkey pb; struct wireaddr *toraddr; diff --git a/connectd/tor_autoservice.c b/connectd/tor_autoservice.c index 7ede2efb0..e471231f5 100644 --- a/connectd/tor_autoservice.c +++ b/connectd/tor_autoservice.c @@ -146,7 +146,7 @@ static struct wireaddr *make_onion(const tal_t *ctx, static struct wireaddr *make_fixed_onion(const tal_t *ctx, struct rbuf *rbuf, - const struct wireaddr *local, const u8 *blob, u16 port) + const struct wireaddr *local, const char *blob, u16 port) { char *line; struct wireaddr *onion; @@ -323,7 +323,7 @@ struct wireaddr *tor_autoservice(const tal_t *ctx, struct wireaddr *tor_fixed_service(const tal_t *ctx, const struct wireaddr_internal *tor_serviceaddr, const char *tor_password, - const u8 *blob, + const char *blob, const struct wireaddr *bind, const u8 index) { diff --git a/connectd/tor_autoservice.h b/connectd/tor_autoservice.h index 6906a40be..e88731ac9 100644 --- a/connectd/tor_autoservice.h +++ b/connectd/tor_autoservice.h @@ -15,7 +15,7 @@ struct wireaddr *tor_autoservice(const tal_t *ctx, struct wireaddr *tor_fixed_service(const tal_t *ctx, const struct wireaddr_internal *tor_serviceaddr, const char *tor_password, - const u8 *blob, + const char *blob, const struct wireaddr *bind, const u8 index);