2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
10 u_int curtime, time_fasttimo, last_slowtimo;
23 lprint("X Redir: X not being redirected.\r\n");
25 lprint("X Redir: In sh/bash/zsh/etc. type: DISPLAY=%s:%d.%d; export DISPLAY\r\n",
26 inet_ntoa(our_addr), x_port, x_screen);
27 lprint("X Redir: In csh/tcsh/etc. type: setenv DISPLAY %s:%d.%d\r\n",
28 inet_ntoa(our_addr), x_port, x_screen);
30 lprint("X Redir: Redirecting to display %d\r\n", x_display);
38 * XXX Allow more than one X redirection?
41 redir_x(inaddr, start_port, display, screen)
50 lprint("X Redir: X already being redirected.\r\n");
53 for (i = 6001 + (start_port-1); i <= 6100; i++) {
54 if (solisten(htons(i), inaddr, htons(6000 + display), 0)) {
63 lprint("X Redir: Error: Couldn't redirect a port for X. Weird.\r\n");
69 * Get our IP address and put it in our_addr
75 struct hostent *he = NULL;
77 if (gethostname(buff,256) == 0)
78 he = gethostbyname(buff);
80 our_addr = *(struct in_addr *)he->h_addr;
81 if (our_addr.s_addr == 0)
82 our_addr.s_addr = loopback_addr.s_addr;
86 struct quehead *qh_link;
87 struct quehead *qh_rlink;
91 insque(void *a, void *b)
93 register struct quehead *element = (struct quehead *) a;
94 register struct quehead *head = (struct quehead *) b;
95 element->qh_link = head->qh_link;
96 head->qh_link = (struct quehead *)element;
97 element->qh_rlink = (struct quehead *)head;
98 ((struct quehead *)(element->qh_link))->qh_rlink
99 = (struct quehead *)element;
105 register struct quehead *element = (struct quehead *) a;
106 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
107 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
108 element->qh_rlink = NULL;
109 /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
115 int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
116 struct in_addr addr, int port)
118 struct ex_list *tmp_ptr;
120 /* First, check if the port is "bound" */
121 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
122 if (port == tmp_ptr->ex_fport &&
123 addr.s_addr == tmp_ptr->ex_addr.s_addr)
128 *ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
129 (*ex_ptr)->ex_fport = port;
130 (*ex_ptr)->ex_addr = addr;
131 (*ex_ptr)->ex_pty = do_pty;
132 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : strdup(exec);
133 (*ex_ptr)->ex_next = tmp_ptr;
137 #ifndef HAVE_STRERROR
140 * For systems with no strerror
144 extern char *sys_errlist[];
150 if (error < sys_nerr)
151 return sys_errlist[error];
153 return "Unknown error.";
162 fork_exec(struct socket *so, const char *ex, int do_pty)
164 /* not implemented */
172 slirp_openpty(amaster, aslave)
173 int *amaster, *aslave;
175 register int master, slave;
180 if ((master = open("/dev/ptmx", O_RDWR)) < 0 ||
181 grantpt(master) < 0 ||
182 unlockpt(master) < 0 ||
183 (ptr = ptsname(master)) == NULL) {
188 if ((slave = open(ptr, O_RDWR)) < 0 ||
189 ioctl(slave, I_PUSH, "ptem") < 0 ||
190 ioctl(slave, I_PUSH, "ldterm") < 0 ||
191 ioctl(slave, I_PUSH, "ttcompat") < 0) {
203 static char line[] = "/dev/ptyXX";
204 register const char *cp1, *cp2;
206 for (cp1 = "pqrsPQRS"; *cp1; cp1++) {
208 for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) {
210 if ((master = open(line, O_RDWR, 0)) == -1) {
212 return (-1); /* out of ptys */
215 /* These will fail */
216 (void) chown(line, getuid(), 0);
217 (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
221 if ((slave = open(line, O_RDWR, 0)) != -1) {
226 (void) close(master);
231 errno = ENOENT; /* out of ptys */
239 * We create and bind a socket, then fork off to another
240 * process, which connects to this socket, after which we
241 * exec the wanted program. If something (strange) happens,
242 * the accept() call could block us forever.
244 * do_pty = 0 Fork/exec inetd style
245 * do_pty = 1 Fork/exec using slirp.telnetd
246 * do_ptr = 2 Fork/exec using pty
249 fork_exec(struct socket *so, const char *ex, int do_pty)
252 struct sockaddr_in addr;
253 socklen_t addrlen = sizeof(addr);
256 const char *argv[256];
260 /* don't want to clobber the original */
265 DEBUG_CALL("fork_exec");
266 DEBUG_ARG("so = %lx", (long)so);
267 DEBUG_ARG("ex = %lx", (long)ex);
268 DEBUG_ARG("do_pty = %lx", (long)do_pty);
272 if (slirp_openpty(&master, &s) == -1) {
273 lprint("Error: openpty failed: %s\n", strerror(errno));
280 addr.sin_family = AF_INET;
282 addr.sin_addr.s_addr = INADDR_ANY;
284 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
285 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
287 lprint("Error: inet socket: %s\n", strerror(errno));
296 lprint("Error: fork failed: %s\n", strerror(errno));
303 /* Set the DISPLAY */
305 (void) close(master);
306 #ifdef TIOCSCTTY /* XXXXX */
308 ioctl(s, TIOCSCTTY, (char *)NULL);
311 getsockname(s, (struct sockaddr *)&addr, &addrlen);
314 * Connect to the socket
315 * XXX If any of these fail, we're in trouble!
317 s = socket(AF_INET, SOCK_STREAM, 0);
318 addr.sin_addr = loopback_addr;
320 ret = connect(s, (struct sockaddr *)&addr, addrlen);
321 } while (ret < 0 && errno == EINTR);
327 sprintf(buff, "%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
328 setenv("DISPLAY", buff, 1);
330 sprintf(buff, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
338 for (s = getdtablesize() - 1; s >= 3; s--)
342 bptr = strdup(ex); /* No need to free() this */
344 /* Setup "slirp.telnetd -x" */
345 argv[i++] = "slirp.telnetd";
350 /* Change the string into argv[] */
352 while (*bptr != ' ' && *bptr != (char)0)
356 argv[i++] = strdup(curarg);
360 execvp(argv[0], (char **)argv);
362 /* Ooops, failed, let's tell the user why */
366 snprintf(buff, sizeof(buff),
367 "Error: execvp of %s failed: %s\n",
368 argv[0], strerror(errno));
369 write(2, buff, strlen(buff)+1);
371 close(0); close(1); close(2); /* XXX */
380 * XXX this could block us...
381 * XXX Should set a timer here, and if accept() doesn't
382 * return after X seconds, declare it a failure
383 * The only reason this will block forever is if socket()
384 * of connect() fail in the child process
387 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
388 } while (so->s < 0 && errno == EINTR);
391 setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
393 setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
397 /* Append the telnet options now */
398 if (so->so_m != NULL && do_pty == 1) {
399 sbappend(so, so->so_m);
415 bptr = (char *)malloc(strlen(str)+1);
428 #ifndef NO_UNIX_SOCKETS
429 struct sockaddr_un sock_un;
431 struct sockaddr_in sock_in;
435 if (slirp_socket_passwd) {
436 s = socket(AF_INET, SOCK_STREAM, 0);
439 sock_in.sin_family = AF_INET;
440 sock_in.sin_addr.s_addr = slirp_socket_addr;
441 sock_in.sin_port = htons(slirp_socket_port);
442 if (connect(s, (struct sockaddr *)&sock_in, sizeof(sock_in)) != 0)
443 slirp_exit(1); /* just exit...*/
444 sprintf(buff, "kill %s:%d", slirp_socket_passwd, slirp_socket_unit);
445 write(s, buff, strlen(buff)+1);
447 #ifndef NO_UNIX_SOCKETS
449 s = socket(AF_UNIX, SOCK_STREAM, 0);
452 sock_un.sun_family = AF_UNIX;
453 strcpy(sock_un.sun_path, socket_path);
454 if (connect(s, (struct sockaddr *)&sock_un,
455 sizeof(sock_un.sun_family) + sizeof(sock_un.sun_path)) != 0)
457 sprintf(buff, "kill none:%d", slirp_socket_unit);
458 write(s, buff, strlen(buff)+1);
471 /* Don't need our data anymore */
472 /* XXX This makes SunOS barf */
476 for (i = 255; i >= 0; i--)
479 signal(SIGQUIT, slirp_exit);
480 signal(SIGHUP, snooze_hup);
483 /* Wait for any signal */
486 /* Just in case ... */
499 /* Don't need our data anymore */
500 /* XXX This makes SunOS barf */
503 signal(SIGQUIT, slirp_exit);
504 signal(SIGHUP, slirp_exit);
505 signal(SIGINT, slirp_exit);
506 signal(SIGTERM, slirp_exit);
508 /* Fudge to get term_raw and term_restore to work */
509 if (NULL == (ttyp = tty_attach (0, slirp_tty))) {
510 lprint ("Error: tty_attach failed in misc.c:relay()\r\n");
514 ttyp->flags |= TTY_CTTY;
523 n = select(s+1, &readfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
528 if (FD_ISSET(0, &readfds)) {
529 n = read(0, buf, 8192);
532 n = writen(s, buf, n);
537 if (FD_ISSET(s, &readfds)) {
538 n = read(s, buf, 8192);
541 n = writen(0, buf, n);
547 /* Just in case.... */
555 void lprint(const char *format, ...)
559 va_start(args, format);
560 monitor_vprintf(cur_mon, format, args);
564 int (*lprint_print) _P((void *, const char *, va_list));
565 char *lprint_ptr, *lprint_ptr2, **lprint_arg;
569 lprint(const char *format, ...)
571 lprint(va_alist) va_dcl
577 va_start(args, format);
581 format = va_arg(args, char *);
584 /* If we're printing to an sbuf, make sure there's enough room */
587 if ((lprint_ptr - lprint_sb->sb_wptr) >=
588 (lprint_sb->sb_datalen - (strlen(format) + 100))) {
589 int deltaw = lprint_sb->sb_wptr - lprint_sb->sb_data;
590 int deltar = lprint_sb->sb_rptr - lprint_sb->sb_data;
591 int deltap = lprint_ptr - lprint_sb->sb_data;
593 lprint_sb->sb_data = (char *)realloc(lprint_sb->sb_data,
594 lprint_sb->sb_datalen + TCP_SNDSPACE);
596 /* Adjust all values */
597 lprint_sb->sb_wptr = lprint_sb->sb_data + deltaw;
598 lprint_sb->sb_rptr = lprint_sb->sb_data + deltar;
599 lprint_ptr = lprint_sb->sb_data + deltap;
601 lprint_sb->sb_datalen += TCP_SNDSPACE;
606 lprint_ptr += (*lprint_print)(*lprint_arg, format, args);
608 /* Check if they want output to be logged to file as well */
612 * otherwise you'll get ^M all over the file
614 int len = strlen(format);
617 bptr1 = bptr2 = strdup(format);
621 memcpy(bptr1, bptr1+1, len+1);
625 vfprintf(lfd, bptr2, args);
636 u_int8_t tos = 0, emu = 0;
637 char buff1[256], buff2[256], buff4[128];
642 if (sscanf(buff, "%256s %256s", buff2, buff1) != 2) {
643 lprint("Error: Bad arguments\r\n");
647 if (sscanf(buff1, "%d:%d", &lport, &fport) != 2) {
649 if (sscanf(buff1, "%d", &fport) != 1) {
650 lprint("Error: Bad first argument\r\n");
655 if (sscanf(buff2, "%128[^:]:%128s", buff1, buff3) != 2) {
657 if (sscanf(buff2, "%256s", buff1) != 1) {
658 lprint("Error: Bad second argument\r\n");
664 if (strcmp(buff3, "lowdelay") == 0)
665 tos = IPTOS_LOWDELAY;
666 else if (strcmp(buff3, "throughput") == 0)
667 tos = IPTOS_THROUGHPUT;
669 lprint("Error: Expecting \"lowdelay\"/\"throughput\"\r\n");
674 if (strcmp(buff1, "ftp") == 0)
676 else if (strcmp(buff1, "irc") == 0)
678 else if (strcmp(buff1, "none") == 0)
679 emu = EMU_NONE; /* ie: no emulation */
681 lprint("Error: Unknown service\r\n");
685 /* First, check that it isn't already emulated */
686 for (emup = tcpemu; emup; emup = emup->next) {
687 if (emup->lport == lport && emup->fport == fport) {
688 lprint("Error: port already emulated\r\n");
694 emup = (struct emu_t *)malloc(sizeof (struct emu_t));
695 emup->lport = (u_int16_t)lport;
696 emup->fport = (u_int16_t)fport;
702 /* And finally, mark all current sessions, if any, as being emulated */
703 for (so = tcb.so_next; so != &tcb; so = so->so_next) {
704 if ((lport && lport == ntohs(so->so_lport)) ||
705 (fport && fport == ntohs(so->so_fport))) {
713 lprint("Adding emulation for %s to port %d/%d\r\n", buff1, emup->lport, emup->fport);
723 * Some BSD-derived systems have a sprintf which returns char *
727 vsprintf_len(string, format, args)
732 vsprintf(string, format, args);
733 return strlen(string);
738 sprintf_len(char *string, const char *format, ...)
740 sprintf_len(va_alist) va_dcl
745 va_start(args, format);
750 string = va_arg(args, char *);
751 format = va_arg(args, char *);
753 vsprintf(string, format, args);
754 return strlen(string);
768 t.tv_usec = usec * 1000;
770 select(0, &fdset, &fdset, &fdset, &t);
774 * Set fd blocking and non-blocking
782 unsigned long opt = 1;
787 ioctlsocket(fd, FIONBIO, &opt);
791 opt = fcntl(fd, F_GETFL, 0);
793 fcntl(fd, F_SETFL, opt);
802 unsigned long opt = 0;
807 ioctlsocket(fd, FIONBIO, &opt);
811 opt = fcntl(fd, F_GETFL, 0);
813 fcntl(fd, F_SETFL, opt);
823 rsh_exec(so,ns, user, host, args)
835 DEBUG_CALL("rsh_exec");
836 DEBUG_ARG("so = %lx", (long)so);
839 lprint("Error: pipe failed: %s\n", strerror(errno));
842 /* #ifdef HAVE_SOCKETPAIR */
844 if (socketpair(PF_UNIX,SOCK_STREAM,0, fd0) == -1) {
847 lprint("Error: openpty failed: %s\n", strerror(errno));
851 if (slirp_openpty(&fd0[0], &fd0[1]) == -1) {
854 lprint("Error: openpty failed: %s\n", strerror(errno));
861 lprint("Error: fork failed: %s\n", strerror(errno));
872 /* Set the DISPLAY */
875 sprintf(buff, "%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
876 setenv("DISPLAY", buff, 1);
878 sprintf(buff, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
886 for (s = 3; s <= 255; s++)
889 execlp("rsh","rsh","-l", user, host, args, NULL);
891 /* Ooops, failed, let's tell the user why */
893 sprintf(buff, "Error: execlp of %s failed: %s\n",
894 "rsh", strerror(errno));
895 write(2, buff, strlen(buff)+1);
896 close(0); close(1); close(2); /* XXX */