poll: Document reentrancy restriction on before()
[secnet] / 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_copy(struct buffer_if *dst, const struct buffer_if *src);
27 extern void *buf_append(struct buffer_if *buf, int32_t amount);
28 extern void *buf_prepend(struct buffer_if *buf, int32_t amount);
29 extern void *buf_unappend(struct buffer_if *buf, int32_t amount);
30 extern void *buf_unprepend(struct buffer_if *buf, int32_t amount);
31
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
57 static inline int32_t buf_remaining_space(const struct buffer_if *buf)
58 {
59 return (buf->base + buf->alloclen) - (buf->start + buf->size);
60 }
61
62 extern void buffer_readonly_view(struct buffer_if *n, const void*, int32_t len);
63 extern 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
68 extern void buf_append_string(struct buffer_if *buf, cstring_t s);
69
70 extern void read_mpbin(MP_INT *a, uint8_t *bin, int binsize);
71
72 extern char *write_mpstring(MP_INT *a);
73
74 extern int32_t write_mpbin(MP_INT *a, uint8_t *buffer, int32_t buflen);
75
76 extern struct log_if *init_log(list_t *loglist);
77
78 extern 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
82 extern int consttime_memeq(const void *s1, const void *s2, size_t n);
83
84 const char *iaddr_to_string(const union iaddr *ia);
85 int iaddr_socklen(const union iaddr *ia);
86
87 void string_item_to_iaddr(const item_t *item, uint16_t port, union iaddr *ia,
88 const char *desc);
89
90 #define MINMAX(ae,be,op) ({ \
91 typeof((ae)) a=(ae); \
92 typeof((be)) b=(be); \
93 a op b ? a : b; \
94 })
95 #define MAX(a,b) MINMAX((a),(b),>)
96 #define MIN(a,b) MINMAX((a),(b),<)
97
98 #endif /* util_h */