globals: Allow global properties to be optional
[qemu/kevin.git] / slirp / misc.c
blob57bdd808e2d400db0fae1ecd210a95319e143d51
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"
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 inline void
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;
31 inline void
32 remque(void *a)
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)
49 return -1;
52 tmp_ptr = *ex_ptr;
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;
59 return 0;
63 #ifdef _WIN32
65 int
66 fork_exec(struct socket *so, const char *ex, int do_pty)
68 /* not implemented */
69 return 0;
72 #else
75 * XXX This is ugly
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
85 int
86 fork_exec(struct socket *so, const char *ex, int do_pty)
88 int s, cs;
89 struct sockaddr_in addr, csaddr;
90 socklen_t addrlen = sizeof(addr);
91 socklen_t csaddrlen = sizeof(csaddr);
92 int opt;
93 const char *argv[256];
94 /* don't want to clobber the original */
95 char *bptr;
96 const char *curarg;
97 int c, i, ret;
98 pid_t pid;
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);
105 if (do_pty == 2) {
106 return 0;
107 } else {
108 addr.sin_family = AF_INET;
109 addr.sin_port = 0;
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 ||
114 listen(s, 1) < 0) {
115 error_report("Error: inet socket: %s", strerror(errno));
116 if (s >= 0) {
117 closesocket(s);
120 return 0;
124 if (getsockname(s, (struct sockaddr *)&csaddr, &csaddrlen) < 0) {
125 closesocket(s);
126 return 0;
128 cs = qemu_socket(AF_INET, SOCK_STREAM, 0);
129 if (cs < 0) {
130 closesocket(s);
131 return 0;
133 csaddr.sin_addr = loopback_addr;
135 * This connect won't block because we've already listen()ed on
136 * the server end (even though we won't accept() the connection
137 * until later on).
139 do {
140 ret = connect(cs, (struct sockaddr *)&csaddr, csaddrlen);
141 } while (ret < 0 && errno == EINTR);
142 if (ret < 0) {
143 closesocket(s);
144 closesocket(cs);
145 return 0;
148 pid = fork();
149 switch(pid) {
150 case -1:
151 error_report("Error: fork failed: %s", strerror(errno));
152 closesocket(cs);
153 close(s);
154 return 0;
156 case 0:
157 setsid();
159 /* Set the DISPLAY */
160 close(s);
161 dup2(cs, 0);
162 dup2(cs, 1);
163 dup2(cs, 2);
164 for (s = getdtablesize() - 1; s >= 3; s--)
165 close(s);
167 i = 0;
168 bptr = g_strdup(ex); /* No need to free() this */
169 if (do_pty == 1) {
170 /* Setup "slirp.telnetd -x" */
171 argv[i++] = "slirp.telnetd";
172 argv[i++] = "-x";
173 argv[i++] = bptr;
174 } else
175 do {
176 /* Change the string into argv[] */
177 curarg = bptr;
178 while (*bptr != ' ' && *bptr != (char)0)
179 bptr++;
180 c = *bptr;
181 *bptr++ = (char)0;
182 argv[i++] = g_strdup(curarg);
183 } while (c);
185 argv[i] = NULL;
186 execvp(argv[0], (char **)argv);
188 /* Ooops, failed, let's tell the user why */
189 fprintf(stderr, "Error: execvp of %s failed: %s\n",
190 argv[0], strerror(errno));
191 close(0); close(1); close(2); /* XXX */
192 exit(1);
194 default:
195 qemu_add_child_watch(pid);
196 closesocket(cs);
198 * This should never block, because we already connect()ed
199 * on the child end before we forked.
201 do {
202 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
203 } while (so->s < 0 && errno == EINTR);
204 closesocket(s);
205 socket_set_fast_reuse(so->s);
206 opt = 1;
207 qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
208 qemu_set_nonblock(so->s);
210 /* Append the telnet options now */
211 if (so->so_m != NULL && do_pty == 1) {
212 sbappend(so, so->so_m);
213 so->so_m = NULL;
216 return 1;
219 #endif
221 void slirp_connection_info(Slirp *slirp, Monitor *mon)
223 const char * const tcpstates[] = {
224 [TCPS_CLOSED] = "CLOSED",
225 [TCPS_LISTEN] = "LISTEN",
226 [TCPS_SYN_SENT] = "SYN_SENT",
227 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
228 [TCPS_ESTABLISHED] = "ESTABLISHED",
229 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
230 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
231 [TCPS_CLOSING] = "CLOSING",
232 [TCPS_LAST_ACK] = "LAST_ACK",
233 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
234 [TCPS_TIME_WAIT] = "TIME_WAIT",
236 struct in_addr dst_addr;
237 struct sockaddr_in src;
238 socklen_t src_len;
239 uint16_t dst_port;
240 struct socket *so;
241 const char *state;
242 char buf[20];
244 monitor_printf(mon, " Protocol[State] FD Source Address Port "
245 "Dest. Address Port RecvQ SendQ\n");
247 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
248 if (so->so_state & SS_HOSTFWD) {
249 state = "HOST_FORWARD";
250 } else if (so->so_tcpcb) {
251 state = tcpstates[so->so_tcpcb->t_state];
252 } else {
253 state = "NONE";
255 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
256 src_len = sizeof(src);
257 getsockname(so->s, (struct sockaddr *)&src, &src_len);
258 dst_addr = so->so_laddr;
259 dst_port = so->so_lport;
260 } else {
261 src.sin_addr = so->so_laddr;
262 src.sin_port = so->so_lport;
263 dst_addr = so->so_faddr;
264 dst_port = so->so_fport;
266 snprintf(buf, sizeof(buf), " TCP[%s]", state);
267 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
268 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
269 ntohs(src.sin_port));
270 monitor_printf(mon, "%15s %5d %5d %5d\n",
271 inet_ntoa(dst_addr), ntohs(dst_port),
272 so->so_rcv.sb_cc, so->so_snd.sb_cc);
275 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
276 if (so->so_state & SS_HOSTFWD) {
277 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
278 src_len = sizeof(src);
279 getsockname(so->s, (struct sockaddr *)&src, &src_len);
280 dst_addr = so->so_laddr;
281 dst_port = so->so_lport;
282 } else {
283 snprintf(buf, sizeof(buf), " UDP[%d sec]",
284 (so->so_expire - curtime) / 1000);
285 src.sin_addr = so->so_laddr;
286 src.sin_port = so->so_lport;
287 dst_addr = so->so_faddr;
288 dst_port = so->so_fport;
290 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
291 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
292 ntohs(src.sin_port));
293 monitor_printf(mon, "%15s %5d %5d %5d\n",
294 inet_ntoa(dst_addr), ntohs(dst_port),
295 so->so_rcv.sb_cc, so->so_snd.sb_cc);
298 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
299 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
300 (so->so_expire - curtime) / 1000);
301 src.sin_addr = so->so_laddr;
302 dst_addr = so->so_faddr;
303 monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
304 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
305 monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
306 so->so_rcv.sb_cc, so->so_snd.sb_cc);