+++ /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 -------------------------------------------------*/