2 * This file is part of secnet.
3 * See README for full list of copyright holders.
5 * secnet is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * secnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * version 3 along with secnet; if not, see
17 * https://www.gnu.org/licenses/gpl.html.
26 /*----- for all comms -----*/
28 struct comm_notify_entry {
31 LIST_ENTRY(comm_notify_entry) entry;
33 LIST_HEAD(comm_notify_list, comm_notify_entry) notify;
35 struct commcommon { /* must be first so that void* is comm_common* */
39 struct comm_notify_list notify;
40 struct buffer_if *rbuf;
43 struct comm_clientinfo *comm_clientinfo_ignore(void *state, dict_t*,
45 void comm_request_notify(void *commst, void *nst, comm_notify_fn *fn);
46 void comm_release_notify(void *commst, void *nst, comm_notify_fn *fn);
48 bool_t comm_notify(struct comm_notify_list *notify, struct buffer_if *buf,
49 const struct comm_addr *ca);
50 /* Either: returns True, with message delivered and buffer freed.
51 * Or: False, if no-one wanted it - buffer still allocd'd.
52 * Ie, like comm_notify_fn. */
54 void comm_apply(struct commcommon *cc, void *st);
56 #define COMM_APPLY(st,cc,prefix,desc,loc) \
59 (cc)->cl.description=desc; \
60 (cc)->ops.clientinfo=comm_clientinfo_ignore; \
61 (cc)->ops.sendmsg=prefix##sendmsg; \
62 (cc)->ops.addr_to_string=prefix##addr_to_string; \
64 /* void COMM_APPLY(SOMETHING *st, struct commcommon *FUNCTIONOF(st),
65 * prefix, "DESC", struct cloc loc);
66 * // Expects in scope: prefix##sendmsg, prefix##addr_to_string.
69 #define COMM_APPLY_STANDARD(st,cc,desc,args) \
70 item_t *item=list_elem(args,0); \
71 if (!item || item->type!=t_dict) { \
72 cfgfatal((cc)->loc,desc,"first argument must be a dictionary\n"); \
74 dict_t *d=item->data.dict; \
75 (cc)->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,desc,(cc)->loc)
76 /* void COMM_APPLY_STANDARD(SOMETHING *st, struct commcommon *cc,
77 * const char *desc, list_t *args);
79 * // item_t *item = <undefined>;
80 * // dict_t *dict = <construction dictionary argument>;
83 /*----- for udp-based comms -----*/
85 #define UDP_MAX_SOCKETS 3 /* 2 ought to do really */
87 #define MAX_AF MAX_RAW(AF_INET6,AF_INET)
92 bool_t experienced[/*0=recv,1=send*/2][MAX_AF+1][/*success?*/2];
97 struct udpsock socks[UDP_MAX_SOCKETS];
98 /* private for udp_socks_* */
99 struct udpcommon *uc; /* link to parent, for cfg, notify list, etc. */
100 struct poll_interest *interest;
105 struct commcommon cc;
112 bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
114 /* Caller should have filled in ->addr. Fills in us->fd,
115 ->experienced; updates ->addr. Logs any errors with lg_[v]perror. */
116 bool_t udp_import_socket(struct udpcommon *uc, struct udpsock *us,
117 int failmsgclass, int fd);
118 /* Like udp_make_socket, but caller provides fd. fd is not closed
121 void udp_destroy_socket(struct udpcommon *uc, struct udpsock *us);
122 /* Idempotent. No errors are possible. */
124 const char *af_name(int af);
125 void udp_sock_experienced(struct log_if *lg, struct udpcommon *uc,
126 struct udpsocks *socks, struct udpsock *us,
127 const union iaddr *dest, int af /* 0 means any */,
128 int r, int errnoval);
130 void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks,
132 void udp_socks_deregister(struct udpcommon *uc, struct udpsocks *socks);
133 void udp_socks_childpersist(struct udpcommon *uc, struct udpsocks *socks);
135 #define UDP_APPLY_STANDARD(st,uc,desc) \
136 (uc)->use_proxy=False; \
137 (uc)->authbind=dict_read_string(d,"authbind",False,"udp",(uc)->cc.loc); \
138 (uc)->port=dict_read_number(d,"port",False,"udp",(uc)->cc.loc,0)
139 /* void UDP_APPLY_STANDARD(SOMETHING *st, struct udpcommon *uc,
141 * // Expects in scope: dict_t *d=...; as from COMM_APPLY_STANDARD
144 #endif /*COMM_COMMON_H*/