2004-04-22 02:10:48 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1995 Danny Gasparovski.
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-04-22 02:10:48 +02:00
|
|
|
* Please read the file COPYRIGHT for the
|
|
|
|
* terms and conditions of the copyright.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 18:49:59 +01:00
|
|
|
#include "qemu/osdep.h"
|
2016-06-22 19:11:19 +02:00
|
|
|
#include "slirp.h"
|
|
|
|
#include "libslirp.h"
|
2012-12-17 18:19:49 +01:00
|
|
|
#include "monitor/monitor.h"
|
2015-03-17 18:29:20 +01:00
|
|
|
#include "qemu/error-report.h"
|
2013-08-21 17:02:47 +02:00
|
|
|
#include "qemu/main-loop.h"
|
2004-04-22 02:10:48 +02:00
|
|
|
|
2009-06-24 14:42:29 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
|
|
|
|
#endif
|
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
inline void
|
2009-03-07 16:32:56 +01:00
|
|
|
insque(void *a, void *b)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
|
|
|
register struct quehead *element = (struct quehead *) a;
|
|
|
|
register struct quehead *head = (struct quehead *) b;
|
|
|
|
element->qh_link = head->qh_link;
|
|
|
|
head->qh_link = (struct quehead *)element;
|
|
|
|
element->qh_rlink = (struct quehead *)head;
|
|
|
|
((struct quehead *)(element->qh_link))->qh_rlink
|
|
|
|
= (struct quehead *)element;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2009-03-07 16:32:56 +01:00
|
|
|
remque(void *a)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
|
|
|
register struct quehead *element = (struct quehead *) a;
|
|
|
|
((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
|
|
|
|
((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
|
|
|
|
element->qh_rlink = NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-24 14:42:28 +02:00
|
|
|
int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
|
|
|
|
struct in_addr addr, int port)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
|
|
|
struct ex_list *tmp_ptr;
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
/* First, check if the port is "bound" */
|
|
|
|
for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
|
2009-06-24 14:42:28 +02:00
|
|
|
if (port == tmp_ptr->ex_fport &&
|
|
|
|
addr.s_addr == tmp_ptr->ex_addr.s_addr)
|
|
|
|
return -1;
|
2004-04-22 02:10:48 +02:00
|
|
|
}
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
tmp_ptr = *ex_ptr;
|
2014-08-19 10:30:17 +02:00
|
|
|
*ex_ptr = g_new(struct ex_list, 1);
|
2004-04-22 02:10:48 +02:00
|
|
|
(*ex_ptr)->ex_fport = port;
|
|
|
|
(*ex_ptr)->ex_addr = addr;
|
|
|
|
(*ex_ptr)->ex_pty = do_pty;
|
2014-08-19 10:30:17 +02:00
|
|
|
(*ex_ptr)->ex_exec = (do_pty == 3) ? exec : g_strdup(exec);
|
2004-04-22 02:10:48 +02:00
|
|
|
(*ex_ptr)->ex_next = tmp_ptr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-06 01:10:26 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
int
|
2007-10-26 21:01:16 +02:00
|
|
|
fork_exec(struct socket *so, const char *ex, int do_pty)
|
2004-09-06 01:10:26 +02:00
|
|
|
{
|
|
|
|
/* not implemented */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
/*
|
|
|
|
* XXX This is ugly
|
|
|
|
* We create and bind a socket, then fork off to another
|
|
|
|
* process, which connects to this socket, after which we
|
|
|
|
* exec the wanted program. If something (strange) happens,
|
|
|
|
* the accept() call could block us forever.
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-04-22 02:10:48 +02:00
|
|
|
* do_pty = 0 Fork/exec inetd style
|
|
|
|
* do_pty = 1 Fork/exec using slirp.telnetd
|
|
|
|
* do_ptr = 2 Fork/exec using pty
|
|
|
|
*/
|
|
|
|
int
|
2007-10-26 21:01:16 +02:00
|
|
|
fork_exec(struct socket *so, const char *ex, int do_pty)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
2018-11-06 16:13:23 +01:00
|
|
|
int s, cs;
|
|
|
|
struct sockaddr_in addr, csaddr;
|
2008-05-10 03:49:53 +02:00
|
|
|
socklen_t addrlen = sizeof(addr);
|
2018-11-06 16:13:23 +01:00
|
|
|
socklen_t csaddrlen = sizeof(csaddr);
|
2004-04-22 02:10:48 +02:00
|
|
|
int opt;
|
2008-09-14 08:45:34 +02:00
|
|
|
const char *argv[256];
|
2004-04-22 02:10:48 +02:00
|
|
|
/* don't want to clobber the original */
|
|
|
|
char *bptr;
|
2007-10-26 21:01:16 +02:00
|
|
|
const char *curarg;
|
2004-12-12 12:45:10 +01:00
|
|
|
int c, i, ret;
|
2011-03-09 18:21:10 +01:00
|
|
|
pid_t pid;
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
DEBUG_CALL("fork_exec");
|
2015-08-29 09:12:35 +02:00
|
|
|
DEBUG_ARG("so = %p", so);
|
|
|
|
DEBUG_ARG("ex = %p", ex);
|
|
|
|
DEBUG_ARG("do_pty = %x", do_pty);
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
if (do_pty == 2) {
|
2007-11-13 02:56:12 +01:00
|
|
|
return 0;
|
2004-04-22 02:10:48 +02:00
|
|
|
} else {
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = 0;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2009-12-02 12:24:42 +01:00
|
|
|
if ((s = qemu_socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
|
2004-04-22 02:10:48 +02:00
|
|
|
bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
|
|
|
|
listen(s, 1) < 0) {
|
2014-03-22 00:42:20 +01:00
|
|
|
error_report("Error: inet socket: %s", strerror(errno));
|
2017-07-09 19:54:22 +02:00
|
|
|
if (s >= 0) {
|
|
|
|
closesocket(s);
|
|
|
|
}
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2018-11-06 16:13:23 +01:00
|
|
|
if (getsockname(s, (struct sockaddr *)&csaddr, &csaddrlen) < 0) {
|
|
|
|
closesocket(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
cs = qemu_socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (cs < 0) {
|
|
|
|
closesocket(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
csaddr.sin_addr = loopback_addr;
|
|
|
|
/*
|
|
|
|
* This connect won't block because we've already listen()ed on
|
|
|
|
* the server end (even though we won't accept() the connection
|
|
|
|
* until later on).
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
ret = connect(cs, (struct sockaddr *)&csaddr, csaddrlen);
|
|
|
|
} while (ret < 0 && errno == EINTR);
|
|
|
|
if (ret < 0) {
|
|
|
|
closesocket(s);
|
|
|
|
closesocket(cs);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-09 18:21:10 +01:00
|
|
|
pid = fork();
|
|
|
|
switch(pid) {
|
2004-04-22 02:10:48 +02:00
|
|
|
case -1:
|
2014-03-22 00:42:20 +01:00
|
|
|
error_report("Error: fork failed: %s", strerror(errno));
|
2018-11-06 16:13:23 +01:00
|
|
|
closesocket(cs);
|
2004-04-22 02:10:48 +02:00
|
|
|
close(s);
|
|
|
|
return 0;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
case 0:
|
2011-07-20 12:20:17 +02:00
|
|
|
setsid();
|
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
/* Set the DISPLAY */
|
2012-01-05 14:18:45 +01:00
|
|
|
close(s);
|
2018-11-06 16:13:23 +01:00
|
|
|
dup2(cs, 0);
|
|
|
|
dup2(cs, 1);
|
|
|
|
dup2(cs, 2);
|
2007-10-26 21:01:16 +02:00
|
|
|
for (s = getdtablesize() - 1; s >= 3; s--)
|
2004-04-22 02:10:48 +02:00
|
|
|
close(s);
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
i = 0;
|
2011-08-21 05:09:37 +02:00
|
|
|
bptr = g_strdup(ex); /* No need to free() this */
|
2004-04-22 02:10:48 +02:00
|
|
|
if (do_pty == 1) {
|
|
|
|
/* Setup "slirp.telnetd -x" */
|
|
|
|
argv[i++] = "slirp.telnetd";
|
|
|
|
argv[i++] = "-x";
|
|
|
|
argv[i++] = bptr;
|
|
|
|
} else
|
|
|
|
do {
|
|
|
|
/* Change the string into argv[] */
|
|
|
|
curarg = bptr;
|
|
|
|
while (*bptr != ' ' && *bptr != (char)0)
|
|
|
|
bptr++;
|
|
|
|
c = *bptr;
|
|
|
|
*bptr++ = (char)0;
|
2014-08-19 10:30:17 +02:00
|
|
|
argv[i++] = g_strdup(curarg);
|
2004-04-22 02:10:48 +02:00
|
|
|
} while (c);
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2009-03-07 16:32:56 +01:00
|
|
|
argv[i] = NULL;
|
2008-09-14 08:45:34 +02:00
|
|
|
execvp(argv[0], (char **)argv);
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
/* Ooops, failed, let's tell the user why */
|
2009-12-25 19:19:18 +01:00
|
|
|
fprintf(stderr, "Error: execvp of %s failed: %s\n",
|
|
|
|
argv[0], strerror(errno));
|
2004-04-22 02:10:48 +02:00
|
|
|
close(0); close(1); close(2); /* XXX */
|
|
|
|
exit(1);
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
default:
|
2011-03-09 18:21:10 +01:00
|
|
|
qemu_add_child_watch(pid);
|
2018-11-06 16:13:23 +01:00
|
|
|
closesocket(cs);
|
2012-01-05 14:18:45 +01:00
|
|
|
/*
|
2018-11-06 16:13:23 +01:00
|
|
|
* This should never block, because we already connect()ed
|
|
|
|
* on the child end before we forked.
|
2012-01-05 14:18:45 +01:00
|
|
|
*/
|
|
|
|
do {
|
|
|
|
so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
|
|
|
|
} while (so->s < 0 && errno == EINTR);
|
|
|
|
closesocket(s);
|
2013-10-02 12:23:15 +02:00
|
|
|
socket_set_fast_reuse(so->s);
|
2012-01-05 14:18:45 +01:00
|
|
|
opt = 1;
|
2013-03-08 19:58:32 +01:00
|
|
|
qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
|
2013-03-27 10:10:43 +01:00
|
|
|
qemu_set_nonblock(so->s);
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
/* Append the telnet options now */
|
2009-03-07 16:32:56 +01:00
|
|
|
if (so->so_m != NULL && do_pty == 1) {
|
2004-04-22 02:10:48 +02:00
|
|
|
sbappend(so, so->so_m);
|
2009-03-07 16:32:56 +01:00
|
|
|
so->so_m = NULL;
|
2004-04-22 02:10:48 +02:00
|
|
|
}
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-24 14:42:31 +02:00
|
|
|
void slirp_connection_info(Slirp *slirp, Monitor *mon)
|
2009-06-24 14:42:29 +02:00
|
|
|
{
|
|
|
|
const char * const tcpstates[] = {
|
|
|
|
[TCPS_CLOSED] = "CLOSED",
|
|
|
|
[TCPS_LISTEN] = "LISTEN",
|
|
|
|
[TCPS_SYN_SENT] = "SYN_SENT",
|
|
|
|
[TCPS_SYN_RECEIVED] = "SYN_RCVD",
|
|
|
|
[TCPS_ESTABLISHED] = "ESTABLISHED",
|
|
|
|
[TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
|
|
|
|
[TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
|
|
|
|
[TCPS_CLOSING] = "CLOSING",
|
|
|
|
[TCPS_LAST_ACK] = "LAST_ACK",
|
|
|
|
[TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
|
|
|
|
[TCPS_TIME_WAIT] = "TIME_WAIT",
|
|
|
|
};
|
|
|
|
struct in_addr dst_addr;
|
|
|
|
struct sockaddr_in src;
|
|
|
|
socklen_t src_len;
|
|
|
|
uint16_t dst_port;
|
|
|
|
struct socket *so;
|
|
|
|
const char *state;
|
|
|
|
char buf[20];
|
|
|
|
|
|
|
|
monitor_printf(mon, " Protocol[State] FD Source Address Port "
|
|
|
|
"Dest. Address Port RecvQ SendQ\n");
|
|
|
|
|
2009-06-24 14:42:31 +02:00
|
|
|
for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
|
2009-06-24 14:42:29 +02:00
|
|
|
if (so->so_state & SS_HOSTFWD) {
|
|
|
|
state = "HOST_FORWARD";
|
|
|
|
} else if (so->so_tcpcb) {
|
|
|
|
state = tcpstates[so->so_tcpcb->t_state];
|
|
|
|
} else {
|
|
|
|
state = "NONE";
|
|
|
|
}
|
|
|
|
if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
|
|
|
|
src_len = sizeof(src);
|
|
|
|
getsockname(so->s, (struct sockaddr *)&src, &src_len);
|
|
|
|
dst_addr = so->so_laddr;
|
|
|
|
dst_port = so->so_lport;
|
|
|
|
} else {
|
|
|
|
src.sin_addr = so->so_laddr;
|
|
|
|
src.sin_port = so->so_lport;
|
|
|
|
dst_addr = so->so_faddr;
|
|
|
|
dst_port = so->so_fport;
|
|
|
|
}
|
2012-02-24 12:33:49 +01:00
|
|
|
snprintf(buf, sizeof(buf), " TCP[%s]", state);
|
|
|
|
monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
|
2009-06-24 14:42:29 +02:00
|
|
|
src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
|
|
|
|
ntohs(src.sin_port));
|
|
|
|
monitor_printf(mon, "%15s %5d %5d %5d\n",
|
|
|
|
inet_ntoa(dst_addr), ntohs(dst_port),
|
|
|
|
so->so_rcv.sb_cc, so->so_snd.sb_cc);
|
|
|
|
}
|
|
|
|
|
2009-06-24 14:42:31 +02:00
|
|
|
for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
|
2009-06-24 14:42:29 +02:00
|
|
|
if (so->so_state & SS_HOSTFWD) {
|
2012-02-24 12:33:49 +01:00
|
|
|
snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
|
2009-06-24 14:42:29 +02:00
|
|
|
src_len = sizeof(src);
|
|
|
|
getsockname(so->s, (struct sockaddr *)&src, &src_len);
|
|
|
|
dst_addr = so->so_laddr;
|
|
|
|
dst_port = so->so_lport;
|
|
|
|
} else {
|
2012-02-24 12:33:49 +01:00
|
|
|
snprintf(buf, sizeof(buf), " UDP[%d sec]",
|
2009-06-24 14:42:29 +02:00
|
|
|
(so->so_expire - curtime) / 1000);
|
|
|
|
src.sin_addr = so->so_laddr;
|
|
|
|
src.sin_port = so->so_lport;
|
|
|
|
dst_addr = so->so_faddr;
|
|
|
|
dst_port = so->so_fport;
|
|
|
|
}
|
2012-02-24 12:33:49 +01:00
|
|
|
monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
|
2009-06-24 14:42:29 +02:00
|
|
|
src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
|
|
|
|
ntohs(src.sin_port));
|
|
|
|
monitor_printf(mon, "%15s %5d %5d %5d\n",
|
|
|
|
inet_ntoa(dst_addr), ntohs(dst_port),
|
|
|
|
so->so_rcv.sb_cc, so->so_snd.sb_cc);
|
|
|
|
}
|
2011-07-20 12:20:18 +02:00
|
|
|
|
|
|
|
for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
|
2012-02-24 12:33:49 +01:00
|
|
|
snprintf(buf, sizeof(buf), " ICMP[%d sec]",
|
2011-07-20 12:20:18 +02:00
|
|
|
(so->so_expire - curtime) / 1000);
|
|
|
|
src.sin_addr = so->so_laddr;
|
|
|
|
dst_addr = so->so_faddr;
|
2012-02-24 12:33:49 +01:00
|
|
|
monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
|
2011-07-20 12:20:18 +02:00
|
|
|
src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
|
|
|
|
monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
|
|
|
|
so->so_rcv.sb_cc, so->so_snd.sb_cc);
|
|
|
|
}
|
2009-06-24 14:42:29 +02:00
|
|
|
}
|