Commit | Line | Data |
---|---|---|
c215a4bc IJ |
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 | |
9c6a8729 | 7 | * the Free Software Foundation; either version 3 of the License, or |
c215a4bc IJ |
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 | */ | |
54d5ef00 IJ |
19 | |
20 | #ifndef COMM_COMMON_H | |
21 | #define COMM_COMMON_H | |
22 | ||
23 | #include "secnet.h" | |
2d7478f7 | 24 | #include "util.h" |
54d5ef00 | 25 | |
763e458f IJ |
26 | /*----- for all comms -----*/ |
27 | ||
54d5ef00 IJ |
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 | }; | |
42 | ||
2d80199d IJ |
43 | struct comm_clientinfo *comm_clientinfo_ignore(void *state, dict_t*, |
44 | struct cloc cloc); | |
54d5ef00 IJ |
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); | |
47 | ||
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. */ | |
53 | ||
54 | void comm_apply(struct commcommon *cc, void *st); | |
55 | ||
56 | #define COMM_APPLY(st,cc,prefix,desc,loc) \ | |
952f601f | 57 | NEW(st); \ |
54d5ef00 IJ |
58 | (cc)->loc=loc; \ |
59 | (cc)->cl.description=desc; \ | |
2d80199d | 60 | (cc)->ops.clientinfo=comm_clientinfo_ignore; \ |
54d5ef00 IJ |
61 | (cc)->ops.sendmsg=prefix##sendmsg; \ |
62 | (cc)->ops.addr_to_string=prefix##addr_to_string; \ | |
63 | comm_apply((cc),(st)) | |
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. | |
67 | */ | |
68 | ||
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"); \ | |
73 | } \ | |
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); | |
78 | * // Declares: | |
79 | * // item_t *item = <undefined>; | |
80 | * // dict_t *dict = <construction dictionary argument>; | |
81 | */ | |
82 | ||
763e458f IJ |
83 | /*----- for udp-based comms -----*/ |
84 | ||
85 | #define UDP_MAX_SOCKETS 3 /* 2 ought to do really */ | |
86 | ||
2d7478f7 IJ |
87 | #define MAX_AF MAX_RAW(AF_INET6,AF_INET) |
88 | ||
763e458f IJ |
89 | struct udpsock { |
90 | union iaddr addr; | |
91 | int fd; | |
2d7478f7 | 92 | bool_t experienced[/*0=recv,1=send*/2][MAX_AF+1][/*success?*/2]; |
763e458f IJ |
93 | }; |
94 | ||
95 | struct udpsocks { | |
96 | int n_socks; | |
97 | struct udpsock socks[UDP_MAX_SOCKETS]; | |
f7af3192 IJ |
98 | /* private for udp_socks_* */ |
99 | struct udpcommon *uc; /* link to parent, for cfg, notify list, etc. */ | |
5c679ae0 | 100 | struct poll_interest *interest; |
c72d679d | 101 | const char *desc; |
763e458f IJ |
102 | }; |
103 | ||
104 | struct udpcommon { | |
105 | struct commcommon cc; | |
106 | int port; | |
107 | string_t authbind; | |
108 | bool_t use_proxy; | |
109 | union iaddr proxy; | |
110 | }; | |
111 | ||
53f4e666 IJ |
112 | bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us, |
113 | int failmsgclass); | |
9c44ef13 IJ |
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 | |
119 | on error */ | |
53f4e666 | 120 | |
5f8b7a8e IJ |
121 | void udp_destroy_socket(struct udpcommon *uc, struct udpsock *us); |
122 | /* Idempotent. No errors are possible. */ | |
123 | ||
2d7478f7 IJ |
124 | const char *af_name(int af); |
125 | void udp_sock_experienced(struct log_if *lg, struct udpcommon *uc, | |
c72d679d | 126 | struct udpsocks *socks, struct udpsock *us, |
6c5889e6 | 127 | const union iaddr *dest, int af /* 0 means any */, |
2d7478f7 IJ |
128 | int r, int errnoval); |
129 | ||
c72d679d IJ |
130 | void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks, |
131 | const char *desc); | |
5c679ae0 | 132 | void udp_socks_deregister(struct udpcommon *uc, struct udpsocks *socks); |
32654a31 | 133 | void udp_socks_childpersist(struct udpcommon *uc, struct udpsocks *socks); |
763e458f IJ |
134 | |
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); \ | |
9c44ef13 | 138 | (uc)->port=dict_read_number(d,"port",False,"udp",(uc)->cc.loc,0) |
763e458f IJ |
139 | /* void UDP_APPLY_STANDARD(SOMETHING *st, struct udpcommon *uc, |
140 | * const char *desc); | |
141 | * // Expects in scope: dict_t *d=...; as from COMM_APPLY_STANDARD | |
142 | */ | |
143 | ||
54d5ef00 | 144 | #endif /*COMM_COMMON_H*/ |