2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
8 #include "qemu/osdep.h"
12 #include "monitor/monitor.h"
13 #include "qemu/error-report.h"
14 #include "qemu/main-loop.h"
17 int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
21 insque(void *a, void *b)
23 register struct quehead *element = (struct quehead *) a;
24 register struct quehead *head = (struct quehead *) b;
25 element->qh_link = head->qh_link;
26 head->qh_link = (struct quehead *)element;
27 element->qh_rlink = (struct quehead *)head;
28 ((struct quehead *)(element->qh_link))->qh_rlink
29 = (struct quehead *)element;
35 register struct quehead *element = (struct quehead *) a;
36 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
37 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
38 element->qh_rlink = NULL;
41 int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
42 struct in_addr addr, int port)
44 struct ex_list *tmp_ptr;
46 /* First, check if the port is "bound" */
47 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
48 if (port == tmp_ptr->ex_fport &&
49 addr.s_addr == tmp_ptr->ex_addr.s_addr)
54 *ex_ptr = g_new(struct ex_list, 1);
55 (*ex_ptr)->ex_fport = port;
56 (*ex_ptr)->ex_addr = addr;
57 (*ex_ptr)->ex_pty = do_pty;
58 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : g_strdup(exec);
59 (*ex_ptr)->ex_next = tmp_ptr;
67 fork_exec(struct socket *so, const char *ex, int do_pty)
77 * We create and bind a socket, then fork off to another
78 * process, which connects to this socket, after which we
79 * exec the wanted program. If something (strange) happens,
80 * the accept() call could block us forever.
82 * do_pty = 0 Fork/exec inetd style
83 * do_pty = 1 Fork/exec using slirp.telnetd
84 * do_ptr = 2 Fork/exec using pty
87 fork_exec(struct socket *so, const char *ex, int do_pty)
90 struct sockaddr_in addr;
91 socklen_t addrlen = sizeof(addr);
93 const char *argv[256];
94 /* don't want to clobber the original */
100 DEBUG_CALL("fork_exec");
101 DEBUG_ARG("so = %p", so);
102 DEBUG_ARG("ex = %p", ex);
103 DEBUG_ARG("do_pty = %x", do_pty);
108 addr.sin_family = AF_INET;
110 addr.sin_addr.s_addr = INADDR_ANY;
112 if ((s = qemu_socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
113 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
115 error_report("Error: inet socket: %s", strerror(errno));
125 error_report("Error: fork failed: %s", strerror(errno));
132 /* Set the DISPLAY */
133 getsockname(s, (struct sockaddr *)&addr, &addrlen);
136 * Connect to the socket
137 * XXX If any of these fail, we're in trouble!
139 s = qemu_socket(AF_INET, SOCK_STREAM, 0);
140 addr.sin_addr = loopback_addr;
142 ret = connect(s, (struct sockaddr *)&addr, addrlen);
143 } while (ret < 0 && errno == EINTR);
148 for (s = getdtablesize() - 1; s >= 3; s--)
152 bptr = g_strdup(ex); /* No need to free() this */
154 /* Setup "slirp.telnetd -x" */
155 argv[i++] = "slirp.telnetd";
160 /* Change the string into argv[] */
162 while (*bptr != ' ' && *bptr != (char)0)
166 argv[i++] = g_strdup(curarg);
170 execvp(argv[0], (char **)argv);
172 /* Ooops, failed, let's tell the user why */
173 fprintf(stderr, "Error: execvp of %s failed: %s\n",
174 argv[0], strerror(errno));
175 close(0); close(1); close(2); /* XXX */
179 qemu_add_child_watch(pid);
181 * XXX this could block us...
182 * XXX Should set a timer here, and if accept() doesn't
183 * return after X seconds, declare it a failure
184 * The only reason this will block forever is if socket()
185 * of connect() fail in the child process
188 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
189 } while (so->s < 0 && errno == EINTR);
191 socket_set_fast_reuse(so->s);
193 qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
194 qemu_set_nonblock(so->s);
196 /* Append the telnet options now */
197 if (so->so_m != NULL && do_pty == 1) {
198 sbappend(so, so->so_m);
207 void slirp_connection_info(Slirp *slirp, Monitor *mon)
209 const char * const tcpstates[] = {
210 [TCPS_CLOSED] = "CLOSED",
211 [TCPS_LISTEN] = "LISTEN",
212 [TCPS_SYN_SENT] = "SYN_SENT",
213 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
214 [TCPS_ESTABLISHED] = "ESTABLISHED",
215 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
216 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
217 [TCPS_CLOSING] = "CLOSING",
218 [TCPS_LAST_ACK] = "LAST_ACK",
219 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
220 [TCPS_TIME_WAIT] = "TIME_WAIT",
222 struct in_addr dst_addr;
223 struct sockaddr_in src;
230 monitor_printf(mon, " Protocol[State] FD Source Address Port "
231 "Dest. Address Port RecvQ SendQ\n");
233 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
234 if (so->so_state & SS_HOSTFWD) {
235 state = "HOST_FORWARD";
236 } else if (so->so_tcpcb) {
237 state = tcpstates[so->so_tcpcb->t_state];
241 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
242 src_len = sizeof(src);
243 getsockname(so->s, (struct sockaddr *)&src, &src_len);
244 dst_addr = so->so_laddr;
245 dst_port = so->so_lport;
247 src.sin_addr = so->so_laddr;
248 src.sin_port = so->so_lport;
249 dst_addr = so->so_faddr;
250 dst_port = so->so_fport;
252 snprintf(buf, sizeof(buf), " TCP[%s]", state);
253 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
254 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
255 ntohs(src.sin_port));
256 monitor_printf(mon, "%15s %5d %5d %5d\n",
257 inet_ntoa(dst_addr), ntohs(dst_port),
258 so->so_rcv.sb_cc, so->so_snd.sb_cc);
261 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
262 if (so->so_state & SS_HOSTFWD) {
263 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
264 src_len = sizeof(src);
265 getsockname(so->s, (struct sockaddr *)&src, &src_len);
266 dst_addr = so->so_laddr;
267 dst_port = so->so_lport;
269 snprintf(buf, sizeof(buf), " UDP[%d sec]",
270 (so->so_expire - curtime) / 1000);
271 src.sin_addr = so->so_laddr;
272 src.sin_port = so->so_lport;
273 dst_addr = so->so_faddr;
274 dst_port = so->so_fport;
276 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
277 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
278 ntohs(src.sin_port));
279 monitor_printf(mon, "%15s %5d %5d %5d\n",
280 inet_ntoa(dst_addr), ntohs(dst_port),
281 so->so_rcv.sb_cc, so->so_snd.sb_cc);
284 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
285 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
286 (so->so_expire - curtime) / 1000);
287 src.sin_addr = so->so_laddr;
288 dst_addr = so->so_faddr;
289 monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
290 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
291 monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
292 so->so_rcv.sb_cc, so->so_snd.sb_cc);