Skip to content

Commit

Permalink
wireaddr: clean up tor parsing.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Nov 14, 2021
1 parent 9d18180 commit b2c7629
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 46 deletions.
2 changes: 1 addition & 1 deletion common/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion common/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>

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 */
64 changes: 26 additions & 38 deletions common/wireaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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:"));
Expand Down
4 changes: 2 additions & 2 deletions common/wireaddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions connectd/tor_autoservice.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion connectd/tor_autoservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit b2c7629

Please sign in to comment.