dump-guest-memory: add "detach" support
[qemu.git] / slirp / misc.c
blobe2eea2e4b319c5966ceb83e8acf6abc3f5150ef8
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
8 #include "qemu/osdep.h"
9 #include <slirp.h>
10 #include <libslirp.h>
12 #include "monitor/monitor.h"
13 #include "qemu/error-report.h"
14 #include "qemu/main-loop.h"
16 #ifdef DEBUG
17 int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
18 #endif
20 struct quehead {
21 struct quehead *qh_link;
22 struct quehead *qh_rlink;
25 inline void
26 insque(void *a, void *b)
28 register struct quehead *element = (struct quehead *) a;
29 register struct quehead *head = (struct quehead *) b;
30 element->qh_link = head->qh_link;
31 head->qh_link = (struct quehead *)element;
32 element->qh_rlink = (struct quehead *)head;
33 ((struct quehead *)(element->qh_link))->qh_rlink
34 = (struct quehead *)element;
37 inline void
38 remque(void *a)
40 register struct quehead *element = (struct quehead *) a;
41 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
42 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
43 element->qh_rlink = NULL;
46 int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
47 struct in_addr addr, int port)
49 struct ex_list *tmp_ptr;
51 /* First, check if the port is "bound" */
52 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
53 if (port == tmp_ptr->ex_fport &&
54 addr.s_addr == tmp_ptr->ex_addr.s_addr)
55 return -1;
58 tmp_ptr = *ex_ptr;
59 *ex_ptr = g_new(struct ex_list, 1);
60 (*ex_ptr)->ex_fport = port;
61 (*ex_ptr)->ex_addr = addr;
62 (*ex_ptr)->ex_pty = do_pty;
63 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : g_strdup(exec);
64 (*ex_ptr)->ex_next = tmp_ptr;
65 return 0;
68 #ifndef HAVE_STRERROR
71 * For systems with no strerror
74 extern int sys_nerr;
75 extern char *sys_errlist[];
77 char *
78 strerror(error)
79 int error;
81 if (error < sys_nerr)
82 return sys_errlist[error];
83 else
84 return "Unknown error.";
87 #endif
90 #ifdef _WIN32
92 int
93 fork_exec(struct socket *so, const char *ex, int do_pty)
95 /* not implemented */
96 return 0;
99 #else
102 * XXX This is ugly
103 * We create and bind a socket, then fork off to another
104 * process, which connects to this socket, after which we
105 * exec the wanted program. If something (strange) happens,
106 * the accept() call could block us forever.
108 * do_pty = 0 Fork/exec inetd style
109 * do_pty = 1 Fork/exec using slirp.telnetd
110 * do_ptr = 2 Fork/exec using pty
113 fork_exec(struct socket *so, const char *ex, int do_pty)
115 int s;
116 struct sockaddr_in addr;
117 socklen_t addrlen = sizeof(addr);
118 int opt;
119 const char *argv[256];
120 /* don't want to clobber the original */
121 char *bptr;
122 const char *curarg;
123 int c, i, ret;
124 pid_t pid;
126 DEBUG_CALL("fork_exec");
127 DEBUG_ARG("so = %p", so);
128 DEBUG_ARG("ex = %p", ex);
129 DEBUG_ARG("do_pty = %x", do_pty);
131 if (do_pty == 2) {
132 return 0;
133 } else {
134 addr.sin_family = AF_INET;
135 addr.sin_port = 0;
136 addr.sin_addr.s_addr = INADDR_ANY;
138 if ((s = qemu_socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
139 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
140 listen(s, 1) < 0) {
141 error_report("Error: inet socket: %s", strerror(errno));
142 closesocket(s);
144 return 0;
148 pid = fork();
149 switch(pid) {
150 case -1:
151 error_report("Error: fork failed: %s", strerror(errno));
152 close(s);
153 return 0;
155 case 0:
156 setsid();
158 /* Set the DISPLAY */
159 getsockname(s, (struct sockaddr *)&addr, &addrlen);
160 close(s);
162 * Connect to the socket
163 * XXX If any of these fail, we're in trouble!
165 s = qemu_socket(AF_INET, SOCK_STREAM, 0);
166 addr.sin_addr = loopback_addr;
167 do {
168 ret = connect(s, (struct sockaddr *)&addr, addrlen);
169 } while (ret < 0 && errno == EINTR);
171 dup2(s, 0);
172 dup2(s, 1);
173 dup2(s, 2);
174 for (s = getdtablesize() - 1; s >= 3; s--)
175 close(s);
177 i = 0;
178 bptr = g_strdup(ex); /* No need to free() this */
179 if (do_pty == 1) {
180 /* Setup "slirp.telnetd -x" */
181 argv[i++] = "slirp.telnetd";
182 argv[i++] = "-x";
183 argv[i++] = bptr;
184 } else
185 do {
186 /* Change the string into argv[] */
187 curarg = bptr;
188 while (*bptr != ' ' && *bptr != (char)0)
189 bptr++;
190 c = *bptr;
191 *bptr++ = (char)0;
192 argv[i++] = g_strdup(curarg);
193 } while (c);
195 argv[i] = NULL;
196 execvp(argv[0], (char **)argv);
198 /* Ooops, failed, let's tell the user why */
199 fprintf(stderr, "Error: execvp of %s failed: %s\n",
200 argv[0], strerror(errno));
201 close(0); close(1); close(2); /* XXX */
202 exit(1);
204 default:
205 qemu_add_child_watch(pid);
207 * XXX this could block us...
208 * XXX Should set a timer here, and if accept() doesn't
209 * return after X seconds, declare it a failure
210 * The only reason this will block forever is if socket()
211 * of connect() fail in the child process
213 do {
214 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
215 } while (so->s < 0 && errno == EINTR);
216 closesocket(s);
217 socket_set_fast_reuse(so->s);
218 opt = 1;
219 qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
220 qemu_set_nonblock(so->s);
222 /* Append the telnet options now */
223 if (so->so_m != NULL && do_pty == 1) {
224 sbappend(so, so->so_m);
225 so->so_m = NULL;
228 return 1;
231 #endif
233 void slirp_connection_info(Slirp *slirp, Monitor *mon)
235 const char * const tcpstates[] = {
236 [TCPS_CLOSED] = "CLOSED",
237 [TCPS_LISTEN] = "LISTEN",
238 [TCPS_SYN_SENT] = "SYN_SENT",
239 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
240 [TCPS_ESTABLISHED] = "ESTABLISHED",
241 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
242 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
243 [TCPS_CLOSING] = "CLOSING",
244 [TCPS_LAST_ACK] = "LAST_ACK",
245 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
246 [TCPS_TIME_WAIT] = "TIME_WAIT",
248 struct in_addr dst_addr;
249 struct sockaddr_in src;
250 socklen_t src_len;
251 uint16_t dst_port;
252 struct socket *so;
253 const char *state;
254 char buf[20];
256 monitor_printf(mon, " Protocol[State] FD Source Address Port "
257 "Dest. Address Port RecvQ SendQ\n");
259 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
260 if (so->so_state & SS_HOSTFWD) {
261 state = "HOST_FORWARD";
262 } else if (so->so_tcpcb) {
263 state = tcpstates[so->so_tcpcb->t_state];
264 } else {
265 state = "NONE";
267 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
268 src_len = sizeof(src);
269 getsockname(so->s, (struct sockaddr *)&src, &src_len);
270 dst_addr = so->so_laddr;
271 dst_port = so->so_lport;
272 } else {
273 src.sin_addr = so->so_laddr;
274 src.sin_port = so->so_lport;
275 dst_addr = so->so_faddr;
276 dst_port = so->so_fport;
278 snprintf(buf, sizeof(buf), " TCP[%s]", state);
279 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
280 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
281 ntohs(src.sin_port));
282 monitor_printf(mon, "%15s %5d %5d %5d\n",
283 inet_ntoa(dst_addr), ntohs(dst_port),
284 so->so_rcv.sb_cc, so->so_snd.sb_cc);
287 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
288 if (so->so_state & SS_HOSTFWD) {
289 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
290 src_len = sizeof(src);
291 getsockname(so->s, (struct sockaddr *)&src, &src_len);
292 dst_addr = so->so_laddr;
293 dst_port = so->so_lport;
294 } else {
295 snprintf(buf, sizeof(buf), " UDP[%d sec]",
296 (so->so_expire - curtime) / 1000);
297 src.sin_addr = so->so_laddr;
298 src.sin_port = so->so_lport;
299 dst_addr = so->so_faddr;
300 dst_port = so->so_fport;
302 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
303 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
304 ntohs(src.sin_port));
305 monitor_printf(mon, "%15s %5d %5d %5d\n",
306 inet_ntoa(dst_addr), ntohs(dst_port),
307 so->so_rcv.sb_cc, so->so_snd.sb_cc);
310 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
311 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
312 (so->so_expire - curtime) / 1000);
313 src.sin_addr = so->so_laddr;
314 dst_addr = so->so_faddr;
315 monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
316 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
317 monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
318 so->so_rcv.sb_cc, so->so_snd.sb_cc);