blob: 0bee8643371659160490d49ea0f8b55c3f3b9c8d [file] [log] [blame]
bellardf0cbd3e2004-04-22 00:10:48 +00001/*
2 * Copyright (c) 1995 Danny Gasparovski.
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardf0cbd3e2004-04-22 00:10:48 +00004 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
7
bellardf0cbd3e2004-04-22 00:10:48 +00008#include <slirp.h>
Jan Kiszka6dbe5532009-06-24 14:42:29 +02009#include <libslirp.h>
10
11#include "monitor.h"
bellardf0cbd3e2004-04-22 00:10:48 +000012
Jan Kiszka9f349492009-06-24 14:42:29 +020013#ifdef DEBUG
14int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
15#endif
16
bellardf0cbd3e2004-04-22 00:10:48 +000017struct quehead {
18 struct quehead *qh_link;
19 struct quehead *qh_rlink;
20};
21
22inline void
blueswir1511d2b12009-03-07 15:32:56 +000023insque(void *a, void *b)
bellardf0cbd3e2004-04-22 00:10:48 +000024{
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;
32}
33
34inline void
blueswir1511d2b12009-03-07 15:32:56 +000035remque(void *a)
bellardf0cbd3e2004-04-22 00:10:48 +000036{
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;
bellardf0cbd3e2004-04-22 00:10:48 +000041}
42
Jan Kiszkaa13a4122009-06-24 14:42:28 +020043int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
44 struct in_addr addr, int port)
bellardf0cbd3e2004-04-22 00:10:48 +000045{
46 struct ex_list *tmp_ptr;
ths5fafdf22007-09-16 21:08:06 +000047
bellardf0cbd3e2004-04-22 00:10:48 +000048 /* First, check if the port is "bound" */
49 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +020050 if (port == tmp_ptr->ex_fport &&
51 addr.s_addr == tmp_ptr->ex_addr.s_addr)
52 return -1;
bellardf0cbd3e2004-04-22 00:10:48 +000053 }
ths5fafdf22007-09-16 21:08:06 +000054
bellardf0cbd3e2004-04-22 00:10:48 +000055 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;
aliguorie1c5a2b2009-01-08 19:18:21 +000060 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : strdup(exec);
bellardf0cbd3e2004-04-22 00:10:48 +000061 (*ex_ptr)->ex_next = tmp_ptr;
62 return 0;
63}
64
65#ifndef HAVE_STRERROR
66
67/*
68 * For systems with no strerror
69 */
70
71extern int sys_nerr;
72extern char *sys_errlist[];
73
74char *
75strerror(error)
76 int error;
77{
78 if (error < sys_nerr)
79 return sys_errlist[error];
80 else
81 return "Unknown error.";
82}
83
84#endif
85
86
bellarda3d4af02004-09-05 23:10:26 +000087#ifdef _WIN32
88
89int
blueswir19634d902007-10-26 19:01:16 +000090fork_exec(struct socket *so, const char *ex, int do_pty)
bellarda3d4af02004-09-05 23:10:26 +000091{
92 /* not implemented */
93 return 0;
94}
95
96#else
97
bellardf0cbd3e2004-04-22 00:10:48 +000098/*
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.
ths5fafdf22007-09-16 21:08:06 +0000104 *
bellardf0cbd3e2004-04-22 00:10:48 +0000105 * do_pty = 0 Fork/exec inetd style
106 * do_pty = 1 Fork/exec using slirp.telnetd
107 * do_ptr = 2 Fork/exec using pty
108 */
109int
blueswir19634d902007-10-26 19:01:16 +0000110fork_exec(struct socket *so, const char *ex, int do_pty)
bellardf0cbd3e2004-04-22 00:10:48 +0000111{
112 int s;
113 struct sockaddr_in addr;
balrog242acf32008-05-10 01:49:53 +0000114 socklen_t addrlen = sizeof(addr);
bellardf0cbd3e2004-04-22 00:10:48 +0000115 int opt;
blueswir17ccfb2e2008-09-14 06:45:34 +0000116 const char *argv[256];
bellardf0cbd3e2004-04-22 00:10:48 +0000117 /* don't want to clobber the original */
118 char *bptr;
blueswir19634d902007-10-26 19:01:16 +0000119 const char *curarg;
bellard7b91a172004-12-12 11:45:10 +0000120 int c, i, ret;
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100121 pid_t pid;
ths5fafdf22007-09-16 21:08:06 +0000122
bellardf0cbd3e2004-04-22 00:10:48 +0000123 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);
ths5fafdf22007-09-16 21:08:06 +0000127
bellardf0cbd3e2004-04-22 00:10:48 +0000128 if (do_pty == 2) {
balrog3f9b2b12007-11-13 01:56:12 +0000129 return 0;
bellardf0cbd3e2004-04-22 00:10:48 +0000130 } else {
131 addr.sin_family = AF_INET;
132 addr.sin_port = 0;
133 addr.sin_addr.s_addr = INADDR_ANY;
ths3b46e622007-09-17 08:09:54 +0000134
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100135 if ((s = qemu_socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
bellardf0cbd3e2004-04-22 00:10:48 +0000136 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
137 listen(s, 1) < 0) {
138 lprint("Error: inet socket: %s\n", strerror(errno));
bellard379ff532004-07-12 22:33:07 +0000139 closesocket(s);
ths3b46e622007-09-17 08:09:54 +0000140
bellardf0cbd3e2004-04-22 00:10:48 +0000141 return 0;
142 }
143 }
ths5fafdf22007-09-16 21:08:06 +0000144
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100145 pid = fork();
146 switch(pid) {
bellardf0cbd3e2004-04-22 00:10:48 +0000147 case -1:
148 lprint("Error: fork failed: %s\n", strerror(errno));
149 close(s);
bellardf0cbd3e2004-04-22 00:10:48 +0000150 return 0;
ths3b46e622007-09-17 08:09:54 +0000151
bellardf0cbd3e2004-04-22 00:10:48 +0000152 case 0:
Jan Kiszka565465f2011-07-20 12:20:17 +0200153 setsid();
154
bellardf0cbd3e2004-04-22 00:10:48 +0000155 /* Set the DISPLAY */
Stefan Weil9f1134d2012-01-05 14:18:45 +0100156 getsockname(s, (struct sockaddr *)&addr, &addrlen);
157 close(s);
158 /*
159 * Connect to the socket
160 * XXX If any of these fail, we're in trouble!
161 */
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);
ths3b46e622007-09-17 08:09:54 +0000167
bellardf0cbd3e2004-04-22 00:10:48 +0000168 dup2(s, 0);
169 dup2(s, 1);
170 dup2(s, 2);
blueswir19634d902007-10-26 19:01:16 +0000171 for (s = getdtablesize() - 1; s >= 3; s--)
bellardf0cbd3e2004-04-22 00:10:48 +0000172 close(s);
ths3b46e622007-09-17 08:09:54 +0000173
bellardf0cbd3e2004-04-22 00:10:48 +0000174 i = 0;
Anthony Liguori7267c092011-08-20 22:09:37 -0500175 bptr = g_strdup(ex); /* No need to free() this */
bellardf0cbd3e2004-04-22 00:10:48 +0000176 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);
ths3b46e622007-09-17 08:09:54 +0000191
blueswir1511d2b12009-03-07 15:32:56 +0000192 argv[i] = NULL;
blueswir17ccfb2e2008-09-14 06:45:34 +0000193 execvp(argv[0], (char **)argv);
ths3b46e622007-09-17 08:09:54 +0000194
bellardf0cbd3e2004-04-22 00:10:48 +0000195 /* Ooops, failed, let's tell the user why */
Kirill A. Shutemovf0d98b02009-12-25 18:19:18 +0000196 fprintf(stderr, "Error: execvp of %s failed: %s\n",
197 argv[0], strerror(errno));
bellardf0cbd3e2004-04-22 00:10:48 +0000198 close(0); close(1); close(2); /* XXX */
199 exit(1);
ths3b46e622007-09-17 08:09:54 +0000200
bellardf0cbd3e2004-04-22 00:10:48 +0000201 default:
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100202 qemu_add_child_watch(pid);
Stefan Weil9f1134d2012-01-05 14:18:45 +0100203 /*
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
209 */
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));
Paolo Bonzini1c5970a2012-03-22 01:02:52 +0100218 socket_set_nonblock(so->s);
ths3b46e622007-09-17 08:09:54 +0000219
bellardf0cbd3e2004-04-22 00:10:48 +0000220 /* Append the telnet options now */
blueswir1511d2b12009-03-07 15:32:56 +0000221 if (so->so_m != NULL && do_pty == 1) {
bellardf0cbd3e2004-04-22 00:10:48 +0000222 sbappend(so, so->so_m);
blueswir1511d2b12009-03-07 15:32:56 +0000223 so->so_m = NULL;
bellardf0cbd3e2004-04-22 00:10:48 +0000224 }
ths3b46e622007-09-17 08:09:54 +0000225
bellardf0cbd3e2004-04-22 00:10:48 +0000226 return 1;
227 }
228}
229#endif
230
231#ifndef HAVE_STRDUP
232char *
233strdup(str)
234 const char *str;
235{
236 char *bptr;
ths5fafdf22007-09-16 21:08:06 +0000237
bellardf0cbd3e2004-04-22 00:10:48 +0000238 bptr = (char *)malloc(strlen(str)+1);
239 strcpy(bptr, str);
ths5fafdf22007-09-16 21:08:06 +0000240
bellardf0cbd3e2004-04-22 00:10:48 +0000241 return bptr;
242}
243#endif
244
aliguori376253e2009-03-05 23:01:23 +0000245#include "monitor.h"
thsc9f10302007-10-30 23:19:52 +0000246
blueswir131a60e22007-10-26 18:42:59 +0000247void lprint(const char *format, ...)
248{
249 va_list args;
250
251 va_start(args, format);
Markus Armbruster8631b602010-02-18 11:41:55 +0100252 monitor_vprintf(default_mon, format, args);
blueswir131a60e22007-10-26 18:42:59 +0000253 va_end(args);
254}
bellardf0cbd3e2004-04-22 00:10:48 +0000255
bellardf0cbd3e2004-04-22 00:10:48 +0000256void
blueswir1511d2b12009-03-07 15:32:56 +0000257u_sleep(int usec)
bellardf0cbd3e2004-04-22 00:10:48 +0000258{
259 struct timeval t;
260 fd_set fdset;
ths5fafdf22007-09-16 21:08:06 +0000261
bellardf0cbd3e2004-04-22 00:10:48 +0000262 FD_ZERO(&fdset);
ths5fafdf22007-09-16 21:08:06 +0000263
bellardf0cbd3e2004-04-22 00:10:48 +0000264 t.tv_sec = 0;
265 t.tv_usec = usec * 1000;
ths5fafdf22007-09-16 21:08:06 +0000266
bellardf0cbd3e2004-04-22 00:10:48 +0000267 select(0, &fdset, &fdset, &fdset, &t);
268}
269
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200270void slirp_connection_info(Slirp *slirp, Monitor *mon)
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200271{
272 const char * const tcpstates[] = {
273 [TCPS_CLOSED] = "CLOSED",
274 [TCPS_LISTEN] = "LISTEN",
275 [TCPS_SYN_SENT] = "SYN_SENT",
276 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
277 [TCPS_ESTABLISHED] = "ESTABLISHED",
278 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
279 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
280 [TCPS_CLOSING] = "CLOSING",
281 [TCPS_LAST_ACK] = "LAST_ACK",
282 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
283 [TCPS_TIME_WAIT] = "TIME_WAIT",
284 };
285 struct in_addr dst_addr;
286 struct sockaddr_in src;
287 socklen_t src_len;
288 uint16_t dst_port;
289 struct socket *so;
290 const char *state;
291 char buf[20];
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200292
293 monitor_printf(mon, " Protocol[State] FD Source Address Port "
294 "Dest. Address Port RecvQ SendQ\n");
295
Jan Kiszka460fec62009-06-24 14:42:31 +0200296 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200297 if (so->so_state & SS_HOSTFWD) {
298 state = "HOST_FORWARD";
299 } else if (so->so_tcpcb) {
300 state = tcpstates[so->so_tcpcb->t_state];
301 } else {
302 state = "NONE";
303 }
304 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
305 src_len = sizeof(src);
306 getsockname(so->s, (struct sockaddr *)&src, &src_len);
307 dst_addr = so->so_laddr;
308 dst_port = so->so_lport;
309 } else {
310 src.sin_addr = so->so_laddr;
311 src.sin_port = so->so_lport;
312 dst_addr = so->so_faddr;
313 dst_port = so->so_fport;
314 }
Alon Levyf293d8b2012-02-24 13:33:49 +0200315 snprintf(buf, sizeof(buf), " TCP[%s]", state);
316 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200317 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
318 ntohs(src.sin_port));
319 monitor_printf(mon, "%15s %5d %5d %5d\n",
320 inet_ntoa(dst_addr), ntohs(dst_port),
321 so->so_rcv.sb_cc, so->so_snd.sb_cc);
322 }
323
Jan Kiszka460fec62009-06-24 14:42:31 +0200324 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200325 if (so->so_state & SS_HOSTFWD) {
Alon Levyf293d8b2012-02-24 13:33:49 +0200326 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200327 src_len = sizeof(src);
328 getsockname(so->s, (struct sockaddr *)&src, &src_len);
329 dst_addr = so->so_laddr;
330 dst_port = so->so_lport;
331 } else {
Alon Levyf293d8b2012-02-24 13:33:49 +0200332 snprintf(buf, sizeof(buf), " UDP[%d sec]",
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200333 (so->so_expire - curtime) / 1000);
334 src.sin_addr = so->so_laddr;
335 src.sin_port = so->so_lport;
336 dst_addr = so->so_faddr;
337 dst_port = so->so_fport;
338 }
Alon Levyf293d8b2012-02-24 13:33:49 +0200339 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200340 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
341 ntohs(src.sin_port));
342 monitor_printf(mon, "%15s %5d %5d %5d\n",
343 inet_ntoa(dst_addr), ntohs(dst_port),
344 so->so_rcv.sb_cc, so->so_snd.sb_cc);
345 }
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200346
347 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
Alon Levyf293d8b2012-02-24 13:33:49 +0200348 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200349 (so->so_expire - curtime) / 1000);
350 src.sin_addr = so->so_laddr;
351 dst_addr = so->so_faddr;
Alon Levyf293d8b2012-02-24 13:33:49 +0200352 monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200353 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
354 monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
355 so->so_rcv.sb_cc, so->so_snd.sb_cc);
356 }
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200357}