core-lightning/common/wire_error.c
Rusty Russell 7401b26824 cleanup: remove unneeded includes in C files.
Before:
 Ten builds, laptop -j5, no ccache:

```
real	0m36.686000-38.956000(38.608+/-0.65)s
user	2m32.864000-42.253000(40.7545+/-2.7)s
sys	0m16.618000-18.316000(17.8531+/-0.48)s
```

 Ten builds, laptop -j5, ccache (warm):

```
real	0m8.212000-8.577000(8.39989+/-0.13)s
user	0m12.731000-13.212000(12.9751+/-0.17)s
sys	0m3.697000-3.902000(3.83722+/-0.064)s
```

After:
 Ten builds, laptop -j5, no ccache: 8% faster

```
real	0m33.802000-35.773000(35.468+/-0.54)s
user	2m19.073000-27.754000(26.2542+/-2.3)s
sys	0m15.784000-17.173000(16.7165+/-0.37)s
```

 Ten builds, laptop -j5, ccache (warm): 1% faster

```
real	0m8.200000-8.485000(8.30138+/-0.097)s
user	0m12.485000-13.100000(12.7344+/-0.19)s
sys	0m3.702000-3.889000(3.78787+/-0.056)s
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2021-09-17 09:43:22 +09:30

124 lines
2.9 KiB
C

#include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h>
#include <common/type_to_string.h>
#include <common/wire_error.h>
#include <wire/peer_wire.h>
u8 *towire_errorfmtv(const tal_t *ctx,
const struct channel_id *channel,
const char *fmt,
va_list ap)
{
char *estr;
u8 *msg;
estr = tal_vfmt(ctx, fmt, ap);
/* We need tal_len to work, so we use copy. */
msg = towire_error(ctx, channel,
(u8 *)tal_dup_arr(estr, char, estr, strlen(estr), 0));
tal_free(estr);
return msg;
}
u8 *towire_errorfmt(const tal_t *ctx,
const struct channel_id *channel,
const char *fmt, ...)
{
va_list ap;
u8 *msg;
va_start(ap, fmt);
msg = towire_errorfmtv(ctx, channel, fmt, ap);
va_end(ap);
return msg;
}
u8 *towire_warningfmtv(const tal_t *ctx,
const struct channel_id *channel,
const char *fmt,
va_list ap)
{
/* BOLT #1:
*
* The channel is referred to by `channel_id`, unless `channel_id` is
* 0 (i.e. all bytes are 0), in which case it refers to all
* channels. */
static const struct channel_id all_channels;
char *estr;
u8 *msg;
estr = tal_vfmt(ctx, fmt, ap);
/* We need tal_len to work, so we use copy. */
msg = towire_warning(ctx, channel ? channel : &all_channels,
(u8 *)tal_dup_arr(estr, char, estr, strlen(estr), 0));
tal_free(estr);
return msg;
}
u8 *towire_warningfmt(const tal_t *ctx,
const struct channel_id *channel,
const char *fmt, ...)
{
va_list ap;
u8 *msg;
va_start(ap, fmt);
msg = towire_warningfmtv(ctx, channel, fmt, ap);
va_end(ap);
return msg;
}
bool channel_id_is_all(const struct channel_id *channel_id)
{
return memeqzero(channel_id, sizeof(*channel_id));
}
char *sanitize_error(const tal_t *ctx, const u8 *errmsg,
struct channel_id *channel_id)
{
struct channel_id dummy;
u8 *data;
size_t i;
bool warning;
if (!channel_id)
channel_id = &dummy;
if (fromwire_error(ctx, errmsg, channel_id, &data))
warning = false;
else if (fromwire_warning(ctx, errmsg, channel_id, &data))
warning = true;
else
return tal_fmt(ctx, "Invalid ERROR message '%s'",
tal_hex(ctx, errmsg));
/* BOLT #1:
*
* The receiving node:
*...
* - if `data` is not composed solely of printable ASCII characters
* (For reference: the printable character set includes byte values 32
* through 126, inclusive):
* - SHOULD NOT print out `data` verbatim.
*/
for (i = 0; i < tal_count(data); i++) {
if (data[i] < 32 || data[i] > 127) {
/* Convert to hex, minus NUL term */
data = (u8 *)tal_hex(ctx, data);
tal_resize(&data, strlen((const char *)data));
break;
}
}
return tal_fmt(ctx, "%s%s%s: %.*s",
warning ? "warning" : "error",
channel_id_is_all(channel_id) ? "": " channel ",
channel_id_is_all(channel_id) ? ""
: type_to_string(tmpctx, struct channel_id, channel_id),
(int)tal_count(data), (char *)data);
}