+++ /dev/null
-/* -*-c-*-
- *
- * $Id: forward.c,v 1.1 1999/07/01 08:56:23 mdw Exp $
- *
- * Port forwarding
- *
- * (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: forward.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 <sys/time.h>
-#include <unistd.h>
-
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
-#include <mLib/alloc.h>
-#include <mLib/conn.h>
-#include <mLib/sel.h>
-
-#include "chan.h"
-#include "forward.h"
-#include "fw.h"
-
-/*----- Data structures ---------------------------------------------------*/
-
-/* --- Port forwarding data --- */
-
-typedef struct fw {
- int fd_a; /* Client's file descriptor */
- int fd_b; /* Server's file descriptor */
- unsigned state; /* Current state of the world */
- conn c; /* Nonblocking connect to server */
- chan ab; /* Channel from @a@ to @b@ */
- chan ba; /* Channel from @b@ to @a@ */
-} fw;
-
-#define S_AB 1u /* Channel from @a@ to @b@ open */
-#define S_BA 2u /* Channel from @b@ to @a@ open */
-#define S_NOTCONN 4u /* Not connected to @b@ yet */
-#define S_NOTID 8u /* Not finished identification */
-
-/*----- Main code ---------------------------------------------------------*/
-
-/* --- @done@ --- *
- *
- * Arguments: @fw *f@ = pointer to forwarder
- *
- * Returns: ---
- *
- * Use: Tidies up a forwarder that nobody wants any more.
- */
-
-static void done(fw *f)
-{
- close(f->fd_a);
- close(f->fd_b);
- free(f);
-}
-
-/* --- @ident@ --- *
- *
- * Arguments: @void *vp@ = pointer to forwarder
- *
- * Returns: ---
- *
- * Use: Handles completion of client identification.
- */
-
-static void ident(void *vp)
-{
- fw *f = vp;
- f->state &= ~S_NOTID;
- if (!f->state)
- done(f);
-}
-
-/* --- @closeba@ --- *
- *
- * Arguments: @void *vp@ = pointer to forwarder
- *
- * Returns: ---
- *
- * Use: Handles the closing of the %$b \rightarrow a$% channel.
- */
-
-static void closeba(void *vp)
-{
- fw *f = vp;
- f->state &= ~S_BA;
- if (!f->state)
- done(f);
-}
-
-/* --- @closeab@ --- *
- *
- * Arguments: @void *vp@ = pointer to forwarder
- *
- * Returns: ---
- *
- * Use: Handles the closing of the %$a \rightarrow b$% channel.
- */
-
-static void closeab(void *vp)
-{
- fw *f = vp;
- f->state &= ~S_AB;
- if (!f->state)
- done(f);
-}
-
-/* --- @go@ --- *
- *
- * Arguments: @int fd@ = newly connected socket
- * @void *vp@ = pointer to forwarder
- *
- * Returns: ---
- *
- * Use: Completes the forwarder once the outbound connection is set
- * up.
- */
-
-static void go(int fd, void *vp)
-{
- fw *f = vp;
-
- /* --- If it all went tits-up, deal with that --- */
-
- if (fd == -1) {
- chan_close(&f->ab);
- close(f->fd_a);
- free(f);
- return;
- }
-
- /* --- OK, finish configuring the forwarder --- */
-
- f->fd_b = fd;
- chan_dest(&f->ab, fd);
- chan_open(&f->ba, fd, f->fd_a, closeba, f);
- f->state |= S_BA;
- f->state &= ~S_NOTCONN;
-}
-
-/* --- @forward@ --- *
- *
- * Arguments: @int fd@ = file descriptor attached to client
- * @struct sockaddr_in *sin@ = pointer to destination address
- * @const id_req *q@ = pointer to identification request block
- *
- * Returns: ---
- *
- * Use: Start a port forwarding job.
- */
-
-void forward(int fd, struct sockaddr_in *sin, const id_req *q)
-{
- fw *f;
- int nfd;
-
- /* --- Set up the new socket --- */
-
- if ((nfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
- return;
-
- {
- int opt = 1;
- setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
- setsockopt(nfd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
- }
-
- /* --- Initialize the easy bits --- */
-
- f = xmalloc(sizeof(*f));
- f->fd_a = fd;
- f->fd_b = -1;
- f->state = S_NOTCONN | S_AB | S_NOTID;
-
- /* --- Open the %$a \rightarrow b$% channel --- */
-
- chan_open(&f->ab, fd, -1, closeab, f);
- conn_init(&f->c, sel, nfd, (struct sockaddr *)sin, sizeof(*sin), go, f);
- identify(q, ident, f);
-}
-
-/*----- That's all, folks -------------------------------------------------*/
+++ /dev/null
-/* -*-c-*-
- *
- * $Id: forward.h,v 1.1 1999/07/01 08:56:23 mdw Exp $
- *
- * Port forwarding
- *
- * (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: forward.h,v $
- * Revision 1.1 1999/07/01 08:56:23 mdw
- * Initial revision
- *
- */
-
-#ifndef FORWARD_H
-#define FORWARD_H
-
-#ifdef __cplusplus
- extern "C" {
-#endif
-
-/*----- Header files ------------------------------------------------------*/
-
-#include <sys/socket.h>
-#include <netinet/in.h>
-
-#ifndef IDENTIFY_H
-# include "identify.h"
-#endif
-
-/*----- Functions provided ------------------------------------------------*/
-
-/* --- @forward@ --- *
- *
- * Arguments: @int fd@ = file descriptor attached to client
- * @struct sockaddr_in *sin@ = pointer to destination address
- * @const id_req *q@ = pointer to identification request block
- *
- * Returns: ---
- *
- * Use: Start a port forwarding job.
- */
-
-extern void forward(int /*fd*/, struct sockaddr_in */*sin*/,
- const id_req */*q*/);
-
-/*----- That's all, folks -------------------------------------------------*/
-
-#ifdef __cplusplus
- }
-#endif
-
-#endif
+++ /dev/null
-/* -*-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 -------------------------------------------------*/
+++ /dev/null
-/* -*-c-*-
- *
- * $Id: listener.h,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.h,v $
- * Revision 1.1 1999/07/01 08:56:23 mdw
- * Initial revision
- *
- */
-
-#ifndef LISTENER_H
-#define LISTENER_H
-
-#ifdef __cplusplus
- extern "C" {
-#endif
-
-/*----- Header files ------------------------------------------------------*/
-
-#include <stdio.h>
-
-#include <sys/socket.h>
-#include <netinet/in.h>
-
-#include <mLib/sel.h>
-
-#ifndef ACL_H
-# include "acl.h"
-#endif
-
-/*----- Data structures ---------------------------------------------------*/
-
-/* --- A listener object --- */
-
-typedef struct listener {
- sel_file rd; /* Read selector for new connects */
- struct sockaddr_in sin; /* Destination address */
- const char *desc; /* Description of forwarder */
- acl_entry *acl; /* Access controls */
-} listener;
-
-/*----- Functions provided ------------------------------------------------*/
-
-/* --- @listener_dump@ --- *
- *
- * Arguments: @listener *l@ = pointer to listener block
- * @FILE *fp@ = stream to dump on
- *
- * Returns: ---
- *
- * Use: Dumps a listener to an output stream.
- */
-
-extern void listener_dump(listener */*l*/, FILE */*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.
- */
-
-extern listener *listener_add(int /*fd*/,
- unsigned /*sp*/, struct sockaddr_in */*sin*/);
-
-/*----- That's all, folks -------------------------------------------------*/
-
-#ifdef __cplusplus
- }
-#endif
-
-#endif