3 * $Id: listener.c,v 1.1 1999/07/01 08:56:23 mdw Exp $
5 * A port forwarding listener
7 * (c) 1999 Mark Wooding
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 --------------------------------------------------*
31 * $Log: listener.c,v $
32 * Revision 1.1 1999/07/01 08:56:23 mdw
37 /*----- Header files ------------------------------------------------------*/
46 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
55 #include <mLib/alloc.h>
56 #include <mLib/dstr.h>
57 #include <mLib/report.h>
67 /*----- Main code ---------------------------------------------------------*/
69 /* --- @doclose@ --- *
71 * Arguments: @void *vp@ = pointer to file descriptor to close
75 * Use: Closes a file descriptor once it's no longer useful.
78 static void doclose(void *vp)
85 /* --- @newconn@ --- *
87 * Arguments: @int fd@ = file descriptor which is ready
88 * @unsigned state@ = state in which file descriptor is
89 * @void *vp@ = pointer to listener block
93 * Use: Responds to a new connection.
96 static void newconn(int fd, unsigned state, void *vp)
100 int lsinsz = sizeof(q.lsin), rsinsz = sizeof(q.rsin);
104 /* --- Accept the new connection --- */
106 if ((nfd = accept(fd, (struct sockaddr *)&q.rsin, &rsinsz)) < 0)
108 if (getsockname(nfd, (struct sockaddr *)&q.lsin, &lsinsz)) {
114 /* --- Find out whether this connection is allowed --- */
116 if (!acl_check(l->acl, q.rsin.sin_addr)) {
117 int *fdp = CREATE(int);
120 identify(&q, doclose, fdp);
124 /* --- Set the socket nonblocking --- */
126 if ((f = fcntl(nfd, F_GETFL)) >= 0)
127 fcntl(nfd, F_SETFL, f | O_NONBLOCK);
129 /* --- Open a new forwarding context for the connection --- */
132 forward(nfd, &l->sin, &q);
135 /* --- @listener_dump@ --- *
137 * Arguments: @listener *l@ = pointer to listener block
138 * @FILE *fp@ = stream to dump on
142 * Use: Dumps a listener to an output stream.
145 void listener_dump(listener *l, FILE *fp)
147 struct sockaddr_in sin;
148 int sinsz = sizeof(sin);
150 getsockname(l->rd.fd, (struct sockaddr *)&sin, &sinsz);
151 fprintf(fp, "forward port %u to ", ntohs(sin.sin_port));
152 fputs(inet_ntoa(l->sin.sin_addr), fp);
153 fprintf(fp, ":%u; acl:\n", ntohs(l->sin.sin_port));
155 acl_dump(l->acl, fp);
158 /* --- @listener_add@ --- *
160 * Arguments: @int fd@ = created listening socket
161 * @unsigned sp@ = source port number
162 * @struct sockaddr_in *sin@ = pointer to destination address
164 * Returns: The address of the new listener.
166 * Use: Adds a forwarding listener.
169 listener *listener_add(int fd, unsigned sp, struct sockaddr_in *sin)
172 listener *l = CREATE(listener);
177 f = fcntl(fd, F_GETFL);
179 fcntl(fd, F_SETFL, f | O_NONBLOCK);
180 sel_initfile(sel, &l->rd, fd, SEL_READ, newconn, l);
182 dstr_putf(&d, "fw %u -> %s:%u",
183 ntohs(sp), inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
184 l->desc = xstrdup(d.buf);
189 /*----- That's all, folks -------------------------------------------------*/