chiark / gitweb /
Update manual style.
[fwd] / addr.h
1 /* -*-c-*-
2  *
3  * $Id: addr.h,v 1.2 1999/07/27 18:30:53 mdw Exp $
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
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: addr.h,v $
32  * Revision 1.2  1999/07/27 18:30:53  mdw
33  * Various minor portability fixes.
34  *
35  * Revision 1.1  1999/07/26 23:34:26  mdw
36  * Socket address type management.
37  *
38  */
39
40 #ifndef ADDR_H
41 #define ADDR_H
42
43 #ifdef __cplusplus
44   extern "C" {
45 #endif
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include <sys/types.h>
50 #include <sys/socket.h>
51
52 #include <mLib/dstr.h>
53
54 #ifndef REFFD_H
55 #  include "reffd.h"
56 #endif
57
58 #ifndef SCAN_H
59 #  include "scan.h"
60 #endif
61
62 /*----- Data structures ---------------------------------------------------*/
63     
64 /* --- A generic socket address --- *
65  *
66  * Not all systems understand @sa_len@ fields.  (In particular, Linux
67  * doesn't.)  Some fairly ugly hacking is then performed on particular
68  * address types.
69  */
70
71 typedef struct addr {
72   struct addr_ops *ops;
73   size_t sz;
74 } addr;
75
76 typedef struct gen_addr {
77   addr a;
78   struct sockaddr sa;
79 } gen_addr;
80
81 #define ADDRSZ(sz) (sizeof(addr) + (sz))
82
83 /* --- Address configuration --- *
84  *
85  * An address family will want to extend this.
86  */
87
88 typedef struct addr_opts {
89   unsigned f;
90 } addr_opts;
91
92 #define ADDRF_NOLOG 1u
93
94 /* --- Address types --- *
95  *
96  * For things like Internet addresses, source and destinations look
97  * different.
98  */
99
100 enum {
101   ADDR_SRC,
102   ADDR_DEST
103 };
104
105 /* --- Description of an address type handler --- */
106
107 typedef struct addr_ops {
108   const char *name;                     /* Protocol's internal name */
109   int pf;                               /* Protocol family number */
110
111   /* --- @read@ --- *
112    *
113    * Arguments: @scanner *sc@ = pointer to scanner to read from
114    *            @unsigned type@ = type of address to be read
115    *
116    * Returns:   A filled-in socket address.
117    *
118    * Use:       Parses a textual representation of a socket address.
119    */
120
121   addr *(*read)(scanner */*sc*/, unsigned /*type*/);
122
123   /* --- @destroy@ --- *
124    *
125    * Arguments: @addr *a@ = pointer to an address block
126    *
127    * Returns:   ---
128    *
129    * Use:       Disposes of an address block in some suitable fashion.
130    */
131
132   void (*destroy)(addr */*a*/);
133
134   /* --- @print@ --- *
135    *
136    * Arguments: @addr *a@ = pointer to socket address to read
137    *            @unsigned type@ = type of address to be written
138    *            @dstr *d@ = string on which to write the description
139    *
140    * Returns:   ---
141    *
142    * Use:       Writes a textual representation of a socket address to
143    *            a string.
144    */
145
146   void (*print)(addr */*a*/, unsigned /*type*/, dstr */*d*/);
147
148   /* --- @initopts@ --- *
149    *
150    * Arguments: ---
151    *
152    * Returns:   A pointer to a protocol-specific data block for a listener
153    *
154    * Use:       Creates a data block for a listener.  This is attached to the
155    *            listener data structure.  Options can then be requested, and
156    *            are added to the block when necessary.
157    */
158
159   addr_opts *(*initopts)(void);
160
161   /* --- @option@ --- *
162    *
163    * Arguments: @scanner *sc@ = pointer to a scanner to read from
164    *            @addr_opts *ao@ = data block to modify (from @init@), or null
165    *
166    * Returns:   Nonzero to claim the option.
167    *
168    * Use:       Parses an option, either global or listener-specific.
169    */
170
171   int (*option)(scanner */*sc*/, addr_opts */*ao*/);
172
173   /* --- @accept@ --- *
174    *
175    * Arguments: @int fd@ = listening file descriptor
176    *            @addr_opts *ao@ = data block to get configuration from
177    *            @const char *desc@ = description of the listener
178    *
179    * Returns:   Pointer to a reference counted file descriptor.
180    *
181    * Use:       Accepts, verifies and logs an incoming connection.
182    */
183
184   reffd *(*accept)(int /*fd*/, addr_opts */*ao*/, const char */*desc*/);
185
186   /* --- @freeopts@ --- *
187    *
188    * Arguments: @addr_opts *ao@ = data block to remove
189    *
190    * Returns:   ---
191    *
192    * Use:       Throws away all the configuration data for an address type.
193    */
194
195   void (*freeopts)(addr_opts */*ao*/);
196
197   /* --- @bound@ --- *
198    *
199    * Arguments: @addr *a@ = pointer to an address
200    *            @addr_opts *ao@ = pointer to attributes block
201    *
202    * Returns:   ---
203    *
204    * Use:       Reports that a file descriptor has been (successfully) bound
205    *            to an address.
206    */
207
208   void (*bound)(addr */*a*/, addr_opts */*ao*/);
209
210   /* --- @unbind@ --- *
211    *
212    * Arguments: @addr *a@ = pointer to an address
213    *
214    * Returns:   ---
215    *
216    * Use:       Unbinds an address.  This is used when tidying up.  The main
217    *            purpose is to let the Unix-domain handler remove its socket
218    *            node from the filesystem.
219    */
220
221   void (*unbind)(addr */*a*/);
222
223 } addr_ops;
224   
225 /*----- That's all, folks -------------------------------------------------*/
226
227 #ifdef __cplusplus
228   }
229 #endif
230
231 #endif