]> git.proxmox.com Git - mirror_qemu.git/blob - slirp/misc.c
slirp: remove Monitor dependency, return a string for info
[mirror_qemu.git] / slirp / misc.c
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
3 *
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
7
8 #include "qemu/osdep.h"
9 #include "slirp.h"
10 #include "libslirp.h"
11 #include "qemu/error-report.h"
12 #include "qemu/main-loop.h"
13
14 #ifdef DEBUG
15 int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
16 #endif
17
18 inline void
19 insque(void *a, void *b)
20 {
21 register struct quehead *element = (struct quehead *) a;
22 register struct quehead *head = (struct quehead *) b;
23 element->qh_link = head->qh_link;
24 head->qh_link = (struct quehead *)element;
25 element->qh_rlink = (struct quehead *)head;
26 ((struct quehead *)(element->qh_link))->qh_rlink
27 = (struct quehead *)element;
28 }
29
30 inline void
31 remque(void *a)
32 {
33 register struct quehead *element = (struct quehead *) a;
34 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
35 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
36 element->qh_rlink = NULL;
37 }
38
39 int add_exec(struct ex_list **ex_ptr, void *chardev, const char *cmdline,
40 struct in_addr addr, int port)
41 {
42 struct ex_list *tmp_ptr;
43
44 /* First, check if the port is "bound" */
45 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
46 if (port == tmp_ptr->ex_fport &&
47 addr.s_addr == tmp_ptr->ex_addr.s_addr)
48 return -1;
49 }
50
51 tmp_ptr = *ex_ptr;
52 *ex_ptr = g_new0(struct ex_list, 1);
53 (*ex_ptr)->ex_fport = port;
54 (*ex_ptr)->ex_addr = addr;
55 if (chardev) {
56 (*ex_ptr)->ex_chardev = chardev;
57 } else {
58 (*ex_ptr)->ex_exec = g_strdup(cmdline);
59 }
60 (*ex_ptr)->ex_next = tmp_ptr;
61 return 0;
62 }
63
64
65 #ifdef _WIN32
66
67 int
68 fork_exec(struct socket *so, const char *ex)
69 {
70 /* not implemented */
71 return 0;
72 }
73
74 #else
75
76 /*
77 * XXX This is ugly
78 * We create and bind a socket, then fork off to another
79 * process, which connects to this socket, after which we
80 * exec the wanted program. If something (strange) happens,
81 * the accept() call could block us forever.
82 */
83 int
84 fork_exec(struct socket *so, const char *ex)
85 {
86 int s, cs;
87 struct sockaddr_in addr, csaddr;
88 socklen_t addrlen = sizeof(addr);
89 socklen_t csaddrlen = sizeof(csaddr);
90 int opt;
91 const char *argv[256];
92 /* don't want to clobber the original */
93 char *bptr;
94 const char *curarg;
95 int c, i, ret;
96 pid_t pid;
97
98 DEBUG_CALL("fork_exec");
99 DEBUG_ARG("so = %p", so);
100 DEBUG_ARG("ex = %p", ex);
101
102 addr.sin_family = AF_INET;
103 addr.sin_port = 0;
104 addr.sin_addr.s_addr = INADDR_ANY;
105
106 s = qemu_socket(AF_INET, SOCK_STREAM, 0);
107 if (s < 0 || bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
108 listen(s, 1) < 0) {
109 error_report("Error: inet socket: %s", strerror(errno));
110 if (s >= 0) {
111 closesocket(s);
112 }
113 return 0;
114 }
115
116 if (getsockname(s, (struct sockaddr *)&csaddr, &csaddrlen) < 0) {
117 closesocket(s);
118 return 0;
119 }
120 cs = qemu_socket(AF_INET, SOCK_STREAM, 0);
121 if (cs < 0) {
122 closesocket(s);
123 return 0;
124 }
125 csaddr.sin_addr = loopback_addr;
126 /*
127 * This connect won't block because we've already listen()ed on
128 * the server end (even though we won't accept() the connection
129 * until later on).
130 */
131 do {
132 ret = connect(cs, (struct sockaddr *)&csaddr, csaddrlen);
133 } while (ret < 0 && errno == EINTR);
134 if (ret < 0) {
135 closesocket(s);
136 closesocket(cs);
137 return 0;
138 }
139
140 pid = fork();
141 switch(pid) {
142 case -1:
143 error_report("Error: fork failed: %s", strerror(errno));
144 closesocket(cs);
145 close(s);
146 return 0;
147
148 case 0:
149 setsid();
150
151 /* Set the DISPLAY */
152 close(s);
153 dup2(cs, 0);
154 dup2(cs, 1);
155 dup2(cs, 2);
156 for (s = getdtablesize() - 1; s >= 3; s--)
157 close(s);
158
159 i = 0;
160 bptr = g_strdup(ex); /* No need to free() this */
161 do {
162 /* Change the string into argv[] */
163 curarg = bptr;
164 while (*bptr != ' ' && *bptr != (char)0)
165 bptr++;
166 c = *bptr;
167 *bptr++ = (char)0;
168 argv[i++] = g_strdup(curarg);
169 } while (c);
170
171 argv[i] = NULL;
172 execvp(argv[0], (char **)argv);
173
174 /* Ooops, failed, let's tell the user why */
175 fprintf(stderr, "Error: execvp of %s failed: %s\n",
176 argv[0], strerror(errno));
177 close(0); close(1); close(2); /* XXX */
178 exit(1);
179
180 default:
181 qemu_add_child_watch(pid);
182 closesocket(cs);
183 /*
184 * This should never block, because we already connect()ed
185 * on the child end before we forked.
186 */
187 do {
188 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
189 } while (so->s < 0 && errno == EINTR);
190 closesocket(s);
191 socket_set_fast_reuse(so->s);
192 opt = 1;
193 qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
194 qemu_set_nonblock(so->s);
195 return 1;
196 }
197 }
198 #endif
199
200 char *slirp_connection_info(Slirp *slirp)
201 {
202 GString *str = g_string_new(NULL);
203 const char * const tcpstates[] = {
204 [TCPS_CLOSED] = "CLOSED",
205 [TCPS_LISTEN] = "LISTEN",
206 [TCPS_SYN_SENT] = "SYN_SENT",
207 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
208 [TCPS_ESTABLISHED] = "ESTABLISHED",
209 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
210 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
211 [TCPS_CLOSING] = "CLOSING",
212 [TCPS_LAST_ACK] = "LAST_ACK",
213 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
214 [TCPS_TIME_WAIT] = "TIME_WAIT",
215 };
216 struct in_addr dst_addr;
217 struct sockaddr_in src;
218 socklen_t src_len;
219 uint16_t dst_port;
220 struct socket *so;
221 const char *state;
222 char buf[20];
223
224 g_string_append_printf(str,
225 " Protocol[State] FD Source Address Port "
226 "Dest. Address Port RecvQ SendQ\n");
227
228 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
229 if (so->so_state & SS_HOSTFWD) {
230 state = "HOST_FORWARD";
231 } else if (so->so_tcpcb) {
232 state = tcpstates[so->so_tcpcb->t_state];
233 } else {
234 state = "NONE";
235 }
236 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
237 src_len = sizeof(src);
238 getsockname(so->s, (struct sockaddr *)&src, &src_len);
239 dst_addr = so->so_laddr;
240 dst_port = so->so_lport;
241 } else {
242 src.sin_addr = so->so_laddr;
243 src.sin_port = so->so_lport;
244 dst_addr = so->so_faddr;
245 dst_port = so->so_fport;
246 }
247 snprintf(buf, sizeof(buf), " TCP[%s]", state);
248 g_string_append_printf(str, "%-19s %3d %15s %5d ", buf, so->s,
249 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
250 ntohs(src.sin_port));
251 g_string_append_printf(str, "%15s %5d %5d %5d\n",
252 inet_ntoa(dst_addr), ntohs(dst_port),
253 so->so_rcv.sb_cc, so->so_snd.sb_cc);
254 }
255
256 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
257 if (so->so_state & SS_HOSTFWD) {
258 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
259 src_len = sizeof(src);
260 getsockname(so->s, (struct sockaddr *)&src, &src_len);
261 dst_addr = so->so_laddr;
262 dst_port = so->so_lport;
263 } else {
264 snprintf(buf, sizeof(buf), " UDP[%d sec]",
265 (so->so_expire - curtime) / 1000);
266 src.sin_addr = so->so_laddr;
267 src.sin_port = so->so_lport;
268 dst_addr = so->so_faddr;
269 dst_port = so->so_fport;
270 }
271 g_string_append_printf(str, "%-19s %3d %15s %5d ", buf, so->s,
272 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
273 ntohs(src.sin_port));
274 g_string_append_printf(str, "%15s %5d %5d %5d\n",
275 inet_ntoa(dst_addr), ntohs(dst_port),
276 so->so_rcv.sb_cc, so->so_snd.sb_cc);
277 }
278
279 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
280 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
281 (so->so_expire - curtime) / 1000);
282 src.sin_addr = so->so_laddr;
283 dst_addr = so->so_faddr;
284 g_string_append_printf(str, "%-19s %3d %15s - ", buf, so->s,
285 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
286 g_string_append_printf(str, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
287 so->so_rcv.sb_cc, so->so_snd.sb_cc);
288 }
289
290 return g_string_free(str, FALSE);
291 }