home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sources.x
- From: subbarao@concorde.fc.hp.com (Kartik Subbarao)
- Subject: v19i089: xroute - Route X-Protocol through Internet gateways, Part01/01
- Message-ID: <1993May21.154743.10753@sparky.imd.sterling.com>
- X-Md4-Signature: 84abc5c42e32c7e4e3c64fd479f9eae4
- Sender: chris@sparky.imd.sterling.com (Chris Olson)
- Organization: Sterling Software
- Date: Fri, 21 May 1993 15:47:43 GMT
- Approved: chris@sparky.imd.sterling.com
-
- Submitted-by: subbarao@concorde.fc.hp.com (Kartik Subbarao)
- Posting-number: Volume 19, Issue 89
- Archive-name: xroute/part01
- Environment: X11, Inet
-
- xroute routes X packets from one machine to another. It is typically used
- to enable programs to run on a gateway which does not route arbitrary packets
- between "internal" and "external" hosts. It's a handy program to have around
- when dealing with the increasingly common situation of having to run X
- programs to and from remote sites across internet gateways. See the man page
- for more on how to use xroute.
-
- -Kartik
-
- #!/bin/sh
- # This is a shell archive (produced by shar 3.49)
- # To extract the files from this archive, save it to a file, remove
- # everything above the "!/bin/sh" line above, and type "sh file_name".
- #
- # made 05/19/1993 03:13 UTC by subbarao@concorde
- # Source directory /u/subbarao/x
- #
- # existing files will NOT be overwritten unless -c is specified
- #
- # This shar contains:
- # length mode name
- # ------ ---------- ------------------------------------------
- # 18 -rw-r--r-- xroute/Makefile
- # 583 -rw-r--r-- xroute/README
- # 2816 -rw-r--r-- xroute/xroute.1
- # 3794 -rw-r--r-- xroute/xroute.c
- #
- # ============= xroute/Makefile ==============
- if test ! -d 'xroute'; then
- echo 'x - creating directory xroute'
- mkdir 'xroute'
- fi
- if test -f 'xroute/Makefile' -a X"$1" != X"-c"; then
- echo 'x - skipping xroute/Makefile (File already exists)'
- else
- echo 'x - extracting xroute/Makefile (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'xroute/Makefile' &&
- CFLAGS=-O
- xroute:
- SHAR_EOF
- chmod 0644 xroute/Makefile ||
- echo 'restore of xroute/Makefile failed'
- Wc_c="`wc -c < 'xroute/Makefile'`"
- test 18 -eq "$Wc_c" ||
- echo 'xroute/Makefile: original size 18, current size' "$Wc_c"
- fi
- # ============= xroute/README ==============
- if test -f 'xroute/README' -a X"$1" != X"-c"; then
- echo 'x - skipping xroute/README (File already exists)'
- else
- echo 'x - extracting xroute/README (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'xroute/README' &&
- xroute routes X packets from one machine to another. It is typically used
- to enable programs to run on a gateway which does not route arbitrary packets
- between "internal" and "external" hosts. It's a handy program to have around
- when dealing with the increasingly common situation of having to run X
- programs to and from remote sites across internet gateways. See the man page
- for more on how to use xroute.
- X
- xroute compiles and runs fine on various HPs, Suns, SGIs and DECs. If you
- have any suggestions, questions, comments, etc, feel free to send me mail -
- <subbarao@fc.hp.com>.
- SHAR_EOF
- chmod 0644 xroute/README ||
- echo 'restore of xroute/README failed'
- Wc_c="`wc -c < 'xroute/README'`"
- test 583 -eq "$Wc_c" ||
- echo 'xroute/README: original size 583, current size' "$Wc_c"
- fi
- # ============= xroute/xroute.1 ==============
- if test -f 'xroute/xroute.1' -a X"$1" != X"-c"; then
- echo 'x - skipping xroute/xroute.1 (File already exists)'
- else
- echo 'x - extracting xroute/xroute.1 (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'xroute/xroute.1' &&
- .TH XROUTE 1 "May 18, 1993"
- .SH NAME
- xroute \- route X packets through a gateway
- .SH SYNOPSIS
- .B xroute
- [-n numclients] [-d gdisp] host[:display]
- .br
- .SH DESCRIPTION
- XXroute was written to solve the problem of getting an X application to work
- over a gateway that will not route packets directly between "internal" and
- "external" hosts.
- X
- The X server listens on port 6000 + \fId\fP for requests made to display
- \fId\fP. For example, "xterm -display zoof:2" sends packets to port 6002 on
- zoof, data destined for zoof:3 goes to port 6003, etc. By acting as a
- pipe between an X port on the gateway machine and an X port on the local
- machine, xroute provides a way for internal hosts to X off of external
- hosts, and vice versa.
- X
- .SH EXAMPLE
- Let's say that I wanted to run xrn from a foo.bar.edu, a host on the outside,
- pass it through the gateway, and display it on concorde, my machine.
- X
- First, I would pick an arbitrary display number to set my remote DISPLAY
- variable:
- X
- foo.bar.edu% setenv DISPLAY gateway.biz.com:2
- X
- Then, I would xhost (or add the corresponding line to .Xauthority) BOTH the
- gateway and the external machine. (This is necessary due to where the X
- packets look like they're coming from).
- X
- concorde% xhost gateway.biz.com foo.bar.edu
- X
- If I couldn't resolve foo.bar.edu by name locally, I'd have to use nslookup
- on the gateway, snarf the IP address, and xhost the IP address.
- X
- We now set up the route between concorde and foo.bar.edu:
- X
- gateway% xroute -d 2 concorde:0
- X
- With the route in place, I can now run xrn:
- X
- foo.bar.edu% xrn
- X
- And automagically, it pops up on concorde:0. (Alternatively, I could have
- not specified the -d argument, and would set my DISPLAY on foo.bar.edu
- according to the number that xroute printed)
- X
- Note that the reverse is just as easily possible. If I want to display an X
- application from concorde to foo.bar.edu, I would set up the proper xhosting
- on foo.bar.edu, and would run
- X
- xroute -d 2 foo.bar.edu:0
- X
- on the gateway.
- X
- .SH OPTIONS
- .TP
- .B\-d
- specifies the port number (6000 + gdisp) on the gateway to act as the proxy "X
- server". When omitted, xroute picks the lowest port available >= 6000, and
- prints the corresponding display number.
- .TP
- .B\-n
- specifies the number of X clients you wish to route through the gateway to
- your system. It defaults to 1. The main program exits after \fInumclients\fP
- clients have established connections.
- .TP
- If no display is specified in the local hostname, it defaults to 0.
- X
- .SH AUTHOR
- Kartik Subbarao <subbarao@fc.hp.com>
- X
- .SH COMMENTS
- Routing X packets the standard way outperforms \fBxroute\fP. Nevertheless,
- this program is very useful when, due to security concerns or other factors,
- arbitrary packets cannot be routed between internal and external machines.
- X
- Enhancements, questions, comments, etc. are welcome.
- SHAR_EOF
- chmod 0644 xroute/xroute.1 ||
- echo 'restore of xroute/xroute.1 failed'
- Wc_c="`wc -c < 'xroute/xroute.1'`"
- test 2816 -eq "$Wc_c" ||
- echo 'xroute/xroute.1: original size 2816, current size' "$Wc_c"
- fi
- # ============= xroute/xroute.c ==============
- if test -f 'xroute/xroute.c' -a X"$1" != X"-c"; then
- echo 'x - skipping xroute/xroute.c (File already exists)'
- else
- echo 'x - extracting xroute/xroute.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'xroute/xroute.c' &&
- /* xroute.c, by Kartik Subbarao <subbarao@concorde.fc.hp.com> */
- X
- # include <stdio.h>
- # include <sys/types.h>
- # include <sys/time.h>
- # include <netdb.h>
- # include <netinet/in.h>
- # include <arpa/inet.h>
- # include <sys/socket.h>
- # include <string.h>
- # include <sys/wait.h>
- #ifdef sgi
- # define _BSD_SIGNALS
- #endif
- # include <signal.h>
- # include <errno.h>
- X
- # define XPORT 6000
- X
- char buf[8192];
- X
- usage()
- {
- X fprintf(stderr,"usage: xroute [-n numclients] [-d gdisp] host[:display]\n");
- X exit(1);
- }
- X
- #ifdef hpux
- waiter() {int st;while (wait3(&st, WNOHANG, NULL) > 0);signal(SIGCHLD, waiter);}
- #else
- waiter() { union wait st; while (wait3(&st, WNOHANG, NULL) > 0); }
- #endif
- X
- main(argc, argv)
- int argc; char *argv[];
- {
- X int o; extern char *optarg; extern int optind; char *lhost, *p;
- X int ldisp, gdisp = -1; /* display numbers */
- X int ls, gs, rs; /* sockets -- local, gateway, remote */
- X struct sockaddr_in rad; int radlen = sizeof rad;
- X int numcli = 1, pid;
- X
- X while ((o = getopt(argc, argv, "n:d:")) != -1)
- X switch (o) {
- X case 'n': if (optarg) numcli = atoi(optarg); break;
- X case 'd': if (optarg) gdisp = atoi(optarg); break;
- X default: usage();
- X }
- X
- X if (!argv[optind]) usage();
- X ldisp = (p = strchr(argv[optind], ':')) && p[1] ? atoi(p+1) : 0;
- X if (p) *p = '\0'; lhost = argv[optind];
- X
- X gs = tcpssocket(gdisp);
- X signal(SIGCHLD, waiter);
- X signal(SIGHUP, SIG_IGN);
- X
- X if (fork()) exit(0); /* background ourself */
- X
- X for (; numcli; numcli--) {
- X while ((rs = accept(gs, &rad, &radlen)) == -1 && errno == EINTR);
- X if (rs == -1) exit(1);
- X switch (fork()) {
- X case -1: exit(1);
- X case 0: ls = tcpcsocket(lhost, XPORT + ldisp); route(ls, rs); break;
- X default: close(rs);
- X }
- X }
- X exit(0);
- }
- X
- route(ls, rs)
- int ls, rs;
- {
- X fd_set rdset, trdset, wrset, twrset;
- X int maxfds, n, tmp; char *b;
- X
- X maxfds = (ls > rs ? ls : rs) + 1;
- X FD_ZERO(&rdset); FD_ZERO(&trdset); FD_ZERO(&wrset); FD_ZERO(&twrset);
- X FD_SET(ls, &rdset); FD_SET(ls, &wrset);
- X FD_SET(rs, &rdset); FD_SET(rs, &wrset);
- X trdset = rdset; twrset = wrset;
- X
- X for (;; rdset = trdset, wrset = twrset) {
- X if (select(maxfds, &rdset, &wrset, NULL, NULL) == -1) exit(1);
- X
- X if (FD_ISSET(ls, &rdset) && FD_ISSET(rs, &wrset)) {
- X if ((n = read(ls, buf, sizeof buf)) <= 0) exit(n);
- X for (b = buf; n; n -= tmp, b += tmp)
- X if ((tmp = write(rs, b, n)) <= 0) exit(tmp);
- X }
- X if (FD_ISSET(rs, &rdset) && FD_ISSET(ls, &wrset)) {
- X if ((n = read(rs, buf, sizeof buf)) <= 0) exit(n);
- X for (b = buf; n; n -= tmp, b += tmp)
- X if ((tmp = write(ls, b, n)) <= 0) exit(tmp);
- X }
- X }
- }
- X
- tcpssocket(gdisp)
- int gdisp;
- {
- X int i, s; struct sockaddr_in sa;
- X
- X if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
- X perror("socket"), exit(1);
- X
- X sa.sin_family = AF_INET; sa.sin_addr.s_addr = htonl(INADDR_ANY);
- X
- X if (gdisp == -1) /* If it wasn't specified */
- X for (i = XPORT; ; i++) { /* find the first available port > XPORT */
- X sa.sin_port = htons(i);
- X if (bind(s, &sa, sizeof(sa)) == 0) {
- X printf("%d\n", i-XPORT); break;
- X }
- X }
- X else {
- X sa.sin_port = htons(XPORT+gdisp);
- X if (bind(s, &sa, sizeof(sa)) == -1) perror("bind"), exit(1);
- X }
- X
- X if (listen(s, 5) == -1) perror("listen"), exit(1);
- X
- X return s;
- }
- X
- tcpcsocket(host, port)
- char *host; int port;
- {
- X int s, e;
- X struct sockaddr_in sa;
- X struct hostent *hent;
- X extern char *sys_errlist[];
- X
- X if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) exit(1);
- X
- X if (! (hent = gethostbyname(host))) {
- X e = errno;
- X if ((sa.sin_addr.s_addr = inet_addr(host)) == -1)
- X fprintf(stderr, "%s: %s\n", host, sys_errlist[e]), exit(1);
- X }
- X else bcopy(hent->h_addr, (char *) &sa.sin_addr, hent->h_length);
- X
- X sa.sin_family = AF_INET; sa.sin_port = htons(port);
- X
- X if (connect(s, &sa, sizeof(sa)) == -1)
- X fprintf(stderr, "%s: connect: %s\n", host, sys_errlist[errno]), exit(1);
- X
- X return s;
- }
- SHAR_EOF
- chmod 0644 xroute/xroute.c ||
- echo 'restore of xroute/xroute.c failed'
- Wc_c="`wc -c < 'xroute/xroute.c'`"
- test 3794 -eq "$Wc_c" ||
- echo 'xroute/xroute.c: original size 3794, current size' "$Wc_c"
- fi
- exit 0
-
- exit 0 # Just in case...
- --
- // chris@IMD.Sterling.COM | Send comp.sources.x submissions to:
- \X/ Amiga - The only way to fly! | sources-x@imd.sterling.com
- "It's intuitively obvious to the |
- most casual observer..." | GCS d+/-- p+ c++ l+ m+ s++/+ g+ w+ t+ r+ x+
-