mirror of
https://github.com/aljazceru/lightning.git
synced 2026-02-02 04:34:20 +01:00
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 <rusty@rustcorp.com.au>
This commit is contained in:
committed by
Christian Decker
parent
9d18180172
commit
b2c762969c
@@ -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);
|
||||
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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:"));
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user