3 * $Id: addr.h,v 1.4 2003/11/29 20:36:07 mdw Exp $
5 * Generic interface to network address handlers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.4 2003/11/29 20:36:07 mdw
33 * Privileged outgoing connections.
35 * Revision 1.3 2003/11/25 14:08:23 mdw
36 * Debianization. Socket target options. Internet binding.
38 * Revision 1.2 1999/07/27 18:30:53 mdw
39 * Various minor portability fixes.
41 * Revision 1.1 1999/07/26 23:34:26 mdw
42 * Socket address type management.
53 /*----- Header files ------------------------------------------------------*/
55 #include <sys/types.h>
56 #include <sys/socket.h>
58 #include <mLib/dstr.h>
59 #include <mLib/conn.h>
73 /*----- Data structures ---------------------------------------------------*/
75 /* --- A generic socket address --- *
77 * Not all systems understand @sa_len@ fields. (In particular, Linux
78 * doesn't.) Some fairly ugly hacking is then performed on particular
87 #define ADDRSZ(sz) (sizeof(addr) + (sz))
89 /* --- Address configuration --- *
91 * An address family will want to extend this.
94 typedef struct addr_opts
{
98 #define ADDRF_NOLOG 1u
100 /* --- Address types --- *
102 * For things like Internet addresses, source and destinations look
112 /* --- Description of an address type handler --- */
114 typedef struct addr_ops
{
115 const char *name
; /* Protocol's internal name */
119 * Arguments: @scanner *sc@ = pointer to scanner to read from
120 * @unsigned type@ = type of address to be read
122 * Returns: A filled-in socket address.
124 * Use: Parses a textual representation of a socket address.
127 addr
*(*read
)(scanner */
*sc*/
, unsigned /*type*/);
129 /* --- @destroy@ --- *
131 * Arguments: @addr *a@ = pointer to an address block
135 * Use: Disposes of an address block in some suitable fashion.
138 void (*destroy
)(addr */
*a*/
);
142 * Arguments: @addr *a@ = pointer to socket address to read
143 * @unsigned type@ = type of address to be written
144 * @dstr *d@ = string on which to write the description
148 * Use: Writes a textual representation of a socket address to
152 void (*print
)(addr */
*a*/
, unsigned /*type*/, dstr */
*d*/
);
154 /* --- @initsrcopts@ --- *
158 * Returns: A pointer to a protocol-specific data block for a listener
160 * Use: Creates a data block for a listener. This is attached to the
161 * listener data structure. Options can then be requested, and
162 * are added to the block when necessary.
165 addr_opts
*(*initsrcopts
)(void);
167 /* --- @option@ --- *
169 * Arguments: @scanner *sc@ = pointer to a scanner to read from
170 * @unsigned type@ = kind of option this is
171 * @addr_opts *ao@ = data block to modify (from @init@), or null
173 * Returns: Nonzero to claim the option.
175 * Use: Parses a source option, either global or listener-specific.
178 int (*option
)(scanner */
*sc*/
, addr_opts */
*ao*/
, unsigned /*type*/);
180 /* --- @confirm@ --- *
182 * Arguments: @addr *a@ = pointer to an address structure
183 * @unsigned type@ = kind of address this is
184 * @addr_opts *ao@ = address options
188 * Use: Called during initialization when an address is fully
192 void (*confirm
)(addr */
*a*/
, unsigned /*type*/, addr_opts */
*ao*/
);
194 /* --- @freesrcopts@ --- *
196 * Arguments: @addr_opts *ao@ = data block to remove
200 * Use: Throws away all the configuration data for an address type.
203 void (*freesrcopts
)(addr_opts */
*ao*/
);
207 * Arguments: @addr *a@ = the address to bind to
208 * @addr_opts *ao@ = the address options
210 * Returns: File descriptor of bound socket if OK, or @-1@ on error.
212 * Use: Binds a listening socket. The tedious stuff with @listen@
216 int (*bind
)(addr */
*a*/
, addr_opts */
*ao*/
);
218 /* --- @unbind@ --- *
220 * Arguments: @addr *a@ = pointer to an address
224 * Use: Unbinds an address. This is used when tidying up. The main
225 * purpose is to let the Unix-domain handler remove its socket
226 * node from the filesystem.
229 void (*unbind
)(addr */
*a*/
);
231 /* --- @accept@ --- *
233 * Arguments: @int fd@ = listening file descriptor
234 * @addr_opts *ao@ = data block to get configuration from
235 * @const char *desc@ = description of the listener
237 * Returns: Pointer to a reference counted file descriptor.
239 * Use: Accepts, verifies and logs an incoming connection.
242 reffd
*(*accept
)(int /*fd*/, addr_opts */
*ao*/
, const char */
*desc*/
);
244 /* --- @inittargopts@ --- *
248 * Returns: A pointer to a protocol-specific data block for a connecter
250 * Use: Creates a data block for a target. This is attached to the
251 * target data structure. Options can then be requested, and
252 * are added to the block when necessary.
255 addr_opts
*(*inittargopts
)(void);
257 /* --- @freetargopts@ --- *
259 * Arguments: @addr_opts *ao@ = data block to remove
263 * Use: Throws away all the configuration data for an address type.
266 void (*freetargopts
)(addr_opts */
*ao*/
);
268 /* --- @connect@ --- *
270 * Arguments: @addr *a@ = destination address
271 * @addr_opts *ao@ = target address options
272 * @conn *c@ = connection structure
273 * @endpt *e@ = endpoint structure
275 * Returns: Zero if OK, @-1@ on some error.
277 * Use: Requests that a connection be made, or at least set in
278 * motion. An address may do one of these things:
282 * * Call @starget_connected@ with @-1@ or a connected file
283 * descriptor and the pointer @e@.
285 * * Call @conn_init@ or @conn_fd@, giving @starget_connected@
286 * and @e@ as the function to call.
289 int (*connect
)(addr */
*a*/
, addr_opts */
*ao*/
, conn */
*c*/
, endpt */
*e*/
);
293 /*----- That's all, folks -------------------------------------------------*/