You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.5 KiB
59 lines
1.5 KiB
#include <arpa/inet.h>
|
|
#include <ccan/tal/str/str.h>
|
|
#include <common/type_to_string.h>
|
|
#include <common/utils.h>
|
|
#include <common/wireaddr.h>
|
|
#include <wire/wire.h>
|
|
|
|
/* Returns false if we didn't parse it, and *cursor == NULL if malformed. */
|
|
bool fromwire_wireaddr(const u8 **cursor, size_t *max, struct wireaddr *addr)
|
|
{
|
|
addr->type = fromwire_u8(cursor, max);
|
|
|
|
switch (addr->type) {
|
|
case ADDR_TYPE_IPV4:
|
|
addr->addrlen = 4;
|
|
break;
|
|
case ADDR_TYPE_IPV6:
|
|
addr->addrlen = 16;
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
fromwire(cursor, max, addr->addr, addr->addrlen);
|
|
addr->port = fromwire_u16(cursor, max);
|
|
|
|
return *cursor != NULL;
|
|
}
|
|
|
|
void towire_wireaddr(u8 **pptr, const struct wireaddr *addr)
|
|
{
|
|
towire_u8(pptr, addr->type);
|
|
towire(pptr, addr->addr, addr->addrlen);
|
|
towire_u16(pptr, addr->port);
|
|
}
|
|
|
|
static char *fmt_wireaddr(const tal_t *ctx, const struct wireaddr *a)
|
|
{
|
|
char addrstr[INET6_ADDRSTRLEN];
|
|
char *ret, *hex;
|
|
|
|
switch (a->type) {
|
|
case ADDR_TYPE_IPV4:
|
|
if (!inet_ntop(AF_INET, a->addr, addrstr, INET_ADDRSTRLEN))
|
|
return "Unprintable-ipv4-address";
|
|
return tal_fmt(ctx, "%s:%u", addrstr, a->port);
|
|
case ADDR_TYPE_IPV6:
|
|
if (!inet_ntop(AF_INET6, a->addr, addrstr, INET6_ADDRSTRLEN))
|
|
return "Unprintable-ipv6-address";
|
|
return tal_fmt(ctx, "%s:%u", addrstr, a->port);
|
|
case ADDR_TYPE_PADDING:
|
|
break;
|
|
}
|
|
|
|
hex = tal_hexstr(ctx, a->addr, a->addrlen);
|
|
ret = tal_fmt(ctx, "Unknown type %u %s:%u", a->type, hex, a->port);
|
|
tal_free(hex);
|
|
return ret;
|
|
}
|
|
REGISTER_TYPE_TO_STRING(wireaddr, fmt_wireaddr);
|
|
|