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