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
;
24 lprint("X Redir: X not being redirected.\r\n");
26 lprint("X Redir: In sh/bash/zsh/etc. type: DISPLAY=%s:%d.%d; export DISPLAY\r\n",
27 inet_ntoa(our_addr
), x_port
, x_screen
);
28 lprint("X Redir: In csh/tcsh/etc. type: setenv DISPLAY %s:%d.%d\r\n",
29 inet_ntoa(our_addr
), x_port
, x_screen
);
31 lprint("X Redir: Redirecting to display %d\r\n", x_display
);
39 * XXX Allow more than one X redirection?
42 redir_x(inaddr
, start_port
, display
, screen
)
51 lprint("X Redir: X already being redirected.\r\n");
54 for (i
= 6001 + (start_port
-1); i
<= 6100; i
++) {
55 if (solisten(htons(i
), inaddr
, htons(6000 + display
), 0)) {
64 lprint("X Redir: Error: Couldn't redirect a port for X. Weird.\r\n");
70 * Get our IP address and put it in our_addr
76 struct hostent
*he
= NULL
;
78 if (gethostname(buff
,256) == 0)
79 he
= gethostbyname(buff
);
81 our_addr
= *(struct in_addr
*)he
->h_addr
;
82 if (our_addr
.s_addr
== 0)
83 our_addr
.s_addr
= loopback_addr
.s_addr
;
87 struct quehead
*qh_link
;
88 struct quehead
*qh_rlink
;
95 register struct quehead
*element
= (struct quehead
*) a
;
96 register struct quehead
*head
= (struct quehead
*) b
;
97 element
->qh_link
= head
->qh_link
;
98 head
->qh_link
= (struct quehead
*)element
;
99 element
->qh_rlink
= (struct quehead
*)head
;
100 ((struct quehead
*)(element
->qh_link
))->qh_rlink
101 = (struct quehead
*)element
;
108 register struct quehead
*element
= (struct quehead
*) a
;
109 ((struct quehead
*)(element
->qh_link
))->qh_rlink
= element
->qh_rlink
;
110 ((struct quehead
*)(element
->qh_rlink
))->qh_link
= element
->qh_link
;
111 element
->qh_rlink
= NULL
;
112 /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
119 add_exec(ex_ptr
, do_pty
, exec
, addr
, port
)
120 struct ex_list
**ex_ptr
;
126 struct ex_list
*tmp_ptr
;
128 /* First, check if the port is "bound" */
129 for (tmp_ptr
= *ex_ptr
; tmp_ptr
; tmp_ptr
= tmp_ptr
->ex_next
) {
130 if (port
== tmp_ptr
->ex_fport
&& addr
== tmp_ptr
->ex_addr
)
135 *ex_ptr
= (struct ex_list
*)malloc(sizeof(struct ex_list
));
136 (*ex_ptr
)->ex_fport
= port
;
137 (*ex_ptr
)->ex_addr
= addr
;
138 (*ex_ptr
)->ex_pty
= do_pty
;
139 (*ex_ptr
)->ex_exec
= (do_pty
== 3) ? exec
: strdup(exec
);
140 (*ex_ptr
)->ex_next
= tmp_ptr
;
144 #ifndef HAVE_STRERROR
147 * For systems with no strerror
151 extern char *sys_errlist
[];
157 if (error
< sys_nerr
)
158 return sys_errlist
[error
];
160 return "Unknown error.";
169 fork_exec(struct socket
*so
, const char *ex
, int do_pty
)
171 /* not implemented */
179 slirp_openpty(amaster
, aslave
)
180 int *amaster
, *aslave
;
182 register int master
, slave
;
187 if ((master
= open("/dev/ptmx", O_RDWR
)) < 0 ||
188 grantpt(master
) < 0 ||
189 unlockpt(master
) < 0 ||
190 (ptr
= ptsname(master
)) == NULL
) {
195 if ((slave
= open(ptr
, O_RDWR
)) < 0 ||
196 ioctl(slave
, I_PUSH
, "ptem") < 0 ||
197 ioctl(slave
, I_PUSH
, "ldterm") < 0 ||
198 ioctl(slave
, I_PUSH
, "ttcompat") < 0) {
210 static char line
[] = "/dev/ptyXX";
211 register const char *cp1
, *cp2
;
213 for (cp1
= "pqrsPQRS"; *cp1
; cp1
++) {
215 for (cp2
= "0123456789abcdefghijklmnopqrstuv"; *cp2
; cp2
++) {
217 if ((master
= open(line
, O_RDWR
, 0)) == -1) {
219 return (-1); /* out of ptys */
222 /* These will fail */
223 (void) chown(line
, getuid(), 0);
224 (void) chmod(line
, S_IRUSR
|S_IWUSR
|S_IWGRP
);
228 if ((slave
= open(line
, O_RDWR
, 0)) != -1) {
233 (void) close(master
);
238 errno
= ENOENT
; /* out of ptys */
246 * We create and bind a socket, then fork off to another
247 * process, which connects to this socket, after which we
248 * exec the wanted program. If something (strange) happens,
249 * the accept() call could block us forever.
251 * do_pty = 0 Fork/exec inetd style
252 * do_pty = 1 Fork/exec using slirp.telnetd
253 * do_ptr = 2 Fork/exec using pty
256 fork_exec(struct socket
*so
, const char *ex
, int do_pty
)
259 struct sockaddr_in addr
;
260 socklen_t addrlen
= sizeof(addr
);
263 const char *argv
[256];
267 /* don't want to clobber the original */
272 DEBUG_CALL("fork_exec");
273 DEBUG_ARG("so = %lx", (long)so
);
274 DEBUG_ARG("ex = %lx", (long)ex
);
275 DEBUG_ARG("do_pty = %lx", (long)do_pty
);
279 if (slirp_openpty(&master
, &s
) == -1) {
280 lprint("Error: openpty failed: %s\n", strerror(errno
));
287 addr
.sin_family
= AF_INET
;
289 addr
.sin_addr
.s_addr
= INADDR_ANY
;
291 if ((s
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0 ||
292 bind(s
, (struct sockaddr
*)&addr
, addrlen
) < 0 ||
294 lprint("Error: inet socket: %s\n", strerror(errno
));
303 lprint("Error: fork failed: %s\n", strerror(errno
));
310 /* Set the DISPLAY */
312 (void) close(master
);
313 #ifdef TIOCSCTTY /* XXXXX */
315 ioctl(s
, TIOCSCTTY
, (char *)NULL
);
318 getsockname(s
, (struct sockaddr
*)&addr
, &addrlen
);
321 * Connect to the socket
322 * XXX If any of these fail, we're in trouble!
324 s
= socket(AF_INET
, SOCK_STREAM
, 0);
325 addr
.sin_addr
= loopback_addr
;
327 ret
= connect(s
, (struct sockaddr
*)&addr
, addrlen
);
328 } while (ret
< 0 && errno
== EINTR
);
334 sprintf(buff
, "%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
335 setenv("DISPLAY", buff
, 1);
337 sprintf(buff
, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
345 for (s
= getdtablesize() - 1; s
>= 3; s
--)
349 bptr
= strdup(ex
); /* No need to free() this */
351 /* Setup "slirp.telnetd -x" */
352 argv
[i
++] = "slirp.telnetd";
357 /* Change the string into argv[] */
359 while (*bptr
!= ' ' && *bptr
!= (char)0)
363 argv
[i
++] = strdup(curarg
);
367 execvp(argv
[0], (char **)argv
);
369 /* Ooops, failed, let's tell the user why */
373 snprintf(buff
, sizeof(buff
),
374 "Error: execvp of %s failed: %s\n",
375 argv
[0], strerror(errno
));
376 write(2, buff
, strlen(buff
)+1);
378 close(0); close(1); close(2); /* XXX */
387 * XXX this could block us...
388 * XXX Should set a timer here, and if accept() doesn't
389 * return after X seconds, declare it a failure
390 * The only reason this will block forever is if socket()
391 * of connect() fail in the child process
394 so
->s
= accept(s
, (struct sockaddr
*)&addr
, &addrlen
);
395 } while (so
->s
< 0 && errno
== EINTR
);
398 setsockopt(so
->s
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&opt
,sizeof(int));
400 setsockopt(so
->s
,SOL_SOCKET
,SO_OOBINLINE
,(char *)&opt
,sizeof(int));
404 /* Append the telnet options now */
405 if (so
->so_m
!= 0 && do_pty
== 1) {
406 sbappend(so
, so
->so_m
);
422 bptr
= (char *)malloc(strlen(str
)+1);
435 #ifndef NO_UNIX_SOCKETS
436 struct sockaddr_un sock_un
;
438 struct sockaddr_in sock_in
;
442 if (slirp_socket_passwd
) {
443 s
= socket(AF_INET
, SOCK_STREAM
, 0);
446 sock_in
.sin_family
= AF_INET
;
447 sock_in
.sin_addr
.s_addr
= slirp_socket_addr
;
448 sock_in
.sin_port
= htons(slirp_socket_port
);
449 if (connect(s
, (struct sockaddr
*)&sock_in
, sizeof(sock_in
)) != 0)
450 slirp_exit(1); /* just exit...*/
451 sprintf(buff
, "kill %s:%d", slirp_socket_passwd
, slirp_socket_unit
);
452 write(s
, buff
, strlen(buff
)+1);
454 #ifndef NO_UNIX_SOCKETS
456 s
= socket(AF_UNIX
, SOCK_STREAM
, 0);
459 sock_un
.sun_family
= AF_UNIX
;
460 strcpy(sock_un
.sun_path
, socket_path
);
461 if (connect(s
, (struct sockaddr
*)&sock_un
,
462 sizeof(sock_un
.sun_family
) + sizeof(sock_un
.sun_path
)) != 0)
464 sprintf(buff
, "kill none:%d", slirp_socket_unit
);
465 write(s
, buff
, strlen(buff
)+1);
478 /* Don't need our data anymore */
479 /* XXX This makes SunOS barf */
483 for (i
= 255; i
>= 0; i
--)
486 signal(SIGQUIT
, slirp_exit
);
487 signal(SIGHUP
, snooze_hup
);
490 /* Wait for any signal */
493 /* Just in case ... */
506 /* Don't need our data anymore */
507 /* XXX This makes SunOS barf */
510 signal(SIGQUIT
, slirp_exit
);
511 signal(SIGHUP
, slirp_exit
);
512 signal(SIGINT
, slirp_exit
);
513 signal(SIGTERM
, slirp_exit
);
515 /* Fudge to get term_raw and term_restore to work */
516 if (NULL
== (ttyp
= tty_attach (0, slirp_tty
))) {
517 lprint ("Error: tty_attach failed in misc.c:relay()\r\n");
521 ttyp
->flags
|= TTY_CTTY
;
530 n
= select(s
+1, &readfds
, (fd_set
*)0, (fd_set
*)0, (struct timeval
*)0);
535 if (FD_ISSET(0, &readfds
)) {
536 n
= read(0, buf
, 8192);
539 n
= writen(s
, buf
, n
);
544 if (FD_ISSET(s
, &readfds
)) {
545 n
= read(s
, buf
, 8192);
548 n
= writen(0, buf
, n
);
554 /* Just in case.... */
560 extern void term_vprintf(const char *fmt
, va_list ap
);
562 void lprint(const char *format
, ...)
566 va_start(args
, format
);
567 term_vprintf(format
, args
);
571 int (*lprint_print
) _P((void *, const char *, va_list));
572 char *lprint_ptr
, *lprint_ptr2
, **lprint_arg
;
576 lprint(const char *format
, ...)
578 lprint(va_alist
) va_dcl
584 va_start(args
, format
);
588 format
= va_arg(args
, char *);
591 /* If we're printing to an sbuf, make sure there's enough room */
594 if ((lprint_ptr
- lprint_sb
->sb_wptr
) >=
595 (lprint_sb
->sb_datalen
- (strlen(format
) + 100))) {
596 int deltaw
= lprint_sb
->sb_wptr
- lprint_sb
->sb_data
;
597 int deltar
= lprint_sb
->sb_rptr
- lprint_sb
->sb_data
;
598 int deltap
= lprint_ptr
- lprint_sb
->sb_data
;
600 lprint_sb
->sb_data
= (char *)realloc(lprint_sb
->sb_data
,
601 lprint_sb
->sb_datalen
+ TCP_SNDSPACE
);
603 /* Adjust all values */
604 lprint_sb
->sb_wptr
= lprint_sb
->sb_data
+ deltaw
;
605 lprint_sb
->sb_rptr
= lprint_sb
->sb_data
+ deltar
;
606 lprint_ptr
= lprint_sb
->sb_data
+ deltap
;
608 lprint_sb
->sb_datalen
+= TCP_SNDSPACE
;
613 lprint_ptr
+= (*lprint_print
)(*lprint_arg
, format
, args
);
615 /* Check if they want output to be logged to file as well */
619 * otherwise you'll get ^M all over the file
621 int len
= strlen(format
);
624 bptr1
= bptr2
= strdup(format
);
628 memcpy(bptr1
, bptr1
+1, len
+1);
632 vfprintf(lfd
, bptr2
, args
);
643 u_int8_t tos
= 0, emu
= 0;
644 char buff1
[256], buff2
[256], buff4
[128];
649 if (sscanf(buff
, "%256s %256s", buff2
, buff1
) != 2) {
650 lprint("Error: Bad arguments\r\n");
654 if (sscanf(buff1
, "%d:%d", &lport
, &fport
) != 2) {
656 if (sscanf(buff1
, "%d", &fport
) != 1) {
657 lprint("Error: Bad first argument\r\n");
662 if (sscanf(buff2
, "%128[^:]:%128s", buff1
, buff3
) != 2) {
664 if (sscanf(buff2
, "%256s", buff1
) != 1) {
665 lprint("Error: Bad second argument\r\n");
671 if (strcmp(buff3
, "lowdelay") == 0)
672 tos
= IPTOS_LOWDELAY
;
673 else if (strcmp(buff3
, "throughput") == 0)
674 tos
= IPTOS_THROUGHPUT
;
676 lprint("Error: Expecting \"lowdelay\"/\"throughput\"\r\n");
681 if (strcmp(buff1
, "ftp") == 0)
683 else if (strcmp(buff1
, "irc") == 0)
685 else if (strcmp(buff1
, "none") == 0)
686 emu
= EMU_NONE
; /* ie: no emulation */
688 lprint("Error: Unknown service\r\n");
692 /* First, check that it isn't already emulated */
693 for (emup
= tcpemu
; emup
; emup
= emup
->next
) {
694 if (emup
->lport
== lport
&& emup
->fport
== fport
) {
695 lprint("Error: port already emulated\r\n");
701 emup
= (struct emu_t
*)malloc(sizeof (struct emu_t
));
702 emup
->lport
= (u_int16_t
)lport
;
703 emup
->fport
= (u_int16_t
)fport
;
709 /* And finally, mark all current sessions, if any, as being emulated */
710 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so
->so_next
) {
711 if ((lport
&& lport
== ntohs(so
->so_lport
)) ||
712 (fport
&& fport
== ntohs(so
->so_fport
))) {
720 lprint("Adding emulation for %s to port %d/%d\r\n", buff1
, emup
->lport
, emup
->fport
);
730 * Some BSD-derived systems have a sprintf which returns char *
734 vsprintf_len(string
, format
, args
)
739 vsprintf(string
, format
, args
);
740 return strlen(string
);
745 sprintf_len(char *string
, const char *format
, ...)
747 sprintf_len(va_alist
) va_dcl
752 va_start(args
, format
);
757 string
= va_arg(args
, char *);
758 format
= va_arg(args
, char *);
760 vsprintf(string
, format
, args
);
761 return strlen(string
);
776 t
.tv_usec
= usec
* 1000;
778 select(0, &fdset
, &fdset
, &fdset
, &t
);
782 * Set fd blocking and non-blocking
792 ioctlsocket(fd
, FIONBIO
, &opt
);
796 opt
= fcntl(fd
, F_GETFL
, 0);
798 fcntl(fd
, F_SETFL
, opt
);
809 ioctlsocket(fd
, FIONBIO
, &opt
);
813 opt
= fcntl(fd
, F_GETFL
, 0);
815 fcntl(fd
, F_SETFL
, opt
);
825 rsh_exec(so
,ns
, user
, host
, args
)
837 DEBUG_CALL("rsh_exec");
838 DEBUG_ARG("so = %lx", (long)so
);
841 lprint("Error: pipe failed: %s\n", strerror(errno
));
844 /* #ifdef HAVE_SOCKETPAIR */
846 if (socketpair(PF_UNIX
,SOCK_STREAM
,0, fd0
) == -1) {
849 lprint("Error: openpty failed: %s\n", strerror(errno
));
853 if (slirp_openpty(&fd0
[0], &fd0
[1]) == -1) {
856 lprint("Error: openpty failed: %s\n", strerror(errno
));
863 lprint("Error: fork failed: %s\n", strerror(errno
));
874 /* Set the DISPLAY */
877 sprintf(buff
, "%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
878 setenv("DISPLAY", buff
, 1);
880 sprintf(buff
, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr
), x_port
, x_screen
);
888 for (s
= 3; s
<= 255; s
++)
891 execlp("rsh","rsh","-l", user
, host
, args
, NULL
);
893 /* Ooops, failed, let's tell the user why */
895 sprintf(buff
, "Error: execlp of %s failed: %s\n",
896 "rsh", strerror(errno
));
897 write(2, buff
, strlen(buff
)+1);
898 close(0); close(1); close(2); /* XXX */