2 * This file is part of secnet.
3 * See README for full list of copyright holders.
5 * secnet is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * secnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * version 3 along with secnet; if not, see
17 * https://www.gnu.org/licenses/gpl.html.
28 #define BUF_ASSERT_FREE(buf) do { buffer_assert_free((buf), \
29 __FILE__,__LINE__); } \
31 #define BUF_ASSERT_USED(buf) do { buffer_assert_used((buf), \
32 __FILE__,__LINE__); } \
34 #define BUF_ALLOC(buf,own) do { buffer_assert_free((buf),__FILE__,__LINE__); \
35 (buf)->free=False; (buf)->owner=(own); (buf)->start=(buf)->base; \
36 (buf)->size=0; } while(0)
37 #define BUF_FREE(buf) do { (buf)->free=True; } while(0)
39 extern void buffer_assert_free(struct buffer_if *buffer, cstring_t file,
41 extern void buffer_assert_used(struct buffer_if *buffer, cstring_t file,
43 extern void buffer_new(struct buffer_if *buffer, int32_t len);
44 extern void buffer_init(struct buffer_if *buffer, int32_t max_start_pad);
45 extern void buffer_destroy(struct buffer_if *buffer);
46 extern void buffer_copy(struct buffer_if *dst, const struct buffer_if *src);
47 extern void *buf_append(struct buffer_if *buf, int32_t amount);
48 extern void *buf_prepend(struct buffer_if *buf, int32_t amount);
49 extern void *buf_unappend(struct buffer_if *buf, int32_t amount);
50 extern void *buf_unprepend(struct buffer_if *buf, int32_t amount);
52 /* These construct a message in a buffer, truncating if necessary.
53 * _string is only safe for trusted input and *not* UTF-8 (sorry).
54 * _packet_string is safe for any input, including untrusted.
55 * _terminate arranges for the buffer to be null-terminated (and
56 * maybe for a trailing `...' to indicate truncation), and returns
57 * a pointer to the null-terminated string. */
58 void truncmsg_add_string(struct buffer_if *buf, cstring_t s);
59 void truncmsg_add_packet_string(struct buffer_if*, int32_t, const uint8_t*);
60 const char *truncmsg_terminate(const struct buffer_if *buf);
65 * F: initialised but free (no memory allocated), no leak if discarded
66 * Z: contains no message yet
67 * M: contains some message; you may call truncmsg_add_*
73 void priomsg_new(struct priomsg *pm, int32_t maxlen); /* UF -> Z */
74 void priomsg_destroy(struct priomsg *pm, int32_t maxlen); /* FZM -> F */
75 void priomsg_reset(struct priomsg *pm); /* FZM -> Z */
76 bool_t priomsg_update_p(struct priomsg *pm, int prio); /* ZM -> M */
77 /* returns true iff message of priority prio ought to be added,
78 * caller should then call truncmsg_add_*.
79 * pm may be NULL, in which case it just returns false */
80 const char *priomsg_getmessage(const struct priomsg *pm, const char *defmsg);
81 /* return value is null-terminated, valid until next call
82 * or until defmsg is no longer valid ZM */
84 bool_t priomsg_update_fixed(struct priomsg *pm, int prio, const char *m);
85 /* convenience combination of _update_p and truncmsg_add_string */
88 * void BUF_ADD_BYTES(append, struct buffer_if*, const void*, int32_t size);
89 * void BUF_ADD_BYTES(prepend, struct buffer_if*, const void*, int32_t size);
90 * void BUF_GET_BYTES(unappend, struct buffer_if*, void*, int32_t size);
91 * void BUF_GET_BYTES(unprepend, struct buffer_if*, void*, int32_t size);
92 * // all of these evaluate size twice
94 * void BUF_ADD_OBJ(append, struct_buffer_if*, const OBJECT& something);
95 * void BUF_ADD_OBJ(prepend, struct_buffer_if*, const OBJECT& something);
96 * void BUF_GET_OBJ(unappend, struct_buffer_if*, OBJECT& something);
97 * void BUF_GET_OBJ(unprepend, struct_buffer_if*, OBJECT& something);
99 #define BUF_ADD_BYTES(appendprepend, bufp, datap, size) \
100 (buf_un##appendprepend /* ensures we have correct direction */, \
101 memcpy(buf_##appendprepend((bufp),(size)),(datap),(size)))
102 #define BUF_ADD_OBJ(appendprepend, bufp, obj) \
103 BUF_ADD_BYTES(appendprepend,(bufp),&(obj),sizeof((obj)))
104 #define BUF_GET_BYTES(unappendunprepend, bufp, datap, size) \
105 (BUF_GET__DOESNOTEXIST__buf_un##unappendunprepend, \
106 memcpy((datap),buf_##unappendunprepend((bufp),(size)),(size)))
107 #define BUF_GET_OBJ(unappendunprepend, bufp, obj) \
108 BUF_ADD_BYTES(unappendunprepend,&(obj),(bufp),sizeof((obj)))
109 #define BUF_GET__DOESNOTEXIST__buf_ununappend 0
110 #define BUF_GET__DOESNOTEXIST__buf_ununprepend 0
112 static inline int32_t buf_remaining_space(const struct buffer_if *buf)
114 return (buf->base + buf->alloclen) - (buf->start + buf->size);
117 extern void buffer_readonly_view(struct buffer_if *n, const void*, int32_t len);
118 extern void buffer_readonly_clone(struct buffer_if *n, const struct buffer_if*);
119 /* Caller must only use unappend, unprepend et al. on n.
120 * New buffer state (in n) before this can be undefined. After use,
121 * it must NOT be freed. */
123 extern void buf_append_string(struct buffer_if *buf, cstring_t s);
124 /* Append a two-byte length and the string to the buffer. Length is in
125 * network byte order. */
127 static inline int hex_encode_size(int binsize) { return binsize*2 + 1; }
128 extern void hex_encode(const uint8_t *bin, int binsize, char *buf);
129 /* Convert a byte array to hex into a supplied buffer. */
130 extern string_t hex_encode_alloc(const uint8_t *bin, int binsize);
131 /* Returns the result in a freshly allocated string. */
133 extern bool_t hex_decode(uint8_t *buffer, int32_t buflen, int32_t *outlen,
134 cstring_t hb, bool_t allow_odd_nibble);
135 /* Convert a hex string to binary, storing the result in buffer. If
136 * allow_odd_nibble then it is acceptable if the input is an odd number of
137 * digits, and an additional leading zero digit is assumed; otherwise, this
138 * is not acceptable and the conversion fails.
140 * The input is processed left to right until it is consumed, the buffer is
141 * full, or an error is encountered in the input. The length of output
142 * produced is stored in *outlen. Returns true if the entire input was
143 * processed without error; otherwise false. */
145 extern void read_mpbin(MP_INT *a, uint8_t *bin, int binsize);
146 /* Convert a buffer into its MP_INT representation */
148 extern char *write_mpstring(MP_INT *a);
149 /* Convert a MP_INT into a hex string */
151 extern struct log_if *init_log(list_t *loglist);
153 extern void send_nak(const struct comm_addr *dest, uint32_t our_index,
154 uint32_t their_index, uint32_t msgtype,
155 struct buffer_if *buf, const char *logwhy);
157 extern int consttime_memeq(const void *s1, const void *s2, size_t n);
159 void hash_hash(const struct hash_if *hashi, const void *msg, int32_t len,
160 uint8_t *digest /* hi->hlen bytes */);
162 const char *iaddr_to_string(const union iaddr *ia);
163 int iaddr_socklen(const union iaddr *ia);
165 void string_item_to_iaddr(const item_t *item, uint16_t port, union iaddr *ia,
169 /*----- pathprefix_template -----*/
171 struct pathprefix_template {
176 void pathprefix_template_init(struct pathprefix_template *out,
177 const char *prefix, int maxsuffix);
178 static inline void pathprefix_template_setsuffix
179 (struct pathprefix_template *upd, const char *suffix)
180 { strcpy(upd->write_here,suffix); }
184 * SBUF_DEFINE(int nbufs, size_t size);
185 * // Generates a number of definitions and statements organising
186 * // nbufs rotating char[size] buffers such that subsequent code
190 #define SBUF_DEFINE(nbufs, size) \
191 static int static_bufs__bufnum; \
192 static char static_bufs__bufs[(nbufs)][(size)]; \
193 static_bufs__bufnum++; \
194 static_bufs__bufnum %= (nbufs); \
195 static_bufs__bufs[static_bufs__bufnum]
196 #define SBUF (static_bufs__bufs[static_bufs__bufnum])
198 /*----- line-buffered asynch input -----*/
200 enum async_linebuf_result {
201 async_linebuf_nothing,
204 async_linebuf_broken,
207 const char *pollbadbit(int revents); /* returns 0, or bad bit description */
209 enum async_linebuf_result
210 async_linebuf_read(struct pollfd *pfd, struct buffer_if *buf,
211 const char **emsg_out);
212 /* Implements reading whole lines, asynchronously. Use like
214 * - set up the fd, which should be readable, O_NONBLOCK
215 * - set up and initialise buffer, which should be big enough
216 * for one line plus its trailing newline, and be empty
218 * - in your beforepoll_fn, be interested in POLLIN
219 * - in your afterpoll_fn, repeatedly call this function
220 * until it doesn't return `nothing'
221 * - after you're done, simply close fd and free or reset buf
222 * State on return from async_linebuf_read depends on return value:
224 * async_linebuf_nothing:
226 * No complete lines available right now. You should return
227 * from afterpoll. buf should be left untouched until the
228 * next call to async_linebuf_read.
232 * buf->base contains a input line as a nul-terminated string
233 * (\n replaced by \0); *emsg_out==0. You must call
234 * async_linebuf_read again before returning from afterpoll.
238 * EOF on stream. buf->base contains any partial
239 * (non-newline-terminated) line; *emsg_out!=0 iff there was
240 * such a partial line. You can call async_linebuf_read again
241 * if you like but it will probably just return eof again.
245 * Fatal problem (might be overly long lines, nuls in input
246 * data, bad bits in pfd->revents, errors from read, etc.)
248 * *emsg_out is the error message describing the problem;
249 * this message might be stored in buf, might be from
250 * strerror, or might be a constant.
252 * You must not call async_linebuf_read again. buf contents
253 * is undefined: it is only safe to reset or free.
255 * While using this function, do not look at buf->start or ->size
256 * or anything after the first '\0' in buf.
258 * If you decide to stop reading with async_linebuf_read that's
259 * fine and you can reset or free buf, but you risk missing some
260 * read-but-not-reported data.
263 /*----- some handy macros -----*/
265 #define CL_GET_STR_ARG(ix,vn,what) \
266 item_t *vn##_i=list_elem(args,ix); \
267 if (!vn##_i) cfgfatal(loc,"make-public","need " what); \
268 if (vn##_i->type!=t_string) cfgfatal(vn##_i->loc,"make-public", \
269 what "must be string"); \
270 const char *vn=vn##_i->data.string
272 #define MINMAX(ae,be,op) ({ \
273 typeof((ae)) a=(ae); \
274 typeof((be)) b=(be); \
277 #define MAX(a,b) MINMAX((a),(b),>)
278 #define MIN(a,b) MINMAX((a),(b),<)
280 #define MAX_RAW(a,b) ((a)>(b)?(a):(b))
281 #define MIN_RAW(a,b) ((a)<(b)?(a):(b))
283 static inline bool_t iswouldblock(int e)
284 { return e==EWOULDBLOCK || e==EAGAIN; }