blob: 6543dc777298463931ae82fbb94619c8b0ee5902 [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
Paolo Bonzini83c90892012-12-17 18:19:49 +010011#include "monitor/monitor.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010012#include "qemu/main-loop.h"
bellardf0cbd3e2004-04-22 00:10:48 +000013
Jan Kiszka9f349492009-06-24 14:42:29 +020014#ifdef DEBUG
15int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
16#endif
17
bellardf0cbd3e2004-04-22 00:10:48 +000018struct quehead {
19 struct quehead *qh_link;
20 struct quehead *qh_rlink;
21};
22
23inline void
blueswir1511d2b12009-03-07 15:32:56 +000024insque(void *a, void *b)
bellardf0cbd3e2004-04-22 00:10:48 +000025{
26 register struct quehead *element = (struct quehead *) a;
27 register struct quehead *head = (struct quehead *) b;
28 element->qh_link = head->qh_link;
29 head->qh_link = (struct quehead *)element;
30 element->qh_rlink = (struct quehead *)head;
31 ((struct quehead *)(element->qh_link))->qh_rlink
32 = (struct quehead *)element;
33}
34
35inline void
blueswir1511d2b12009-03-07 15:32:56 +000036remque(void *a)
bellardf0cbd3e2004-04-22 00:10:48 +000037{
38 register struct quehead *element = (struct quehead *) a;
39 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
40 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
41 element->qh_rlink = NULL;
bellardf0cbd3e2004-04-22 00:10:48 +000042}
43
Jan Kiszkaa13a4122009-06-24 14:42:28 +020044int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
45 struct in_addr addr, int port)
bellardf0cbd3e2004-04-22 00:10:48 +000046{
47 struct ex_list *tmp_ptr;
ths5fafdf22007-09-16 21:08:06 +000048
bellardf0cbd3e2004-04-22 00:10:48 +000049 /* First, check if the port is "bound" */
50 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +020051 if (port == tmp_ptr->ex_fport &&
52 addr.s_addr == tmp_ptr->ex_addr.s_addr)
53 return -1;
bellardf0cbd3e2004-04-22 00:10:48 +000054 }
ths5fafdf22007-09-16 21:08:06 +000055
bellardf0cbd3e2004-04-22 00:10:48 +000056 tmp_ptr = *ex_ptr;
zhanghailiang2fd5d862014-08-19 16:30:17 +080057 *ex_ptr = g_new(struct ex_list, 1);
bellardf0cbd3e2004-04-22 00:10:48 +000058 (*ex_ptr)->ex_fport = port;
59 (*ex_ptr)->ex_addr = addr;
60 (*ex_ptr)->ex_pty = do_pty;
zhanghailiang2fd5d862014-08-19 16:30:17 +080061 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : g_strdup(exec);
bellardf0cbd3e2004-04-22 00:10:48 +000062 (*ex_ptr)->ex_next = tmp_ptr;
63 return 0;
64}
65
66#ifndef HAVE_STRERROR
67
68/*
69 * For systems with no strerror
70 */
71
72extern int sys_nerr;
73extern char *sys_errlist[];
74
75char *
76strerror(error)
77 int error;
78{
79 if (error < sys_nerr)
80 return sys_errlist[error];
81 else
82 return "Unknown error.";
83}
84
85#endif
86
87
bellarda3d4af02004-09-05 23:10:26 +000088#ifdef _WIN32
89
90int
blueswir19634d902007-10-26 19:01:16 +000091fork_exec(struct socket *so, const char *ex, int do_pty)
bellarda3d4af02004-09-05 23:10:26 +000092{
93 /* not implemented */
94 return 0;
95}
96
97#else
98
bellardf0cbd3e2004-04-22 00:10:48 +000099/*
100 * XXX This is ugly
101 * We create and bind a socket, then fork off to another
102 * process, which connects to this socket, after which we
103 * exec the wanted program. If something (strange) happens,
104 * the accept() call could block us forever.
ths5fafdf22007-09-16 21:08:06 +0000105 *
bellardf0cbd3e2004-04-22 00:10:48 +0000106 * do_pty = 0 Fork/exec inetd style
107 * do_pty = 1 Fork/exec using slirp.telnetd
108 * do_ptr = 2 Fork/exec using pty
109 */
110int
blueswir19634d902007-10-26 19:01:16 +0000111fork_exec(struct socket *so, const char *ex, int do_pty)
bellardf0cbd3e2004-04-22 00:10:48 +0000112{
113 int s;
114 struct sockaddr_in addr;
balrog242acf32008-05-10 01:49:53 +0000115 socklen_t addrlen = sizeof(addr);
bellardf0cbd3e2004-04-22 00:10:48 +0000116 int opt;
blueswir17ccfb2e2008-09-14 06:45:34 +0000117 const char *argv[256];
bellardf0cbd3e2004-04-22 00:10:48 +0000118 /* don't want to clobber the original */
119 char *bptr;
blueswir19634d902007-10-26 19:01:16 +0000120 const char *curarg;
bellard7b91a172004-12-12 11:45:10 +0000121 int c, i, ret;
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100122 pid_t pid;
ths5fafdf22007-09-16 21:08:06 +0000123
bellardf0cbd3e2004-04-22 00:10:48 +0000124 DEBUG_CALL("fork_exec");
125 DEBUG_ARG("so = %lx", (long)so);
126 DEBUG_ARG("ex = %lx", (long)ex);
127 DEBUG_ARG("do_pty = %lx", (long)do_pty);
ths5fafdf22007-09-16 21:08:06 +0000128
bellardf0cbd3e2004-04-22 00:10:48 +0000129 if (do_pty == 2) {
balrog3f9b2b12007-11-13 01:56:12 +0000130 return 0;
bellardf0cbd3e2004-04-22 00:10:48 +0000131 } else {
132 addr.sin_family = AF_INET;
133 addr.sin_port = 0;
134 addr.sin_addr.s_addr = INADDR_ANY;
ths3b46e622007-09-17 08:09:54 +0000135
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100136 if ((s = qemu_socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
bellardf0cbd3e2004-04-22 00:10:48 +0000137 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
138 listen(s, 1) < 0) {
Cole Robinson02d16082014-03-21 19:42:20 -0400139 error_report("Error: inet socket: %s", strerror(errno));
bellard379ff532004-07-12 22:33:07 +0000140 closesocket(s);
ths3b46e622007-09-17 08:09:54 +0000141
bellardf0cbd3e2004-04-22 00:10:48 +0000142 return 0;
143 }
144 }
ths5fafdf22007-09-16 21:08:06 +0000145
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100146 pid = fork();
147 switch(pid) {
bellardf0cbd3e2004-04-22 00:10:48 +0000148 case -1:
Cole Robinson02d16082014-03-21 19:42:20 -0400149 error_report("Error: fork failed: %s", strerror(errno));
bellardf0cbd3e2004-04-22 00:10:48 +0000150 close(s);
bellardf0cbd3e2004-04-22 00:10:48 +0000151 return 0;
ths3b46e622007-09-17 08:09:54 +0000152
bellardf0cbd3e2004-04-22 00:10:48 +0000153 case 0:
Jan Kiszka565465f2011-07-20 12:20:17 +0200154 setsid();
155
bellardf0cbd3e2004-04-22 00:10:48 +0000156 /* Set the DISPLAY */
Stefan Weil9f1134d2012-01-05 14:18:45 +0100157 getsockname(s, (struct sockaddr *)&addr, &addrlen);
158 close(s);
159 /*
160 * Connect to the socket
161 * XXX If any of these fail, we're in trouble!
162 */
163 s = qemu_socket(AF_INET, SOCK_STREAM, 0);
164 addr.sin_addr = loopback_addr;
165 do {
166 ret = connect(s, (struct sockaddr *)&addr, addrlen);
167 } while (ret < 0 && errno == EINTR);
ths3b46e622007-09-17 08:09:54 +0000168
bellardf0cbd3e2004-04-22 00:10:48 +0000169 dup2(s, 0);
170 dup2(s, 1);
171 dup2(s, 2);
blueswir19634d902007-10-26 19:01:16 +0000172 for (s = getdtablesize() - 1; s >= 3; s--)
bellardf0cbd3e2004-04-22 00:10:48 +0000173 close(s);
ths3b46e622007-09-17 08:09:54 +0000174
bellardf0cbd3e2004-04-22 00:10:48 +0000175 i = 0;
Anthony Liguori7267c092011-08-20 22:09:37 -0500176 bptr = g_strdup(ex); /* No need to free() this */
bellardf0cbd3e2004-04-22 00:10:48 +0000177 if (do_pty == 1) {
178 /* Setup "slirp.telnetd -x" */
179 argv[i++] = "slirp.telnetd";
180 argv[i++] = "-x";
181 argv[i++] = bptr;
182 } else
183 do {
184 /* Change the string into argv[] */
185 curarg = bptr;
186 while (*bptr != ' ' && *bptr != (char)0)
187 bptr++;
188 c = *bptr;
189 *bptr++ = (char)0;
zhanghailiang2fd5d862014-08-19 16:30:17 +0800190 argv[i++] = g_strdup(curarg);
bellardf0cbd3e2004-04-22 00:10:48 +0000191 } while (c);
ths3b46e622007-09-17 08:09:54 +0000192
blueswir1511d2b12009-03-07 15:32:56 +0000193 argv[i] = NULL;
blueswir17ccfb2e2008-09-14 06:45:34 +0000194 execvp(argv[0], (char **)argv);
ths3b46e622007-09-17 08:09:54 +0000195
bellardf0cbd3e2004-04-22 00:10:48 +0000196 /* Ooops, failed, let's tell the user why */
Kirill A. Shutemovf0d98b02009-12-25 18:19:18 +0000197 fprintf(stderr, "Error: execvp of %s failed: %s\n",
198 argv[0], strerror(errno));
bellardf0cbd3e2004-04-22 00:10:48 +0000199 close(0); close(1); close(2); /* XXX */
200 exit(1);
ths3b46e622007-09-17 08:09:54 +0000201
bellardf0cbd3e2004-04-22 00:10:48 +0000202 default:
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100203 qemu_add_child_watch(pid);
Stefan Weil9f1134d2012-01-05 14:18:45 +0100204 /*
205 * XXX this could block us...
206 * XXX Should set a timer here, and if accept() doesn't
207 * return after X seconds, declare it a failure
208 * The only reason this will block forever is if socket()
209 * of connect() fail in the child process
210 */
211 do {
212 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
213 } while (so->s < 0 && errno == EINTR);
214 closesocket(s);
Sebastian Ottlikaad12392013-10-02 12:23:15 +0200215 socket_set_fast_reuse(so->s);
Stefan Weil9f1134d2012-01-05 14:18:45 +0100216 opt = 1;
Stefan Weil9957fc72013-03-08 19:58:32 +0100217 qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100218 qemu_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
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200231void slirp_connection_info(Slirp *slirp, Monitor *mon)
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200232{
233 const char * const tcpstates[] = {
234 [TCPS_CLOSED] = "CLOSED",
235 [TCPS_LISTEN] = "LISTEN",
236 [TCPS_SYN_SENT] = "SYN_SENT",
237 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
238 [TCPS_ESTABLISHED] = "ESTABLISHED",
239 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
240 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
241 [TCPS_CLOSING] = "CLOSING",
242 [TCPS_LAST_ACK] = "LAST_ACK",
243 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
244 [TCPS_TIME_WAIT] = "TIME_WAIT",
245 };
246 struct in_addr dst_addr;
247 struct sockaddr_in src;
248 socklen_t src_len;
249 uint16_t dst_port;
250 struct socket *so;
251 const char *state;
252 char buf[20];
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200253
254 monitor_printf(mon, " Protocol[State] FD Source Address Port "
255 "Dest. Address Port RecvQ SendQ\n");
256
Jan Kiszka460fec62009-06-24 14:42:31 +0200257 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200258 if (so->so_state & SS_HOSTFWD) {
259 state = "HOST_FORWARD";
260 } else if (so->so_tcpcb) {
261 state = tcpstates[so->so_tcpcb->t_state];
262 } else {
263 state = "NONE";
264 }
265 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
266 src_len = sizeof(src);
267 getsockname(so->s, (struct sockaddr *)&src, &src_len);
268 dst_addr = so->so_laddr;
269 dst_port = so->so_lport;
270 } else {
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 }
Alon Levyf293d8b2012-02-24 13:33:49 +0200276 snprintf(buf, sizeof(buf), " TCP[%s]", state);
277 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200278 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 }
284
Jan Kiszka460fec62009-06-24 14:42:31 +0200285 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200286 if (so->so_state & SS_HOSTFWD) {
Alon Levyf293d8b2012-02-24 13:33:49 +0200287 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200288 src_len = sizeof(src);
289 getsockname(so->s, (struct sockaddr *)&src, &src_len);
290 dst_addr = so->so_laddr;
291 dst_port = so->so_lport;
292 } else {
Alon Levyf293d8b2012-02-24 13:33:49 +0200293 snprintf(buf, sizeof(buf), " UDP[%d sec]",
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200294 (so->so_expire - curtime) / 1000);
295 src.sin_addr = so->so_laddr;
296 src.sin_port = so->so_lport;
297 dst_addr = so->so_faddr;
298 dst_port = so->so_fport;
299 }
Alon Levyf293d8b2012-02-24 13:33:49 +0200300 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200301 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
302 ntohs(src.sin_port));
303 monitor_printf(mon, "%15s %5d %5d %5d\n",
304 inet_ntoa(dst_addr), ntohs(dst_port),
305 so->so_rcv.sb_cc, so->so_snd.sb_cc);
306 }
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200307
308 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
Alon Levyf293d8b2012-02-24 13:33:49 +0200309 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200310 (so->so_expire - curtime) / 1000);
311 src.sin_addr = so->so_laddr;
312 dst_addr = so->so_faddr;
Alon Levyf293d8b2012-02-24 13:33:49 +0200313 monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200314 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
315 monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
316 so->so_rcv.sb_cc, so->so_snd.sb_cc);
317 }
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200318}