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