fds: Make many fds nonblocking
[secnet] / comm-common.h
CommitLineData
54d5ef00
IJ
1
2#ifndef COMM_COMMON_H
3#define COMM_COMMON_H
4
5#include "secnet.h"
6
763e458f
IJ
7/*----- for all comms -----*/
8
54d5ef00
IJ
9struct comm_notify_entry {
10 comm_notify_fn *fn;
11 void *state;
12 LIST_ENTRY(comm_notify_entry) entry;
13};
14LIST_HEAD(comm_notify_list, comm_notify_entry) notify;
15
16struct 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
24void comm_request_notify(void *commst, void *nst, comm_notify_fn *fn);
25void comm_release_notify(void *commst, void *nst, comm_notify_fn *fn);
26
27bool_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
33void comm_apply(struct commcommon *cc, void *st);
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),(st))
42 /* void COMM_APPLY(SOMETHING *st, struct commcommon *FUNCTIONOF(st),
43 * prefix, "DESC", struct cloc loc);
44 * // Expects in scope: prefix##sendmsg, prefix##addr_to_string.
45 */
46
47#define COMM_APPLY_STANDARD(st,cc,desc,args) \
48 item_t *item=list_elem(args,0); \
49 if (!item || item->type!=t_dict) { \
50 cfgfatal((cc)->loc,desc,"first argument must be a dictionary\n"); \
51 } \
52 dict_t *d=item->data.dict; \
53 (cc)->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,desc,(cc)->loc)
54 /* void COMM_APPLY_STANDARD(SOMETHING *st, struct commcommon *cc,
55 * const char *desc, list_t *args);
56 * // Declares:
57 * // item_t *item = <undefined>;
58 * // dict_t *dict = <construction dictionary argument>;
59 */
60
763e458f
IJ
61/*----- for udp-based comms -----*/
62
63#define UDP_MAX_SOCKETS 3 /* 2 ought to do really */
64
65struct udpsock {
66 union iaddr addr;
67 int fd;
68};
69
70struct udpsocks {
71 int n_socks;
72 struct udpsock socks[UDP_MAX_SOCKETS];
f7af3192
IJ
73 /* private for udp_socks_* */
74 struct udpcommon *uc; /* link to parent, for cfg, notify list, etc. */
5c679ae0 75 struct poll_interest *interest;
763e458f
IJ
76};
77
78struct udpcommon {
79 struct commcommon cc;
80 int port;
81 string_t authbind;
82 bool_t use_proxy;
83 union iaddr proxy;
84};
85
53f4e666
IJ
86bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
87 int failmsgclass);
88 /* Fills in us->fd. Logs any errors with lg_[v]perror. */
89
5f8b7a8e
IJ
90void udp_destroy_socket(struct udpcommon *uc, struct udpsock *us);
91 /* Idempotent. No errors are possible. */
92
f7af3192 93void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks);
5c679ae0 94void udp_socks_deregister(struct udpcommon *uc, struct udpsocks *socks);
763e458f
IJ
95
96#define UDP_APPLY_STANDARD(st,uc,desc) \
97 (uc)->use_proxy=False; \
98 (uc)->authbind=dict_read_string(d,"authbind",False,"udp",(uc)->cc.loc); \
99 (uc)->port=dict_read_number(d,"port",True,"udp",(uc)->cc.loc,0)
100 /* void UDP_APPLY_STANDARD(SOMETHING *st, struct udpcommon *uc,
101 * const char *desc);
102 * // Expects in scope: dict_t *d=...; as from COMM_APPLY_STANDARD
103 */
104
54d5ef00 105#endif /*COMM_COMMON_H*/