Provide a `--pidfile' option in `fw'.
[fwd] / addr.h
CommitLineData
479a34d9 1/* -*-c-*-
2 *
7481fc9c 3 * $Id: addr.h,v 1.5 2004/04/08 01:36:25 mdw Exp $
479a34d9 4 *
5 * Generic interface to network address handlers
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the `fw' port forwarder.
13 *
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
479a34d9 29#ifndef ADDR_H
30#define ADDR_H
31
32#ifdef __cplusplus
33 extern "C" {
34#endif
35
36/*----- Header files ------------------------------------------------------*/
37
e0ce9d38 38#include <sys/types.h>
479a34d9 39#include <sys/socket.h>
40
41#include <mLib/dstr.h>
0ac54f22 42#include <mLib/conn.h>
479a34d9 43
44#ifndef REFFD_H
45# include "reffd.h"
46#endif
47
0ac54f22 48#ifndef ENDPT_H
49# include "endpt.h"
50#endif
51
479a34d9 52#ifndef SCAN_H
53# include "scan.h"
54#endif
55
56/*----- Data structures ---------------------------------------------------*/
57
58/* --- A generic socket address --- *
59 *
60 * Not all systems understand @sa_len@ fields. (In particular, Linux
61 * doesn't.) Some fairly ugly hacking is then performed on particular
62 * address types.
63 */
64
65typedef struct addr {
66 struct addr_ops *ops;
67 size_t sz;
68} addr;
69
479a34d9 70#define ADDRSZ(sz) (sizeof(addr) + (sz))
71
72/* --- Address configuration --- *
73 *
74 * An address family will want to extend this.
75 */
76
77typedef struct addr_opts {
78 unsigned f;
79} addr_opts;
80
81#define ADDRF_NOLOG 1u
82
83/* --- Address types --- *
84 *
85 * For things like Internet addresses, source and destinations look
86 * different.
87 */
88
89enum {
90 ADDR_SRC,
0ac54f22 91 ADDR_DEST,
92 ADDR_GLOBAL
479a34d9 93};
94
95/* --- Description of an address type handler --- */
96
97typedef struct addr_ops {
98 const char *name; /* Protocol's internal name */
479a34d9 99
100 /* --- @read@ --- *
101 *
102 * Arguments: @scanner *sc@ = pointer to scanner to read from
103 * @unsigned type@ = type of address to be read
104 *
105 * Returns: A filled-in socket address.
106 *
107 * Use: Parses a textual representation of a socket address.
108 */
109
110 addr *(*read)(scanner */*sc*/, unsigned /*type*/);
111
112 /* --- @destroy@ --- *
113 *
114 * Arguments: @addr *a@ = pointer to an address block
115 *
116 * Returns: ---
117 *
118 * Use: Disposes of an address block in some suitable fashion.
119 */
120
121 void (*destroy)(addr */*a*/);
122
123 /* --- @print@ --- *
124 *
125 * Arguments: @addr *a@ = pointer to socket address to read
126 * @unsigned type@ = type of address to be written
127 * @dstr *d@ = string on which to write the description
128 *
129 * Returns: ---
130 *
131 * Use: Writes a textual representation of a socket address to
132 * a string.
133 */
134
135 void (*print)(addr */*a*/, unsigned /*type*/, dstr */*d*/);
136
0ac54f22 137 /* --- @initsrcopts@ --- *
479a34d9 138 *
139 * Arguments: ---
140 *
141 * Returns: A pointer to a protocol-specific data block for a listener
142 *
143 * Use: Creates a data block for a listener. This is attached to the
144 * listener data structure. Options can then be requested, and
145 * are added to the block when necessary.
146 */
147
0ac54f22 148 addr_opts *(*initsrcopts)(void);
479a34d9 149
150 /* --- @option@ --- *
151 *
152 * Arguments: @scanner *sc@ = pointer to a scanner to read from
0ac54f22 153 * @unsigned type@ = kind of option this is
479a34d9 154 * @addr_opts *ao@ = data block to modify (from @init@), or null
155 *
156 * Returns: Nonzero to claim the option.
157 *
0ac54f22 158 * Use: Parses a source option, either global or listener-specific.
159 */
160
161 int (*option)(scanner */*sc*/, addr_opts */*ao*/, unsigned /*type*/);
162
ee599f55 163 /* --- @confirm@ --- *
164 *
165 * Arguments: @addr *a@ = pointer to an address structure
166 * @unsigned type@ = kind of address this is
167 * @addr_opts *ao@ = address options
168 *
169 * Returns: ---
170 *
171 * Use: Called during initialization when an address is fully
172 * configured.
173 */
174
175 void (*confirm)(addr */*a*/, unsigned /*type*/, addr_opts */*ao*/);
176
0ac54f22 177 /* --- @freesrcopts@ --- *
178 *
179 * Arguments: @addr_opts *ao@ = data block to remove
180 *
181 * Returns: ---
182 *
183 * Use: Throws away all the configuration data for an address type.
184 */
185
186 void (*freesrcopts)(addr_opts */*ao*/);
187
188 /* --- @bind@ --- *
189 *
190 * Arguments: @addr *a@ = the address to bind to
191 * @addr_opts *ao@ = the address options
192 *
193 * Returns: File descriptor of bound socket if OK, or @-1@ on error.
194 *
195 * Use: Binds a listening socket. The tedious stuff with @listen@
196 * isn't necessary.
197 */
198
199 int (*bind)(addr */*a*/, addr_opts */*ao*/);
200
201 /* --- @unbind@ --- *
202 *
203 * Arguments: @addr *a@ = pointer to an address
204 *
205 * Returns: ---
206 *
207 * Use: Unbinds an address. This is used when tidying up. The main
208 * purpose is to let the Unix-domain handler remove its socket
209 * node from the filesystem.
479a34d9 210 */
211
0ac54f22 212 void (*unbind)(addr */*a*/);
479a34d9 213
214 /* --- @accept@ --- *
215 *
216 * Arguments: @int fd@ = listening file descriptor
217 * @addr_opts *ao@ = data block to get configuration from
218 * @const char *desc@ = description of the listener
219 *
220 * Returns: Pointer to a reference counted file descriptor.
221 *
222 * Use: Accepts, verifies and logs an incoming connection.
223 */
224
225 reffd *(*accept)(int /*fd*/, addr_opts */*ao*/, const char */*desc*/);
226
0ac54f22 227 /* --- @inittargopts@ --- *
479a34d9 228 *
0ac54f22 229 * Arguments: ---
479a34d9 230 *
0ac54f22 231 * Returns: A pointer to a protocol-specific data block for a connecter
479a34d9 232 *
0ac54f22 233 * Use: Creates a data block for a target. This is attached to the
234 * target data structure. Options can then be requested, and
235 * are added to the block when necessary.
479a34d9 236 */
237
0ac54f22 238 addr_opts *(*inittargopts)(void);
479a34d9 239
0ac54f22 240 /* --- @freetargopts@ --- *
479a34d9 241 *
0ac54f22 242 * Arguments: @addr_opts *ao@ = data block to remove
479a34d9 243 *
244 * Returns: ---
245 *
0ac54f22 246 * Use: Throws away all the configuration data for an address type.
479a34d9 247 */
248
0ac54f22 249 void (*freetargopts)(addr_opts */*ao*/);
479a34d9 250
0ac54f22 251 /* --- @connect@ --- *
479a34d9 252 *
0ac54f22 253 * Arguments: @addr *a@ = destination address
254 * @addr_opts *ao@ = target address options
255 * @conn *c@ = connection structure
256 * @endpt *e@ = endpoint structure
479a34d9 257 *
0ac54f22 258 * Returns: Zero if OK, @-1@ on some error.
479a34d9 259 *
0ac54f22 260 * Use: Requests that a connection be made, or at least set in
261 * motion. An address may do one of these things:
262 *
263 * * Return @-1@.
264 *
265 * * Call @starget_connected@ with @-1@ or a connected file
266 * descriptor and the pointer @e@.
267 *
268 * * Call @conn_init@ or @conn_fd@, giving @starget_connected@
269 * and @e@ as the function to call.
479a34d9 270 */
271
0ac54f22 272 int (*connect)(addr */*a*/, addr_opts */*ao*/, conn */*c*/, endpt */*e*/);
479a34d9 273
274} addr_ops;
275
276/*----- That's all, folks -------------------------------------------------*/
277
278#ifdef __cplusplus
279 }
280#endif
281
282#endif