Merge remote-tracking branch 'qemu-kvm/uq/master' into staging
[qemu.git] / slirp / misc.c
blobd4df972d1332187d185f61908d03f0e18c5db723
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"
13 #ifdef DEBUG
14 int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
15 #endif
17 struct quehead {
18 struct quehead *qh_link;
19 struct quehead *qh_rlink;
22 inline void
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;
34 inline void
35 remque(void *a)
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)
52 return -1;
55 tmp_ptr = *ex_ptr;
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;
62 return 0;
65 #ifndef HAVE_STRERROR
68 * For systems with no strerror
71 extern int sys_nerr;
72 extern char *sys_errlist[];
74 char *
75 strerror(error)
76 int error;
78 if (error < sys_nerr)
79 return sys_errlist[error];
80 else
81 return "Unknown error.";
84 #endif
87 #ifdef _WIN32
89 int
90 fork_exec(struct socket *so, const char *ex, int do_pty)
92 /* not implemented */
93 return 0;
96 #else
99 * XXX This is ugly
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)
112 int s;
113 struct sockaddr_in addr;
114 socklen_t addrlen = sizeof(addr);
115 int opt;
116 const char *argv[256];
117 /* don't want to clobber the original */
118 char *bptr;
119 const char *curarg;
120 int c, i, ret;
121 pid_t pid;
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);
128 if (do_pty == 2) {
129 return 0;
130 } else {
131 addr.sin_family = AF_INET;
132 addr.sin_port = 0;
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 ||
137 listen(s, 1) < 0) {
138 lprint("Error: inet socket: %s\n", strerror(errno));
139 closesocket(s);
141 return 0;
145 pid = fork();
146 switch(pid) {
147 case -1:
148 lprint("Error: fork failed: %s\n", strerror(errno));
149 close(s);
150 return 0;
152 case 0:
153 setsid();
155 /* Set the DISPLAY */
156 getsockname(s, (struct sockaddr *)&addr, &addrlen);
157 close(s);
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;
164 do {
165 ret = connect(s, (struct sockaddr *)&addr, addrlen);
166 } while (ret < 0 && errno == EINTR);
168 dup2(s, 0);
169 dup2(s, 1);
170 dup2(s, 2);
171 for (s = getdtablesize() - 1; s >= 3; s--)
172 close(s);
174 i = 0;
175 bptr = g_strdup(ex); /* No need to free() this */
176 if (do_pty == 1) {
177 /* Setup "slirp.telnetd -x" */
178 argv[i++] = "slirp.telnetd";
179 argv[i++] = "-x";
180 argv[i++] = bptr;
181 } else
182 do {
183 /* Change the string into argv[] */
184 curarg = bptr;
185 while (*bptr != ' ' && *bptr != (char)0)
186 bptr++;
187 c = *bptr;
188 *bptr++ = (char)0;
189 argv[i++] = strdup(curarg);
190 } while (c);
192 argv[i] = NULL;
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 */
199 exit(1);
201 default:
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
210 do {
211 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
212 } while (so->s < 0 && errno == EINTR);
213 closesocket(s);
214 opt = 1;
215 setsockopt(so->s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(int));
216 opt = 1;
217 setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, (char *)&opt, sizeof(int));
218 socket_set_nonblock(so->s);
220 /* Append the telnet options now */
221 if (so->so_m != NULL && do_pty == 1) {
222 sbappend(so, so->so_m);
223 so->so_m = NULL;
226 return 1;
229 #endif
231 #ifndef HAVE_STRDUP
232 char *
233 strdup(str)
234 const char *str;
236 char *bptr;
238 bptr = (char *)malloc(strlen(str)+1);
239 strcpy(bptr, str);
241 return bptr;
243 #endif
245 #include "monitor/monitor.h"
247 void lprint(const char *format, ...)
249 va_list args;
251 va_start(args, format);
252 monitor_vprintf(default_mon, format, args);
253 va_end(args);
256 void slirp_connection_info(Slirp *slirp, Monitor *mon)
258 const char * const tcpstates[] = {
259 [TCPS_CLOSED] = "CLOSED",
260 [TCPS_LISTEN] = "LISTEN",
261 [TCPS_SYN_SENT] = "SYN_SENT",
262 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
263 [TCPS_ESTABLISHED] = "ESTABLISHED",
264 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
265 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
266 [TCPS_CLOSING] = "CLOSING",
267 [TCPS_LAST_ACK] = "LAST_ACK",
268 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
269 [TCPS_TIME_WAIT] = "TIME_WAIT",
271 struct in_addr dst_addr;
272 struct sockaddr_in src;
273 socklen_t src_len;
274 uint16_t dst_port;
275 struct socket *so;
276 const char *state;
277 char buf[20];
279 monitor_printf(mon, " Protocol[State] FD Source Address Port "
280 "Dest. Address Port RecvQ SendQ\n");
282 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
283 if (so->so_state & SS_HOSTFWD) {
284 state = "HOST_FORWARD";
285 } else if (so->so_tcpcb) {
286 state = tcpstates[so->so_tcpcb->t_state];
287 } else {
288 state = "NONE";
290 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
291 src_len = sizeof(src);
292 getsockname(so->s, (struct sockaddr *)&src, &src_len);
293 dst_addr = so->so_laddr;
294 dst_port = so->so_lport;
295 } else {
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 snprintf(buf, sizeof(buf), " TCP[%s]", state);
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->udb.so_next; so != &slirp->udb; so = so->so_next) {
311 if (so->so_state & SS_HOSTFWD) {
312 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
313 src_len = sizeof(src);
314 getsockname(so->s, (struct sockaddr *)&src, &src_len);
315 dst_addr = so->so_laddr;
316 dst_port = so->so_lport;
317 } else {
318 snprintf(buf, sizeof(buf), " UDP[%d sec]",
319 (so->so_expire - curtime) / 1000);
320 src.sin_addr = so->so_laddr;
321 src.sin_port = so->so_lport;
322 dst_addr = so->so_faddr;
323 dst_port = so->so_fport;
325 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
326 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
327 ntohs(src.sin_port));
328 monitor_printf(mon, "%15s %5d %5d %5d\n",
329 inet_ntoa(dst_addr), ntohs(dst_port),
330 so->so_rcv.sb_cc, so->so_snd.sb_cc);
333 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
334 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
335 (so->so_expire - curtime) / 1000);
336 src.sin_addr = so->so_laddr;
337 dst_addr = so->so_faddr;
338 monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
339 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
340 monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
341 so->so_rcv.sb_cc, so->so_snd.sb_cc);