Build and test the new crypto toys
[secnet] / util.h
CommitLineData
c215a4bc
IJ
1/*
2 * This file is part of secnet.
3 * See README for full list of copyright holders.
4 *
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
9c6a8729 7 * the Free Software Foundation; either version 3 of the License, or
c215a4bc
IJ
8 * (at your option) any later version.
9 *
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.
14 *
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.
18 */
19
2fe58dfd
SE
20#ifndef util_h
21#define util_h
22
2fe58dfd 23#include "secnet.h"
2fe58dfd
SE
24#include <gmp.h>
25
3b83c932
SE
26#include "hackypar.h"
27
2fe58dfd
SE
28#define BUF_ASSERT_FREE(buf) do { buffer_assert_free((buf), \
29 __FILE__,__LINE__); } \
30while(0)
31#define BUF_ASSERT_USED(buf) do { buffer_assert_used((buf), \
32 __FILE__,__LINE__); } \
33while(0)
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)
38
fe5e9cc4 39extern void buffer_assert_free(struct buffer_if *buffer, cstring_t file,
1caa23ff 40 int line);
fe5e9cc4 41extern void buffer_assert_used(struct buffer_if *buffer, cstring_t file,
1caa23ff
IJ
42 int line);
43extern void buffer_new(struct buffer_if *buffer, int32_t len);
44extern void buffer_init(struct buffer_if *buffer, int32_t max_start_pad);
46d06c39 45extern void buffer_destroy(struct buffer_if *buffer);
05f39b4d 46extern void buffer_copy(struct buffer_if *dst, const struct buffer_if *src);
1caa23ff
IJ
47extern void *buf_append(struct buffer_if *buf, int32_t amount);
48extern void *buf_prepend(struct buffer_if *buf, int32_t amount);
49extern void *buf_unappend(struct buffer_if *buf, int32_t amount);
50extern void *buf_unprepend(struct buffer_if *buf, int32_t amount);
2fe58dfd 51
8438de14
IJ
52/*
53 * void BUF_ADD_BYTES(append, struct buffer_if*, const void*, int32_t size);
54 * void BUF_ADD_BYTES(prepend, struct buffer_if*, const void*, int32_t size);
55 * void BUF_GET_BYTES(unappend, struct buffer_if*, void*, int32_t size);
56 * void BUF_GET_BYTES(unprepend, struct buffer_if*, void*, int32_t size);
57 * // all of these evaluate size twice
58 *
59 * void BUF_ADD_OBJ(append, struct_buffer_if*, const OBJECT& something);
60 * void BUF_ADD_OBJ(prepend, struct_buffer_if*, const OBJECT& something);
61 * void BUF_GET_OBJ(unappend, struct_buffer_if*, OBJECT& something);
62 * void BUF_GET_OBJ(unprepend, struct_buffer_if*, OBJECT& something);
63 */
64#define BUF_ADD_BYTES(appendprepend, bufp, datap, size) \
65 (buf_un##appendprepend /* ensures we have correct direction */, \
66 memcpy(buf_##appendprepend((bufp),(size)),(datap),(size)))
67#define BUF_ADD_OBJ(appendprepend, bufp, obj) \
68 BUF_ADD_BYTES(appendprepend,(bufp),&(obj),sizeof((obj)))
69#define BUF_GET_BYTES(unappendunprepend, bufp, datap, size) \
70 (BUF_GET__DOESNOTEXIST__buf_un##unappendunprepend, \
71 memcpy((datap),buf_##unappendunprepend((bufp),(size)),(size)))
72#define BUF_GET_OBJ(unappendunprepend, bufp, obj) \
73 BUF_ADD_BYTES(unappendunprepend,&(obj),(bufp),sizeof((obj)))
74#define BUF_GET__DOESNOTEXIST__buf_ununappend 0
75#define BUF_GET__DOESNOTEXIST__buf_ununprepend 0
76
92795040
IJ
77static inline int32_t buf_remaining_space(const struct buffer_if *buf)
78{
2ec49059 79 return (buf->base + buf->alloclen) - (buf->start + buf->size);
92795040
IJ
80}
81
28db900b
IJ
82extern void buffer_readonly_view(struct buffer_if *n, const void*, int32_t len);
83extern void buffer_readonly_clone(struct buffer_if *n, const struct buffer_if*);
84 /* Caller must only use unappend, unprepend et al. on n.
85 * New buffer state (in n) before this can be undefined. After use,
86 * it must NOT be freed. */
87
fe5e9cc4 88extern void buf_append_string(struct buffer_if *buf, cstring_t s);
a8f672ea
IJ
89 /* Append a two-byte length and the string to the buffer. Length is in
90 * network byte order. */
2fe58dfd 91
36a81f9c
IJ
92static inline int hex_encode_size(int binsize) { return binsize*2 + 1; }
93extern void hex_encode(const uint8_t *bin, int binsize, char *buf);
94 /* Convert a byte array to hex into a supplied buffer. */
95extern string_t hex_encode_alloc(const uint8_t *bin, int binsize);
96 /* Returns the result in a freshly allocated string. */
7be31e47
MW
97
98extern bool_t hex_decode(uint8_t *buffer, int32_t buflen, int32_t *outlen,
99 cstring_t hb, bool_t allow_odd_nibble);
100 /* Convert a hex string to binary, storing the result in buffer. If
101 * allow_odd_nibble then it is acceptable if the input is an odd number of
102 * digits, and an additional leading zero digit is assumed; otherwise, this
103 * is not acceptable and the conversion fails.
104 *
105 * The input is processed left to right until it is consumed, the buffer is
106 * full, or an error is encountered in the input. The length of output
107 * produced is stored in *outlen. Returns true if the entire input was
e53b161a 108 * processed without error; otherwise false. */
7be31e47 109
2fe58dfd 110extern void read_mpbin(MP_INT *a, uint8_t *bin, int binsize);
a8f672ea 111 /* Convert a buffer into its MP_INT representation */
2fe58dfd
SE
112
113extern char *write_mpstring(MP_INT *a);
a8f672ea 114 /* Convert a MP_INT into a hex string */
2fe58dfd 115
1caa23ff 116extern int32_t write_mpbin(MP_INT *a, uint8_t *buffer, int32_t buflen);
a8f672ea 117 /* Convert a MP_INT into a buffer; return length; truncate if necessary */
2fe58dfd
SE
118
119extern struct log_if *init_log(list_t *loglist);
120
8534d602
IJ
121extern void send_nak(const struct comm_addr *dest, uint32_t our_index,
122 uint32_t their_index, uint32_t msgtype,
123 struct buffer_if *buf, const char *logwhy);
124
5ad34db2
IJ
125extern int consttime_memeq(const void *s1, const void *s2, size_t n);
126
a32d56fb 127const char *iaddr_to_string(const union iaddr *ia);
a32d56fb
IJ
128int iaddr_socklen(const union iaddr *ia);
129
bb839899
IJ
130void string_item_to_iaddr(const item_t *item, uint16_t port, union iaddr *ia,
131 const char *desc);
132
a0fac2f1 133
5e7a63be
IJ
134/*
135 * SBUF_DEFINE(int nbufs, size_t size);
136 * // Generates a number of definitions and statements organising
137 * // nbufs rotating char[size] buffers such that subsequent code
138 * // may refer to:
139 * char *const SBUF;
140 */
141#define SBUF_DEFINE(nbufs, size) \
142 static int static_bufs__bufnum; \
143 static char static_bufs__bufs[(nbufs)][(size)]; \
144 static_bufs__bufnum++; \
145 static_bufs__bufnum %= (nbufs); \
146 static_bufs__bufs[static_bufs__bufnum]
147#define SBUF (static_bufs__bufs[static_bufs__bufnum])
5f37eb10 148
a0fac2f1
IJ
149/*----- line-buffered asynch input -----*/
150
151enum async_linebuf_result {
152 async_linebuf_nothing,
153 async_linebuf_ok,
154 async_linebuf_eof,
155 async_linebuf_broken,
156};
157
ae5ae3bf
IJ
158const char *pollbadbit(int revents); /* returns 0, or bad bit description */
159
a0fac2f1
IJ
160enum async_linebuf_result
161async_linebuf_read(struct pollfd *pfd, struct buffer_if *buf,
162 const char **emsg_out);
163 /* Implements reading whole lines, asynchronously. Use like
164 * this:
165 * - set up the fd, which should be readable, O_NONBLOCK
166 * - set up and initialise buffer, which should be big enough
167 * for one line plus its trailing newline, and be empty
168 * with start==base
169 * - in your beforepoll_fn, be interested in POLLIN
170 * - in your afterpoll_fn, repeatedly call this function
171 * until it doesn't return `nothing'
172 * - after you're done, simply close fd and free or reset buf
173 * State on return from async_linebuf_read depends on return value:
174 *
175 * async_linebuf_nothing:
176 *
177 * No complete lines available right now. You should return
178 * from afterpoll. buf should be left untouched until the
179 * next call to async_linebuf_read.
180 *
181 * async_linebuf_ok:
182 *
183 * buf->base contains a input line as a nul-terminated string
184 * (\n replaced by \0); *emsg_out==0. You must call
185 * async_linebuf_read again before returning from afterpoll.
186 *
187 * async_linebuf_eof:
188 *
189 * EOF on stream. buf->base contains any partial
190 * (non-newline-terminated) line; *emsg_out!=0 iff there was
191 * such a partial line. You can call async_linebuf_read again
192 * if you like but it will probably just return eof again.
193 *
194 * broken:
195 *
196 * Fatal problem (might be overly long lines, nuls in input
197 * data, bad bits in pfd->revents, errors from read, etc.)
198 *
199 * *emsg_out is the error message describing the problem;
200 * this message might be stored in buf, might be from
201 * strerror, or might be a constant.
202 *
203 * You must not call async_linebuf_read again. buf contents
204 * is undefined: it is only safe to reset or free.
205 *
206 * While using this function, do not look at buf->start or ->size
207 * or anything after the first '\0' in buf.
208 *
209 * If you decide to stop reading with async_linebuf_read that's
210 * fine and you can reset or free buf, but you risk missing some
211 * read-but-not-reported data.
212 */
213
214/*----- some handy macros -----*/
215
ceb05e5d
IJ
216#define MINMAX(ae,be,op) ({ \
217 typeof((ae)) a=(ae); \
218 typeof((be)) b=(be); \
219 a op b ? a : b; \
220 })
221#define MAX(a,b) MINMAX((a),(b),>)
222#define MIN(a,b) MINMAX((a),(b),<)
223
c9c047dc
IJ
224#define MAX_RAW(a,b) ((a)>(b)?(a):(b))
225#define MIN_RAW(a,b) ((a)<(b)?(a):(b))
226
ba703386
IJ
227static inline bool_t iswouldblock(int e)
228 { return e==EWOULDBLOCK || e==EAGAIN; }
229
2fe58dfd 230#endif /* util_h */