home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sbin / routed / inet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-16  |  5.9 KB  |  215 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)inet.c    5.8 (Berkeley) 6/1/90";
  36. #endif /* not lint */
  37.  
  38. /*
  39.  * Temporarily, copy these routines from the kernel,
  40.  * as we need to know about subnets.
  41.  */
  42. #include "defs.h"
  43.  
  44. extern struct interface *ifnet;
  45.  
  46. /*
  47.  * Formulate an Internet address from network + host.
  48.  */
  49. struct in_addr
  50. inet_makeaddr(net, host)
  51.     u_long net, host;
  52. {
  53.     register struct interface *ifp;
  54.     register u_long mask;
  55.     u_long addr;
  56.  
  57.     if (IN_CLASSA(net))
  58.         mask = IN_CLASSA_HOST;
  59.     else if (IN_CLASSB(net))
  60.         mask = IN_CLASSB_HOST;
  61.     else
  62.         mask = IN_CLASSC_HOST;
  63.     for (ifp = ifnet; ifp; ifp = ifp->int_next)
  64.         if ((ifp->int_netmask & net) == ifp->int_net) {
  65.             mask = ~ifp->int_subnetmask;
  66.             break;
  67.         }
  68.     addr = net | (host & mask);
  69.     addr = htonl(addr);
  70.     return (*(struct in_addr *)&addr);
  71. }
  72.  
  73. /*
  74.  * Return the network number from an internet address.
  75.  */
  76. inet_netof(in)
  77.     struct in_addr in;
  78. {
  79.     register u_long i = ntohl(in.s_addr);
  80.     register u_long net;
  81.     register struct interface *ifp;
  82.  
  83.     if (IN_CLASSA(i))
  84.         net = i & IN_CLASSA_NET;
  85.     else if (IN_CLASSB(i))
  86.         net = i & IN_CLASSB_NET;
  87.     else
  88.         net = i & IN_CLASSC_NET;
  89.  
  90.     /*
  91.      * Check whether network is a subnet;
  92.      * if so, return subnet number.
  93.      */
  94.     for (ifp = ifnet; ifp; ifp = ifp->int_next)
  95.         if ((ifp->int_netmask & net) == ifp->int_net)
  96.             return (i & ifp->int_subnetmask);
  97.     return (net);
  98. }
  99.  
  100. /*
  101.  * Return the host portion of an internet address.
  102.  */
  103. inet_lnaof(in)
  104.     struct in_addr in;
  105. {
  106.     register u_long i = ntohl(in.s_addr);
  107.     register u_long net, host;
  108.     register struct interface *ifp;
  109.  
  110.     if (IN_CLASSA(i)) {
  111.         net = i & IN_CLASSA_NET;
  112.         host = i & IN_CLASSA_HOST;
  113.     } else if (IN_CLASSB(i)) {
  114.         net = i & IN_CLASSB_NET;
  115.         host = i & IN_CLASSB_HOST;
  116.     } else {
  117.         net = i & IN_CLASSC_NET;
  118.         host = i & IN_CLASSC_HOST;
  119.     }
  120.  
  121.     /*
  122.      * Check whether network is a subnet;
  123.      * if so, use the modified interpretation of `host'.
  124.      */
  125.     for (ifp = ifnet; ifp; ifp = ifp->int_next)
  126.         if ((ifp->int_netmask & net) == ifp->int_net)
  127.             return (host &~ ifp->int_subnetmask);
  128.     return (host);
  129. }
  130.  
  131. /*
  132.  * Return RTF_HOST if the address is
  133.  * for an Internet host, RTF_SUBNET for a subnet,
  134.  * 0 for a network.
  135.  */
  136. inet_rtflags(sin)
  137.     struct sockaddr_in *sin;
  138. {
  139.     register u_long i = ntohl(sin->sin_addr.s_addr);
  140.     register u_long net, host;
  141.     register struct interface *ifp;
  142.  
  143.     if (IN_CLASSA(i)) {
  144.         net = i & IN_CLASSA_NET;
  145.         host = i & IN_CLASSA_HOST;
  146.     } else if (IN_CLASSB(i)) {
  147.         net = i & IN_CLASSB_NET;
  148.         host = i & IN_CLASSB_HOST;
  149.     } else {
  150.         net = i & IN_CLASSC_NET;
  151.         host = i & IN_CLASSC_HOST;
  152.     }
  153.  
  154.     /*
  155.      * Check whether this network is subnetted;
  156.      * if so, check whether this is a subnet or a host.
  157.      */
  158.     for (ifp = ifnet; ifp; ifp = ifp->int_next)
  159.         if (net == ifp->int_net) {
  160.             if (host &~ ifp->int_subnetmask)
  161.                 return (RTF_HOST);
  162.             else if (ifp->int_subnetmask != ifp->int_netmask)
  163.                 return (RTF_SUBNET);
  164.             else
  165.                 return (0);        /* network */
  166.         }
  167.     if (host == 0)
  168.         return (0);    /* network */
  169.     else
  170.         return (RTF_HOST);
  171. }
  172.  
  173. /*
  174.  * Return true if a route to subnet/host of route rt should be sent to dst.
  175.  * Send it only if dst is on the same logical network if not "internal",
  176.  * otherwise only if the route is the "internal" route for the logical net.
  177.  */
  178. inet_sendroute(rt, dst)
  179.     struct rt_entry *rt;
  180.     struct sockaddr_in *dst;
  181. {
  182.     register u_long r =
  183.         ntohl(((struct sockaddr_in *)&rt->rt_dst)->sin_addr.s_addr);
  184.     register u_long d = ntohl(dst->sin_addr.s_addr);
  185.  
  186.     if (IN_CLASSA(r)) {
  187.         if ((r & IN_CLASSA_NET) == (d & IN_CLASSA_NET)) {
  188.             if ((r & IN_CLASSA_HOST) == 0)
  189.                 return ((rt->rt_state & RTS_INTERNAL) == 0);
  190.             return (1);
  191.         }
  192.         if (r & IN_CLASSA_HOST)
  193.             return (0);
  194.         return ((rt->rt_state & RTS_INTERNAL) != 0);
  195.     } else if (IN_CLASSB(r)) {
  196.         if ((r & IN_CLASSB_NET) == (d & IN_CLASSB_NET)) {
  197.             if ((r & IN_CLASSB_HOST) == 0)
  198.                 return ((rt->rt_state & RTS_INTERNAL) == 0);
  199.             return (1);
  200.         }
  201.         if (r & IN_CLASSB_HOST)
  202.             return (0);
  203.         return ((rt->rt_state & RTS_INTERNAL) != 0);
  204.     } else {
  205.         if ((r & IN_CLASSC_NET) == (d & IN_CLASSC_NET)) {
  206.             if ((r & IN_CLASSC_HOST) == 0)
  207.                 return ((rt->rt_state & RTS_INTERNAL) == 0);
  208.             return (1);
  209.         }
  210.         if (r & IN_CLASSC_HOST)
  211.             return (0);
  212.         return ((rt->rt_state & RTS_INTERNAL) != 0);
  213.     }
  214. }
  215.