chiark / gitweb /
site: When reporting resolution complete, print only iaddr
[secnet.git] / util.h
1 #ifndef util_h
2 #define util_h
3
4 #include "secnet.h"
5 #include <gmp.h>
6
7 #include "hackypar.h"
8
9 #define BUF_ASSERT_FREE(buf) do { buffer_assert_free((buf), \
10                                                      __FILE__,__LINE__); } \
11 while(0)
12 #define BUF_ASSERT_USED(buf) do { buffer_assert_used((buf), \
13                                                      __FILE__,__LINE__); } \
14 while(0)
15 #define BUF_ALLOC(buf,own) do { buffer_assert_free((buf),__FILE__,__LINE__); \
16          (buf)->free=False; (buf)->owner=(own); (buf)->start=(buf)->base; \
17          (buf)->size=0; } while(0)
18 #define BUF_FREE(buf) do { (buf)->free=True; } while(0)
19
20 extern void buffer_assert_free(struct buffer_if *buffer, cstring_t file,
21                                int line);
22 extern void buffer_assert_used(struct buffer_if *buffer, cstring_t file,
23                                int line);
24 extern void buffer_new(struct buffer_if *buffer, int32_t len);
25 extern void buffer_init(struct buffer_if *buffer, int32_t max_start_pad);
26 extern void buffer_destroy(struct buffer_if *buffer);
27 extern void buffer_copy(struct buffer_if *dst, const struct buffer_if *src);
28 extern void *buf_append(struct buffer_if *buf, int32_t amount);
29 extern void *buf_prepend(struct buffer_if *buf, int32_t amount);
30 extern void *buf_unappend(struct buffer_if *buf, int32_t amount);
31 extern void *buf_unprepend(struct buffer_if *buf, int32_t amount);
32
33 /*
34  * void BUF_ADD_BYTES(append,    struct buffer_if*, const void*, int32_t size);
35  * void BUF_ADD_BYTES(prepend,   struct buffer_if*, const void*, int32_t size);
36  * void BUF_GET_BYTES(unappend,  struct buffer_if*,       void*, int32_t size);
37  * void BUF_GET_BYTES(unprepend, struct buffer_if*,       void*, int32_t size);
38  *     // all of these evaluate size twice
39  *
40  * void BUF_ADD_OBJ(append,    struct_buffer_if*, const OBJECT& something);
41  * void BUF_ADD_OBJ(prepend,   struct_buffer_if*, const OBJECT& something);
42  * void BUF_GET_OBJ(unappend,  struct_buffer_if*,       OBJECT& something);
43  * void BUF_GET_OBJ(unprepend, struct_buffer_if*,       OBJECT& something);
44  */
45 #define BUF_ADD_BYTES(appendprepend, bufp, datap, size)                 \
46     (buf_un##appendprepend /* ensures we have correct direction */,     \
47      memcpy(buf_##appendprepend((bufp),(size)),(datap),(size)))
48 #define BUF_ADD_OBJ(appendprepend, bufp, obj) \
49     BUF_ADD_BYTES(appendprepend,(bufp),&(obj),sizeof((obj)))
50 #define BUF_GET_BYTES(unappendunprepend, bufp, datap, size)             \
51     (BUF_GET__DOESNOTEXIST__buf_un##unappendunprepend,                  \
52      memcpy((datap),buf_##unappendunprepend((bufp),(size)),(size)))
53 #define BUF_GET_OBJ(unappendunprepend, bufp, obj) \
54     BUF_ADD_BYTES(unappendunprepend,&(obj),(bufp),sizeof((obj)))
55 #define BUF_GET__DOESNOTEXIST__buf_ununappend  0
56 #define BUF_GET__DOESNOTEXIST__buf_ununprepend 0
57
58 static inline int32_t buf_remaining_space(const struct buffer_if *buf)
59 {
60     return (buf->base + buf->alloclen) - (buf->start + buf->size);
61 }
62
63 extern void buffer_readonly_view(struct buffer_if *n, const void*, int32_t len);
64 extern void buffer_readonly_clone(struct buffer_if *n, const struct buffer_if*);
65   /* Caller must only use unappend, unprepend et al. on n.
66    * New buffer state (in n) before this can be undefined.  After use,
67    * it must NOT be freed. */
68
69 extern void buf_append_string(struct buffer_if *buf, cstring_t s);
70
71 extern void read_mpbin(MP_INT *a, uint8_t *bin, int binsize);
72
73 extern char *write_mpstring(MP_INT *a);
74
75 extern int32_t write_mpbin(MP_INT *a, uint8_t *buffer, int32_t buflen);
76
77 extern struct log_if *init_log(list_t *loglist);
78
79 extern void send_nak(const struct comm_addr *dest, uint32_t our_index,
80                      uint32_t their_index, uint32_t msgtype,
81                      struct buffer_if *buf, const char *logwhy);
82
83 extern int consttime_memeq(const void *s1, const void *s2, size_t n);
84
85 const char *iaddr_to_string(const union iaddr *ia);
86 int iaddr_socklen(const union iaddr *ia);
87
88 void string_item_to_iaddr(const item_t *item, uint16_t port, union iaddr *ia,
89                           const char *desc);
90
91
92 /*
93  * SBUF_DEFINE(int nbufs, size_t size);
94  *   // Generates a number of definitions and statements organising
95  *   // nbufs rotating char[size] buffers such that subsequent code
96  *   // may refer to:
97  * char *const SBUF;
98  */
99 #define SBUF_DEFINE(nbufs, size)                        \
100     static int static_bufs__bufnum;                     \
101     static char static_bufs__bufs[(nbufs)][(size)];     \
102     static_bufs__bufnum++;                              \
103     static_bufs__bufnum %= (nbufs);                     \
104     static_bufs__bufs[static_bufs__bufnum]
105 #define SBUF (static_bufs__bufs[static_bufs__bufnum])
106
107 /*----- line-buffered asynch input -----*/
108
109 enum async_linebuf_result {
110     async_linebuf_nothing,
111     async_linebuf_ok,
112     async_linebuf_eof,
113     async_linebuf_broken,
114 };
115
116 const char *pollbadbit(int revents); /* returns 0, or bad bit description */
117
118 enum async_linebuf_result
119 async_linebuf_read(struct pollfd *pfd, struct buffer_if *buf,
120                    const char **emsg_out);
121    /* Implements reading whole lines, asynchronously.  Use like
122     * this:
123     *   - set up the fd, which should be readable, O_NONBLOCK
124     *   - set up and initialise buffer, which should be big enough
125     *     for one line plus its trailing newline, and be empty
126     *     with start==base
127     *   - in your beforepoll_fn, be interested in POLLIN
128     *   - in your afterpoll_fn, repeatedly call this function
129     *     until it doesn't return `nothing'
130     *   - after you're done, simply close fd and free or reset buf
131     * State on return from async_linebuf_read depends on return value:
132     *
133     *   async_linebuf_nothing:
134     *
135     *      No complete lines available right now.  You should return
136     *      from afterpoll.  buf should be left untouched until the
137     *      next call to async_linebuf_read.
138     *
139     *   async_linebuf_ok:
140     *
141     *      buf->base contains a input line as a nul-terminated string
142     *      (\n replaced by \0); *emsg_out==0.  You must call
143     *      async_linebuf_read again before returning from afterpoll.
144     *
145     *   async_linebuf_eof:
146     *
147     *      EOF on stream.  buf->base contains any partial
148     *      (non-newline-terminated) line; *emsg_out!=0 iff there was
149     *      such a partial line.  You can call async_linebuf_read again
150     *      if you like but it will probably just return eof again.
151     *
152     *   broken:
153     *
154     *      Fatal problem (might be overly long lines, nuls in input
155     *      data, bad bits in pfd->revents, errors from read, etc.)
156     *
157     *      *emsg_out is the error message describing the problem;
158     *      this message might be stored in buf, might be from
159     *      strerror, or might be a constant.
160     *
161     *      You must not call async_linebuf_read again.  buf contents
162     *      is undefined: it is only safe to reset or free.
163     *
164     * While using this function, do not look at buf->start or ->size
165     * or anything after the first '\0' in buf.
166     *
167     * If you decide to stop reading with async_linebuf_read that's
168     * fine and you can reset or free buf, but you risk missing some
169     * read-but-not-reported data.
170     */
171
172 /*----- some handy macros -----*/
173
174 #define MINMAX(ae,be,op) ({                     \
175         typeof((ae)) a=(ae);                    \
176         typeof((be)) b=(be);                    \
177         a op b ? a : b;                         \
178     })
179 #define MAX(a,b) MINMAX((a),(b),>)
180 #define MIN(a,b) MINMAX((a),(b),<)
181
182 #define MAX_RAW(a,b) ((a)>(b)?(a):(b))
183 #define MIN_RAW(a,b) ((a)<(b)?(a):(b))
184
185 static inline bool_t iswouldblock(int e)
186     { return e==EWOULDBLOCK || e==EAGAIN; }
187
188 #endif /* util_h */