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