2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
14 int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
17 u_int curtime, time_fasttimo, last_slowtimo;
20 struct quehead *qh_link;
21 struct quehead *qh_rlink;
25 insque(void *a, void *b)
27 register struct quehead *element = (struct quehead *) a;
28 register struct quehead *head = (struct quehead *) b;
29 element->qh_link = head->qh_link;
30 head->qh_link = (struct quehead *)element;
31 element->qh_rlink = (struct quehead *)head;
32 ((struct quehead *)(element->qh_link))->qh_rlink
33 = (struct quehead *)element;
39 register struct quehead *element = (struct quehead *) a;
40 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
41 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
42 element->qh_rlink = NULL;
45 int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
46 struct in_addr addr, int port)
48 struct ex_list *tmp_ptr;
50 /* First, check if the port is "bound" */
51 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
52 if (port == tmp_ptr->ex_fport &&
53 addr.s_addr == tmp_ptr->ex_addr.s_addr)
58 *ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
59 (*ex_ptr)->ex_fport = port;
60 (*ex_ptr)->ex_addr = addr;
61 (*ex_ptr)->ex_pty = do_pty;
62 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : strdup(exec);
63 (*ex_ptr)->ex_next = tmp_ptr;
70 * For systems with no strerror
74 extern char *sys_errlist[];
81 return sys_errlist[error];
83 return "Unknown error.";
92 fork_exec(struct socket *so, const char *ex, int do_pty)
102 * We create and bind a socket, then fork off to another
103 * process, which connects to this socket, after which we
104 * exec the wanted program. If something (strange) happens,
105 * the accept() call could block us forever.
107 * do_pty = 0 Fork/exec inetd style
108 * do_pty = 1 Fork/exec using slirp.telnetd
109 * do_ptr = 2 Fork/exec using pty
112 fork_exec(struct socket *so, const char *ex, int do_pty)
115 struct sockaddr_in addr;
116 socklen_t addrlen = sizeof(addr);
119 const char *argv[256];
120 /* don't want to clobber the original */
125 DEBUG_CALL("fork_exec");
126 DEBUG_ARG("so = %lx", (long)so);
127 DEBUG_ARG("ex = %lx", (long)ex);
128 DEBUG_ARG("do_pty = %lx", (long)do_pty);
133 addr.sin_family = AF_INET;
135 addr.sin_addr.s_addr = INADDR_ANY;
137 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
138 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
140 lprint("Error: inet socket: %s\n", strerror(errno));
149 lprint("Error: fork failed: %s\n", strerror(errno));
156 /* Set the DISPLAY */
158 (void) close(master);
159 #ifdef TIOCSCTTY /* XXXXX */
161 ioctl(s, TIOCSCTTY, (char *)NULL);
164 getsockname(s, (struct sockaddr *)&addr, &addrlen);
167 * Connect to the socket
168 * XXX If any of these fail, we're in trouble!
170 s = socket(AF_INET, SOCK_STREAM, 0);
171 addr.sin_addr = loopback_addr;
173 ret = connect(s, (struct sockaddr *)&addr, addrlen);
174 } while (ret < 0 && errno == EINTR);
180 for (s = getdtablesize() - 1; s >= 3; s--)
184 bptr = strdup(ex); /* No need to free() this */
186 /* Setup "slirp.telnetd -x" */
187 argv[i++] = "slirp.telnetd";
192 /* Change the string into argv[] */
194 while (*bptr != ' ' && *bptr != (char)0)
198 argv[i++] = strdup(curarg);
202 execvp(argv[0], (char **)argv);
204 /* Ooops, failed, let's tell the user why */
208 snprintf(buff, sizeof(buff),
209 "Error: execvp of %s failed: %s\n",
210 argv[0], strerror(errno));
211 write(2, buff, strlen(buff)+1);
213 close(0); close(1); close(2); /* XXX */
222 * XXX this could block us...
223 * XXX Should set a timer here, and if accept() doesn't
224 * return after X seconds, declare it a failure
225 * The only reason this will block forever is if socket()
226 * of connect() fail in the child process
229 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
230 } while (so->s < 0 && errno == EINTR);
233 setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
235 setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
239 /* Append the telnet options now */
240 if (so->so_m != NULL && do_pty == 1) {
241 sbappend(so, so->so_m);
257 bptr = (char *)malloc(strlen(str)+1);
266 void lprint(const char *format, ...)
270 va_start(args, format);
271 monitor_vprintf(cur_mon, format, args);
281 * Some BSD-derived systems have a sprintf which returns char *
285 vsprintf_len(string, format, args)
290 vsprintf(string, format, args);
291 return strlen(string);
296 sprintf_len(char *string, const char *format, ...)
298 sprintf_len(va_alist) va_dcl
303 va_start(args, format);
308 string = va_arg(args, char *);
309 format = va_arg(args, char *);
311 vsprintf(string, format, args);
312 return strlen(string);
326 t.tv_usec = usec * 1000;
328 select(0, &fdset, &fdset, &fdset, &t);
332 * Set fd blocking and non-blocking
340 unsigned long opt = 1;
345 ioctlsocket(fd, FIONBIO, &opt);
349 opt = fcntl(fd, F_GETFL, 0);
351 fcntl(fd, F_SETFL, opt);
360 unsigned long opt = 0;
365 ioctlsocket(fd, FIONBIO, &opt);
369 opt = fcntl(fd, F_GETFL, 0);
371 fcntl(fd, F_SETFL, opt);
375 void slirp_connection_info(Monitor *mon)
377 const char * const tcpstates[] = {
378 [TCPS_CLOSED] = "CLOSED",
379 [TCPS_LISTEN] = "LISTEN",
380 [TCPS_SYN_SENT] = "SYN_SENT",
381 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
382 [TCPS_ESTABLISHED] = "ESTABLISHED",
383 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
384 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
385 [TCPS_CLOSING] = "CLOSING",
386 [TCPS_LAST_ACK] = "LAST_ACK",
387 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
388 [TCPS_TIME_WAIT] = "TIME_WAIT",
390 struct in_addr dst_addr;
391 struct sockaddr_in src;
399 monitor_printf(mon, " Protocol[State] FD Source Address Port "
400 "Dest. Address Port RecvQ SendQ\n");
402 for (so = tcb.so_next; so != &tcb; so = so->so_next) {
403 if (so->so_state & SS_HOSTFWD) {
404 state = "HOST_FORWARD";
405 } else if (so->so_tcpcb) {
406 state = tcpstates[so->so_tcpcb->t_state];
410 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
411 src_len = sizeof(src);
412 getsockname(so->s, (struct sockaddr *)&src, &src_len);
413 dst_addr = so->so_laddr;
414 dst_port = so->so_lport;
416 src.sin_addr = so->so_laddr;
417 src.sin_port = so->so_lport;
418 dst_addr = so->so_faddr;
419 dst_port = so->so_fport;
421 n = snprintf(buf, sizeof(buf), " TCP[%s]", state);
422 memset(&buf[n], ' ', 19 - n);
424 monitor_printf(mon, "%s %3d %15s %5d ", buf, so->s,
425 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
426 ntohs(src.sin_port));
427 monitor_printf(mon, "%15s %5d %5d %5d\n",
428 inet_ntoa(dst_addr), ntohs(dst_port),
429 so->so_rcv.sb_cc, so->so_snd.sb_cc);
432 for (so = udb.so_next; so != &udb; so = so->so_next) {
433 if (so->so_state & SS_HOSTFWD) {
434 n = snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
435 src_len = sizeof(src);
436 getsockname(so->s, (struct sockaddr *)&src, &src_len);
437 dst_addr = so->so_laddr;
438 dst_port = so->so_lport;
440 n = snprintf(buf, sizeof(buf), " UDP[%d sec]",
441 (so->so_expire - curtime) / 1000);
442 src.sin_addr = so->so_laddr;
443 src.sin_port = so->so_lport;
444 dst_addr = so->so_faddr;
445 dst_port = so->so_fport;
447 memset(&buf[n], ' ', 19 - n);
449 monitor_printf(mon, "%s %3d %15s %5d ", buf, so->s,
450 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
451 ntohs(src.sin_port));
452 monitor_printf(mon, "%15s %5d %5d %5d\n",
453 inet_ntoa(dst_addr), ntohs(dst_port),
454 so->so_rcv.sb_cc, so->so_snd.sb_cc);