1 #include <ccan/mem/mem.h>
2 #include <ccan/tal/str/str.h>
3 #include <common/type_to_string.h>
4 #include <common/wire_error.h>
5 #include <wire/peer_wire.h>
6 
towire_errorfmtv(const tal_t * ctx,const struct channel_id * channel,const char * fmt,va_list ap)7 u8 *towire_errorfmtv(const tal_t *ctx,
8 		     const struct channel_id *channel,
9 		     const char *fmt,
10 		     va_list ap)
11 {
12 	char *estr;
13 	u8 *msg;
14 
15 	estr = tal_vfmt(ctx, fmt, ap);
16 	/* We need tal_len to work, so we use copy. */
17 	msg = towire_error(ctx, channel,
18 			   (u8 *)tal_dup_arr(estr, char, estr, strlen(estr), 0));
19 	tal_free(estr);
20 
21 	return msg;
22 }
23 
towire_errorfmt(const tal_t * ctx,const struct channel_id * channel,const char * fmt,...)24 u8 *towire_errorfmt(const tal_t *ctx,
25 		    const struct channel_id *channel,
26 		    const char *fmt, ...)
27 {
28 	va_list ap;
29 	u8 *msg;
30 
31 	va_start(ap, fmt);
32 	msg = towire_errorfmtv(ctx, channel, fmt, ap);
33 	va_end(ap);
34 
35 	return msg;
36 }
37 
towire_warningfmtv(const tal_t * ctx,const struct channel_id * channel,const char * fmt,va_list ap)38 u8 *towire_warningfmtv(const tal_t *ctx,
39 		       const struct channel_id *channel,
40 		       const char *fmt,
41 		       va_list ap)
42 {
43 	/* BOLT #1:
44 	 *
45 	 * The channel is referred to by `channel_id`, unless `channel_id` is
46 	 * 0 (i.e. all bytes are 0), in which case it refers to all
47 	 * channels. */
48 	static const struct channel_id all_channels;
49 	char *estr;
50 	u8 *msg;
51 
52 	estr = tal_vfmt(ctx, fmt, ap);
53 	/* We need tal_len to work, so we use copy. */
54 	msg = towire_warning(ctx, channel ? channel : &all_channels,
55 			     (u8 *)tal_dup_arr(estr, char, estr, strlen(estr), 0));
56 	tal_free(estr);
57 
58 	return msg;
59 }
60 
towire_warningfmt(const tal_t * ctx,const struct channel_id * channel,const char * fmt,...)61 u8 *towire_warningfmt(const tal_t *ctx,
62 		      const struct channel_id *channel,
63 		      const char *fmt, ...)
64 {
65 	va_list ap;
66 	u8 *msg;
67 
68 	va_start(ap, fmt);
69 	msg = towire_warningfmtv(ctx, channel, fmt, ap);
70 	va_end(ap);
71 
72 	return msg;
73 }
74 
channel_id_is_all(const struct channel_id * channel_id)75 bool channel_id_is_all(const struct channel_id *channel_id)
76 {
77 	return memeqzero(channel_id, sizeof(*channel_id));
78 }
79 
sanitize_error(const tal_t * ctx,const u8 * errmsg,struct channel_id * channel_id)80 char *sanitize_error(const tal_t *ctx, const u8 *errmsg,
81 		     struct channel_id *channel_id)
82 {
83 	struct channel_id dummy;
84 	u8 *data;
85 	size_t i;
86 	bool warning;
87 
88 	if (!channel_id)
89 		channel_id = &dummy;
90 
91 	if (fromwire_error(ctx, errmsg, channel_id, &data))
92 		warning = false;
93 	else if (fromwire_warning(ctx, errmsg, channel_id, &data))
94 		warning = true;
95 	else
96 		return tal_fmt(ctx, "Invalid ERROR message '%s'",
97 			       tal_hex(ctx, errmsg));
98 
99 	/* BOLT #1:
100 	 *
101 	 * The receiving node:
102 	 *...
103 	 *  - if `data` is not composed solely of printable ASCII characters
104 	 *   (For reference: the printable character set includes byte values 32
105 	 *   through 126, inclusive):
106 	 *    - SHOULD NOT print out `data` verbatim.
107 	 */
108 	for (i = 0; i < tal_count(data); i++) {
109 		if (data[i] < 32 || data[i] > 127) {
110 			/* Convert to hex, minus NUL term */
111 			data = (u8 *)tal_hex(ctx, data);
112 			tal_resize(&data, strlen((const char *)data));
113 			break;
114 		}
115 	}
116 
117 	return tal_fmt(ctx, "%s%s%s: %.*s",
118 		       warning ? "warning" : "error",
119 		       channel_id_is_all(channel_id) ? "": " channel ",
120 		       channel_id_is_all(channel_id) ? ""
121 		       : type_to_string(tmpctx, struct channel_id, channel_id),
122 		       (int)tal_count(data), (char *)data);
123 }
124