7da1fef28a51426c832dfab088a7d8f4255e7b9d
3 * Generic interface to network address handlers
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fw' port forwarder.
12 * `fw' is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * `fw' is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with `fw'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 /*----- Header files ------------------------------------------------------*/
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <mLib/dstr.h>
40 #include <mLib/conn.h>
54 /*----- Data structures ---------------------------------------------------*/
56 /* --- A generic socket address --- *
58 * Not all systems understand @sa_len@ fields. (In particular, Linux
59 * doesn't.) Some fairly ugly hacking is then performed on particular
68 #define ADDRSZ(sz) (sizeof(addr) + (sz))
70 /* --- Address configuration --- *
72 * An address family will want to extend this.
75 typedef struct addr_opts
{
79 #define ADDRF_NOLOG 1u
81 /* --- Address types --- *
83 * For things like Internet addresses, source and destinations look
93 /* --- Description of an address type handler --- */
95 typedef struct addr_ops
{
96 const char *name
; /* Protocol's internal name */
100 * Arguments: @scanner *sc@ = pointer to scanner to read from
101 * @unsigned type@ = type of address to be read
103 * Returns: A filled-in socket address.
105 * Use: Parses a textual representation of a socket address.
108 addr
*(*read
)(scanner */
*sc*/
, unsigned /*type*/);
110 /* --- @destroy@ --- *
112 * Arguments: @addr *a@ = pointer to an address block
116 * Use: Disposes of an address block in some suitable fashion.
119 void (*destroy
)(addr */
*a*/
);
123 * Arguments: @addr *a@ = pointer to socket address to read
124 * @unsigned type@ = type of address to be written
125 * @dstr *d@ = string on which to write the description
129 * Use: Writes a textual representation of a socket address to
133 void (*print
)(addr */
*a*/
, unsigned /*type*/, dstr */
*d*/
);
135 /* --- @initsrcopts@ --- *
139 * Returns: A pointer to a protocol-specific data block for a listener
141 * Use: Creates a data block for a listener. This is attached to the
142 * listener data structure. Options can then be requested, and
143 * are added to the block when necessary.
146 addr_opts
*(*initsrcopts
)(void);
148 /* --- @option@ --- *
150 * Arguments: @scanner *sc@ = pointer to a scanner to read from
151 * @unsigned type@ = kind of option this is
152 * @addr_opts *ao@ = data block to modify (from @init@), or null
154 * Returns: Nonzero to claim the option.
156 * Use: Parses a source option, either global or listener-specific.
159 int (*option
)(scanner */
*sc*/
, addr_opts */
*ao*/
, unsigned /*type*/);
161 /* --- @confirm@ --- *
163 * Arguments: @addr *a@ = pointer to an address structure
164 * @unsigned type@ = kind of address this is
165 * @addr_opts *ao@ = address options
169 * Use: Called during initialization when an address is fully
173 void (*confirm
)(addr */
*a*/
, unsigned /*type*/, addr_opts */
*ao*/
);
175 /* --- @freesrcopts@ --- *
177 * Arguments: @addr_opts *ao@ = data block to remove
181 * Use: Throws away all the configuration data for an address type.
184 void (*freesrcopts
)(addr_opts */
*ao*/
);
188 * Arguments: @addr *a@ = the address to bind to
189 * @addr_opts *ao@ = the address options
191 * Returns: File descriptor of bound socket if OK, or @-1@ on error.
193 * Use: Binds a listening socket. The tedious stuff with @listen@
197 int (*bind
)(addr */
*a*/
, addr_opts */
*ao*/
);
199 /* --- @unbind@ --- *
201 * Arguments: @addr *a@ = pointer to an address
205 * Use: Unbinds an address. This is used when tidying up. The main
206 * purpose is to let the Unix-domain handler remove its socket
207 * node from the filesystem.
210 void (*unbind
)(addr */
*a*/
);
212 /* --- @accept@ --- *
214 * Arguments: @int fd@ = listening file descriptor
215 * @addr_opts *ao@ = data block to get configuration from
216 * @const char *desc@ = description of the listener
218 * Returns: Pointer to a reference counted file descriptor.
220 * Use: Accepts, verifies and logs an incoming connection.
223 reffd
*(*accept
)(int /*fd*/, addr_opts */
*ao*/
, const char */
*desc*/
);
225 /* --- @inittargopts@ --- *
229 * Returns: A pointer to a protocol-specific data block for a connecter
231 * Use: Creates a data block for a target. This is attached to the
232 * target data structure. Options can then be requested, and
233 * are added to the block when necessary.
236 addr_opts
*(*inittargopts
)(void);
238 /* --- @freetargopts@ --- *
240 * Arguments: @addr_opts *ao@ = data block to remove
244 * Use: Throws away all the configuration data for an address type.
247 void (*freetargopts
)(addr_opts */
*ao*/
);
249 /* --- @connect@ --- *
251 * Arguments: @addr *a@ = destination address
252 * @addr_opts *ao@ = target address options
253 * @conn *c@ = connection structure
254 * @endpt *e@ = endpoint structure
256 * Returns: Zero if OK, @-1@ on some error.
258 * Use: Requests that a connection be made, or at least set in
259 * motion. An address may do one of these things:
263 * * Call @starget_connected@ with @-1@ or a connected file
264 * descriptor and the pointer @e@.
266 * * Call @conn_init@ or @conn_fd@, giving @starget_connected@
267 * and @e@ as the function to call.
270 int (*connect
)(addr */
*a*/
, addr_opts */
*ao*/
, conn */
*c*/
, endpt */
*e*/
);
274 /*----- That's all, folks -------------------------------------------------*/