]> Git Repo - qemu.git/blame - slirp/misc.c
s390x/vfio: ap: Introduce VFIO AP device
[qemu.git] / slirp / misc.c
CommitLineData
f0cbd3ec
FB
1/*
2 * Copyright (c) 1995 Danny Gasparovski.
5fafdf24 3 *
f0cbd3ec
FB
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
7
7df7482b 8#include "qemu/osdep.h"
a9c94277
MA
9#include "slirp.h"
10#include "libslirp.h"
83c9089e 11#include "monitor/monitor.h"
d49b6836 12#include "qemu/error-report.h"
6a1751b7 13#include "qemu/main-loop.h"
f0cbd3ec 14
9f349498
JK
15#ifdef DEBUG
16int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
17#endif
18
f0cbd3ec 19inline void
511d2b14 20insque(void *a, void *b)
f0cbd3ec
FB
21{
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;
29}
30
31inline void
511d2b14 32remque(void *a)
f0cbd3ec
FB
33{
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;
f0cbd3ec
FB
38}
39
a13a4126
JK
40int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
41 struct in_addr addr, int port)
f0cbd3ec
FB
42{
43 struct ex_list *tmp_ptr;
5fafdf24 44
f0cbd3ec
FB
45 /* First, check if the port is "bound" */
46 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
a13a4126
JK
47 if (port == tmp_ptr->ex_fport &&
48 addr.s_addr == tmp_ptr->ex_addr.s_addr)
49 return -1;
f0cbd3ec 50 }
5fafdf24 51
f0cbd3ec 52 tmp_ptr = *ex_ptr;
2fd5d864 53 *ex_ptr = g_new(struct ex_list, 1);
f0cbd3ec
FB
54 (*ex_ptr)->ex_fport = port;
55 (*ex_ptr)->ex_addr = addr;
56 (*ex_ptr)->ex_pty = do_pty;
2fd5d864 57 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : g_strdup(exec);
f0cbd3ec
FB
58 (*ex_ptr)->ex_next = tmp_ptr;
59 return 0;
60}
61
f0cbd3ec 62
a3d4af03
FB
63#ifdef _WIN32
64
65int
9634d903 66fork_exec(struct socket *so, const char *ex, int do_pty)
a3d4af03
FB
67{
68 /* not implemented */
69 return 0;
70}
71
72#else
73
f0cbd3ec
FB
74/*
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.
5fafdf24 80 *
f0cbd3ec
FB
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
84 */
85int
9634d903 86fork_exec(struct socket *so, const char *ex, int do_pty)
f0cbd3ec
FB
87{
88 int s;
89 struct sockaddr_in addr;
242acf3a 90 socklen_t addrlen = sizeof(addr);
f0cbd3ec 91 int opt;
7ccfb2eb 92 const char *argv[256];
f0cbd3ec
FB
93 /* don't want to clobber the original */
94 char *bptr;
9634d903 95 const char *curarg;
7b91a172 96 int c, i, ret;
4d54ec78 97 pid_t pid;
5fafdf24 98
f0cbd3ec 99 DEBUG_CALL("fork_exec");
ecc804ca
SW
100 DEBUG_ARG("so = %p", so);
101 DEBUG_ARG("ex = %p", ex);
102 DEBUG_ARG("do_pty = %x", do_pty);
5fafdf24 103
f0cbd3ec 104 if (do_pty == 2) {
3f9b2b1f 105 return 0;
f0cbd3ec
FB
106 } else {
107 addr.sin_family = AF_INET;
108 addr.sin_port = 0;
109 addr.sin_addr.s_addr = INADDR_ANY;
3b46e624 110
40ff6d7e 111 if ((s = qemu_socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
f0cbd3ec
FB
112 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
113 listen(s, 1) < 0) {
02d16089 114 error_report("Error: inet socket: %s", strerror(errno));
12dccfe4
PM
115 if (s >= 0) {
116 closesocket(s);
117 }
3b46e624 118
f0cbd3ec
FB
119 return 0;
120 }
121 }
5fafdf24 122
4d54ec78
PB
123 pid = fork();
124 switch(pid) {
f0cbd3ec 125 case -1:
02d16089 126 error_report("Error: fork failed: %s", strerror(errno));
f0cbd3ec 127 close(s);
f0cbd3ec 128 return 0;
3b46e624 129
f0cbd3ec 130 case 0:
565465fc
JK
131 setsid();
132
f0cbd3ec 133 /* Set the DISPLAY */
9f1134d4
SW
134 getsockname(s, (struct sockaddr *)&addr, &addrlen);
135 close(s);
136 /*
137 * Connect to the socket
138 * XXX If any of these fail, we're in trouble!
139 */
140 s = qemu_socket(AF_INET, SOCK_STREAM, 0);
141 addr.sin_addr = loopback_addr;
142 do {
143 ret = connect(s, (struct sockaddr *)&addr, addrlen);
144 } while (ret < 0 && errno == EINTR);
3b46e624 145
f0cbd3ec
FB
146 dup2(s, 0);
147 dup2(s, 1);
148 dup2(s, 2);
9634d903 149 for (s = getdtablesize() - 1; s >= 3; s--)
f0cbd3ec 150 close(s);
3b46e624 151
f0cbd3ec 152 i = 0;
7267c094 153 bptr = g_strdup(ex); /* No need to free() this */
f0cbd3ec
FB
154 if (do_pty == 1) {
155 /* Setup "slirp.telnetd -x" */
156 argv[i++] = "slirp.telnetd";
157 argv[i++] = "-x";
158 argv[i++] = bptr;
159 } else
160 do {
161 /* Change the string into argv[] */
162 curarg = bptr;
163 while (*bptr != ' ' && *bptr != (char)0)
164 bptr++;
165 c = *bptr;
166 *bptr++ = (char)0;
2fd5d864 167 argv[i++] = g_strdup(curarg);
f0cbd3ec 168 } while (c);
3b46e624 169
511d2b14 170 argv[i] = NULL;
7ccfb2eb 171 execvp(argv[0], (char **)argv);
3b46e624 172
f0cbd3ec 173 /* Ooops, failed, let's tell the user why */
f0d98b05
KS
174 fprintf(stderr, "Error: execvp of %s failed: %s\n",
175 argv[0], strerror(errno));
f0cbd3ec
FB
176 close(0); close(1); close(2); /* XXX */
177 exit(1);
3b46e624 178
f0cbd3ec 179 default:
4d54ec78 180 qemu_add_child_watch(pid);
9f1134d4
SW
181 /*
182 * XXX this could block us...
183 * XXX Should set a timer here, and if accept() doesn't
184 * return after X seconds, declare it a failure
185 * The only reason this will block forever is if socket()
186 * of connect() fail in the child process
187 */
188 do {
189 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
190 } while (so->s < 0 && errno == EINTR);
191 closesocket(s);
aad1239a 192 socket_set_fast_reuse(so->s);
9f1134d4 193 opt = 1;
9957fc7f 194 qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
f9e8cacc 195 qemu_set_nonblock(so->s);
3b46e624 196
f0cbd3ec 197 /* Append the telnet options now */
511d2b14 198 if (so->so_m != NULL && do_pty == 1) {
f0cbd3ec 199 sbappend(so, so->so_m);
511d2b14 200 so->so_m = NULL;
f0cbd3ec 201 }
3b46e624 202
f0cbd3ec
FB
203 return 1;
204 }
205}
206#endif
207
9f8bd042 208void slirp_connection_info(Slirp *slirp, Monitor *mon)
6dbe553f
JK
209{
210 const char * const tcpstates[] = {
211 [TCPS_CLOSED] = "CLOSED",
212 [TCPS_LISTEN] = "LISTEN",
213 [TCPS_SYN_SENT] = "SYN_SENT",
214 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
215 [TCPS_ESTABLISHED] = "ESTABLISHED",
216 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
217 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
218 [TCPS_CLOSING] = "CLOSING",
219 [TCPS_LAST_ACK] = "LAST_ACK",
220 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
221 [TCPS_TIME_WAIT] = "TIME_WAIT",
222 };
223 struct in_addr dst_addr;
224 struct sockaddr_in src;
225 socklen_t src_len;
226 uint16_t dst_port;
227 struct socket *so;
228 const char *state;
229 char buf[20];
6dbe553f
JK
230
231 monitor_printf(mon, " Protocol[State] FD Source Address Port "
232 "Dest. Address Port RecvQ SendQ\n");
233
460fec67 234 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
6dbe553f
JK
235 if (so->so_state & SS_HOSTFWD) {
236 state = "HOST_FORWARD";
237 } else if (so->so_tcpcb) {
238 state = tcpstates[so->so_tcpcb->t_state];
239 } else {
240 state = "NONE";
241 }
242 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
243 src_len = sizeof(src);
244 getsockname(so->s, (struct sockaddr *)&src, &src_len);
245 dst_addr = so->so_laddr;
246 dst_port = so->so_lport;
247 } else {
248 src.sin_addr = so->so_laddr;
249 src.sin_port = so->so_lport;
250 dst_addr = so->so_faddr;
251 dst_port = so->so_fport;
252 }
f293d8b1
AL
253 snprintf(buf, sizeof(buf), " TCP[%s]", state);
254 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
6dbe553f
JK
255 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
256 ntohs(src.sin_port));
257 monitor_printf(mon, "%15s %5d %5d %5d\n",
258 inet_ntoa(dst_addr), ntohs(dst_port),
259 so->so_rcv.sb_cc, so->so_snd.sb_cc);
260 }
261
460fec67 262 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
6dbe553f 263 if (so->so_state & SS_HOSTFWD) {
f293d8b1 264 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
6dbe553f
JK
265 src_len = sizeof(src);
266 getsockname(so->s, (struct sockaddr *)&src, &src_len);
267 dst_addr = so->so_laddr;
268 dst_port = so->so_lport;
269 } else {
f293d8b1 270 snprintf(buf, sizeof(buf), " UDP[%d sec]",
6dbe553f
JK
271 (so->so_expire - curtime) / 1000);
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;
276 }
f293d8b1 277 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
6dbe553f
JK
278 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
279 ntohs(src.sin_port));
280 monitor_printf(mon, "%15s %5d %5d %5d\n",
281 inet_ntoa(dst_addr), ntohs(dst_port),
282 so->so_rcv.sb_cc, so->so_snd.sb_cc);
283 }
e6d43cfb
JK
284
285 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
f293d8b1 286 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
e6d43cfb
JK
287 (so->so_expire - curtime) / 1000);
288 src.sin_addr = so->so_laddr;
289 dst_addr = so->so_faddr;
f293d8b1 290 monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
e6d43cfb
JK
291 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
292 monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
293 so->so_rcv.sb_cc, so->so_snd.sb_cc);
294 }
6dbe553f 295}
This page took 0.915786 seconds and 4 git commands to generate.