3 * $Id: inet.c,v 1.3 2000/08/01 17:59:56 mdw Exp $
5 * Protocol specific definitions for IPv4 sockets
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.3 2000/08/01 17:59:56 mdw
33 * Switch over to using `size_t' for socket address lengths.
35 * Revision 1.2 1999/07/27 18:30:53 mdw
36 * Various minor portability fixes.
38 * Revision 1.1 1999/07/26 23:34:11 mdw
39 * New socket address types.
43 /*----- Header files ------------------------------------------------------*/
53 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
61 #include <mLib/alloc.h>
62 #include <mLib/dstr.h>
63 #include <mLib/report.h>
74 /*----- Data structures ---------------------------------------------------*/
76 typedef struct inet_addrx {
78 struct sockaddr_in sin;
81 typedef struct inet_opts {
87 /*----- Protocol operations -----------------------------------------------*/
91 static addr *inet_read(scanner *sc, unsigned type)
93 inet_addrx *ia = xmalloc(sizeof(*ia));
95 ia->a.ops = &inet_ops;
96 ia->a.sz = sizeof(struct sockaddr_in);
97 memset(&ia->sin, 0, sizeof(ia->sin));
98 ia->sin.sin_family = AF_INET;
100 /* --- Read the host address part --- */
104 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "port") == 0)
106 ia->sin.sin_addr.s_addr = htonl(INADDR_ANY);
111 conf_name(sc, '.', &d);
112 if ((h = gethostbyname(d.buf)) == 0)
113 error(sc, "couldn't resolve Internet address `%s'", d.buf);
114 memcpy(&ia->sin.sin_addr, h->h_addr, sizeof(struct in_addr));
121 /* --- Read the port number --- */
126 if (sc->t != CTOK_WORD)
127 error(sc, "parse error, TCP port expected");
128 if (isdigit((unsigned char)sc->d.buf[0]))
129 ia->sin.sin_port = htons(atoi(sc->d.buf));
130 else if ((s = getservbyname(sc->d.buf, "tcp")) == 0)
131 error(sc, "unknown tcp service `%s'", sc->d.buf);
133 ia->sin.sin_port = s->s_port;
140 /* --- @destroy@ --- */
142 static void inet_destroy(addr *a)
144 inet_addrx *ia = (inet_addrx *)a;
148 /* --- @print@ --- */
150 static void inet_print(addr *a, unsigned type, dstr *d)
152 inet_addrx *ia = (inet_addrx *)a;
155 dstr_putf(d, "inet:%u", (unsigned)ntohs(ia->sin.sin_port));
158 dstr_putf(d, "inet:%s:%u",
159 inet_ntoa(ia->sin.sin_addr),
160 (unsigned)ntohs(ia->sin.sin_port));
165 /* --- @initopts@ --- */
167 static addr_opts *inet_initopts(void)
169 inet_opts *io = CREATE(inet_opts);
171 io->acltail = &io->acl;
175 /* --- @option@ --- */
177 static int inet_option(scanner *sc, addr_opts *ao)
179 inet_opts *io = (inet_opts *)ao;
181 CONF_BEGIN(sc, "inet", "Internet socket")
185 /* --- Access control limitations --- */
187 if ((strcmp(sc->d.buf, "allow") == 0 && (act = ACL_ALLOW, 1)) ||
188 (strcmp(sc->d.buf, "deny") == 0 && (act = ACL_DENY, 1))) {
194 /* --- Find the host or network address --- */
197 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "from") == 0)
199 conf_name(sc, '.', &d);
200 if ((n = getnetbyname(d.buf)) != 0)
201 a.s_addr = htonl(n->n_net);
202 else if ((h = gethostbyname(d.buf)) == 0)
203 error(sc, "couldn't resolve address `%s'", d.buf);
205 memcpy(&a, h->h_addr, sizeof(struct in_addr));
207 /* --- Find the netmask, if any --- */
214 conf_name(sc, '.', &d);
215 if (strchr(d.buf, '.') == 0) {
220 m.s_addr = htonl((~0ul << (32 - n)) & 0xffffffff);
222 #ifdef HAVE_INET_ATON
223 if (!inet_aton(d.buf, &m))
224 error(sc, "bad netmask `%s'", d.buf);
226 m.s_addr = inet_addr(d.buf);
232 /* --- Add the access control entry --- */
234 acl_add(io ? &io->acltail : 0, act, a, m);
238 /* --- Anything unrecognized --- */
243 /* --- @accept@ --- */
245 static reffd *inet_accept(int fd, addr_opts *ao, const char *desc)
247 inet_opts *io = (inet_opts *)ao;
250 size_t lsinsz = sizeof(q.lsin), rsinsz = sizeof(q.rsin);
252 /* --- Accept the new connection --- */
254 if ((nfd = accept(fd, (struct sockaddr *)&q.rsin, &rsinsz)) < 0)
256 if (getsockname(nfd, (struct sockaddr *)&q.lsin, &lsinsz)) {
261 q.r = reffd_init(nfd);
263 /* --- Find out whether this connection is allowed --- */
265 if (!acl_check(io->acl, q.rsin.sin_addr)) {
267 if (!(io->ao.f & ADDRF_NOLOG))
273 /* --- Everything seems to be OK --- */
276 if (!(io->ao.f & ADDRF_NOLOG))
281 /* --- @freeopts@ --- */
283 static void inet_freeopts(addr_opts *ao)
285 inet_opts *io = (inet_opts *)ao;
290 /* --- Ops table --- */
292 addr_ops inet_ops = {
294 inet_read, inet_destroy, inet_print,
295 inet_initopts, inet_option, inet_accept, inet_freeopts, 0, 0
298 /*----- That's all, folks -------------------------------------------------*/