chiark / gitweb /
util.[ch]: Factor out hex encoding and decoding utilities.
[secnet.git] / util.h
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
7  * the Free Software Foundation; either version 3 of the License, or
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
20 #ifndef util_h
21 #define util_h
22
23 #include "secnet.h"
24 #include <gmp.h>
25
26 #include "hackypar.h"
27
28 #define BUF_ASSERT_FREE(buf) do { buffer_assert_free((buf), \
29                                                      __FILE__,__LINE__); } \
30 while(0)
31 #define BUF_ASSERT_USED(buf) do { buffer_assert_used((buf), \
32                                                      __FILE__,__LINE__); } \
33 while(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
39 extern void buffer_assert_free(struct buffer_if *buffer, cstring_t file,
40                                int line);
41 extern void buffer_assert_used(struct buffer_if *buffer, cstring_t file,
42                                int line);
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);
51
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
77 static inline int32_t buf_remaining_space(const struct buffer_if *buf)
78 {
79     return (buf->base + buf->alloclen) - (buf->start + buf->size);
80 }
81
82 extern void buffer_readonly_view(struct buffer_if *n, const void*, int32_t len);
83 extern 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
88 extern void buf_append_string(struct buffer_if *buf, cstring_t s);
89
90 extern string_t hex_encode(const uint8_t *bin, int binsize);
91   /* Convert a byte array to hex, returning the result in a freshly allocated
92    * string.
93    */
94
95 extern bool_t hex_decode(uint8_t *buffer, int32_t buflen, int32_t *outlen,
96                          cstring_t hb, bool_t allow_odd_nibble);
97   /* Convert a hex string to binary, storing the result in buffer.  If
98    * allow_odd_nibble then it is acceptable if the input is an odd number of
99    * digits, and an additional leading zero digit is assumed; otherwise, this
100    * is not acceptable and the conversion fails.
101    *
102    * The input is processed left to right until it is consumed, the buffer is
103    * full, or an error is encountered in the input.  The length of output
104    * produced is stored in *outlen.  Returns true if the entire input was
105    * processed without error; otherwise false.
106    */
107
108 extern void read_mpbin(MP_INT *a, uint8_t *bin, int binsize);
109
110 extern char *write_mpstring(MP_INT *a);
111
112 extern int32_t write_mpbin(MP_INT *a, uint8_t *buffer, int32_t buflen);
113
114 extern struct log_if *init_log(list_t *loglist);
115
116 extern void send_nak(const struct comm_addr *dest, uint32_t our_index,
117                      uint32_t their_index, uint32_t msgtype,
118                      struct buffer_if *buf, const char *logwhy);
119
120 extern int consttime_memeq(const void *s1, const void *s2, size_t n);
121
122 const char *iaddr_to_string(const union iaddr *ia);
123 int iaddr_socklen(const union iaddr *ia);
124
125 void string_item_to_iaddr(const item_t *item, uint16_t port, union iaddr *ia,
126                           const char *desc);
127
128
129 /*
130  * SBUF_DEFINE(int nbufs, size_t size);
131  *   // Generates a number of definitions and statements organising
132  *   // nbufs rotating char[size] buffers such that subsequent code
133  *   // may refer to:
134  * char *const SBUF;
135  */
136 #define SBUF_DEFINE(nbufs, size)                        \
137     static int static_bufs__bufnum;                     \
138     static char static_bufs__bufs[(nbufs)][(size)];     \
139     static_bufs__bufnum++;                              \
140     static_bufs__bufnum %= (nbufs);                     \
141     static_bufs__bufs[static_bufs__bufnum]
142 #define SBUF (static_bufs__bufs[static_bufs__bufnum])
143
144 /*----- line-buffered asynch input -----*/
145
146 enum async_linebuf_result {
147     async_linebuf_nothing,
148     async_linebuf_ok,
149     async_linebuf_eof,
150     async_linebuf_broken,
151 };
152
153 const char *pollbadbit(int revents); /* returns 0, or bad bit description */
154
155 enum async_linebuf_result
156 async_linebuf_read(struct pollfd *pfd, struct buffer_if *buf,
157                    const char **emsg_out);
158    /* Implements reading whole lines, asynchronously.  Use like
159     * this:
160     *   - set up the fd, which should be readable, O_NONBLOCK
161     *   - set up and initialise buffer, which should be big enough
162     *     for one line plus its trailing newline, and be empty
163     *     with start==base
164     *   - in your beforepoll_fn, be interested in POLLIN
165     *   - in your afterpoll_fn, repeatedly call this function
166     *     until it doesn't return `nothing'
167     *   - after you're done, simply close fd and free or reset buf
168     * State on return from async_linebuf_read depends on return value:
169     *
170     *   async_linebuf_nothing:
171     *
172     *      No complete lines available right now.  You should return
173     *      from afterpoll.  buf should be left untouched until the
174     *      next call to async_linebuf_read.
175     *
176     *   async_linebuf_ok:
177     *
178     *      buf->base contains a input line as a nul-terminated string
179     *      (\n replaced by \0); *emsg_out==0.  You must call
180     *      async_linebuf_read again before returning from afterpoll.
181     *
182     *   async_linebuf_eof:
183     *
184     *      EOF on stream.  buf->base contains any partial
185     *      (non-newline-terminated) line; *emsg_out!=0 iff there was
186     *      such a partial line.  You can call async_linebuf_read again
187     *      if you like but it will probably just return eof again.
188     *
189     *   broken:
190     *
191     *      Fatal problem (might be overly long lines, nuls in input
192     *      data, bad bits in pfd->revents, errors from read, etc.)
193     *
194     *      *emsg_out is the error message describing the problem;
195     *      this message might be stored in buf, might be from
196     *      strerror, or might be a constant.
197     *
198     *      You must not call async_linebuf_read again.  buf contents
199     *      is undefined: it is only safe to reset or free.
200     *
201     * While using this function, do not look at buf->start or ->size
202     * or anything after the first '\0' in buf.
203     *
204     * If you decide to stop reading with async_linebuf_read that's
205     * fine and you can reset or free buf, but you risk missing some
206     * read-but-not-reported data.
207     */
208
209 /*----- some handy macros -----*/
210
211 #define MINMAX(ae,be,op) ({                     \
212         typeof((ae)) a=(ae);                    \
213         typeof((be)) b=(be);                    \
214         a op b ? a : b;                         \
215     })
216 #define MAX(a,b) MINMAX((a),(b),>)
217 #define MIN(a,b) MINMAX((a),(b),<)
218
219 #define MAX_RAW(a,b) ((a)>(b)?(a):(b))
220 #define MIN_RAW(a,b) ((a)<(b)?(a):(b))
221
222 static inline bool_t iswouldblock(int e)
223     { return e==EWOULDBLOCK || e==EAGAIN; }
224
225 #endif /* util_h */