home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / x / volume19 / xroute / part01 < prev    next >
Encoding:
Text File  |  1993-05-26  |  10.9 KB  |  349 lines

  1. Newsgroups: comp.sources.x
  2. From: subbarao@concorde.fc.hp.com (Kartik Subbarao)
  3. Subject: v19i089:  xroute - Route X-Protocol through Internet gateways, Part01/01
  4. Message-ID: <1993May21.154743.10753@sparky.imd.sterling.com>
  5. X-Md4-Signature: 84abc5c42e32c7e4e3c64fd479f9eae4
  6. Sender: chris@sparky.imd.sterling.com (Chris Olson)
  7. Organization: Sterling Software
  8. Date: Fri, 21 May 1993 15:47:43 GMT
  9. Approved: chris@sparky.imd.sterling.com
  10.  
  11. Submitted-by: subbarao@concorde.fc.hp.com (Kartik Subbarao)
  12. Posting-number: Volume 19, Issue 89
  13. Archive-name: xroute/part01
  14. Environment: X11, Inet
  15.  
  16. xroute routes X packets from one machine to another. It is typically used
  17. to enable programs to run on a gateway which does not route arbitrary packets
  18. between "internal" and "external" hosts. It's a handy program to have around
  19. when dealing with the increasingly common situation of having to run X
  20. programs to and from remote sites across internet gateways. See the man page
  21. for more on how to use xroute.
  22.  
  23.      -Kartik
  24.  
  25. #!/bin/sh
  26. # This is a shell archive (produced by shar 3.49)
  27. # To extract the files from this archive, save it to a file, remove
  28. # everything above the "!/bin/sh" line above, and type "sh file_name".
  29. #
  30. # made 05/19/1993 03:13 UTC by subbarao@concorde
  31. # Source directory /u/subbarao/x
  32. #
  33. # existing files will NOT be overwritten unless -c is specified
  34. #
  35. # This shar contains:
  36. # length  mode       name
  37. # ------ ---------- ------------------------------------------
  38. #     18 -rw-r--r-- xroute/Makefile
  39. #    583 -rw-r--r-- xroute/README
  40. #   2816 -rw-r--r-- xroute/xroute.1
  41. #   3794 -rw-r--r-- xroute/xroute.c
  42. #
  43. # ============= xroute/Makefile ==============
  44. if test ! -d 'xroute'; then
  45.     echo 'x - creating directory xroute'
  46.     mkdir 'xroute'
  47. fi
  48. if test -f 'xroute/Makefile' -a X"$1" != X"-c"; then
  49.     echo 'x - skipping xroute/Makefile (File already exists)'
  50. else
  51. echo 'x - extracting xroute/Makefile (Text)'
  52. sed 's/^X//' << 'SHAR_EOF' > 'xroute/Makefile' &&
  53. CFLAGS=-O
  54. xroute:
  55. SHAR_EOF
  56. chmod 0644 xroute/Makefile ||
  57. echo 'restore of xroute/Makefile failed'
  58. Wc_c="`wc -c < 'xroute/Makefile'`"
  59. test 18 -eq "$Wc_c" ||
  60.     echo 'xroute/Makefile: original size 18, current size' "$Wc_c"
  61. fi
  62. # ============= xroute/README ==============
  63. if test -f 'xroute/README' -a X"$1" != X"-c"; then
  64.     echo 'x - skipping xroute/README (File already exists)'
  65. else
  66. echo 'x - extracting xroute/README (Text)'
  67. sed 's/^X//' << 'SHAR_EOF' > 'xroute/README' &&
  68. xroute routes X packets from one machine to another. It is typically used
  69. to enable programs to run on a gateway which does not route arbitrary packets
  70. between "internal" and "external" hosts. It's a handy program to have around
  71. when dealing with the increasingly common situation of having to run X
  72. programs to and from remote sites across internet gateways. See the man page
  73. for more on how to use xroute.
  74. X
  75. xroute compiles and runs fine on various HPs, Suns, SGIs and DECs. If you 
  76. have any suggestions, questions, comments, etc, feel free to send me mail - 
  77. <subbarao@fc.hp.com>.
  78. SHAR_EOF
  79. chmod 0644 xroute/README ||
  80. echo 'restore of xroute/README failed'
  81. Wc_c="`wc -c < 'xroute/README'`"
  82. test 583 -eq "$Wc_c" ||
  83.     echo 'xroute/README: original size 583, current size' "$Wc_c"
  84. fi
  85. # ============= xroute/xroute.1 ==============
  86. if test -f 'xroute/xroute.1' -a X"$1" != X"-c"; then
  87.     echo 'x - skipping xroute/xroute.1 (File already exists)'
  88. else
  89. echo 'x - extracting xroute/xroute.1 (Text)'
  90. sed 's/^X//' << 'SHAR_EOF' > 'xroute/xroute.1' &&
  91. .TH XROUTE 1 "May 18, 1993"
  92. .SH NAME
  93. xroute \- route X packets through a gateway
  94. .SH SYNOPSIS
  95. .B xroute
  96. [-n numclients] [-d gdisp] host[:display]
  97. .br
  98. .SH DESCRIPTION
  99. XXroute was written to solve the problem of getting an X application to work
  100. over a gateway that will not route packets directly between "internal" and 
  101. "external" hosts.
  102. X
  103. The X server listens on port 6000 + \fId\fP for requests made to display 
  104. \fId\fP. For example, "xterm -display zoof:2" sends packets to port 6002 on 
  105. zoof, data destined for zoof:3 goes to port 6003, etc. By acting as a 
  106. pipe between an X port on the gateway machine and an X port on the local 
  107. machine, xroute provides a way for internal hosts to X off of external
  108. hosts, and vice versa.
  109. X
  110. .SH EXAMPLE
  111. Let's say that I wanted to run xrn from a foo.bar.edu, a host on the outside,
  112. pass it through the gateway, and display it on concorde, my machine. 
  113. X
  114. First, I would pick an arbitrary display number to set my remote DISPLAY
  115. variable:
  116. X
  117. foo.bar.edu% setenv DISPLAY gateway.biz.com:2
  118. X
  119. Then, I would xhost (or add the corresponding line to .Xauthority) BOTH the
  120. gateway and the external machine. (This is necessary due to where the X 
  121. packets look like they're coming from).
  122. X
  123. concorde% xhost gateway.biz.com foo.bar.edu 
  124. X
  125. If I couldn't resolve foo.bar.edu by name locally, I'd have to use nslookup 
  126. on the gateway, snarf the IP address, and xhost the IP address.
  127. X
  128. We now set up the route between concorde and foo.bar.edu:
  129. X
  130. gateway% xroute -d 2 concorde:0
  131. X
  132. With the route in place, I can now run xrn:
  133. X
  134. foo.bar.edu% xrn
  135. X
  136. And automagically, it pops up on concorde:0. (Alternatively, I could have
  137. not specified the -d argument, and would set my DISPLAY on foo.bar.edu 
  138. according to the number that xroute printed)
  139. X
  140. Note that the reverse is just as easily possible. If I want to display an X 
  141. application from concorde to foo.bar.edu, I would set up the proper xhosting 
  142. on foo.bar.edu, and would run 
  143. X
  144. xroute -d 2 foo.bar.edu:0 
  145. X
  146. on the gateway.
  147. X
  148. .SH OPTIONS
  149. .TP
  150. .B\-d
  151. specifies the port number (6000 + gdisp) on the gateway to act as the proxy "X
  152. server". When omitted, xroute picks the lowest port available >= 6000, and
  153. prints the corresponding display number.
  154. .TP
  155. .B\-n 
  156. specifies the number of X clients you wish to route through the gateway to 
  157. your system. It defaults to 1. The main program exits after \fInumclients\fP 
  158. clients have established connections.
  159. .TP
  160. If no display is specified in the local hostname, it defaults to 0.
  161. X
  162. .SH AUTHOR
  163. Kartik Subbarao <subbarao@fc.hp.com>
  164. X
  165. .SH COMMENTS
  166. Routing X packets the standard way outperforms \fBxroute\fP. Nevertheless, 
  167. this program is very useful when, due to security concerns or other factors, 
  168. arbitrary packets cannot be routed between internal and external machines. 
  169. X
  170. Enhancements, questions, comments, etc. are welcome.
  171. SHAR_EOF
  172. chmod 0644 xroute/xroute.1 ||
  173. echo 'restore of xroute/xroute.1 failed'
  174. Wc_c="`wc -c < 'xroute/xroute.1'`"
  175. test 2816 -eq "$Wc_c" ||
  176.     echo 'xroute/xroute.1: original size 2816, current size' "$Wc_c"
  177. fi
  178. # ============= xroute/xroute.c ==============
  179. if test -f 'xroute/xroute.c' -a X"$1" != X"-c"; then
  180.     echo 'x - skipping xroute/xroute.c (File already exists)'
  181. else
  182. echo 'x - extracting xroute/xroute.c (Text)'
  183. sed 's/^X//' << 'SHAR_EOF' > 'xroute/xroute.c' &&
  184. /* xroute.c, by Kartik Subbarao <subbarao@concorde.fc.hp.com> */
  185. X
  186. # include <stdio.h>
  187. # include <sys/types.h>
  188. # include <sys/time.h>
  189. # include <netdb.h>
  190. # include <netinet/in.h>
  191. # include <arpa/inet.h>
  192. # include <sys/socket.h>
  193. # include <string.h>
  194. # include <sys/wait.h>
  195. #ifdef sgi
  196. # define _BSD_SIGNALS
  197. #endif
  198. # include <signal.h>
  199. # include <errno.h>
  200. X
  201. # define XPORT 6000
  202. X
  203. char buf[8192];
  204. X
  205. usage()
  206. {
  207. X    fprintf(stderr,"usage: xroute [-n numclients] [-d gdisp] host[:display]\n");
  208. X    exit(1);
  209. }
  210. X
  211. #ifdef hpux
  212. waiter() {int st;while (wait3(&st, WNOHANG, NULL) > 0);signal(SIGCHLD, waiter);}
  213. #else
  214. waiter() { union wait st; while (wait3(&st, WNOHANG, NULL) > 0); }
  215. #endif
  216. X
  217. main(argc, argv)
  218. int argc; char *argv[];
  219. {
  220. X    int o; extern char *optarg; extern int optind; char *lhost, *p;
  221. X    int ldisp, gdisp = -1; /* display numbers */
  222. X    int ls, gs, rs; /* sockets -- local, gateway, remote */
  223. X    struct sockaddr_in rad; int radlen = sizeof rad;
  224. X    int numcli = 1, pid;
  225. X    
  226. X    while ((o = getopt(argc, argv, "n:d:")) != -1)
  227. X        switch (o) {    
  228. X        case 'n': if (optarg) numcli = atoi(optarg); break;
  229. X        case 'd': if (optarg) gdisp = atoi(optarg); break;
  230. X        default: usage();
  231. X        }
  232. X    
  233. X    if (!argv[optind]) usage();
  234. X    ldisp = (p = strchr(argv[optind], ':')) && p[1] ? atoi(p+1) : 0;
  235. X    if (p) *p = '\0'; lhost = argv[optind];
  236. X
  237. X    gs = tcpssocket(gdisp);
  238. X    signal(SIGCHLD, waiter);
  239. X    signal(SIGHUP, SIG_IGN);
  240. X
  241. X    if (fork()) exit(0); /* background ourself */
  242. X
  243. X    for (; numcli; numcli--) {
  244. X        while ((rs = accept(gs, &rad, &radlen)) == -1 && errno == EINTR);
  245. X        if (rs == -1) exit(1);
  246. X        switch (fork()) {
  247. X        case -1: exit(1);
  248. X        case 0: ls = tcpcsocket(lhost, XPORT + ldisp); route(ls, rs); break;
  249. X        default: close(rs);
  250. X        }
  251. X    }
  252. X    exit(0);
  253. }
  254. X
  255. route(ls, rs)
  256. int ls, rs;
  257. {
  258. X    fd_set rdset, trdset, wrset, twrset;
  259. X    int maxfds, n, tmp; char *b;
  260. X    
  261. X    maxfds = (ls > rs ? ls : rs) + 1;
  262. X    FD_ZERO(&rdset); FD_ZERO(&trdset); FD_ZERO(&wrset); FD_ZERO(&twrset);
  263. X    FD_SET(ls, &rdset); FD_SET(ls, &wrset);
  264. X    FD_SET(rs, &rdset); FD_SET(rs, &wrset);
  265. X    trdset = rdset; twrset = wrset;
  266. X    
  267. X    for (;; rdset = trdset, wrset = twrset) {
  268. X        if (select(maxfds, &rdset, &wrset, NULL, NULL) == -1) exit(1);
  269. X
  270. X        if (FD_ISSET(ls, &rdset) && FD_ISSET(rs, &wrset)) {
  271. X            if ((n = read(ls, buf, sizeof buf)) <= 0) exit(n);
  272. X            for (b = buf; n; n -= tmp, b += tmp)
  273. X                if ((tmp = write(rs, b, n)) <= 0) exit(tmp);
  274. X        }
  275. X        if (FD_ISSET(rs, &rdset) && FD_ISSET(ls, &wrset)) {
  276. X            if ((n = read(rs, buf, sizeof buf)) <= 0) exit(n);
  277. X            for (b = buf; n; n -= tmp, b += tmp)
  278. X                if ((tmp = write(ls, b, n)) <= 0) exit(tmp);
  279. X        }
  280. X    }
  281. }
  282. X
  283. tcpssocket(gdisp)
  284. int gdisp;
  285. {
  286. X    int i, s; struct sockaddr_in sa;
  287. X        
  288. X    if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  289. X        perror("socket"), exit(1);
  290. X
  291. X       sa.sin_family = AF_INET; sa.sin_addr.s_addr = htonl(INADDR_ANY);
  292. X
  293. X    if (gdisp == -1) /* If it wasn't specified */
  294. X        for (i = XPORT; ; i++) { /* find the first available port > XPORT */
  295. X            sa.sin_port = htons(i); 
  296. X            if (bind(s, &sa, sizeof(sa)) == 0) {
  297. X                printf("%d\n", i-XPORT); break;
  298. X            }
  299. X        }
  300. X    else {
  301. X        sa.sin_port = htons(XPORT+gdisp); 
  302. X        if (bind(s, &sa, sizeof(sa)) == -1) perror("bind"), exit(1);
  303. X    }
  304. X
  305. X    if (listen(s, 5) == -1) perror("listen"), exit(1);
  306. X
  307. X    return s;
  308. }
  309. X
  310. tcpcsocket(host, port)
  311. char *host; int port;
  312. {
  313. X    int s, e;
  314. X    struct sockaddr_in sa;
  315. X    struct hostent *hent;
  316. X    extern char *sys_errlist[];
  317. X
  318. X    if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) exit(1);
  319. X
  320. X    if (! (hent = gethostbyname(host))) {
  321. X        e = errno;
  322. X        if ((sa.sin_addr.s_addr = inet_addr(host)) == -1)
  323. X            fprintf(stderr, "%s: %s\n", host, sys_errlist[e]), exit(1);
  324. X    }
  325. X       else bcopy(hent->h_addr, (char *) &sa.sin_addr, hent->h_length);
  326. X
  327. X    sa.sin_family = AF_INET; sa.sin_port = htons(port);
  328. X
  329. X    if (connect(s, &sa, sizeof(sa)) == -1)
  330. X        fprintf(stderr, "%s: connect: %s\n", host, sys_errlist[errno]), exit(1);
  331. X
  332. X    return s;
  333. }
  334. SHAR_EOF
  335. chmod 0644 xroute/xroute.c ||
  336. echo 'restore of xroute/xroute.c failed'
  337. Wc_c="`wc -c < 'xroute/xroute.c'`"
  338. test 3794 -eq "$Wc_c" ||
  339.     echo 'xroute/xroute.c: original size 3794, current size' "$Wc_c"
  340. fi
  341. exit 0
  342.  
  343. exit 0 # Just in case...
  344. -- 
  345.   // chris@IMD.Sterling.COM       | Send comp.sources.x submissions to:
  346. \X/  Amiga - The only way to fly! |    sources-x@imd.sterling.com
  347.  "It's intuitively obvious to the |
  348.   most casual observer..."        | GCS d+/-- p+ c++ l+ m+ s++/+ g+ w+ t+ r+ x+
  349.