block: update bdrv_drain_all()/bdrv_drain() comments
[qemu/ar7.git] / slirp / misc.c
blob578e8b2c16a8cb054971730fb7ecfadd8a43ee2c
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 <slirp.h>
9 #include <libslirp.h>
11 #include "monitor/monitor.h"
12 #include "qemu/error-report.h"
13 #include "qemu/main-loop.h"
15 #ifdef DEBUG
16 int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
17 #endif
19 struct quehead {
20 struct quehead *qh_link;
21 struct quehead *qh_rlink;
24 inline void
25 insque(void *a, void *b)
27 register struct quehead *element = (struct quehead *) a;
28 register struct quehead *head = (struct quehead *) b;
29 element->qh_link = head->qh_link;
30 head->qh_link = (struct quehead *)element;
31 element->qh_rlink = (struct quehead *)head;
32 ((struct quehead *)(element->qh_link))->qh_rlink
33 = (struct quehead *)element;
36 inline void
37 remque(void *a)
39 register struct quehead *element = (struct quehead *) a;
40 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
41 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
42 element->qh_rlink = NULL;
45 int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
46 struct in_addr addr, int port)
48 struct ex_list *tmp_ptr;
50 /* First, check if the port is "bound" */
51 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
52 if (port == tmp_ptr->ex_fport &&
53 addr.s_addr == tmp_ptr->ex_addr.s_addr)
54 return -1;
57 tmp_ptr = *ex_ptr;
58 *ex_ptr = g_new(struct ex_list, 1);
59 (*ex_ptr)->ex_fport = port;
60 (*ex_ptr)->ex_addr = addr;
61 (*ex_ptr)->ex_pty = do_pty;
62 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : g_strdup(exec);
63 (*ex_ptr)->ex_next = tmp_ptr;
64 return 0;
67 #ifndef HAVE_STRERROR
70 * For systems with no strerror
73 extern int sys_nerr;
74 extern char *sys_errlist[];
76 char *
77 strerror(error)
78 int error;
80 if (error < sys_nerr)
81 return sys_errlist[error];
82 else
83 return "Unknown error.";
86 #endif
89 #ifdef _WIN32
91 int
92 fork_exec(struct socket *so, const char *ex, int do_pty)
94 /* not implemented */
95 return 0;
98 #else
101 * XXX This is ugly
102 * We create and bind a socket, then fork off to another
103 * process, which connects to this socket, after which we
104 * exec the wanted program. If something (strange) happens,
105 * the accept() call could block us forever.
107 * do_pty = 0 Fork/exec inetd style
108 * do_pty = 1 Fork/exec using slirp.telnetd
109 * do_ptr = 2 Fork/exec using pty
112 fork_exec(struct socket *so, const char *ex, int do_pty)
114 int s;
115 struct sockaddr_in addr;
116 socklen_t addrlen = sizeof(addr);
117 int opt;
118 const char *argv[256];
119 /* don't want to clobber the original */
120 char *bptr;
121 const char *curarg;
122 int c, i, ret;
123 pid_t pid;
125 DEBUG_CALL("fork_exec");
126 DEBUG_ARG("so = %lx", (long)so);
127 DEBUG_ARG("ex = %lx", (long)ex);
128 DEBUG_ARG("do_pty = %lx", (long)do_pty);
130 if (do_pty == 2) {
131 return 0;
132 } else {
133 addr.sin_family = AF_INET;
134 addr.sin_port = 0;
135 addr.sin_addr.s_addr = INADDR_ANY;
137 if ((s = qemu_socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
138 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
139 listen(s, 1) < 0) {
140 error_report("Error: inet socket: %s", strerror(errno));
141 closesocket(s);
143 return 0;
147 pid = fork();
148 switch(pid) {
149 case -1:
150 error_report("Error: fork failed: %s", strerror(errno));
151 close(s);
152 return 0;
154 case 0:
155 setsid();
157 /* Set the DISPLAY */
158 getsockname(s, (struct sockaddr *)&addr, &addrlen);
159 close(s);
161 * Connect to the socket
162 * XXX If any of these fail, we're in trouble!
164 s = qemu_socket(AF_INET, SOCK_STREAM, 0);
165 addr.sin_addr = loopback_addr;
166 do {
167 ret = connect(s, (struct sockaddr *)&addr, addrlen);
168 } while (ret < 0 && errno == EINTR);
170 dup2(s, 0);
171 dup2(s, 1);
172 dup2(s, 2);
173 for (s = getdtablesize() - 1; s >= 3; s--)
174 close(s);
176 i = 0;
177 bptr = g_strdup(ex); /* No need to free() this */
178 if (do_pty == 1) {
179 /* Setup "slirp.telnetd -x" */
180 argv[i++] = "slirp.telnetd";
181 argv[i++] = "-x";
182 argv[i++] = bptr;
183 } else
184 do {
185 /* Change the string into argv[] */
186 curarg = bptr;
187 while (*bptr != ' ' && *bptr != (char)0)
188 bptr++;
189 c = *bptr;
190 *bptr++ = (char)0;
191 argv[i++] = g_strdup(curarg);
192 } while (c);
194 argv[i] = NULL;
195 execvp(argv[0], (char **)argv);
197 /* Ooops, failed, let's tell the user why */
198 fprintf(stderr, "Error: execvp of %s failed: %s\n",
199 argv[0], strerror(errno));
200 close(0); close(1); close(2); /* XXX */
201 exit(1);
203 default:
204 qemu_add_child_watch(pid);
206 * XXX this could block us...
207 * XXX Should set a timer here, and if accept() doesn't
208 * return after X seconds, declare it a failure
209 * The only reason this will block forever is if socket()
210 * of connect() fail in the child process
212 do {
213 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
214 } while (so->s < 0 && errno == EINTR);
215 closesocket(s);
216 socket_set_fast_reuse(so->s);
217 opt = 1;
218 qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
219 qemu_set_nonblock(so->s);
221 /* Append the telnet options now */
222 if (so->so_m != NULL && do_pty == 1) {
223 sbappend(so, so->so_m);
224 so->so_m = NULL;
227 return 1;
230 #endif
232 void slirp_connection_info(Slirp *slirp, Monitor *mon)
234 const char * const tcpstates[] = {
235 [TCPS_CLOSED] = "CLOSED",
236 [TCPS_LISTEN] = "LISTEN",
237 [TCPS_SYN_SENT] = "SYN_SENT",
238 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
239 [TCPS_ESTABLISHED] = "ESTABLISHED",
240 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
241 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
242 [TCPS_CLOSING] = "CLOSING",
243 [TCPS_LAST_ACK] = "LAST_ACK",
244 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
245 [TCPS_TIME_WAIT] = "TIME_WAIT",
247 struct in_addr dst_addr;
248 struct sockaddr_in src;
249 socklen_t src_len;
250 uint16_t dst_port;
251 struct socket *so;
252 const char *state;
253 char buf[20];
255 monitor_printf(mon, " Protocol[State] FD Source Address Port "
256 "Dest. Address Port RecvQ SendQ\n");
258 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
259 if (so->so_state & SS_HOSTFWD) {
260 state = "HOST_FORWARD";
261 } else if (so->so_tcpcb) {
262 state = tcpstates[so->so_tcpcb->t_state];
263 } else {
264 state = "NONE";
266 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
267 src_len = sizeof(src);
268 getsockname(so->s, (struct sockaddr *)&src, &src_len);
269 dst_addr = so->so_laddr;
270 dst_port = so->so_lport;
271 } else {
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 snprintf(buf, sizeof(buf), " TCP[%s]", state);
278 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
279 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
280 ntohs(src.sin_port));
281 monitor_printf(mon, "%15s %5d %5d %5d\n",
282 inet_ntoa(dst_addr), ntohs(dst_port),
283 so->so_rcv.sb_cc, so->so_snd.sb_cc);
286 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
287 if (so->so_state & SS_HOSTFWD) {
288 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
289 src_len = sizeof(src);
290 getsockname(so->s, (struct sockaddr *)&src, &src_len);
291 dst_addr = so->so_laddr;
292 dst_port = so->so_lport;
293 } else {
294 snprintf(buf, sizeof(buf), " UDP[%d sec]",
295 (so->so_expire - curtime) / 1000);
296 src.sin_addr = so->so_laddr;
297 src.sin_port = so->so_lport;
298 dst_addr = so->so_faddr;
299 dst_port = so->so_fport;
301 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
302 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
303 ntohs(src.sin_port));
304 monitor_printf(mon, "%15s %5d %5d %5d\n",
305 inet_ntoa(dst_addr), ntohs(dst_port),
306 so->so_rcv.sb_cc, so->so_snd.sb_cc);
309 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
310 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
311 (so->so_expire - curtime) / 1000);
312 src.sin_addr = so->so_laddr;
313 dst_addr = so->so_faddr;
314 monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
315 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
316 monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
317 so->so_rcv.sb_cc, so->so_snd.sb_cc);