get resid right even if >32767
[unix-history] / usr / src / sys / netinet / in_pcb.c
index bc914f7..fe834c8 100644 (file)
@@ -1,4 +1,4 @@
-/*     in_pcb.c        4.34    82/10/20        */
+/*     in_pcb.c        6.1     83/07/29        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 struct in_addr zeroin_addr;
 
 
 struct in_addr zeroin_addr;
 
-in_pcbreserve(so, sndcc, rcvcc)
-       struct socket *so;
-       int sndcc, rcvcc;
-{
-
-       if (sbreserve(&so->so_snd, sndcc) == 0)
-               goto bad;
-       if (sbreserve(&so->so_rcv, rcvcc) == 0)
-               goto bad2;
-       return (0);
-bad2:
-       sbrelease(&so->so_snd);
-bad:
-       return (ENOBUFS);
-}
-
 in_pcballoc(so, head)
        struct socket *so;
        struct inpcb *head;
 in_pcballoc(so, head)
        struct socket *so;
        struct inpcb *head;
@@ -39,8 +23,8 @@ in_pcballoc(so, head)
        struct mbuf *m;
        register struct inpcb *inp;
 
        struct mbuf *m;
        register struct inpcb *inp;
 
-       m = m_getclr(M_DONTWAIT);
-       if (m == 0)
+       m = m_getclr(M_DONTWAIT, MT_PCB);
+       if (m == NULL)
                return (ENOBUFS);
        inp = mtod(m, struct inpcb *);
        inp->inp_head = head;
                return (ENOBUFS);
        inp = mtod(m, struct inpcb *);
        inp->inp_head = head;
@@ -61,14 +45,14 @@ in_pcbbind(inp, nam)
 
        if (ifnet == 0)
                return (EADDRNOTAVAIL);
 
        if (ifnet == 0)
                return (EADDRNOTAVAIL);
-       if (inp->inp_lport || inp->inp_laddr.s_addr)
+       if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
                return (EINVAL);
        if (nam == 0)
                goto noname;
        sin = mtod(nam, struct sockaddr_in *);
        if (nam->m_len != sizeof (*sin))
                return (EINVAL);
                return (EINVAL);
        if (nam == 0)
                goto noname;
        sin = mtod(nam, struct sockaddr_in *);
        if (nam->m_len != sizeof (*sin))
                return (EINVAL);
-       if (sin->sin_addr.s_addr) {
+       if (sin->sin_addr.s_addr != INADDR_ANY) {
                int tport = sin->sin_port;
 
                sin->sin_port = 0;              /* yech... */
                int tport = sin->sin_port;
 
                sin->sin_port = 0;              /* yech... */
@@ -78,16 +62,16 @@ in_pcbbind(inp, nam)
        }
        lport = sin->sin_port;
        if (lport) {
        }
        lport = sin->sin_port;
        if (lport) {
-               u_short aport = lport;
+               u_short aport = htons(lport);
                int wild = 0;
 
                int wild = 0;
 
-#if vax || pdp11 || ns16032
-               aport = htons(aport);
-#endif
                /* GROSS */
                if (aport < IPPORT_RESERVED && u.u_uid != 0)
                        return (EACCES);
                /* GROSS */
                if (aport < IPPORT_RESERVED && u.u_uid != 0)
                        return (EACCES);
-               if ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0)
+               /* even GROSSER, but this is the Internet */
+               if ((so->so_options & SO_REUSEADDR) == 0 &&
+                   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
+                    (so->so_options & SO_ACCEPTCONN) == 0))
                        wild = INPLOOKUP_WILDCARD;
                if (in_pcblookup(head,
                    zeroin_addr, 0, sin->sin_addr, lport, wild))
                        wild = INPLOOKUP_WILDCARD;
                if (in_pcblookup(head,
                    zeroin_addr, 0, sin->sin_addr, lport, wild))
@@ -124,9 +108,9 @@ in_pcbconnect(inp, nam)
                return (EINVAL);
        if (sin->sin_family != AF_INET)
                return (EAFNOSUPPORT);
                return (EINVAL);
        if (sin->sin_family != AF_INET)
                return (EAFNOSUPPORT);
-       if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0)
+       if (sin->sin_addr.s_addr == INADDR_ANY || sin->sin_port == 0)
                return (EADDRNOTAVAIL);
                return (EADDRNOTAVAIL);
-       if (inp->inp_laddr.s_addr == 0) {
+       if (inp->inp_laddr.s_addr == INADDR_ANY) {
                ifp = if_ifonnetof(in_netof(sin->sin_addr));
                if (ifp == 0) {
                        /*
                ifp = if_ifonnetof(in_netof(sin->sin_addr));
                if (ifp == 0) {
                        /*
@@ -148,8 +132,11 @@ in_pcbconnect(inp, nam)
            inp->inp_lport,
            0))
                return (EADDRINUSE);
            inp->inp_lport,
            0))
                return (EADDRINUSE);
-       if (inp->inp_laddr.s_addr == 0)
+       if (inp->inp_laddr.s_addr == INADDR_ANY) {
+               if (inp->inp_lport == 0)
+                       in_pcbbind(inp, (struct mbuf *)0);
                inp->inp_laddr = ifaddr->sin_addr;
                inp->inp_laddr = ifaddr->sin_addr;
+       }
        inp->inp_faddr = sin->sin_addr;
        inp->inp_fport = sin->sin_port;
        return (0);
        inp->inp_faddr = sin->sin_addr;
        inp->inp_fport = sin->sin_port;
        return (0);
@@ -159,7 +146,7 @@ in_pcbdisconnect(inp)
        struct inpcb *inp;
 {
 
        struct inpcb *inp;
 {
 
-       inp->inp_faddr.s_addr = 0;
+       inp->inp_faddr.s_addr = INADDR_ANY;
        inp->inp_fport = 0;
        if (inp->inp_socket->so_state & SS_NOFDREF)
                in_pcbdetach(inp);
        inp->inp_fport = 0;
        if (inp->inp_socket->so_state & SS_NOFDREF)
                in_pcbdetach(inp);
@@ -192,6 +179,20 @@ in_setsockaddr(inp, nam)
        sin->sin_addr = inp->inp_laddr;
 }
 
        sin->sin_addr = inp->inp_laddr;
 }
 
+in_setpeeraddr(inp, nam)
+       register struct inpcb *inp;
+       struct mbuf *nam;
+{
+       register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
+       
+       nam->m_len = sizeof (*sin);
+       sin = mtod(nam, struct sockaddr_in *);
+       bzero((caddr_t)sin, sizeof (*sin));
+       sin->sin_family = AF_INET;
+       sin->sin_port = inp->inp_fport;
+       sin->sin_addr = inp->inp_faddr;
+}
+
 /*
  * Pass an error to all internet connections
  * associated with address sin.  Call the
 /*
  * Pass an error to all internet connections
  * associated with address sin.  Call the
@@ -236,23 +237,23 @@ in_pcblookup(head, faddr, fport, laddr, lport, flags)
                if (inp->inp_lport != lport)
                        continue;
                wildcard = 0;
                if (inp->inp_lport != lport)
                        continue;
                wildcard = 0;
-               if (inp->inp_laddr.s_addr != 0) {
-                       if (laddr.s_addr == 0)
+               if (inp->inp_laddr.s_addr != INADDR_ANY) {
+                       if (laddr.s_addr == INADDR_ANY)
                                wildcard++;
                        else if (inp->inp_laddr.s_addr != laddr.s_addr)
                                continue;
                } else {
                                wildcard++;
                        else if (inp->inp_laddr.s_addr != laddr.s_addr)
                                continue;
                } else {
-                       if (laddr.s_addr != 0)
+                       if (laddr.s_addr != INADDR_ANY)
                                wildcard++;
                }
                                wildcard++;
                }
-               if (inp->inp_faddr.s_addr != 0) {
-                       if (faddr.s_addr == 0)
+               if (inp->inp_faddr.s_addr != INADDR_ANY) {
+                       if (faddr.s_addr == INADDR_ANY)
                                wildcard++;
                        else if (inp->inp_faddr.s_addr != faddr.s_addr ||
                            inp->inp_fport != fport)
                                continue;
                } else {
                                wildcard++;
                        else if (inp->inp_faddr.s_addr != faddr.s_addr ||
                            inp->inp_fport != fport)
                                continue;
                } else {
-                       if (faddr.s_addr != 0)
+                       if (faddr.s_addr != INADDR_ANY)
                                wildcard++;
                }
                if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
                                wildcard++;
                }
                if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)