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