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 * Get our IP address and put it in our_addr
26 struct hostent *he = NULL;
28 if (gethostname(buff,256) == 0)
29 he = gethostbyname(buff);
31 our_addr = *(struct in_addr *)he->h_addr;
32 if (our_addr.s_addr == 0)
33 our_addr.s_addr = loopback_addr.s_addr;
37 struct quehead *qh_link;
38 struct quehead *qh_rlink;
42 insque(void *a, void *b)
44 register struct quehead *element = (struct quehead *) a;
45 register struct quehead *head = (struct quehead *) b;
46 element->qh_link = head->qh_link;
47 head->qh_link = (struct quehead *)element;
48 element->qh_rlink = (struct quehead *)head;
49 ((struct quehead *)(element->qh_link))->qh_rlink
50 = (struct quehead *)element;
56 register struct quehead *element = (struct quehead *) a;
57 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
58 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
59 element->qh_rlink = NULL;
62 int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
63 struct in_addr addr, int port)
65 struct ex_list *tmp_ptr;
67 /* First, check if the port is "bound" */
68 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
69 if (port == tmp_ptr->ex_fport &&
70 addr.s_addr == tmp_ptr->ex_addr.s_addr)
75 *ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
76 (*ex_ptr)->ex_fport = port;
77 (*ex_ptr)->ex_addr = addr;
78 (*ex_ptr)->ex_pty = do_pty;
79 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : strdup(exec);
80 (*ex_ptr)->ex_next = tmp_ptr;
87 * For systems with no strerror
91 extern char *sys_errlist[];
98 return sys_errlist[error];
100 return "Unknown error.";
109 fork_exec(struct socket *so, const char *ex, int do_pty)
111 /* not implemented */
119 * We create and bind a socket, then fork off to another
120 * process, which connects to this socket, after which we
121 * exec the wanted program. If something (strange) happens,
122 * the accept() call could block us forever.
124 * do_pty = 0 Fork/exec inetd style
125 * do_pty = 1 Fork/exec using slirp.telnetd
126 * do_ptr = 2 Fork/exec using pty
129 fork_exec(struct socket *so, const char *ex, int do_pty)
132 struct sockaddr_in addr;
133 socklen_t addrlen = sizeof(addr);
136 const char *argv[256];
137 /* don't want to clobber the original */
142 DEBUG_CALL("fork_exec");
143 DEBUG_ARG("so = %lx", (long)so);
144 DEBUG_ARG("ex = %lx", (long)ex);
145 DEBUG_ARG("do_pty = %lx", (long)do_pty);
150 addr.sin_family = AF_INET;
152 addr.sin_addr.s_addr = INADDR_ANY;
154 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
155 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
157 lprint("Error: inet socket: %s\n", strerror(errno));
166 lprint("Error: fork failed: %s\n", strerror(errno));
173 /* Set the DISPLAY */
175 (void) close(master);
176 #ifdef TIOCSCTTY /* XXXXX */
178 ioctl(s, TIOCSCTTY, (char *)NULL);
181 getsockname(s, (struct sockaddr *)&addr, &addrlen);
184 * Connect to the socket
185 * XXX If any of these fail, we're in trouble!
187 s = socket(AF_INET, SOCK_STREAM, 0);
188 addr.sin_addr = loopback_addr;
190 ret = connect(s, (struct sockaddr *)&addr, addrlen);
191 } while (ret < 0 && errno == EINTR);
197 for (s = getdtablesize() - 1; s >= 3; s--)
201 bptr = strdup(ex); /* No need to free() this */
203 /* Setup "slirp.telnetd -x" */
204 argv[i++] = "slirp.telnetd";
209 /* Change the string into argv[] */
211 while (*bptr != ' ' && *bptr != (char)0)
215 argv[i++] = strdup(curarg);
219 execvp(argv[0], (char **)argv);
221 /* Ooops, failed, let's tell the user why */
225 snprintf(buff, sizeof(buff),
226 "Error: execvp of %s failed: %s\n",
227 argv[0], strerror(errno));
228 write(2, buff, strlen(buff)+1);
230 close(0); close(1); close(2); /* XXX */
239 * XXX this could block us...
240 * XXX Should set a timer here, and if accept() doesn't
241 * return after X seconds, declare it a failure
242 * The only reason this will block forever is if socket()
243 * of connect() fail in the child process
246 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
247 } while (so->s < 0 && errno == EINTR);
250 setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
252 setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
256 /* Append the telnet options now */
257 if (so->so_m != NULL && do_pty == 1) {
258 sbappend(so, so->so_m);
274 bptr = (char *)malloc(strlen(str)+1);
283 void lprint(const char *format, ...)
287 va_start(args, format);
288 monitor_vprintf(cur_mon, format, args);
298 * Some BSD-derived systems have a sprintf which returns char *
302 vsprintf_len(string, format, args)
307 vsprintf(string, format, args);
308 return strlen(string);
313 sprintf_len(char *string, const char *format, ...)
315 sprintf_len(va_alist) va_dcl
320 va_start(args, format);
325 string = va_arg(args, char *);
326 format = va_arg(args, char *);
328 vsprintf(string, format, args);
329 return strlen(string);
343 t.tv_usec = usec * 1000;
345 select(0, &fdset, &fdset, &fdset, &t);
349 * Set fd blocking and non-blocking
357 unsigned long opt = 1;
362 ioctlsocket(fd, FIONBIO, &opt);
366 opt = fcntl(fd, F_GETFL, 0);
368 fcntl(fd, F_SETFL, opt);
377 unsigned long opt = 0;
382 ioctlsocket(fd, FIONBIO, &opt);
386 opt = fcntl(fd, F_GETFL, 0);
388 fcntl(fd, F_SETFL, opt);
392 void slirp_connection_info(Monitor *mon)
394 const char * const tcpstates[] = {
395 [TCPS_CLOSED] = "CLOSED",
396 [TCPS_LISTEN] = "LISTEN",
397 [TCPS_SYN_SENT] = "SYN_SENT",
398 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
399 [TCPS_ESTABLISHED] = "ESTABLISHED",
400 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
401 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
402 [TCPS_CLOSING] = "CLOSING",
403 [TCPS_LAST_ACK] = "LAST_ACK",
404 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
405 [TCPS_TIME_WAIT] = "TIME_WAIT",
407 struct in_addr dst_addr;
408 struct sockaddr_in src;
416 monitor_printf(mon, " Protocol[State] FD Source Address Port "
417 "Dest. Address Port RecvQ SendQ\n");
419 for (so = tcb.so_next; so != &tcb; so = so->so_next) {
420 if (so->so_state & SS_HOSTFWD) {
421 state = "HOST_FORWARD";
422 } else if (so->so_tcpcb) {
423 state = tcpstates[so->so_tcpcb->t_state];
427 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
428 src_len = sizeof(src);
429 getsockname(so->s, (struct sockaddr *)&src, &src_len);
430 dst_addr = so->so_laddr;
431 dst_port = so->so_lport;
433 src.sin_addr = so->so_laddr;
434 src.sin_port = so->so_lport;
435 dst_addr = so->so_faddr;
436 dst_port = so->so_fport;
438 n = snprintf(buf, sizeof(buf), " TCP[%s]", state);
439 memset(&buf[n], ' ', 19 - n);
441 monitor_printf(mon, "%s %3d %15s %5d ", buf, so->s,
442 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
443 ntohs(src.sin_port));
444 monitor_printf(mon, "%15s %5d %5d %5d\n",
445 inet_ntoa(dst_addr), ntohs(dst_port),
446 so->so_rcv.sb_cc, so->so_snd.sb_cc);
449 for (so = udb.so_next; so != &udb; so = so->so_next) {
450 if (so->so_state & SS_HOSTFWD) {
451 n = snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
452 src_len = sizeof(src);
453 getsockname(so->s, (struct sockaddr *)&src, &src_len);
454 dst_addr = so->so_laddr;
455 dst_port = so->so_lport;
457 n = snprintf(buf, sizeof(buf), " UDP[%d sec]",
458 (so->so_expire - curtime) / 1000);
459 src.sin_addr = so->so_laddr;
460 src.sin_port = so->so_lport;
461 dst_addr = so->so_faddr;
462 dst_port = so->so_fport;
464 memset(&buf[n], ' ', 19 - n);
466 monitor_printf(mon, "%s %3d %15s %5d ", buf, so->s,
467 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
468 ntohs(src.sin_port));
469 monitor_printf(mon, "%15s %5d %5d %5d\n",
470 inet_ntoa(dst_addr), ntohs(dst_port),
471 so->so_rcv.sb_cc, so->so_snd.sb_cc);