chiark / gitweb /
Makefiles: Use Final.sd.mk to implementing RECHECK_RM
[secnet.git] / comm-common.h
1 /*
2  * This file is part of secnet.
3  * See README for full list of copyright holders.
4  *
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.
9  * 
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.
14  * 
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.
18  */
19
20 #ifndef COMM_COMMON_H
21 #define COMM_COMMON_H
22
23 #include "secnet.h"
24 #include "util.h"
25
26 /*----- for all comms -----*/
27
28 struct comm_notify_entry {
29     comm_notify_fn *fn;
30     void *state;
31     LIST_ENTRY(comm_notify_entry) entry;
32 };
33 LIST_HEAD(comm_notify_list, comm_notify_entry) notify;
34
35 struct commcommon { /* must be first so that void* is comm_common* */
36     closure_t cl;
37     struct comm_if ops;
38     struct cloc loc;
39     struct comm_notify_list notify;
40     struct buffer_if *rbuf;
41     struct priomsg why_unwanted;
42 };
43
44 struct comm_clientinfo *comm_clientinfo_ignore(void *state, dict_t*,
45                                                struct cloc cloc);
46 void comm_request_notify(void *commst, void *nst, comm_notify_fn *fn);
47 void comm_release_notify(void *commst, void *nst, comm_notify_fn *fn);
48
49 bool_t comm_notify(struct commcommon*, struct buffer_if *buf,
50                    const struct comm_addr *ca);
51   /* Either: returns True, with message delivered and buffer freed.
52    * Or: False, if no-one wanted it - buffer still allocd'd;
53    *     in that case, cc->why_unwanted has info
54    * Ie, roughly like comm_notify_fn. */
55
56 void comm_apply(struct commcommon *cc, void *st);
57
58 #define COMM_APPLY(st,cc,prefix,desc,loc)               \
59     NEW(st);                                            \
60     (cc)->loc=loc;                                      \
61     (cc)->cl.description=desc;                          \
62     (cc)->ops.clientinfo=comm_clientinfo_ignore;        \
63     (cc)->ops.sendmsg=prefix##sendmsg;                  \
64     (cc)->ops.addr_to_string=prefix##addr_to_string;    \
65     comm_apply((cc),(st))
66    /* void COMM_APPLY(SOMETHING *st, struct commcommon *FUNCTIONOF(st),
67     *                 prefix, "DESC", struct cloc loc);
68     *   // Expects in scope: prefix##sendmsg, prefix##addr_to_string.
69     */
70
71 #define COMM_APPLY_STANDARD(st,cc,desc,args)                            \
72     item_t *item=list_elem(args,0);                                     \
73     if (!item || item->type!=t_dict) {                                  \
74         cfgfatal((cc)->loc,desc,"first argument must be a dictionary\n"); \
75     }                                                                   \
76     dict_t *d=item->data.dict;                                          \
77     (cc)->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,desc,(cc)->loc)
78     /* void COMM_APPLY_STANDARD(SOMETHING *st, struct commcommon *cc,
79      *                          const char *desc, list_t *args);
80      *   // Declares:
81      *   //    item_t *item = <undefined>;
82      *   //    dict_t *dict = <construction dictionary argument>;
83      */
84
85 /*----- for udp-based comms -----*/
86
87 #define UDP_MAX_SOCKETS 3 /* 2 ought to do really */
88
89 #define MAX_AF MAX_RAW(AF_INET6,AF_INET)
90
91 struct udpsock {
92     union iaddr addr;
93     int fd;
94     bool_t experienced[/*0=recv,1=send*/2][MAX_AF+1][/*success?*/2];
95 };
96
97 struct udpsocks {
98     int n_socks;
99     struct udpsock socks[UDP_MAX_SOCKETS];
100     /* private for udp_socks_* */
101     struct udpcommon *uc; /* link to parent, for cfg, notify list, etc. */
102     struct poll_interest *interest;
103     const char *desc;
104 };
105
106 struct udpcommon {
107     struct commcommon cc;
108     int port;
109     string_t authbind;
110     bool_t use_proxy;
111     union iaddr proxy;
112 };
113
114 bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
115                        int failmsgclass);
116   /* Caller should have filled in ->addr.  Fills in us->fd,
117      ->experienced; updates ->addr.  Logs any errors with lg_[v]perror. */
118 bool_t udp_import_socket(struct udpcommon *uc, struct udpsock *us,
119                          int failmsgclass, int fd);
120   /* Like udp_make_socket, but caller provides fd.  fd is not closed
121      on error */
122
123 void udp_destroy_socket(struct udpcommon *uc, struct udpsock *us);
124   /* Idempotent.  No errors are possible. */
125
126 const char *af_name(int af);
127 void udp_sock_experienced(struct log_if *lg, struct udpcommon *uc,
128                           struct udpsocks *socks, struct udpsock *us,
129                           const union iaddr *dest, int af /* 0 means any */,
130                           int r, int errnoval);
131
132 void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks,
133                         const char *desc);
134 void udp_socks_deregister(struct udpcommon *uc, struct udpsocks *socks);
135 void udp_socks_childpersist(struct udpcommon *uc, struct udpsocks *socks);
136
137 #define UDP_APPLY_STANDARD(st,uc,desc)                                  \
138     (uc)->use_proxy=False;                                              \
139     (uc)->authbind=dict_read_string(d,"authbind",False,"udp",(uc)->cc.loc); \
140     (uc)->port=dict_read_number(d,"port",False,"udp",(uc)->cc.loc,0)
141     /* void UDP_APPLY_STANDARD(SOMETHING *st, struct udpcommon *uc,
142      *                         const char *desc);
143      *   // Expects in scope:  dict_t *d=...;   as from COMM_APPLY_STANDARD
144      */
145
146 #endif /*COMM_COMMON_H*/