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