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"
11 #include "monitor/monitor.h"
12 #include "qemu/error-report.h"
13 #include "qemu/main-loop.h"
16 int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
20 insque(void *a, void *b)
22 register struct quehead *element = (struct quehead *) a;
23 register struct quehead *head = (struct quehead *) b;
24 element->qh_link = head->qh_link;
25 head->qh_link = (struct quehead *)element;
26 element->qh_rlink = (struct quehead *)head;
27 ((struct quehead *)(element->qh_link))->qh_rlink
28 = (struct quehead *)element;
34 register struct quehead *element = (struct quehead *) a;
35 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
36 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
37 element->qh_rlink = NULL;
40 int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
41 struct in_addr addr, int port)
43 struct ex_list *tmp_ptr;
45 /* First, check if the port is "bound" */
46 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
47 if (port == tmp_ptr->ex_fport &&
48 addr.s_addr == tmp_ptr->ex_addr.s_addr)
53 *ex_ptr = g_new(struct ex_list, 1);
54 (*ex_ptr)->ex_fport = port;
55 (*ex_ptr)->ex_addr = addr;
56 (*ex_ptr)->ex_pty = do_pty;
57 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : g_strdup(exec);
58 (*ex_ptr)->ex_next = tmp_ptr;
66 fork_exec(struct socket *so, const char *ex, int do_pty)
76 * We create and bind a socket, then fork off to another
77 * process, which connects to this socket, after which we
78 * exec the wanted program. If something (strange) happens,
79 * the accept() call could block us forever.
81 * do_pty = 0 Fork/exec inetd style
82 * do_pty = 1 Fork/exec using slirp.telnetd
83 * do_ptr = 2 Fork/exec using pty
86 fork_exec(struct socket *so, const char *ex, int do_pty)
89 struct sockaddr_in addr;
90 socklen_t addrlen = sizeof(addr);
92 const char *argv[256];
93 /* don't want to clobber the original */
99 DEBUG_CALL("fork_exec");
100 DEBUG_ARG("so = %p", so);
101 DEBUG_ARG("ex = %p", ex);
102 DEBUG_ARG("do_pty = %x", do_pty);
107 addr.sin_family = AF_INET;
109 addr.sin_addr.s_addr = INADDR_ANY;
111 if ((s = qemu_socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
112 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
114 error_report("Error: inet socket: %s", strerror(errno));
126 error_report("Error: fork failed: %s", strerror(errno));
133 /* Set the DISPLAY */
134 getsockname(s, (struct sockaddr *)&addr, &addrlen);
137 * Connect to the socket
138 * XXX If any of these fail, we're in trouble!
140 s = qemu_socket(AF_INET, SOCK_STREAM, 0);
141 addr.sin_addr = loopback_addr;
143 ret = connect(s, (struct sockaddr *)&addr, addrlen);
144 } while (ret < 0 && errno == EINTR);
149 for (s = getdtablesize() - 1; s >= 3; s--)
153 bptr = g_strdup(ex); /* No need to free() this */
155 /* Setup "slirp.telnetd -x" */
156 argv[i++] = "slirp.telnetd";
161 /* Change the string into argv[] */
163 while (*bptr != ' ' && *bptr != (char)0)
167 argv[i++] = g_strdup(curarg);
171 execvp(argv[0], (char **)argv);
173 /* Ooops, failed, let's tell the user why */
174 fprintf(stderr, "Error: execvp of %s failed: %s\n",
175 argv[0], strerror(errno));
176 close(0); close(1); close(2); /* XXX */
180 qemu_add_child_watch(pid);
182 * XXX this could block us...
183 * XXX Should set a timer here, and if accept() doesn't
184 * return after X seconds, declare it a failure
185 * The only reason this will block forever is if socket()
186 * of connect() fail in the child process
189 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
190 } while (so->s < 0 && errno == EINTR);
192 socket_set_fast_reuse(so->s);
194 qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
195 qemu_set_nonblock(so->s);
197 /* Append the telnet options now */
198 if (so->so_m != NULL && do_pty == 1) {
199 sbappend(so, so->so_m);
208 void slirp_connection_info(Slirp *slirp, Monitor *mon)
210 const char * const tcpstates[] = {
211 [TCPS_CLOSED] = "CLOSED",
212 [TCPS_LISTEN] = "LISTEN",
213 [TCPS_SYN_SENT] = "SYN_SENT",
214 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
215 [TCPS_ESTABLISHED] = "ESTABLISHED",
216 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
217 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
218 [TCPS_CLOSING] = "CLOSING",
219 [TCPS_LAST_ACK] = "LAST_ACK",
220 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
221 [TCPS_TIME_WAIT] = "TIME_WAIT",
223 struct in_addr dst_addr;
224 struct sockaddr_in src;
231 monitor_printf(mon, " Protocol[State] FD Source Address Port "
232 "Dest. Address Port RecvQ SendQ\n");
234 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
235 if (so->so_state & SS_HOSTFWD) {
236 state = "HOST_FORWARD";
237 } else if (so->so_tcpcb) {
238 state = tcpstates[so->so_tcpcb->t_state];
242 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
243 src_len = sizeof(src);
244 getsockname(so->s, (struct sockaddr *)&src, &src_len);
245 dst_addr = so->so_laddr;
246 dst_port = so->so_lport;
248 src.sin_addr = so->so_laddr;
249 src.sin_port = so->so_lport;
250 dst_addr = so->so_faddr;
251 dst_port = so->so_fport;
253 snprintf(buf, sizeof(buf), " TCP[%s]", state);
254 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
255 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
256 ntohs(src.sin_port));
257 monitor_printf(mon, "%15s %5d %5d %5d\n",
258 inet_ntoa(dst_addr), ntohs(dst_port),
259 so->so_rcv.sb_cc, so->so_snd.sb_cc);
262 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
263 if (so->so_state & SS_HOSTFWD) {
264 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
265 src_len = sizeof(src);
266 getsockname(so->s, (struct sockaddr *)&src, &src_len);
267 dst_addr = so->so_laddr;
268 dst_port = so->so_lport;
270 snprintf(buf, sizeof(buf), " UDP[%d sec]",
271 (so->so_expire - curtime) / 1000);
272 src.sin_addr = so->so_laddr;
273 src.sin_port = so->so_lport;
274 dst_addr = so->so_faddr;
275 dst_port = so->so_fport;
277 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
278 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
279 ntohs(src.sin_port));
280 monitor_printf(mon, "%15s %5d %5d %5d\n",
281 inet_ntoa(dst_addr), ntohs(dst_port),
282 so->so_rcv.sb_cc, so->so_snd.sb_cc);
285 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
286 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
287 (so->so_expire - curtime) / 1000);
288 src.sin_addr = so->so_laddr;
289 dst_addr = so->so_faddr;
290 monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
291 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
292 monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
293 so->so_rcv.sb_cc, so->so_snd.sb_cc);