fix OneNAND erase/write
[qemu] / slirp / tcp_subr.c
index ba1296d..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.
  *
@@ -42,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#define WANT_SYS_IOCTL_H
 #include <slirp.h>
 
 /* patchable/settable parameters for tcp */
@@ -53,7 +48,7 @@
  * Tcp initialization
  */
 void
-tcp_init()
+tcp_init(void)
 {
        tcp_iss = 1;            /* wrong */
        tcb.so_next = tcb.so_prev = &tcb;
@@ -67,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));
@@ -106,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;
@@ -126,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
@@ -156,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,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;
 
@@ -196,7 +185,7 @@ tcp_newtcpcb(so)
                return ((struct tcpcb *)0);
 
        memset((char *) tp, 0, sizeof(struct tcpcb));
-       tp->seg_next = tp->seg_prev = (tcpiphdrp_32)tp;
+       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;
@@ -261,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;
@@ -272,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 */
@@ -285,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)
@@ -337,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");
@@ -379,8 +366,7 @@ 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;
 
@@ -442,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;
 
@@ -529,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;
@@ -562,14 +546,13 @@ static const struct tos_t tcptos[] = {
 #ifdef CONFIG_QEMU
 static
 #endif
-struct emu_t *tcpemu = 0;
+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;
@@ -624,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;
@@ -649,7 +630,7 @@ tcp_emu(so, m)
                {
                        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);
@@ -673,7 +654,9 @@ 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;
                        }
@@ -978,7 +961,7 @@ do_prompt:
                }
 #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
@@ -1007,8 +990,9 @@ do_prompt:
                        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) {
                        /*
@@ -1038,8 +1022,9 @@ do_prompt:
                        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;
                }
@@ -1062,7 +1047,8 @@ 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:
@@ -1079,25 +1065,28 @@ do_prompt:
                                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;
 
@@ -1240,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;
@@ -1273,6 +1261,11 @@ tcp_ctl(so)
                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;
                        }
@@ -1285,8 +1278,8 @@ tcp_ctl(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);