chiark / gitweb /
comm: Break out some common udp parts
[secnet.git] / comm-common.h
1
2 #ifndef COMM_COMMON_H
3 #define COMM_COMMON_H
4
5 #include "secnet.h"
6
7 /*----- for all comms -----*/
8
9 struct comm_notify_entry {
10     comm_notify_fn *fn;
11     void *state;
12     LIST_ENTRY(comm_notify_entry) entry;
13 };
14 LIST_HEAD(comm_notify_list, comm_notify_entry) notify;
15
16 struct commcommon { /* must be first so that void* is comm_common* */
17     closure_t cl;
18     struct comm_if ops;
19     struct cloc loc;
20     struct comm_notify_list notify;
21     struct buffer_if *rbuf;
22 };
23
24 void comm_request_notify(void *commst, void *nst, comm_notify_fn *fn);
25 void comm_release_notify(void *commst, void *nst, comm_notify_fn *fn);
26
27 bool_t comm_notify(struct comm_notify_list *notify, struct buffer_if *buf,
28                    const struct comm_addr *ca);
29   /* Either: returns True, with message delivered and buffer freed.
30    * Or: False, if no-one wanted it - buffer still allocd'd.
31    * Ie, like comm_notify_fn. */
32
33 void comm_apply(struct commcommon *cc);
34
35 #define COMM_APPLY(st,cc,prefix,desc,loc)               \
36     (st)=safe_malloc(sizeof(*(st)), desc "_apply");     \
37     (cc)->loc=loc;                                      \
38     (cc)->cl.description=desc;                          \
39     (cc)->ops.sendmsg=prefix##sendmsg;                  \
40     (cc)->ops.addr_to_string=prefix##addr_to_string;    \
41     comm_apply((cc))
42    /* Expects in scope: prefix##sendmsg, prefix##addr_to_string. */
43
44 #define COMM_APPLY_STANDARD(st,cc,desc,args)                            \
45     item_t *item=list_elem(args,0);                                     \
46     if (!item || item->type!=t_dict) {                                  \
47         cfgfatal((cc)->loc,desc,"first argument must be a dictionary\n"); \
48     }                                                                   \
49     dict_t *d=item->data.dict;                                          \
50     (cc)->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,desc,(cc)->loc)
51
52 /*----- for udp-based comms -----*/
53
54 #define UDP_MAX_SOCKETS 3 /* 2 ought to do really */
55
56 struct udpsock {
57     union iaddr addr;
58     int fd;
59 };
60
61 struct udpsocks {
62     int n_socks;
63     struct udpsock socks[UDP_MAX_SOCKETS];
64 };
65
66 struct udpcommon {
67     struct commcommon cc;
68     int port;
69     string_t authbind;
70     bool_t use_proxy;
71     union iaddr proxy;
72 };
73
74 int udp_socks_beforepoll(struct udpsocks *s,
75                          struct pollfd *fds, int *nfds_io,
76                          int *timeout_io);
77
78 void udp_socks_afterpoll(struct udpcommon *u, struct udpsocks *s,
79                          struct pollfd *fds, int nfds);
80
81 #define UDP_APPLY_STANDARD(st,uc,desc)                                  \
82     (uc)->use_proxy=False;                                              \
83     (uc)->authbind=NULL;                                                \
84     (uc)->port=dict_read_number(d,"port",True,"udp",(uc)->cc.loc,0)
85     /* Expects in scope:  dict_t *d=...; */
86
87 #endif /*COMM_COMMON_H*/