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 ? */
116 add_exec(struct ex_list
**ex_ptr
, int do_pty
, char *exec
, int 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
&& addr
== tmp_ptr
->ex_addr
)
127 *ex_ptr
= (struct ex_list
*)malloc(sizeof(struct ex_list
));
128 (*ex_ptr
)->ex_fport
= port
;
129 (*ex_ptr
)->ex_addr
= addr
;
130 (*ex_ptr
)->ex_pty
= do_pty
;
131 (*ex_ptr
)->ex_exec
= (do_pty
== 3) ? exec
: strdup(exec
);
132 (*ex_ptr
)->ex_next
= tmp_ptr
;
136 #ifndef HAVE_STRERROR
139 * For systems with no strerror
143 extern char *sys_errlist
[];
149 if (error
< sys_nerr
)
150 return sys_errlist
[error
];
152 return "Unknown error.";
161 fork_exec(struct socket
*so
, const char *ex
, int do_pty
)
163 /* not implemented */
171 slirp_openpty(amaster
, aslave
)
172 int *amaster
, *aslave
;
174 register int master
, slave
;
179 if ((master
= open("/dev/ptmx", O_RDWR
)) < 0 ||
180 grantpt(master
) < 0 ||
181 unlockpt(master
) < 0 ||
182 (ptr
= ptsname(master
)) == NULL
) {
187 if ((slave
= open(ptr
, O_RDWR
)) < 0 ||
188 ioctl(slave
, I_PUSH
, "ptem") < 0 ||
189 ioctl(slave
, I_PUSH
, "ldterm") < 0 ||
190 ioctl(slave
, I_PUSH
, "ttcompat") < 0) {
202 static char line
[] = "/dev/ptyXX";
203 register const char *cp1
, *cp2
;
205 for (cp1
= "pqrsPQRS"; *cp1
; cp1
++) {
207 for (cp2
= "0123456789abcdefghijklmnopqrstuv"; *cp2
; cp2
++) {
209 if ((master
= open(line
, O_RDWR
, 0)) == -1) {
211 return (-1); /* out of ptys */
214 /* These will fail */
215 (void) chown(line
, getuid(), 0);
216 (void) chmod(line
, S_IRUSR
|S_IWUSR
|S_IWGRP
);
220 if ((slave
= open(line
, O_RDWR
, 0)) != -1) {
225 (void) close(master
);
230 errno
= ENOENT
; /* out of ptys */
238 * We create and bind a socket, then fork off to another
239 * process, which connects to this socket, after which we
240 * exec the wanted program. If something (strange) happens,
241 * the accept() call could block us forever.
243 * do_pty = 0 Fork/exec inetd style
244 * do_pty = 1 Fork/exec using slirp.telnetd
245 * do_ptr = 2 Fork/exec using pty
248 fork_exec(struct socket
*so
, const char *ex
, int do_pty
)
251 struct sockaddr_in addr
;
252 socklen_t addrlen
= sizeof(addr
);
255 const char *argv
[256];
259 /* don't want to clobber the original */
264 DEBUG_CALL("fork_exec");
265 DEBUG_ARG("so = %lx", (long)so
);
266 DEBUG_ARG("ex = %lx", (long)ex
);
267 DEBUG_ARG("do_pty = %lx", (long)do_pty
);
271 if (slirp_openpty(&master
, &s
) == -1) {
272 lprint("Error: openpty failed: %s\n", strerror(errno
));
279 addr
.sin_family
= AF_INET
;
281 addr
.sin_addr
.s_addr
= INADDR_ANY
;
283 if ((s
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0 ||
284 bind(s
, (struct sockaddr
*)&addr
, addrlen
) < 0 ||
286 lprint("Error: inet socket: %s\n", strerror(errno
));
295 lprint("Error: fork failed: %s\n", strerror(errno
));
302 /* Set the DISPLAY */
304 (void) close(master
);
305 #ifdef TIOCSCTTY /* XXXXX */
307 ioctl(s
, TIOCSCTTY
, (char *)NULL
);
310 getsockname(s
, (struct sockaddr
*)&addr
, &addrlen
);
313 * Connect to the socket
314 * XXX If any of these fail, we're in trouble!
316 s
= socket(AF_INET
, SOCK_STREAM
, 0);
317 addr
.sin_addr
= loopback_addr
;
319 ret
= connect(s
, (struct sockaddr
*)&addr
, addrlen
);
320 } while (ret
< 0 && errno
== EINTR
);
326 sprintf(buff
, "%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
327 setenv("DISPLAY", buff
, 1);
329 sprintf(buff
, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
337 for (s
= getdtablesize() - 1; s
>= 3; s
--)
341 bptr
= strdup(ex
); /* No need to free() this */
343 /* Setup "slirp.telnetd -x" */
344 argv
[i
++] = "slirp.telnetd";
349 /* Change the string into argv[] */
351 while (*bptr
!= ' ' && *bptr
!= (char)0)
355 argv
[i
++] = strdup(curarg
);
359 execvp(argv
[0], (char **)argv
);
361 /* Ooops, failed, let's tell the user why */
365 snprintf(buff
, sizeof(buff
),
366 "Error: execvp of %s failed: %s\n",
367 argv
[0], strerror(errno
));
368 write(2, buff
, strlen(buff
)+1);
370 close(0); close(1); close(2); /* XXX */
379 * XXX this could block us...
380 * XXX Should set a timer here, and if accept() doesn't
381 * return after X seconds, declare it a failure
382 * The only reason this will block forever is if socket()
383 * of connect() fail in the child process
386 so
->s
= accept(s
, (struct sockaddr
*)&addr
, &addrlen
);
387 } while (so
->s
< 0 && errno
== EINTR
);
390 setsockopt(so
->s
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&opt
,sizeof(int));
392 setsockopt(so
->s
,SOL_SOCKET
,SO_OOBINLINE
,(char *)&opt
,sizeof(int));
396 /* Append the telnet options now */
397 if (so
->so_m
!= NULL
&& do_pty
== 1) {
398 sbappend(so
, so
->so_m
);
414 bptr
= (char *)malloc(strlen(str
)+1);
427 #ifndef NO_UNIX_SOCKETS
428 struct sockaddr_un sock_un
;
430 struct sockaddr_in sock_in
;
434 if (slirp_socket_passwd
) {
435 s
= socket(AF_INET
, SOCK_STREAM
, 0);
438 sock_in
.sin_family
= AF_INET
;
439 sock_in
.sin_addr
.s_addr
= slirp_socket_addr
;
440 sock_in
.sin_port
= htons(slirp_socket_port
);
441 if (connect(s
, (struct sockaddr
*)&sock_in
, sizeof(sock_in
)) != 0)
442 slirp_exit(1); /* just exit...*/
443 sprintf(buff
, "kill %s:%d", slirp_socket_passwd
, slirp_socket_unit
);
444 write(s
, buff
, strlen(buff
)+1);
446 #ifndef NO_UNIX_SOCKETS
448 s
= socket(AF_UNIX
, SOCK_STREAM
, 0);
451 sock_un
.sun_family
= AF_UNIX
;
452 strcpy(sock_un
.sun_path
, socket_path
);
453 if (connect(s
, (struct sockaddr
*)&sock_un
,
454 sizeof(sock_un
.sun_family
) + sizeof(sock_un
.sun_path
)) != 0)
456 sprintf(buff
, "kill none:%d", slirp_socket_unit
);
457 write(s
, buff
, strlen(buff
)+1);
470 /* Don't need our data anymore */
471 /* XXX This makes SunOS barf */
475 for (i
= 255; i
>= 0; i
--)
478 signal(SIGQUIT
, slirp_exit
);
479 signal(SIGHUP
, snooze_hup
);
482 /* Wait for any signal */
485 /* Just in case ... */
498 /* Don't need our data anymore */
499 /* XXX This makes SunOS barf */
502 signal(SIGQUIT
, slirp_exit
);
503 signal(SIGHUP
, slirp_exit
);
504 signal(SIGINT
, slirp_exit
);
505 signal(SIGTERM
, slirp_exit
);
507 /* Fudge to get term_raw and term_restore to work */
508 if (NULL
== (ttyp
= tty_attach (0, slirp_tty
))) {
509 lprint ("Error: tty_attach failed in misc.c:relay()\r\n");
513 ttyp
->flags
|= TTY_CTTY
;
522 n
= select(s
+1, &readfds
, (fd_set
*)0, (fd_set
*)0, (struct timeval
*)0);
527 if (FD_ISSET(0, &readfds
)) {
528 n
= read(0, buf
, 8192);
531 n
= writen(s
, buf
, n
);
536 if (FD_ISSET(s
, &readfds
)) {
537 n
= read(s
, buf
, 8192);
540 n
= writen(0, buf
, n
);
546 /* Just in case.... */
554 void lprint(const char *format
, ...)
558 va_start(args
, format
);
559 monitor_vprintf(cur_mon
, format
, args
);
563 int (*lprint_print
) _P((void *, const char *, va_list));
564 char *lprint_ptr
, *lprint_ptr2
, **lprint_arg
;
568 lprint(const char *format
, ...)
570 lprint(va_alist
) va_dcl
576 va_start(args
, format
);
580 format
= va_arg(args
, char *);
583 /* If we're printing to an sbuf, make sure there's enough room */
586 if ((lprint_ptr
- lprint_sb
->sb_wptr
) >=
587 (lprint_sb
->sb_datalen
- (strlen(format
) + 100))) {
588 int deltaw
= lprint_sb
->sb_wptr
- lprint_sb
->sb_data
;
589 int deltar
= lprint_sb
->sb_rptr
- lprint_sb
->sb_data
;
590 int deltap
= lprint_ptr
- lprint_sb
->sb_data
;
592 lprint_sb
->sb_data
= (char *)realloc(lprint_sb
->sb_data
,
593 lprint_sb
->sb_datalen
+ TCP_SNDSPACE
);
595 /* Adjust all values */
596 lprint_sb
->sb_wptr
= lprint_sb
->sb_data
+ deltaw
;
597 lprint_sb
->sb_rptr
= lprint_sb
->sb_data
+ deltar
;
598 lprint_ptr
= lprint_sb
->sb_data
+ deltap
;
600 lprint_sb
->sb_datalen
+= TCP_SNDSPACE
;
605 lprint_ptr
+= (*lprint_print
)(*lprint_arg
, format
, args
);
607 /* Check if they want output to be logged to file as well */
611 * otherwise you'll get ^M all over the file
613 int len
= strlen(format
);
616 bptr1
= bptr2
= strdup(format
);
620 memcpy(bptr1
, bptr1
+1, len
+1);
624 vfprintf(lfd
, bptr2
, args
);
635 u_int8_t tos
= 0, emu
= 0;
636 char buff1
[256], buff2
[256], buff4
[128];
641 if (sscanf(buff
, "%256s %256s", buff2
, buff1
) != 2) {
642 lprint("Error: Bad arguments\r\n");
646 if (sscanf(buff1
, "%d:%d", &lport
, &fport
) != 2) {
648 if (sscanf(buff1
, "%d", &fport
) != 1) {
649 lprint("Error: Bad first argument\r\n");
654 if (sscanf(buff2
, "%128[^:]:%128s", buff1
, buff3
) != 2) {
656 if (sscanf(buff2
, "%256s", buff1
) != 1) {
657 lprint("Error: Bad second argument\r\n");
663 if (strcmp(buff3
, "lowdelay") == 0)
664 tos
= IPTOS_LOWDELAY
;
665 else if (strcmp(buff3
, "throughput") == 0)
666 tos
= IPTOS_THROUGHPUT
;
668 lprint("Error: Expecting \"lowdelay\"/\"throughput\"\r\n");
673 if (strcmp(buff1
, "ftp") == 0)
675 else if (strcmp(buff1
, "irc") == 0)
677 else if (strcmp(buff1
, "none") == 0)
678 emu
= EMU_NONE
; /* ie: no emulation */
680 lprint("Error: Unknown service\r\n");
684 /* First, check that it isn't already emulated */
685 for (emup
= tcpemu
; emup
; emup
= emup
->next
) {
686 if (emup
->lport
== lport
&& emup
->fport
== fport
) {
687 lprint("Error: port already emulated\r\n");
693 emup
= (struct emu_t
*)malloc(sizeof (struct emu_t
));
694 emup
->lport
= (u_int16_t
)lport
;
695 emup
->fport
= (u_int16_t
)fport
;
701 /* And finally, mark all current sessions, if any, as being emulated */
702 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so
->so_next
) {
703 if ((lport
&& lport
== ntohs(so
->so_lport
)) ||
704 (fport
&& fport
== ntohs(so
->so_fport
))) {
712 lprint("Adding emulation for %s to port %d/%d\r\n", buff1
, emup
->lport
, emup
->fport
);
722 * Some BSD-derived systems have a sprintf which returns char *
726 vsprintf_len(string
, format
, args
)
731 vsprintf(string
, format
, args
);
732 return strlen(string
);
737 sprintf_len(char *string
, const char *format
, ...)
739 sprintf_len(va_alist
) va_dcl
744 va_start(args
, format
);
749 string
= va_arg(args
, char *);
750 format
= va_arg(args
, char *);
752 vsprintf(string
, format
, args
);
753 return strlen(string
);
767 t
.tv_usec
= usec
* 1000;
769 select(0, &fdset
, &fdset
, &fdset
, &t
);
773 * Set fd blocking and non-blocking
781 unsigned long opt
= 1;
786 ioctlsocket(fd
, FIONBIO
, &opt
);
790 opt
= fcntl(fd
, F_GETFL
, 0);
792 fcntl(fd
, F_SETFL
, opt
);
801 unsigned long opt
= 0;
806 ioctlsocket(fd
, FIONBIO
, &opt
);
810 opt
= fcntl(fd
, F_GETFL
, 0);
812 fcntl(fd
, F_SETFL
, opt
);
822 rsh_exec(so
,ns
, user
, host
, args
)
834 DEBUG_CALL("rsh_exec");
835 DEBUG_ARG("so = %lx", (long)so
);
838 lprint("Error: pipe failed: %s\n", strerror(errno
));
841 /* #ifdef HAVE_SOCKETPAIR */
843 if (socketpair(PF_UNIX
,SOCK_STREAM
,0, fd0
) == -1) {
846 lprint("Error: openpty failed: %s\n", strerror(errno
));
850 if (slirp_openpty(&fd0
[0], &fd0
[1]) == -1) {
853 lprint("Error: openpty failed: %s\n", strerror(errno
));
860 lprint("Error: fork failed: %s\n", strerror(errno
));
871 /* Set the DISPLAY */
874 sprintf(buff
, "%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
875 setenv("DISPLAY", buff
, 1);
877 sprintf(buff
, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
885 for (s
= 3; s
<= 255; s
++)
888 execlp("rsh","rsh","-l", user
, host
, args
, NULL
);
890 /* Ooops, failed, let's tell the user why */
892 sprintf(buff
, "Error: execlp of %s failed: %s\n",
893 "rsh", strerror(errno
));
894 write(2, buff
, strlen(buff
)+1);
895 close(0); close(1); close(2); /* XXX */