kvm: Improve upgrade notes when facing unsupported kernels
[qemu] / slirp / tcp_subr.c
index 547a7f6..9d020a6 100644 (file)
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
  * terms and conditions of the copyright.
  */
 
-#define WANT_SYS_IOCTL_H
 #include <slirp.h>
 
 /* patchable/settable parameters for tcp */
-int    tcp_mssdflt = TCP_MSS;
-int    tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
-int    tcp_do_rfc1323 = 0;     /* Don't do rfc1323 performance enhancements */
-int    tcp_rcvspace;   /* You may want to change this */
-int    tcp_sndspace;   /* Keep small if you have an error prone link */
+/* Don't do rfc1323 performance enhancements */
+#define TCP_DO_RFC1323 0
 
 /*
  * Tcp initialization
  */
 void
-tcp_init()
+tcp_init(void)
 {
        tcp_iss = 1;            /* wrong */
        tcb.so_next = tcb.so_prev = &tcb;
-
-       /* tcp_rcvspace = our Window we advertise to the remote */
-       tcp_rcvspace = TCP_RCVSPACE;
-       tcp_sndspace = TCP_SNDSPACE;
-
-       /* Make sure tcp_sndspace is at least 2*MSS */
-       if (tcp_sndspace < 2*(min(if_mtu, if_mru) - sizeof(struct tcpiphdr)))
-               tcp_sndspace = 2*(min(if_mtu, if_mru) - sizeof(struct tcpiphdr));
 }
 
 /*
@@ -78,13 +62,12 @@ tcp_init()
  */
 /* struct tcpiphdr * */
 void
-tcp_template(tp)
-       struct tcpcb *tp;
+tcp_template(struct tcpcb *tp)
 {
        struct socket *so = tp->t_socket;
        register struct tcpiphdr *n = &tp->t_template;
 
-       n->ti_next = n->ti_prev = 0;
+       n->ti_mbuf = NULL;
        n->ti_x1 = 0;
        n->ti_pr = IPPROTO_TCP;
        n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
@@ -117,12 +100,8 @@ tcp_template(tp)
  * segment are as specified by the parameters.
  */
 void
-tcp_respond(tp, ti, m, ack, seq, flags)
-       struct tcpcb *tp;
-       register struct tcpiphdr *ti;
-       register struct mbuf *m;
-       tcp_seq ack, seq;
-       int flags;
+tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
+            tcp_seq ack, tcp_seq seq, int flags)
 {
        register int tlen;
        int win = 0;
@@ -137,7 +116,7 @@ tcp_respond(tp, ti, m, ack, seq, flags)
 
        if (tp)
                win = sbspace(&tp->t_socket->so_rcv);
-       if (m == 0) {
+        if (m == NULL) {
                if ((m = m_get()) == NULL)
                        return;
 #ifdef TCP_COMPAT_42
@@ -145,7 +124,7 @@ tcp_respond(tp, ti, m, ack, seq, flags)
 #else
                tlen = 0;
 #endif
-               m->m_data += if_maxlinkhdr;
+               m->m_data += IF_MAXLINKHDR;
                *mtod(m, struct tcpiphdr *) = *ti;
                ti = mtod(m, struct tcpiphdr *);
                flags = TH_ACK;
@@ -155,7 +134,7 @@ tcp_respond(tp, ti, m, ack, seq, flags)
                 * the mbuf point to ti
                 */
                m->m_data = (caddr_t)ti;
-       
+
                m->m_len = sizeof (struct tcpiphdr);
                tlen = 0;
 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
@@ -167,7 +146,7 @@ tcp_respond(tp, ti, m, ack, seq, flags)
        tlen += sizeof (struct tcpiphdr);
        m->m_len = tlen;
 
-       ti->ti_next = ti->ti_prev = 0;
+        ti->ti_mbuf = NULL;
        ti->ti_x1 = 0;
        ti->ti_seq = htonl(seq);
        ti->ti_ack = htonl(ack);
@@ -186,7 +165,7 @@ tcp_respond(tp, ti, m, ack, seq, flags)
        if(flags & TH_RST)
          ((struct ip *)ti)->ip_ttl = MAXTTL;
        else
-         ((struct ip *)ti)->ip_ttl = ip_defttl;
+         ((struct ip *)ti)->ip_ttl = IPDEFTTL;
 
        (void) ip_output((struct socket *)0, m);
 }
@@ -197,8 +176,7 @@ tcp_respond(tp, ti, m, ack, seq, flags)
  * protocol control block.
  */
 struct tcpcb *
-tcp_newtcpcb(so)
-       struct socket *so;
+tcp_newtcpcb(struct socket *so)
 {
        register struct tcpcb *tp;
 
@@ -207,10 +185,10 @@ tcp_newtcpcb(so)
                return ((struct tcpcb *)0);
 
        memset((char *) tp, 0, sizeof(struct tcpcb));
-       tp->seg_next = tp->seg_prev = (tcpiphdrp_32)tp;
-       tp->t_maxseg = tcp_mssdflt;
+       tp->seg_next = tp->seg_prev = (struct tcpiphdr*)tp;
+       tp->t_maxseg = TCP_MSS;
 
-       tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
+       tp->t_flags = TCP_DO_RFC1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
        tp->t_socket = so;
 
        /*
@@ -219,7 +197,7 @@ tcp_newtcpcb(so)
         * reasonable initial retransmit time.
         */
        tp->t_srtt = TCPTV_SRTTBASE;
-       tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
+       tp->t_rttvar = TCPTV_SRTTDFLT << 2;
        tp->t_rttmin = TCPTV_MIN;
 
        TCPT_RANGESET(tp->t_rxtcur,
@@ -255,9 +233,9 @@ struct tcpcb *tcp_drop(struct tcpcb *tp, int err)
        if (TCPS_HAVERCVDSYN(tp->t_state)) {
                tp->t_state = TCPS_CLOSED;
                (void) tcp_output(tp);
-               tcpstat.tcps_drops++;
+               STAT(tcpstat.tcps_drops++);
        } else
-               tcpstat.tcps_conndrops++;
+               STAT(tcpstat.tcps_conndrops++);
 /*     if (errno == ETIMEDOUT && tp->t_softerror)
  *             errno = tp->t_softerror;
  */
@@ -272,8 +250,7 @@ struct tcpcb *tcp_drop(struct tcpcb *tp, int err)
  *     wake up any sleepers
  */
 struct tcpcb *
-tcp_close(tp)
-       register struct tcpcb *tp;
+tcp_close(struct tcpcb *tp)
 {
        register struct tcpiphdr *t;
        struct socket *so = tp->t_socket;
@@ -283,11 +260,11 @@ tcp_close(tp)
        DEBUG_ARG("tp = %lx", (long )tp);
 
        /* free the reassembly queue, if any */
-       t = (struct tcpiphdr *) tp->seg_next;
-       while (t != (struct tcpiphdr *)tp) {
-               t = (struct tcpiphdr *)t->ti_next;
-               m = (struct mbuf *) REASS_MBUF((struct tcpiphdr *)t->ti_prev);
-               remque_32((struct tcpiphdr *) t->ti_prev);
+       t = tcpfrag_list_first(tp);
+       while (!tcpfrag_list_end(t, tp)) {
+               t = tcpiphdr_next(t);
+               m = tcpiphdr_prev(t)->ti_mbuf;
+               remque(tcpiphdr2qlink(tcpiphdr_prev(t)));
                m_freem(m);
        }
        /* It's static */
@@ -296,7 +273,7 @@ tcp_close(tp)
  */
 /*     free(tp, M_PCB);  */
        free(tp);
-       so->so_tcpcb = 0;
+        so->so_tcpcb = NULL;
        soisfdisconnected(so);
        /* clobber input socket cache if we're closing the cached connection */
        if (so == tcp_last_so)
@@ -305,10 +282,11 @@ tcp_close(tp)
        sbfree(&so->so_rcv);
        sbfree(&so->so_snd);
        sofree(so);
-       tcpstat.tcps_closed++;
+       STAT(tcpstat.tcps_closed++);
        return ((struct tcpcb *)0);
 }
 
+#ifdef notdef
 void
 tcp_drain()
 {
@@ -319,9 +297,6 @@ tcp_drain()
  * When a source quench is received, close congestion window
  * to one segment.  We will gradually open it again as we proceed.
  */
-
-#ifdef notdef
-
 void
 tcp_quench(i, errno)
 
@@ -350,8 +325,7 @@ tcp_quench(i, errno)
  * We can let the user exit from the close as soon as the FIN is acked.
  */
 void
-tcp_sockclosed(tp)
-       struct tcpcb *tp;
+tcp_sockclosed(struct tcpcb *tp)
 {
 
        DEBUG_CALL("tcp_sockclosed");
@@ -392,11 +366,10 @@ tcp_sockclosed(tp)
  * nonblocking.  Connect returns after the SYN is sent, and does
  * not wait for ACK+SYN.
  */
-int tcp_fconnect(so)
-     struct socket *so;
+int tcp_fconnect(struct socket *so)
 {
   int ret=0;
+
   DEBUG_CALL("tcp_fconnect");
   DEBUG_ARG("so = %lx", (long )so);
 
@@ -409,7 +382,7 @@ int tcp_fconnect(so)
     setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(opt ));
     opt = 1;
     setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(opt ));
-   
+
     addr.sin_family = AF_INET;
     if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
       /* It's an alias */
@@ -425,13 +398,13 @@ int tcp_fconnect(so)
     } else
       addr.sin_addr = so->so_faddr;
     addr.sin_port = so->so_fport;
-   
+
     DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
                "addr.sin_addr.s_addr=%.16s\n",
                ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
     /* We don't care what port we get */
     ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
-   
+
     /*
      * If it's not in progress, it failed, so we just return 0,
      * without clearing SS_NOFDREF
@@ -455,12 +428,11 @@ int tcp_fconnect(so)
  * here and SYN the local-host.
  */
 void
-tcp_connect(inso)
-       struct socket *inso;
+tcp_connect(struct socket *inso)
 {
        struct socket *so;
        struct sockaddr_in addr;
-       int addrlen = sizeof(struct sockaddr_in);
+       socklen_t addrlen = sizeof(struct sockaddr_in);
        struct tcpcb *tp;
        int s, opt;
 
@@ -528,7 +500,7 @@ tcp_connect(inso)
  */
 
 /*     soisconnecting(so); */ /* NOFDREF used instead */
-       tcpstat.tcps_connattempt++;
+       STAT(tcpstat.tcps_connattempt++);
 
        tp->t_state = TCPS_SYN_SENT;
        tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
@@ -542,8 +514,7 @@ tcp_connect(inso)
  * Attach a TCPCB to a socket.
  */
 int
-tcp_attach(so)
-       struct socket *so;
+tcp_attach(struct socket *so)
 {
        if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL)
           return -1;
@@ -556,7 +527,7 @@ tcp_attach(so)
 /*
  * Set the socket's type of service field
  */
-struct tos_t tcptos[] = {
+static const struct tos_t tcptos[] = {
          {0, 20, IPTOS_THROUGHPUT, 0}, /* ftp data */
          {21, 21, IPTOS_LOWDELAY,  EMU_FTP},   /* ftp control */
          {0, 23, IPTOS_LOWDELAY, 0},   /* telnet */
@@ -572,14 +543,16 @@ struct tos_t tcptos[] = {
          {0, 0, 0, 0}
 };
 
-struct emu_t *tcpemu = 0;
-       
+#ifdef CONFIG_QEMU
+static
+#endif
+struct emu_t *tcpemu = NULL;
+
 /*
  * Return TOS according to the above table
  */
 u_int8_t
-tcp_tos(so)
-       struct socket *so;
+tcp_tos(struct socket *so)
 {
        int i = 0;
        struct emu_t *emup;
@@ -605,7 +578,9 @@ tcp_tos(so)
        return 0;
 }
 
+#if 0
 int do_echo = -1;
+#endif
 
 /*
  * Emulate programs that try and connect to us
@@ -618,7 +593,7 @@ int do_echo = -1;
  * more checks are needed here
  *
  * XXX Assumes the whole command came in one packet
- *                                        
+ *
  * XXX Some ftp clients will have their TOS set to
  * LOWDELAY and so Nagel will kick in.  Because of this,
  * we'll get the first letter, followed by the rest, so
@@ -632,12 +607,10 @@ int do_echo = -1;
  * NOTE: if you return 0 you MUST m_free() the mbuf!
  */
 int
-tcp_emu(so, m)
-       struct socket *so;
-       struct mbuf *m;
+tcp_emu(struct socket *so, struct mbuf *m)
 {
        u_int n1, n2, n3, n4, n5, n6;
-       char buff[256];
+        char buff[257];
        u_int32_t laddr;
        u_int lport;
        char *bptr;
@@ -648,24 +621,24 @@ tcp_emu(so, m)
 
        switch(so->so_emu) {
                int x, i;
-       
+
         case EMU_IDENT:
                /*
                 * Identification protocol as per rfc-1413
                 */
-       
+
                {
                        struct socket *tmpso;
                        struct sockaddr_in addr;
-                       int addrlen = sizeof(struct sockaddr_in);
+                       socklen_t addrlen = sizeof(struct sockaddr_in);
                        struct sbuf *so_rcv = &so->so_rcv;
-               
+
                        memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
                        so_rcv->sb_wptr += m->m_len;
                        so_rcv->sb_rptr += m->m_len;
                        m->m_data[m->m_len] = 0; /* NULL terminate */
                        if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n')) {
-                               if (sscanf(so_rcv->sb_data, "%d%*[ ,]%d", &n1, &n2) == 2) {
+                               if (sscanf(so_rcv->sb_data, "%u%*[ ,]%u", &n1, &n2) == 2) {
                                        HTONS(n1);
                                        HTONS(n2);
                                        /* n2 is the one on our host */
@@ -681,14 +654,16 @@ tcp_emu(so, m)
                                                }
                                        }
                                }
-                               so_rcv->sb_cc = sprintf(so_rcv->sb_data, "%d,%d\r\n", n1, n2);
+                                so_rcv->sb_cc = snprintf(so_rcv->sb_data,
+                                                         so_rcv->sb_datalen,
+                                                         "%d,%d\r\n", n1, n2);
                                so_rcv->sb_rptr = so_rcv->sb_data;
                                so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc;
                        }
                        m_free(m);
                        return 0;
                }
-       
+
 #if 0
         case EMU_RLOGIN:
                /*
@@ -703,7 +678,7 @@ tcp_emu(so, m)
                        char term[100];
                        struct sbuf *so_snd = &so->so_snd;
                        struct sbuf *so_rcv = &so->so_rcv;
-               
+
                        /* First check if they have a priveladged port, or too much data has arrived */
                        if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
                            (m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
@@ -714,13 +689,13 @@ tcp_emu(so, m)
                                m_free(m);
                                return 0;
                        }
-               
+
                        /* Append the current data */
                        memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
                        so_rcv->sb_wptr += m->m_len;
                        so_rcv->sb_rptr += m->m_len;
                        m_free(m);
-               
+
                        /*
                         * Check if we have all the initial options,
                         * and build argument list to rlogin while we're here
@@ -752,10 +727,10 @@ tcp_emu(so, m)
                                        }
                                }
                        }
-               
+
                        if (n != 4)
                           return 0;
-               
+
                        /* We have it, set our term variable and fork_exec() */
 #ifdef HAVE_SETENV
                        setenv("TERM", term, 1);
@@ -765,15 +740,15 @@ tcp_emu(so, m)
                        fork_exec(so, args, 2);
                        term[0] = 0;
                        so->so_emu = 0;
-               
+
                        /* And finally, send the client a 0 character */
                        so_snd->sb_wptr[0] = 0;
                        so_snd->sb_wptr++;
                        so_snd->sb_cc++;
-               
+
                        return 0;
                }
-       
+
         case EMU_RSH:
                /*
                 * rsh emulation
@@ -787,7 +762,7 @@ tcp_emu(so, m)
                        char *args;
                        struct sbuf *so_snd = &so->so_snd;
                        struct sbuf *so_rcv = &so->so_rcv;
-               
+
                        /* First check if they have a priveladged port, or too much data has arrived */
                        if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
                            (m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
@@ -798,13 +773,13 @@ tcp_emu(so, m)
                                m_free(m);
                                return 0;
                        }
-               
+
                        /* Append the current data */
                        memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
                        so_rcv->sb_wptr += m->m_len;
                        so_rcv->sb_rptr += m->m_len;
                        m_free(m);
-               
+
                        /*
                         * Check if we have all the initial options,
                         * and build argument list to rlogin while we're here
@@ -846,9 +821,9 @@ tcp_emu(so, m)
 
                                ns->so_iptos = tcp_tos(ns);
                                tp = sototcpcb(ns);
-               
+
                                tcp_template(tp);
-               
+
                                /* Compute window scaling to request.  */
                                /*      while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
                                 *              (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
@@ -857,8 +832,8 @@ tcp_emu(so, m)
 
                 /*soisfconnecting(ns);*/
 
-                               tcpstat.tcps_connattempt++;
-                               
+                               STAT(tcpstat.tcps_connattempt++);
+
                                tp->t_state = TCPS_SYN_SENT;
                                tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
                                tp->iss = tcp_iss;
@@ -877,19 +852,19 @@ tcp_emu(so, m)
                 }
               }
                        }
-               
+
                        if (n != 4)
               return 0;
-               
+
                        rsh_exec(so,so->extra, user, inet_ntoa(so->so_faddr), args);
                        so->so_emu = 0;
                        so->extra=NULL;
-               
+
                        /* And finally, send the client a 0 character */
                        so_snd->sb_wptr[0] = 0;
                        so_snd->sb_wptr++;
                        so_snd->sb_cc++;
-               
+
                        return 0;
                }
 
@@ -898,7 +873,7 @@ tcp_emu(so, m)
                        int num;
                        struct sbuf *so_snd = &so->so_snd;
                        struct sbuf *so_rcv = &so->so_rcv;
-               
+
                        /*
                         * If there is binary data here, we save it in so->so_m
                         */
@@ -913,16 +888,16 @@ tcp_emu(so, m)
                            }
                          }
                        } /* if(so->so_m==NULL) */
-               
+
                        /*
                         * Append the line
                         */
                        sbappendsb(so_rcv, m);
-               
+
                        /* To avoid going over the edge of the buffer, we reset it */
                        if (so_snd->sb_cc == 0)
                           so_snd->sb_wptr = so_snd->sb_rptr = so_snd->sb_data;
-               
+
                        /*
                         * A bit of a hack:
                         * If the first packet we get here is 1 byte long, then it
@@ -941,13 +916,13 @@ tcp_emu(so, m)
                          tcp_output(sototcpcb(so)); /* XXX */
                        } else
                          m_free(m);
-               
+
                        num = 0;
                        while (num < so->so_rcv.sb_cc) {
                                if (*(so->so_rcv.sb_rptr + num) == '\n' ||
                                    *(so->so_rcv.sb_rptr + num) == '\r') {
                                        int n;
-                               
+
                                        *(so_rcv->sb_rptr + num) = 0;
                                        if (ctl_password && !ctl_password_ok) {
                                                /* Need a password */
@@ -984,76 +959,78 @@ do_prompt:
                        }
                        return 0;
                }
-#endif 
+#endif
         case EMU_FTP: /* ftp */
-               *(m->m_data+m->m_len) = 0; /* NULL terminate for strstr */
+                *(m->m_data+m->m_len) = 0; /* NUL terminate for strstr */
                if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL) {
                        /*
                         * Need to emulate the PORT command
-                        */             
-                       x = sscanf(bptr, "ORT %d,%d,%d,%d,%d,%d\r\n%256[^\177]",
+                        */
+                       x = sscanf(bptr, "ORT %u,%u,%u,%u,%u,%u\r\n%256[^\177]",
                                   &n1, &n2, &n3, &n4, &n5, &n6, buff);
                        if (x < 6)
                           return 1;
-               
+
                        laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
                        lport = htons((n5 << 8) | (n6));
-               
+
                        if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL)
                           return 1;
-               
+
                        n6 = ntohs(so->so_fport);
-               
+
                        n5 = (n6 >> 8) & 0xff;
                        n6 &= 0xff;
-               
+
                        laddr = ntohl(so->so_faddr.s_addr);
-               
+
                        n1 = ((laddr >> 24) & 0xff);
                        n2 = ((laddr >> 16) & 0xff);
                        n3 = ((laddr >> 8)  & 0xff);
                        n4 =  (laddr & 0xff);
-               
+
                        m->m_len = bptr - m->m_data; /* Adjust length */
-                       m->m_len += sprintf(bptr,"ORT %d,%d,%d,%d,%d,%d\r\n%s",
-                                           n1, n2, n3, n4, n5, n6, x==7?buff:"");
+                        m->m_len += snprintf(bptr, m->m_hdr.mh_size - m->m_len,
+                                             "ORT %d,%d,%d,%d,%d,%d\r\n%s",
+                                             n1, n2, n3, n4, n5, n6, x==7?buff:"");
                        return 1;
                } else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) {
                        /*
                         * Need to emulate the PASV response
                         */
-                       x = sscanf(bptr, "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%256[^\177]",
+                       x = sscanf(bptr, "27 Entering Passive Mode (%u,%u,%u,%u,%u,%u)\r\n%256[^\177]",
                                   &n1, &n2, &n3, &n4, &n5, &n6, buff);
                        if (x < 6)
                           return 1;
-               
+
                        laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
                        lport = htons((n5 << 8) | (n6));
-               
+
                        if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL)
                           return 1;
-               
+
                        n6 = ntohs(so->so_fport);
-               
+
                        n5 = (n6 >> 8) & 0xff;
                        n6 &= 0xff;
-               
+
                        laddr = ntohl(so->so_faddr.s_addr);
-               
+
                        n1 = ((laddr >> 24) & 0xff);
                        n2 = ((laddr >> 16) & 0xff);
                        n3 = ((laddr >> 8)  & 0xff);
                        n4 =  (laddr & 0xff);
-               
+
                        m->m_len = bptr - m->m_data; /* Adjust length */
-                       m->m_len += sprintf(bptr,"27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
-                                           n1, n2, n3, n4, n5, n6, x==7?buff:"");
-               
+                       m->m_len += snprintf(bptr, m->m_hdr.mh_size - m->m_len,
+                                             "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
+                                             n1, n2, n3, n4, n5, n6, x==7?buff:"");
+
                        return 1;
                }
-       
+
                return 1;
-                                 
+
         case EMU_KSH:
                /*
                 * The kshell (Kerberos rsh) and shell services both pass
@@ -1070,9 +1047,10 @@ do_prompt:
                }
                if (m->m_data[m->m_len-1] == '\0' && lport != 0 &&
                    (so = solisten(0, so->so_laddr.s_addr, htons(lport), SS_FACCEPTONCE)) != NULL)
-                       m->m_len = sprintf(m->m_data, "%d", ntohs(so->so_fport))+1;
+                    m->m_len = snprintf(m->m_data, m->m_hdr.mh_size, "%d",
+                                        ntohs(so->so_fport)) + 1;
                return 1;
-       
+
         case EMU_IRC:
                /*
                 * Need to emulate DCC CHAT, DCC SEND and DCC MOVE
@@ -1080,32 +1058,35 @@ do_prompt:
                *(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */
                if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL)
                         return 1;
-       
+
                /* The %256s is for the broken mIRC */
                if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3) {
                        if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
                                return 1;
-               
+
                        m->m_len = bptr - m->m_data; /* Adjust length */
-                       m->m_len += sprintf(bptr, "DCC CHAT chat %lu %u%c\n",
-                            (unsigned long)ntohl(so->so_faddr.s_addr),
-                            ntohs(so->so_fport), 1);
+                        m->m_len += snprintf(bptr, m->m_hdr.mh_size,
+                                             "DCC CHAT chat %lu %u%c\n",
+                                             (unsigned long)ntohl(so->so_faddr.s_addr),
+                                             ntohs(so->so_fport), 1);
                } else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
                        if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
                                return 1;
-               
+
                        m->m_len = bptr - m->m_data; /* Adjust length */
-                       m->m_len += sprintf(bptr, "DCC SEND %s %lu %u %u%c\n",
-                             buff, (unsigned long)ntohl(so->so_faddr.s_addr),
-                             ntohs(so->so_fport), n1, 1);
+                        m->m_len += snprintf(bptr, m->m_hdr.mh_size,
+                                             "DCC SEND %s %lu %u %u%c\n", buff,
+                                             (unsigned long)ntohl(so->so_faddr.s_addr),
+                                             ntohs(so->so_fport), n1, 1);
                } else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
                        if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
                                return 1;
-               
+
                        m->m_len = bptr - m->m_data; /* Adjust length */
-                       m->m_len += sprintf(bptr, "DCC MOVE %s %lu %u %u%c\n",
-                             buff, (unsigned long)ntohl(so->so_faddr.s_addr),
-                             ntohs(so->so_fport), n1, 1);
+                        m->m_len += snprintf(bptr, m->m_hdr.mh_size,
+                                             "DCC MOVE %s %lu %u %u%c\n", buff,
+                                             (unsigned long)ntohl(so->so_faddr.s_addr),
+                                             ntohs(so->so_fport), n1, 1);
                }
                return 1;
 
@@ -1120,43 +1101,43 @@ do_prompt:
                 * any more.
                 *
                 * A typical packet for player version 1.0 (release version):
-                *       
+                *
                 * 0000:50 4E 41 00 05
                 * 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 .....×..gælÜc..P
                 * 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH
                 * 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v
                 * 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB
-                *        
+                *
                 * Now the port number 0x1BD7 is found at offset 0x04 of the
                 * Now the port number 0x1BD7 is found at offset 0x04 of the
                 * second packet. This time we received five bytes first and
                 * then the rest. You never know how many bytes you get.
                 *
                 * A typical packet for player version 2.0 (beta):
-                *       
+                *
                 * 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA...........Á.
                 * 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .guxõc..Win2.0.0
                 * 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/
                 * 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas
                 * 0040:65 2E 72 61 79 53 00 00 06 36 42                e.rayS...6B
-                *       
+                *
                 * Port number 0x1BC1 is found at offset 0x0d.
-                *     
+                *
                 * This is just a horrible switch statement. Variable ra tells
                 * us where we're going.
                 */
-       
+
                bptr = m->m_data;
                while (bptr < m->m_data + m->m_len) {
                        u_short p;
                        static int ra = 0;
                        char ra_tbl[4];
-               
+
                        ra_tbl[0] = 0x50;
                        ra_tbl[1] = 0x4e;
                        ra_tbl[2] = 0x41;
                        ra_tbl[3] = 0;
-               
+
                        switch (ra) {
                         case 0:
                         case 2:
@@ -1166,7 +1147,7 @@ do_prompt:
                                        continue;
                                }
                                break;
-                       
+
                         case 1:
                                /*
                                 * We may get 0x50 several times, ignore them
@@ -1180,14 +1161,14 @@ do_prompt:
                                        continue;
                                }
                                break;
-                       
+
                         case 4:
                                /*
                                 * skip version number
                                 */
                                bptr++;
                                break;
-                       
+
                         case 5:
                                /*
                                 * The difference between versions 1.0 and
@@ -1198,19 +1179,19 @@ do_prompt:
                                   bptr += 8;
                                else
                                   bptr += 4;
-                               break;                         
-                       
+                               break;
+
                         case 6:
                                /* This is the field containing the port
                                 * number that RA-player is listening to.
                                 */
                                lport = (((u_char*)bptr)[0] << 8)
                                + ((u_char *)bptr)[1];
-                               if (lport < 6970)     
+                               if (lport < 6970)
                                   lport += 256;   /* don't know why */
                                if (lport < 6970 || lport > 7170)
                                   return 1;       /* failed */
-                       
+
                                /* try to get udp port between 6970 - 7170 */
                                for (p = 6970; p < 7071; p++) {
                                        if (udp_listen( htons(p),
@@ -1226,15 +1207,15 @@ do_prompt:
                                *(u_char *)bptr++ = p & 0xff;
                                ra = 0;
                                return 1;   /* port redirected, we're done */
-                               break; 
-                       
+                               break;
+
                         default:
-                               ra = 0;                        
+                               ra = 0;
                        }
                        ra++;
                }
-               return 1;                               
-       
+               return 1;
+
         default:
                /* Ooops, not emulated, won't call tcp_emu again */
                so->so_emu = 0;
@@ -1248,8 +1229,7 @@ do_prompt:
  * return 2 if this is a command-line connection
  */
 int
-tcp_ctl(so)
-       struct socket *so;
+tcp_ctl(struct socket *so)
 {
        struct sbuf *sb = &so->so_snd;
        int command;
@@ -1274,34 +1254,39 @@ tcp_ctl(so)
 
        switch(command) {
        default: /* Check for exec's */
-       
+
                /*
                 * Check if it's pty_exec
                 */
                for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
                        if (ex_ptr->ex_fport == so->so_fport &&
                            command == ex_ptr->ex_addr) {
+                               if (ex_ptr->ex_pty == 3) {
+                                       so->s = -1;
+                                       so->extra = (void *)ex_ptr->ex_exec;
+                                       return 1;
+                               }
                                do_pty = ex_ptr->ex_pty;
                                goto do_exec;
                        }
                }
-       
+
                /*
                 * Nothing bound..
                 */
                /* tcp_fconnect(so); */
-       
+
                /* FALLTHROUGH */
        case CTL_ALIAS:
-         sb->sb_cc = sprintf(sb->sb_wptr,
-                             "Error: No application configured.\r\n");
+          sb->sb_cc = snprintf(sb->sb_wptr, sb->sb_datalen - (sb->sb_wptr - sb->sb_data),
+                               "Error: No application configured.\r\n");
          sb->sb_wptr += sb->sb_cc;
          return(0);
 
        do_exec:
                DEBUG_MISC((dfd, " executing %s \n",ex_ptr->ex_exec));
                return(fork_exec(so, ex_ptr->ex_exec, do_pty));
-       
+
 #if 0
        case CTL_CMD:
           for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {