9 #define BUF_ASSERT_FREE(buf) do { buffer_assert_free((buf), \
10 __FILE__,__LINE__); } \
12 #define BUF_ASSERT_USED(buf) do { buffer_assert_used((buf), \
13 __FILE__,__LINE__); } \
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)
20 extern void buffer_assert_free(struct buffer_if *buffer, cstring_t file,
22 extern void buffer_assert_used(struct buffer_if *buffer, cstring_t file,
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);
32 static inline int32_t buf_remaining_space(const struct buffer_if *buf)
34 return (buf->base + buf->alloclen) - (buf->start + buf->size);
37 extern void buffer_readonly_view(struct buffer_if *n, const void*, int32_t len);
38 extern void buffer_readonly_clone(struct buffer_if *n, const struct buffer_if*);
39 /* Caller must only use unappend, unprepend et al. on n.
40 * New buffer state (in n) before this can be undefined. After use,
41 * it must NOT be freed. */
43 extern void buf_append_string(struct buffer_if *buf, cstring_t s);
45 extern void read_mpbin(MP_INT *a, uint8_t *bin, int binsize);
47 extern char *write_mpstring(MP_INT *a);
49 extern int32_t write_mpbin(MP_INT *a, uint8_t *buffer, int32_t buflen);
51 extern struct log_if *init_log(list_t *loglist);
53 extern void send_nak(const struct comm_addr *dest, uint32_t our_index,
54 uint32_t their_index, uint32_t msgtype,
55 struct buffer_if *buf, const char *logwhy);
57 extern int consttime_memeq(const void *s1, const void *s2, size_t n);
59 const char *iaddr_to_string(const union iaddr *ia);
60 bool_t iaddr_equal(const union iaddr *ia, const union iaddr *ib);
61 int iaddr_socklen(const union iaddr *ia);
63 void text2iaddr(const item_t *item, uint16_t port, union iaddr *ia,
67 /*----- line-buffered asynch input -----*/
69 enum async_linebuf_result {
70 async_linebuf_nothing,
76 enum async_linebuf_result
77 async_linebuf_read(struct pollfd *pfd, struct buffer_if *buf,
78 const char **emsg_out);
79 /* Implements reading whole lines, asynchronously. Use like
81 * - set up the fd, which should be readable, O_NONBLOCK
82 * - set up and initialise buffer, which should be big enough
83 * for one lines plus its trailing newline, and be empty
85 * - in your beforepoll_fn, be interested in POLLIN
86 * - in your afterpoll_fn, repeatedly call this function
87 * until it doesn't return `nothing'
88 * - after you're done, simply close fd and free or reset buf
89 * State on exit depends on return value:
90 * nothing: don't touch buf, return from beforepoll
91 * ok: buf->base contains a input line
92 * as a nul-terminated string (\n replaced by \0);
94 * eof: buf->base contains any partial
95 * (non-newline-terminated) line;
96 * *emsg_out!=0 iff there was such a line.
97 * broken: *emsg_out is the error message describing the problem.
98 * this may be stored in buf
99 * buf contents is undefined
100 * While using this function, do not look at buf->start or ->size.
103 /*----- some handy macros -----*/
105 #define MINMAX(ae,be,op) ({ \
106 typeof((ae)) a=(ae); \
107 typeof((be)) b=(be); \
110 #define MAX(a,b) MINMAX((a),(b),>)
111 #define MIN(a,b) MINMAX((a),(b),<)
113 static inline bool_t iswouldblock(int e)
114 { return e==EWOULDBLOCK || e==EAGAIN; }