-/* -*-c-*-
- *
- * $Id: listener.c,v 1.1 1999/07/01 08:56:23 mdw Exp $
- *
- * A port forwarding listener
- *
- * (c) 1999 Mark Wooding
- */
-
-/*----- Licensing notice --------------------------------------------------*
- *
- * This file is part of the `fw' port forwarder.
- *
- * `fw' is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * `fw' is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with `fw'; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: listener.c,v $
- * Revision 1.1 1999/07/01 08:56:23 mdw
- * Initial revision
- *
- */
-
-/*----- Header files ------------------------------------------------------*/
-
-#include "config.h"
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <sys/types.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-
-#include <mLib/alloc.h>
-#include <mLib/dstr.h>
-#include <mLib/report.h>
-#include <mLib/sel.h>
-#include <mLib/sub.h>
-
-#include "acl.h"
-#include "forward.h"
-#include "fw.h"
-#include "identify.h"
-#include "listener.h"
-
-/*----- Main code ---------------------------------------------------------*/
-
-/* --- @doclose@ --- *
- *
- * Arguments: @void *vp@ = pointer to file descriptor to close
- *
- * Returns: ---
- *
- * Use: Closes a file descriptor once it's no longer useful.
- */
-
-static void doclose(void *vp)
-{
- int *fd = vp;
- close(*fd);
- DESTROY(fd);
-}
-
-/* --- @newconn@ --- *
- *
- * Arguments: @int fd@ = file descriptor which is ready
- * @unsigned state@ = state in which file descriptor is
- * @void *vp@ = pointer to listener block
- *
- * Returns: ---
- *
- * Use: Responds to a new connection.
- */
-
-static void newconn(int fd, unsigned state, void *vp)
-{
- int nfd;
- id_req q;
- int lsinsz = sizeof(q.lsin), rsinsz = sizeof(q.rsin);
- int f;
- listener *l = vp;
-
- /* --- Accept the new connection --- */
-
- if ((nfd = accept(fd, (struct sockaddr *)&q.rsin, &rsinsz)) < 0)
- return;
- if (getsockname(nfd, (struct sockaddr *)&q.lsin, &lsinsz)) {
- close(nfd);
- return;
- }
- q.desc = l->desc;
-
- /* --- Find out whether this connection is allowed --- */
-
- if (!acl_check(l->acl, q.rsin.sin_addr)) {
- int *fdp = CREATE(int);
- *fdp = nfd;
- q.act = "refused";
- identify(&q, doclose, fdp);
- return;
- }
-
- /* --- Set the socket nonblocking --- */
-
- if ((f = fcntl(nfd, F_GETFL)) >= 0)
- fcntl(nfd, F_SETFL, f | O_NONBLOCK);
-
- /* --- Open a new forwarding context for the connection --- */
-
- q.act = "accepted";
- forward(nfd, &l->sin, &q);
-}
-
-/* --- @listener_dump@ --- *
- *
- * Arguments: @listener *l@ = pointer to listener block
- * @FILE *fp@ = stream to dump on
- *
- * Returns: ---
- *
- * Use: Dumps a listener to an output stream.
- */
-
-void listener_dump(listener *l, FILE *fp)
-{
- struct sockaddr_in sin;
- int sinsz = sizeof(sin);
-
- getsockname(l->rd.fd, (struct sockaddr *)&sin, &sinsz);
- fprintf(fp, "forward port %u to ", ntohs(sin.sin_port));
- fputs(inet_ntoa(l->sin.sin_addr), fp);
- fprintf(fp, ":%u; acl:\n", ntohs(l->sin.sin_port));
- if (l->acl)
- acl_dump(l->acl, fp);
-}
-
-/* --- @listener_add@ --- *
- *
- * Arguments: @int fd@ = created listening socket
- * @unsigned sp@ = source port number
- * @struct sockaddr_in *sin@ = pointer to destination address
- *
- * Returns: The address of the new listener.
- *
- * Use: Adds a forwarding listener.
- */
-
-listener *listener_add(int fd, unsigned sp, struct sockaddr_in *sin)
-{
- int f;
- listener *l = CREATE(listener);
- dstr d = DSTR_INIT;
-
- l->sin = *sin;
- l->acl = 0;
- f = fcntl(fd, F_GETFL);
- if (f != -1)
- fcntl(fd, F_SETFL, f | O_NONBLOCK);
- sel_initfile(sel, &l->rd, fd, SEL_READ, newconn, l);
- sel_addfile(&l->rd);
- dstr_putf(&d, "fw %u -> %s:%u",
- ntohs(sp), inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
- l->desc = xstrdup(d.buf);
- dstr_destroy(&d);
- return (l);
-}
-
-/*----- That's all, folks -------------------------------------------------*/