2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
13 u_int curtime, time_fasttimo, last_slowtimo;
16 * Get our IP address and put it in our_addr
22 struct hostent *he = NULL;
24 if (gethostname(buff,256) == 0)
25 he = gethostbyname(buff);
27 our_addr = *(struct in_addr *)he->h_addr;
28 if (our_addr.s_addr == 0)
29 our_addr.s_addr = loopback_addr.s_addr;
33 struct quehead *qh_link;
34 struct quehead *qh_rlink;
38 insque(void *a, void *b)
40 register struct quehead *element = (struct quehead *) a;
41 register struct quehead *head = (struct quehead *) b;
42 element->qh_link = head->qh_link;
43 head->qh_link = (struct quehead *)element;
44 element->qh_rlink = (struct quehead *)head;
45 ((struct quehead *)(element->qh_link))->qh_rlink
46 = (struct quehead *)element;
52 register struct quehead *element = (struct quehead *) a;
53 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
54 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
55 element->qh_rlink = NULL;
58 int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
59 struct in_addr addr, int port)
61 struct ex_list *tmp_ptr;
63 /* First, check if the port is "bound" */
64 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
65 if (port == tmp_ptr->ex_fport &&
66 addr.s_addr == tmp_ptr->ex_addr.s_addr)
71 *ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
72 (*ex_ptr)->ex_fport = port;
73 (*ex_ptr)->ex_addr = addr;
74 (*ex_ptr)->ex_pty = do_pty;
75 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : strdup(exec);
76 (*ex_ptr)->ex_next = tmp_ptr;
83 * For systems with no strerror
87 extern char *sys_errlist[];
94 return sys_errlist[error];
96 return "Unknown error.";
105 fork_exec(struct socket *so, const char *ex, int do_pty)
107 /* not implemented */
115 * We create and bind a socket, then fork off to another
116 * process, which connects to this socket, after which we
117 * exec the wanted program. If something (strange) happens,
118 * the accept() call could block us forever.
120 * do_pty = 0 Fork/exec inetd style
121 * do_pty = 1 Fork/exec using slirp.telnetd
122 * do_ptr = 2 Fork/exec using pty
125 fork_exec(struct socket *so, const char *ex, int do_pty)
128 struct sockaddr_in addr;
129 socklen_t addrlen = sizeof(addr);
132 const char *argv[256];
133 /* don't want to clobber the original */
138 DEBUG_CALL("fork_exec");
139 DEBUG_ARG("so = %lx", (long)so);
140 DEBUG_ARG("ex = %lx", (long)ex);
141 DEBUG_ARG("do_pty = %lx", (long)do_pty);
146 addr.sin_family = AF_INET;
148 addr.sin_addr.s_addr = INADDR_ANY;
150 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
151 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
153 lprint("Error: inet socket: %s\n", strerror(errno));
162 lprint("Error: fork failed: %s\n", strerror(errno));
169 /* Set the DISPLAY */
171 (void) close(master);
172 #ifdef TIOCSCTTY /* XXXXX */
174 ioctl(s, TIOCSCTTY, (char *)NULL);
177 getsockname(s, (struct sockaddr *)&addr, &addrlen);
180 * Connect to the socket
181 * XXX If any of these fail, we're in trouble!
183 s = socket(AF_INET, SOCK_STREAM, 0);
184 addr.sin_addr = loopback_addr;
186 ret = connect(s, (struct sockaddr *)&addr, addrlen);
187 } while (ret < 0 && errno == EINTR);
193 for (s = getdtablesize() - 1; s >= 3; s--)
197 bptr = strdup(ex); /* No need to free() this */
199 /* Setup "slirp.telnetd -x" */
200 argv[i++] = "slirp.telnetd";
205 /* Change the string into argv[] */
207 while (*bptr != ' ' && *bptr != (char)0)
211 argv[i++] = strdup(curarg);
215 execvp(argv[0], (char **)argv);
217 /* Ooops, failed, let's tell the user why */
221 snprintf(buff, sizeof(buff),
222 "Error: execvp of %s failed: %s\n",
223 argv[0], strerror(errno));
224 write(2, buff, strlen(buff)+1);
226 close(0); close(1); close(2); /* XXX */
235 * XXX this could block us...
236 * XXX Should set a timer here, and if accept() doesn't
237 * return after X seconds, declare it a failure
238 * The only reason this will block forever is if socket()
239 * of connect() fail in the child process
242 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
243 } while (so->s < 0 && errno == EINTR);
246 setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
248 setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
252 /* Append the telnet options now */
253 if (so->so_m != NULL && do_pty == 1) {
254 sbappend(so, so->so_m);
270 bptr = (char *)malloc(strlen(str)+1);
279 void lprint(const char *format, ...)
283 va_start(args, format);
284 monitor_vprintf(cur_mon, format, args);
294 * Some BSD-derived systems have a sprintf which returns char *
298 vsprintf_len(string, format, args)
303 vsprintf(string, format, args);
304 return strlen(string);
309 sprintf_len(char *string, const char *format, ...)
311 sprintf_len(va_alist) va_dcl
316 va_start(args, format);
321 string = va_arg(args, char *);
322 format = va_arg(args, char *);
324 vsprintf(string, format, args);
325 return strlen(string);
339 t.tv_usec = usec * 1000;
341 select(0, &fdset, &fdset, &fdset, &t);
345 * Set fd blocking and non-blocking
353 unsigned long opt = 1;
358 ioctlsocket(fd, FIONBIO, &opt);
362 opt = fcntl(fd, F_GETFL, 0);
364 fcntl(fd, F_SETFL, opt);
373 unsigned long opt = 0;
378 ioctlsocket(fd, FIONBIO, &opt);
382 opt = fcntl(fd, F_GETFL, 0);
384 fcntl(fd, F_SETFL, opt);
388 void slirp_connection_info(Monitor *mon)
390 const char * const tcpstates[] = {
391 [TCPS_CLOSED] = "CLOSED",
392 [TCPS_LISTEN] = "LISTEN",
393 [TCPS_SYN_SENT] = "SYN_SENT",
394 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
395 [TCPS_ESTABLISHED] = "ESTABLISHED",
396 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
397 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
398 [TCPS_CLOSING] = "CLOSING",
399 [TCPS_LAST_ACK] = "LAST_ACK",
400 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
401 [TCPS_TIME_WAIT] = "TIME_WAIT",
403 struct in_addr dst_addr;
404 struct sockaddr_in src;
412 monitor_printf(mon, " Protocol[State] FD Source Address Port "
413 "Dest. Address Port RecvQ SendQ\n");
415 for (so = tcb.so_next; so != &tcb; so = so->so_next) {
416 if (so->so_state & SS_HOSTFWD) {
417 state = "HOST_FORWARD";
418 } else if (so->so_tcpcb) {
419 state = tcpstates[so->so_tcpcb->t_state];
423 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
424 src_len = sizeof(src);
425 getsockname(so->s, (struct sockaddr *)&src, &src_len);
426 dst_addr = so->so_laddr;
427 dst_port = so->so_lport;
429 src.sin_addr = so->so_laddr;
430 src.sin_port = so->so_lport;
431 dst_addr = so->so_faddr;
432 dst_port = so->so_fport;
434 n = snprintf(buf, sizeof(buf), " TCP[%s]", state);
435 memset(&buf[n], ' ', 19 - n);
437 monitor_printf(mon, "%s %3d %15s %5d ", buf, so->s,
438 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
439 ntohs(src.sin_port));
440 monitor_printf(mon, "%15s %5d %5d %5d\n",
441 inet_ntoa(dst_addr), ntohs(dst_port),
442 so->so_rcv.sb_cc, so->so_snd.sb_cc);
445 for (so = udb.so_next; so != &udb; so = so->so_next) {
446 if (so->so_state & SS_HOSTFWD) {
447 n = snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
448 src_len = sizeof(src);
449 getsockname(so->s, (struct sockaddr *)&src, &src_len);
450 dst_addr = so->so_laddr;
451 dst_port = so->so_lport;
453 n = snprintf(buf, sizeof(buf), " UDP[%d sec]",
454 (so->so_expire - curtime) / 1000);
455 src.sin_addr = so->so_laddr;
456 src.sin_port = so->so_lport;
457 dst_addr = so->so_faddr;
458 dst_port = so->so_fport;
460 memset(&buf[n], ' ', 19 - n);
462 monitor_printf(mon, "%s %3d %15s %5d ", buf, so->s,
463 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
464 ntohs(src.sin_port));
465 monitor_printf(mon, "%15s %5d %5d %5d\n",
466 inet_ntoa(dst_addr), ntohs(dst_port),
467 so->so_rcv.sb_cc, so->so_snd.sb_cc);