3 * $Id: inet.c,v 1.2 1999/07/27 18:30:53 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.2 1999/07/27 18:30:53 mdw
33 * Various minor portability fixes.
35 * Revision 1.1 1999/07/26 23:34:11 mdw
36 * New socket address types.
40 /*----- Header files ------------------------------------------------------*/
50 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
58 #include <mLib/alloc.h>
59 #include <mLib/dstr.h>
60 #include <mLib/report.h>
71 /*----- Data structures ---------------------------------------------------*/
73 typedef struct inet_addrx {
75 struct sockaddr_in sin;
78 typedef struct inet_opts {
84 /*----- Protocol operations -----------------------------------------------*/
88 static addr *inet_read(scanner *sc, unsigned type)
90 inet_addrx *ia = xmalloc(sizeof(*ia));
92 ia->a.ops = &inet_ops;
93 ia->a.sz = sizeof(struct sockaddr_in);
94 memset(&ia->sin, 0, sizeof(ia->sin));
95 ia->sin.sin_family = AF_INET;
97 /* --- Read the host address part --- */
101 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "port") == 0)
103 ia->sin.sin_addr.s_addr = htonl(INADDR_ANY);
108 conf_name(sc, '.', &d);
109 if ((h = gethostbyname(d.buf)) == 0)
110 error(sc, "couldn't resolve Internet address `%s'", d.buf);
111 memcpy(&ia->sin.sin_addr, h->h_addr, sizeof(struct in_addr));
118 /* --- Read the port number --- */
123 if (sc->t != CTOK_WORD)
124 error(sc, "parse error, TCP port expected");
125 if (isdigit((unsigned char)sc->d.buf[0]))
126 ia->sin.sin_port = htons(atoi(sc->d.buf));
127 else if ((s = getservbyname(sc->d.buf, "tcp")) == 0)
128 error(sc, "unknown tcp service `%s'", sc->d.buf);
130 ia->sin.sin_port = s->s_port;
137 /* --- @destroy@ --- */
139 static void inet_destroy(addr *a)
141 inet_addrx *ia = (inet_addrx *)a;
145 /* --- @print@ --- */
147 static void inet_print(addr *a, unsigned type, dstr *d)
149 inet_addrx *ia = (inet_addrx *)a;
152 dstr_putf(d, "inet:%u", (unsigned)ntohs(ia->sin.sin_port));
155 dstr_putf(d, "inet:%s:%u",
156 inet_ntoa(ia->sin.sin_addr),
157 (unsigned)ntohs(ia->sin.sin_port));
162 /* --- @initopts@ --- */
164 static addr_opts *inet_initopts(void)
166 inet_opts *io = CREATE(inet_opts);
168 io->acltail = &io->acl;
172 /* --- @option@ --- */
174 static int inet_option(scanner *sc, addr_opts *ao)
176 inet_opts *io = (inet_opts *)ao;
178 CONF_BEGIN(sc, "inet", "Internet socket")
182 /* --- Access control limitations --- */
184 if ((strcmp(sc->d.buf, "allow") == 0 && (act = ACL_ALLOW, 1)) ||
185 (strcmp(sc->d.buf, "deny") == 0 && (act = ACL_DENY, 1))) {
191 /* --- Find the host or network address --- */
194 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "from") == 0)
196 conf_name(sc, '.', &d);
197 if ((n = getnetbyname(d.buf)) != 0)
198 a.s_addr = htonl(n->n_net);
199 else if ((h = gethostbyname(d.buf)) == 0)
200 error(sc, "couldn't resolve address `%s'", d.buf);
202 memcpy(&a, h->h_addr, sizeof(struct in_addr));
204 /* --- Find the netmask, if any --- */
211 conf_name(sc, '.', &d);
212 if (strchr(d.buf, '.') == 0) {
217 m.s_addr = htonl((~0ul << (32 - n)) & 0xffffffff);
219 #ifdef HAVE_INET_ATON
220 if (!inet_aton(d.buf, &m))
221 error(sc, "bad netmask `%s'", d.buf);
223 m.s_addr = inet_addr(d.buf);
229 /* --- Add the access control entry --- */
231 acl_add(io ? &io->acltail : 0, act, a, m);
235 /* --- Anything unrecognized --- */
240 /* --- @accept@ --- */
242 static reffd *inet_accept(int fd, addr_opts *ao, const char *desc)
244 inet_opts *io = (inet_opts *)ao;
247 int lsinsz = sizeof(q.lsin), rsinsz = sizeof(q.rsin);
249 /* --- Accept the new connection --- */
251 if ((nfd = accept(fd, (struct sockaddr *)&q.rsin, &rsinsz)) < 0)
253 if (getsockname(nfd, (struct sockaddr *)&q.lsin, &lsinsz)) {
258 q.r = reffd_init(nfd);
260 /* --- Find out whether this connection is allowed --- */
262 if (!acl_check(io->acl, q.rsin.sin_addr)) {
264 if (!(io->ao.f & ADDRF_NOLOG))
270 /* --- Everything seems to be OK --- */
273 if (!(io->ao.f & ADDRF_NOLOG))
278 /* --- @freeopts@ --- */
280 static void inet_freeopts(addr_opts *ao)
282 inet_opts *io = (inet_opts *)ao;
287 /* --- Ops table --- */
289 addr_ops inet_ops = {
291 inet_read, inet_destroy, inet_print,
292 inet_initopts, inet_option, inet_accept, inet_freeopts, 0, 0
295 /*----- That's all, folks -------------------------------------------------*/