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;
18 struct quehead *qh_link;
19 struct quehead *qh_rlink;
23 insque(void *a, void *b)
25 register struct quehead *element = (struct quehead *) a;
26 register struct quehead *head = (struct quehead *) b;
27 element->qh_link = head->qh_link;
28 head->qh_link = (struct quehead *)element;
29 element->qh_rlink = (struct quehead *)head;
30 ((struct quehead *)(element->qh_link))->qh_rlink
31 = (struct quehead *)element;
37 register struct quehead *element = (struct quehead *) a;
38 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
39 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
40 element->qh_rlink = NULL;
43 int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
44 struct in_addr addr, int port)
46 struct ex_list *tmp_ptr;
48 /* First, check if the port is "bound" */
49 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
50 if (port == tmp_ptr->ex_fport &&
51 addr.s_addr == tmp_ptr->ex_addr.s_addr)
56 *ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
57 (*ex_ptr)->ex_fport = port;
58 (*ex_ptr)->ex_addr = addr;
59 (*ex_ptr)->ex_pty = do_pty;
60 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : strdup(exec);
61 (*ex_ptr)->ex_next = tmp_ptr;
68 * For systems with no strerror
72 extern char *sys_errlist[];
79 return sys_errlist[error];
81 return "Unknown error.";
90 fork_exec(struct socket *so, const char *ex, int do_pty)
100 * We create and bind a socket, then fork off to another
101 * process, which connects to this socket, after which we
102 * exec the wanted program. If something (strange) happens,
103 * the accept() call could block us forever.
105 * do_pty = 0 Fork/exec inetd style
106 * do_pty = 1 Fork/exec using slirp.telnetd
107 * do_ptr = 2 Fork/exec using pty
110 fork_exec(struct socket *so, const char *ex, int do_pty)
113 struct sockaddr_in addr;
114 socklen_t addrlen = sizeof(addr);
116 const char *argv[256];
117 /* don't want to clobber the original */
123 DEBUG_CALL("fork_exec");
124 DEBUG_ARG("so = %lx", (long)so);
125 DEBUG_ARG("ex = %lx", (long)ex);
126 DEBUG_ARG("do_pty = %lx", (long)do_pty);
131 addr.sin_family = AF_INET;
133 addr.sin_addr.s_addr = INADDR_ANY;
135 if ((s = qemu_socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
136 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
138 lprint("Error: inet socket: %s\n", strerror(errno));
148 lprint("Error: fork failed: %s\n", strerror(errno));
155 /* Set the DISPLAY */
156 getsockname(s, (struct sockaddr *)&addr, &addrlen);
159 * Connect to the socket
160 * XXX If any of these fail, we're in trouble!
162 s = qemu_socket(AF_INET, SOCK_STREAM, 0);
163 addr.sin_addr = loopback_addr;
165 ret = connect(s, (struct sockaddr *)&addr, addrlen);
166 } while (ret < 0 && errno == EINTR);
171 for (s = getdtablesize() - 1; s >= 3; s--)
175 bptr = g_strdup(ex); /* No need to free() this */
177 /* Setup "slirp.telnetd -x" */
178 argv[i++] = "slirp.telnetd";
183 /* Change the string into argv[] */
185 while (*bptr != ' ' && *bptr != (char)0)
189 argv[i++] = strdup(curarg);
193 execvp(argv[0], (char **)argv);
195 /* Ooops, failed, let's tell the user why */
196 fprintf(stderr, "Error: execvp of %s failed: %s\n",
197 argv[0], strerror(errno));
198 close(0); close(1); close(2); /* XXX */
202 qemu_add_child_watch(pid);
204 * XXX this could block us...
205 * XXX Should set a timer here, and if accept() doesn't
206 * return after X seconds, declare it a failure
207 * The only reason this will block forever is if socket()
208 * of connect() fail in the child process
211 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
212 } while (so->s < 0 && errno == EINTR);
215 setsockopt(so->s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(int));
217 setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, (char *)&opt, sizeof(int));
220 /* Append the telnet options now */
221 if (so->so_m != NULL && do_pty == 1) {
222 sbappend(so, so->so_m);
238 bptr = (char *)malloc(strlen(str)+1);
247 void lprint(const char *format, ...)
251 va_start(args, format);
252 monitor_vprintf(default_mon, format, args);
265 t.tv_usec = usec * 1000;
267 select(0, &fdset, &fdset, &fdset, &t);
271 * Set fd blocking and non-blocking
279 unsigned long opt = 1;
284 ioctlsocket(fd, FIONBIO, &opt);
288 opt = fcntl(fd, F_GETFL, 0);
290 fcntl(fd, F_SETFL, opt);
299 unsigned long opt = 0;
304 ioctlsocket(fd, FIONBIO, &opt);
308 opt = fcntl(fd, F_GETFL, 0);
310 fcntl(fd, F_SETFL, opt);
314 void slirp_connection_info(Slirp *slirp, Monitor *mon)
316 const char * const tcpstates[] = {
317 [TCPS_CLOSED] = "CLOSED",
318 [TCPS_LISTEN] = "LISTEN",
319 [TCPS_SYN_SENT] = "SYN_SENT",
320 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
321 [TCPS_ESTABLISHED] = "ESTABLISHED",
322 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
323 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
324 [TCPS_CLOSING] = "CLOSING",
325 [TCPS_LAST_ACK] = "LAST_ACK",
326 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
327 [TCPS_TIME_WAIT] = "TIME_WAIT",
329 struct in_addr dst_addr;
330 struct sockaddr_in src;
337 monitor_printf(mon, " Protocol[State] FD Source Address Port "
338 "Dest. Address Port RecvQ SendQ\n");
340 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
341 if (so->so_state & SS_HOSTFWD) {
342 state = "HOST_FORWARD";
343 } else if (so->so_tcpcb) {
344 state = tcpstates[so->so_tcpcb->t_state];
348 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
349 src_len = sizeof(src);
350 getsockname(so->s, (struct sockaddr *)&src, &src_len);
351 dst_addr = so->so_laddr;
352 dst_port = so->so_lport;
354 src.sin_addr = so->so_laddr;
355 src.sin_port = so->so_lport;
356 dst_addr = so->so_faddr;
357 dst_port = so->so_fport;
359 snprintf(buf, sizeof(buf), " TCP[%s]", state);
360 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
361 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
362 ntohs(src.sin_port));
363 monitor_printf(mon, "%15s %5d %5d %5d\n",
364 inet_ntoa(dst_addr), ntohs(dst_port),
365 so->so_rcv.sb_cc, so->so_snd.sb_cc);
368 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
369 if (so->so_state & SS_HOSTFWD) {
370 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
371 src_len = sizeof(src);
372 getsockname(so->s, (struct sockaddr *)&src, &src_len);
373 dst_addr = so->so_laddr;
374 dst_port = so->so_lport;
376 snprintf(buf, sizeof(buf), " UDP[%d sec]",
377 (so->so_expire - curtime) / 1000);
378 src.sin_addr = so->so_laddr;
379 src.sin_port = so->so_lport;
380 dst_addr = so->so_faddr;
381 dst_port = so->so_fport;
383 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
384 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
385 ntohs(src.sin_port));
386 monitor_printf(mon, "%15s %5d %5d %5d\n",
387 inet_ntoa(dst_addr), ntohs(dst_port),
388 so->so_rcv.sb_cc, so->so_snd.sb_cc);
391 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
392 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
393 (so->so_expire - curtime) / 1000);
394 src.sin_addr = so->so_laddr;
395 dst_addr = so->so_faddr;
396 monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
397 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
398 monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
399 so->so_rcv.sb_cc, so->so_snd.sb_cc);