2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
8 #define WANT_SYS_IOCTL_H
11 u_int curtime
, time_fasttimo
, last_slowtimo
, detach_time
;
12 u_int detach_wait
= 600000; /* 10 minutes */
25 lprint("X Redir: X not being redirected.\r\n");
27 lprint("X Redir: In sh/bash/zsh/etc. type: DISPLAY=%s:%d.%d; export DISPLAY\r\n",
28 inet_ntoa(our_addr
), x_port
, x_screen
);
29 lprint("X Redir: In csh/tcsh/etc. type: setenv DISPLAY %s:%d.%d\r\n",
30 inet_ntoa(our_addr
), x_port
, x_screen
);
32 lprint("X Redir: Redirecting to display %d\r\n", x_display
);
40 * XXX Allow more than one X redirection?
43 redir_x(inaddr
, start_port
, display
, screen
)
52 lprint("X Redir: X already being redirected.\r\n");
55 for (i
= 6001 + (start_port
-1); i
<= 6100; i
++) {
56 if (solisten(htons(i
), inaddr
, htons(6000 + display
), 0)) {
65 lprint("X Redir: Error: Couldn't redirect a port for X. Weird.\r\n");
70 #ifndef HAVE_INET_ATON
76 u_int32_t addr
= inet_addr(cp
);
77 if (addr
== 0xffffffff)
85 * Get our IP address and put it in our_addr
91 struct hostent
*he
= NULL
;
93 if (gethostname(buff
,256) == 0)
94 he
= gethostbyname(buff
);
96 our_addr
= *(struct in_addr
*)he
->h_addr
;
97 if (our_addr
.s_addr
== 0)
98 our_addr
.s_addr
= loopback_addr
.s_addr
;
101 #if SIZEOF_CHAR_P == 8
113 register struct quehead_32
*element
= (struct quehead_32
*) a
;
114 register struct quehead_32
*head
= (struct quehead_32
*) b
;
115 element
->qh_link
= head
->qh_link
;
116 head
->qh_link
= (u_int32_t
)element
;
117 element
->qh_rlink
= (u_int32_t
)head
;
118 ((struct quehead_32
*)(element
->qh_link
))->qh_rlink
119 = (u_int32_t
)element
;
126 register struct quehead_32
*element
= (struct quehead_32
*) a
;
127 ((struct quehead_32
*)(element
->qh_link
))->qh_rlink
= element
->qh_rlink
;
128 ((struct quehead_32
*)(element
->qh_rlink
))->qh_link
= element
->qh_link
;
129 element
->qh_rlink
= 0;
132 #endif /* SIZEOF_CHAR_P == 8 */
135 struct quehead
*qh_link
;
136 struct quehead
*qh_rlink
;
143 register struct quehead
*element
= (struct quehead
*) a
;
144 register struct quehead
*head
= (struct quehead
*) b
;
145 element
->qh_link
= head
->qh_link
;
146 head
->qh_link
= (struct quehead
*)element
;
147 element
->qh_rlink
= (struct quehead
*)head
;
148 ((struct quehead
*)(element
->qh_link
))->qh_rlink
149 = (struct quehead
*)element
;
156 register struct quehead
*element
= (struct quehead
*) a
;
157 ((struct quehead
*)(element
->qh_link
))->qh_rlink
= element
->qh_rlink
;
158 ((struct quehead
*)(element
->qh_rlink
))->qh_link
= element
->qh_link
;
159 element
->qh_rlink
= NULL
;
160 /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
167 add_exec(ex_ptr
, do_pty
, exec
, addr
, port
)
168 struct ex_list
**ex_ptr
;
174 struct ex_list
*tmp_ptr
;
176 /* First, check if the port is "bound" */
177 for (tmp_ptr
= *ex_ptr
; tmp_ptr
; tmp_ptr
= tmp_ptr
->ex_next
) {
178 if (port
== tmp_ptr
->ex_fport
&& addr
== tmp_ptr
->ex_addr
)
183 *ex_ptr
= (struct ex_list
*)malloc(sizeof(struct ex_list
));
184 (*ex_ptr
)->ex_fport
= port
;
185 (*ex_ptr
)->ex_addr
= addr
;
186 (*ex_ptr
)->ex_pty
= do_pty
;
187 (*ex_ptr
)->ex_exec
= strdup(exec
);
188 (*ex_ptr
)->ex_next
= tmp_ptr
;
192 #ifndef HAVE_STRERROR
195 * For systems with no strerror
199 extern char *sys_errlist
[];
205 if (error
< sys_nerr
)
206 return sys_errlist
[error
];
208 return "Unknown error.";
217 fork_exec(so
, ex
, do_pty
)
222 /* not implemented */
229 slirp_openpty(amaster
, aslave
)
230 int *amaster
, *aslave
;
232 register int master
, slave
;
237 if ((master
= open("/dev/ptmx", O_RDWR
)) < 0 ||
238 grantpt(master
) < 0 ||
239 unlockpt(master
) < 0 ||
240 (ptr
= ptsname(master
)) == NULL
) {
245 if ((slave
= open(ptr
, O_RDWR
)) < 0 ||
246 ioctl(slave
, I_PUSH
, "ptem") < 0 ||
247 ioctl(slave
, I_PUSH
, "ldterm") < 0 ||
248 ioctl(slave
, I_PUSH
, "ttcompat") < 0) {
260 static char line
[] = "/dev/ptyXX";
261 register const char *cp1
, *cp2
;
263 for (cp1
= "pqrsPQRS"; *cp1
; cp1
++) {
265 for (cp2
= "0123456789abcdefghijklmnopqrstuv"; *cp2
; cp2
++) {
267 if ((master
= open(line
, O_RDWR
, 0)) == -1) {
269 return (-1); /* out of ptys */
272 /* These will fail */
273 (void) chown(line
, getuid(), 0);
274 (void) chmod(line
, S_IRUSR
|S_IWUSR
|S_IWGRP
);
278 if ((slave
= open(line
, O_RDWR
, 0)) != -1) {
283 (void) close(master
);
288 errno
= ENOENT
; /* out of ptys */
295 * We create and bind a socket, then fork off to another
296 * process, which connects to this socket, after which we
297 * exec the wanted program. If something (strange) happens,
298 * the accept() call could block us forever.
300 * do_pty = 0 Fork/exec inetd style
301 * do_pty = 1 Fork/exec using slirp.telnetd
302 * do_ptr = 2 Fork/exec using pty
305 fork_exec(so
, ex
, do_pty
)
311 struct sockaddr_in addr
;
312 int addrlen
= sizeof(addr
);
319 /* don't want to clobber the original */
324 DEBUG_CALL("fork_exec");
325 DEBUG_ARG("so = %lx", (long)so
);
326 DEBUG_ARG("ex = %lx", (long)ex
);
327 DEBUG_ARG("do_pty = %lx", (long)do_pty
);
330 if (slirp_openpty(&master
, &s
) == -1) {
331 lprint("Error: openpty failed: %s\n", strerror(errno
));
335 addr
.sin_family
= AF_INET
;
337 addr
.sin_addr
.s_addr
= INADDR_ANY
;
339 if ((s
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0 ||
340 bind(s
, (struct sockaddr
*)&addr
, addrlen
) < 0 ||
342 lprint("Error: inet socket: %s\n", strerror(errno
));
351 lprint("Error: fork failed: %s\n", strerror(errno
));
358 /* Set the DISPLAY */
360 (void) close(master
);
361 #ifdef TIOCSCTTY /* XXXXX */
363 ioctl(s
, TIOCSCTTY
, (char *)NULL
);
366 getsockname(s
, (struct sockaddr
*)&addr
, &addrlen
);
369 * Connect to the socket
370 * XXX If any of these fail, we're in trouble!
372 s
= socket(AF_INET
, SOCK_STREAM
, 0);
373 addr
.sin_addr
= loopback_addr
;
375 ret
= connect(s
, (struct sockaddr
*)&addr
, addrlen
);
376 } while (ret
< 0 && errno
== EINTR
);
382 sprintf(buff
, "%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
383 setenv("DISPLAY", buff
, 1);
385 sprintf(buff
, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
393 for (s
= 3; s
<= 255; s
++)
397 bptr
= strdup(ex
); /* No need to free() this */
399 /* Setup "slirp.telnetd -x" */
400 argv
[i
++] = "slirp.telnetd";
405 /* Change the string into argv[] */
407 while (*bptr
!= ' ' && *bptr
!= (char)0)
411 argv
[i
++] = strdup(curarg
);
415 execvp(argv
[0], argv
);
417 /* Ooops, failed, let's tell the user why */
421 sprintf(buff
, "Error: execvp of %s failed: %s\n",
422 argv
[0], strerror(errno
));
423 write(2, buff
, strlen(buff
)+1);
425 close(0); close(1); close(2); /* XXX */
434 * XXX this could block us...
435 * XXX Should set a timer here, and if accept() doesn't
436 * return after X seconds, declare it a failure
437 * The only reason this will block forever is if socket()
438 * of connect() fail in the child process
441 so
->s
= accept(s
, (struct sockaddr
*)&addr
, &addrlen
);
442 } while (so
->s
< 0 && errno
== EINTR
);
445 setsockopt(so
->s
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&opt
,sizeof(int));
447 setsockopt(so
->s
,SOL_SOCKET
,SO_OOBINLINE
,(char *)&opt
,sizeof(int));
451 /* Append the telnet options now */
452 if (so
->so_m
!= 0 && do_pty
== 1) {
453 sbappend(so
, so
->so_m
);
469 bptr
= (char *)malloc(strlen(str
)+1);
482 #ifndef NO_UNIX_SOCKETS
483 struct sockaddr_un sock_un
;
485 struct sockaddr_in sock_in
;
489 if (slirp_socket_passwd
) {
490 s
= socket(AF_INET
, SOCK_STREAM
, 0);
493 sock_in
.sin_family
= AF_INET
;
494 sock_in
.sin_addr
.s_addr
= slirp_socket_addr
;
495 sock_in
.sin_port
= htons(slirp_socket_port
);
496 if (connect(s
, (struct sockaddr
*)&sock_in
, sizeof(sock_in
)) != 0)
497 slirp_exit(1); /* just exit...*/
498 sprintf(buff
, "kill %s:%d", slirp_socket_passwd
, slirp_socket_unit
);
499 write(s
, buff
, strlen(buff
)+1);
501 #ifndef NO_UNIX_SOCKETS
503 s
= socket(AF_UNIX
, SOCK_STREAM
, 0);
506 sock_un
.sun_family
= AF_UNIX
;
507 strcpy(sock_un
.sun_path
, socket_path
);
508 if (connect(s
, (struct sockaddr
*)&sock_un
,
509 sizeof(sock_un
.sun_family
) + sizeof(sock_un
.sun_path
)) != 0)
511 sprintf(buff
, "kill none:%d", slirp_socket_unit
);
512 write(s
, buff
, strlen(buff
)+1);
525 /* Don't need our data anymore */
526 /* XXX This makes SunOS barf */
530 for (i
= 255; i
>= 0; i
--)
533 signal(SIGQUIT
, slirp_exit
);
534 signal(SIGHUP
, snooze_hup
);
537 /* Wait for any signal */
540 /* Just in case ... */
553 /* Don't need our data anymore */
554 /* XXX This makes SunOS barf */
557 signal(SIGQUIT
, slirp_exit
);
558 signal(SIGHUP
, slirp_exit
);
559 signal(SIGINT
, slirp_exit
);
560 signal(SIGTERM
, slirp_exit
);
562 /* Fudge to get term_raw and term_restore to work */
563 if (NULL
== (ttyp
= tty_attach (0, slirp_tty
))) {
564 lprint ("Error: tty_attach failed in misc.c:relay()\r\n");
568 ttyp
->flags
|= TTY_CTTY
;
577 n
= select(s
+1, &readfds
, (fd_set
*)0, (fd_set
*)0, (struct timeval
*)0);
582 if (FD_ISSET(0, &readfds
)) {
583 n
= read(0, buf
, 8192);
586 n
= writen(s
, buf
, n
);
591 if (FD_ISSET(s
, &readfds
)) {
592 n
= read(s
, buf
, 8192);
595 n
= writen(0, buf
, n
);
601 /* Just in case.... */
606 int (*lprint_print
) _P((void *, const char *, va_list));
607 char *lprint_ptr
, *lprint_ptr2
, **lprint_arg
;
611 lprint(const char *format
, ...)
613 lprint(va_alist
) va_dcl
619 va_start(args
, format
);
623 format
= va_arg(args
, char *);
626 /* If we're printing to an sbuf, make sure there's enough room */
629 if ((lprint_ptr
- lprint_sb
->sb_wptr
) >=
630 (lprint_sb
->sb_datalen
- (strlen(format
) + 100))) {
631 int deltaw
= lprint_sb
->sb_wptr
- lprint_sb
->sb_data
;
632 int deltar
= lprint_sb
->sb_rptr
- lprint_sb
->sb_data
;
633 int deltap
= lprint_ptr
- lprint_sb
->sb_data
;
635 lprint_sb
->sb_data
= (char *)realloc(lprint_sb
->sb_data
,
636 lprint_sb
->sb_datalen
+ TCP_SNDSPACE
);
638 /* Adjust all values */
639 lprint_sb
->sb_wptr
= lprint_sb
->sb_data
+ deltaw
;
640 lprint_sb
->sb_rptr
= lprint_sb
->sb_data
+ deltar
;
641 lprint_ptr
= lprint_sb
->sb_data
+ deltap
;
643 lprint_sb
->sb_datalen
+= TCP_SNDSPACE
;
648 lprint_ptr
+= (*lprint_print
)(*lprint_arg
, format
, args
);
650 /* Check if they want output to be logged to file as well */
654 * otherwise you'll get ^M all over the file
656 int len
= strlen(format
);
659 bptr1
= bptr2
= strdup(format
);
663 memcpy(bptr1
, bptr1
+1, len
+1);
667 vfprintf(lfd
, bptr2
, args
);
678 u_int8_t tos
= 0, emu
= 0;
679 char buff1
[256], buff2
[256], buff4
[128];
684 if (sscanf(buff
, "%256s %256s", buff2
, buff1
) != 2) {
685 lprint("Error: Bad arguments\r\n");
689 if (sscanf(buff1
, "%d:%d", &lport
, &fport
) != 2) {
691 if (sscanf(buff1
, "%d", &fport
) != 1) {
692 lprint("Error: Bad first argument\r\n");
697 if (sscanf(buff2
, "%128[^:]:%128s", buff1
, buff3
) != 2) {
699 if (sscanf(buff2
, "%256s", buff1
) != 1) {
700 lprint("Error: Bad second argument\r\n");
706 if (strcmp(buff3
, "lowdelay") == 0)
707 tos
= IPTOS_LOWDELAY
;
708 else if (strcmp(buff3
, "throughput") == 0)
709 tos
= IPTOS_THROUGHPUT
;
711 lprint("Error: Expecting \"lowdelay\"/\"throughput\"\r\n");
716 if (strcmp(buff1
, "ftp") == 0)
718 else if (strcmp(buff1
, "irc") == 0)
720 else if (strcmp(buff1
, "none") == 0)
721 emu
= EMU_NONE
; /* ie: no emulation */
723 lprint("Error: Unknown service\r\n");
727 /* First, check that it isn't already emulated */
728 for (emup
= tcpemu
; emup
; emup
= emup
->next
) {
729 if (emup
->lport
== lport
&& emup
->fport
== fport
) {
730 lprint("Error: port already emulated\r\n");
736 emup
= (struct emu_t
*)malloc(sizeof (struct emu_t
));
737 emup
->lport
= (u_int16_t
)lport
;
738 emup
->fport
= (u_int16_t
)fport
;
744 /* And finally, mark all current sessions, if any, as being emulated */
745 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so
->so_next
) {
746 if ((lport
&& lport
== ntohs(so
->so_lport
)) ||
747 (fport
&& fport
== ntohs(so
->so_fport
))) {
755 lprint("Adding emulation for %s to port %d/%d\r\n", buff1
, emup
->lport
, emup
->fport
);
764 * Some BSD-derived systems have a sprintf which returns char *
768 vsprintf_len(string
, format
, args
)
773 vsprintf(string
, format
, args
);
774 return strlen(string
);
779 sprintf_len(char *string
, const char *format
, ...)
781 sprintf_len(va_alist
) va_dcl
786 va_start(args
, format
);
791 string
= va_arg(args
, char *);
792 format
= va_arg(args
, char *);
794 vsprintf(string
, format
, args
);
795 return strlen(string
);
810 t
.tv_usec
= usec
* 1000;
812 select(0, &fdset
, &fdset
, &fdset
, &t
);
816 * Set fd blocking and non-blocking
826 ioctlsocket(fd
, FIONBIO
, &opt
);
830 opt
= fcntl(fd
, F_GETFL
, 0);
832 fcntl(fd
, F_SETFL
, opt
);
843 ioctlsocket(fd
, FIONBIO
, &opt
);
847 opt
= fcntl(fd
, F_GETFL
, 0);
849 fcntl(fd
, F_SETFL
, opt
);
859 rsh_exec(so
,ns
, user
, host
, args
)
871 DEBUG_CALL("rsh_exec");
872 DEBUG_ARG("so = %lx", (long)so
);
875 lprint("Error: pipe failed: %s\n", strerror(errno
));
878 /* #ifdef HAVE_SOCKETPAIR */
880 if (socketpair(PF_UNIX
,SOCK_STREAM
,0, fd0
) == -1) {
883 lprint("Error: openpty failed: %s\n", strerror(errno
));
887 if (slirp_openpty(&fd0
[0], &fd0
[1]) == -1) {
890 lprint("Error: openpty failed: %s\n", strerror(errno
));
897 lprint("Error: fork failed: %s\n", strerror(errno
));
908 /* Set the DISPLAY */
911 sprintf(buff
, "%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
912 setenv("DISPLAY", buff
, 1);
914 sprintf(buff
, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
922 for (s
= 3; s
<= 255; s
++)
925 execlp("rsh","rsh","-l", user
, host
, args
, NULL
);
927 /* Ooops, failed, let's tell the user why */
929 sprintf(buff
, "Error: execlp of %s failed: %s\n",
930 "rsh", strerror(errno
));
931 write(2, buff
, strlen(buff
)+1);
932 close(0); close(1); close(2); /* XXX */