BSD 4_3_Tahoe release
[unix-history] / usr / src / sys / netns / ns_pcb.c
index d3d026e..7c3f7f0 100644 (file)
@@ -1,9 +1,20 @@
 /*
 /*
- * Copyright (c) 1984, 1985 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California.
+ * All rights reserved.
  *
  *
- *     @(#)ns_pcb.c    6.8 (Berkeley) %G%
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ *     @(#)ns_pcb.c    7.5 (Berkeley) 6/29/88
  */
 
 #include "param.h"
  */
 
 #include "param.h"
@@ -103,69 +114,56 @@ ns_pcbconnect(nsp, nam)
                return (EAFNOSUPPORT);
        if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
                return (EADDRNOTAVAIL);
                return (EAFNOSUPPORT);
        if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
                return (EADDRNOTAVAIL);
-       if (ns_nullhost(nsp->nsp_laddr) &&
-           (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))) {
+       if (ns_nullhost(nsp->nsp_laddr)) {
                register struct route *ro;
                struct ifnet *ifp;
                register struct route *ro;
                struct ifnet *ifp;
+               /* 
+                * If route is known or can be allocated now,
+                * our src addr is taken from the i/f, else punt.
+                */
                ro = &nsp->nsp_route;
                dst = &satons_addr(ro->ro_dst);
 
                ro = &nsp->nsp_route;
                dst = &satons_addr(ro->ro_dst);
 
-               ia = ns_iaonnetof(&sns->sns_addr);
-               if (ia == 0 ||
-                       (ia->ia_ifp->if_flags & IFF_UP) == 0) {
-                       /* 
-                        * If route is known or can be allocated now,
-                        * our src addr is taken from the i/f, else punt.
-                        */
-                       if (ro->ro_rt &&
-                               !ns_hosteq(*dst, sns->sns_addr)) {
+               ia = (struct ns_ifaddr *)0;
+               if (ro->ro_rt) {
+                   if ((!ns_neteq(nsp->nsp_lastdst, sns->sns_addr)) ||
+                       ((ifp = ro->ro_rt->rt_ifp) &&
+                        (ifp->if_flags & IFF_POINTOPOINT) &&
+                        (!ns_hosteq(nsp->nsp_lastdst, sns->sns_addr))) ||
+                       (nsp->nsp_socket->so_options & SO_DONTROUTE)) {
                                RTFREE(ro->ro_rt);
                                ro->ro_rt = (struct rtentry *)0;
                        }
                                RTFREE(ro->ro_rt);
                                ro->ro_rt = (struct rtentry *)0;
                        }
-                       if ((ro->ro_rt == (struct rtentry *)0) ||
-                           (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
-                               /* No route yet, so try to acquire one */
-                               ro->ro_dst.sa_family = AF_NS;
-                               *dst = sns->sns_addr;
-                               dst->x_port = 0;
-                               rtalloc(ro);
-                               if (ro->ro_rt == 0)
-                                       ifp = (struct ifnet *)0;
-                               else
-                                       ifp = ro->ro_rt->rt_ifp;
-                       }
-                       if (ifp) {
-                               for (ia = ns_ifaddr; ia; ia = ia->ia_next)
-                                       if (ia->ia_ifp == ifp)
-                                           break;
-                       }
+               }
+               if ((nsp->nsp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
+                   (ro->ro_rt == (struct rtentry *)0 ||
+                    ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
+                           /* No route yet, so try to acquire one */
+                           ro->ro_dst.sa_family = AF_NS;
+                           *dst = sns->sns_addr;
+                           dst->x_port = 0;
+                           rtalloc(ro);
+               }
+               /*
+                * If we found a route, use the address
+                * corresponding to the outgoing interface
+                */
+               if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp))
+                       for (ia = ns_ifaddr; ia; ia = ia->ia_next)
+                               if (ia->ia_ifp == ifp)
+                                       break;
+               if (ia == 0) {
+                       u_short fport = sns->sns_addr.x_port;
+                       sns->sns_addr.x_port = 0;
+                       ia = (struct ns_ifaddr *)
+                               ifa_ifwithdstaddr((struct sockaddr *)sns);
+                       sns->sns_addr.x_port = fport;
+                       if (ia == 0)
+                               ia = ns_iaonnetof(&sns->sns_addr);
                        if (ia == 0)
                                ia = ns_ifaddr;
                        if (ia == 0)
                                return (EADDRNOTAVAIL);
                        if (ia == 0)
                                ia = ns_ifaddr;
                        if (ia == 0)
                                return (EADDRNOTAVAIL);
-               } else if (ro->ro_rt) {
-                       if (ns_neteq(*dst, sns->sns_addr)) {
-                               /*
-                                * This assume that we have no GH
-                                * type routes.
-                                */
-                               if (ro->ro_rt->rt_flags & RTF_HOST) {
-                                       if (!ns_hosteq(*dst, sns->sns_addr))
-                                               goto re_route;
-
-                               }
-                               if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
-                                       dst->x_host = sns->sns_addr.x_host;
-                               }
-                               /* 
-                                * Otherwise, we go through the same gateway
-                                * and dst is already set up.
-                                */
-                       } else {
-                       re_route:
-                               RTFREE(ro->ro_rt);
-                               ro->ro_rt = (struct rtentry *)0;
-                       }
                }
                nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
                nsp->nsp_lastdst = sns->sns_addr;
                }
                nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
                nsp->nsp_lastdst = sns->sns_addr;