chiark / gitweb /
comm: Break out common code in comm
[secnet.git] / comm-common.h
1
2 #ifndef COMM_COMMON_H
3 #define COMM_COMMON_H
4
5 #include "secnet.h"
6
7 struct comm_notify_entry {
8     comm_notify_fn *fn;
9     void *state;
10     LIST_ENTRY(comm_notify_entry) entry;
11 };
12 LIST_HEAD(comm_notify_list, comm_notify_entry) notify;
13
14 struct commcommon { /* must be first so that void* is comm_common* */
15     closure_t cl;
16     struct comm_if ops;
17     struct cloc loc;
18     struct comm_notify_list notify;
19     struct buffer_if *rbuf;
20 };
21
22 void comm_request_notify(void *commst, void *nst, comm_notify_fn *fn);
23 void comm_release_notify(void *commst, void *nst, comm_notify_fn *fn);
24
25 bool_t comm_notify(struct comm_notify_list *notify, struct buffer_if *buf,
26                    const struct comm_addr *ca);
27   /* Either: returns True, with message delivered and buffer freed.
28    * Or: False, if no-one wanted it - buffer still allocd'd.
29    * Ie, like comm_notify_fn. */
30
31 void comm_apply(struct commcommon *cc, void *st);
32
33 #define COMM_APPLY(st,cc,prefix,desc,loc)               \
34     (st)=safe_malloc(sizeof(*(st)), desc "_apply");     \
35     (cc)->loc=loc;                                      \
36     (cc)->cl.description=desc;                          \
37     (cc)->ops.sendmsg=prefix##sendmsg;                  \
38     (cc)->ops.addr_to_string=prefix##addr_to_string;    \
39     comm_apply((cc),(st))
40    /* void COMM_APPLY(SOMETHING *st, struct commcommon *FUNCTIONOF(st),
41     *                 prefix, "DESC", struct cloc loc);
42     *   // Expects in scope: prefix##sendmsg, prefix##addr_to_string.
43     */
44
45 #define COMM_APPLY_STANDARD(st,cc,desc,args)                            \
46     item_t *item=list_elem(args,0);                                     \
47     if (!item || item->type!=t_dict) {                                  \
48         cfgfatal((cc)->loc,desc,"first argument must be a dictionary\n"); \
49     }                                                                   \
50     dict_t *d=item->data.dict;                                          \
51     (cc)->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,desc,(cc)->loc)
52     /* void COMM_APPLY_STANDARD(SOMETHING *st, struct commcommon *cc,
53      *                          const char *desc, list_t *args);
54      *   // Declares:
55      *   //    item_t *item = <undefined>;
56      *   //    dict_t *dict = <construction dictionary argument>;
57      */
58
59 #endif /*COMM_COMMON_H*/