home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / routed / inet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-17  |  6.0 KB  |  216 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[] = "from: @(#)inet.c    5.8 (Berkeley) 6/1/90";*/
  36. static char rcsid[] = "$Id: inet.c,v 1.4 1993/08/01 18:24:29 mycroft Exp $";
  37. #endif /* not lint */
  38.  
  39. /*
  40.  * Temporarily, copy these routines from the kernel,
  41.  * as we need to know about subnets.
  42.  */
  43. #include "defs.h"
  44.  
  45. extern struct interface *ifnet;
  46.  
  47. /*
  48.  * Formulate an Internet address from network + host.
  49.  */
  50. struct in_addr
  51. inet_makeaddr(net, host)
  52.     u_long net, host;
  53. {
  54.     register struct interface *ifp;
  55.     register u_long mask;
  56.     u_long addr;
  57.  
  58.     if (IN_CLASSA(net))
  59.         mask = IN_CLASSA_HOST;
  60.     else if (IN_CLASSB(net))
  61.         mask = IN_CLASSB_HOST;
  62.     else
  63.         mask = IN_CLASSC_HOST;
  64.     for (ifp = ifnet; ifp; ifp = ifp->int_next)
  65.         if ((ifp->int_netmask & net) == ifp->int_net) {
  66.             mask = ~ifp->int_subnetmask;
  67.             break;
  68.         }
  69.     addr = net | (host & mask);
  70.     addr = htonl(addr);
  71.     return (*(struct in_addr *)&addr);
  72. }
  73.  
  74. /*
  75.  * Return the network number from an internet address.
  76.  */
  77. inet_netof(in)
  78.     struct in_addr in;
  79. {
  80.     register u_long i = ntohl(in.s_addr);
  81.     register u_long net;
  82.     register struct interface *ifp;
  83.  
  84.     if (IN_CLASSA(i))
  85.         net = i & IN_CLASSA_NET;
  86.     else if (IN_CLASSB(i))
  87.         net = i & IN_CLASSB_NET;
  88.     else
  89.         net = i & IN_CLASSC_NET;
  90.  
  91.     /*
  92.      * Check whether network is a subnet;
  93.      * if so, return subnet number.
  94.      */
  95.     for (ifp = ifnet; ifp; ifp = ifp->int_next)
  96.         if ((ifp->int_netmask & net) == ifp->int_net)
  97.             return (i & ifp->int_subnetmask);
  98.     return (net);
  99. }
  100.  
  101. /*
  102.  * Return the host portion of an internet address.
  103.  */
  104. inet_lnaof(in)
  105.     struct in_addr in;
  106. {
  107.     register u_long i = ntohl(in.s_addr);
  108.     register u_long net, host;
  109.     register struct interface *ifp;
  110.  
  111.     if (IN_CLASSA(i)) {
  112.         net = i & IN_CLASSA_NET;
  113.         host = i & IN_CLASSA_HOST;
  114.     } else if (IN_CLASSB(i)) {
  115.         net = i & IN_CLASSB_NET;
  116.         host = i & IN_CLASSB_HOST;
  117.     } else {
  118.         net = i & IN_CLASSC_NET;
  119.         host = i & IN_CLASSC_HOST;
  120.     }
  121.  
  122.     /*
  123.      * Check whether network is a subnet;
  124.      * if so, use the modified interpretation of `host'.
  125.      */
  126.     for (ifp = ifnet; ifp; ifp = ifp->int_next)
  127.         if ((ifp->int_netmask & net) == ifp->int_net)
  128.             return (host &~ ifp->int_subnetmask);
  129.     return (host);
  130. }
  131.  
  132. /*
  133.  * Return RTF_HOST if the address is
  134.  * for an Internet host, RTF_SUBNET for a subnet,
  135.  * 0 for a network.
  136.  */
  137. inet_rtflags(sin)
  138.     struct sockaddr_in *sin;
  139. {
  140.     register u_long i = ntohl(sin->sin_addr.s_addr);
  141.     register u_long net, host;
  142.     register struct interface *ifp;
  143.  
  144.     if (IN_CLASSA(i)) {
  145.         net = i & IN_CLASSA_NET;
  146.         host = i & IN_CLASSA_HOST;
  147.     } else if (IN_CLASSB(i)) {
  148.         net = i & IN_CLASSB_NET;
  149.         host = i & IN_CLASSB_HOST;
  150.     } else {
  151.         net = i & IN_CLASSC_NET;
  152.         host = i & IN_CLASSC_HOST;
  153.     }
  154.  
  155.     /*
  156.      * Check whether this network is subnetted;
  157.      * if so, check whether this is a subnet or a host.
  158.      */
  159.     for (ifp = ifnet; ifp; ifp = ifp->int_next)
  160.         if (net == ifp->int_net) {
  161.             if (host &~ ifp->int_subnetmask)
  162.                 return (RTF_HOST);
  163.             else if (ifp->int_subnetmask != ifp->int_netmask)
  164.                 return (RTF_SUBNET);
  165.             else
  166.                 return (0);        /* network */
  167.         }
  168.     if (host == 0)
  169.         return (0);    /* network */
  170.     else
  171.         return (RTF_HOST);
  172. }
  173.  
  174. /*
  175.  * Return true if a route to subnet/host of route rt should be sent to dst.
  176.  * Send it only if dst is on the same logical network if not "internal",
  177.  * otherwise only if the route is the "internal" route for the logical net.
  178.  */
  179. inet_sendroute(rt, dst)
  180.     struct rt_entry *rt;
  181.     struct sockaddr_in *dst;
  182. {
  183.     register u_long r =
  184.         ntohl(((struct sockaddr_in *)&rt->rt_dst)->sin_addr.s_addr);
  185.     register u_long d = ntohl(dst->sin_addr.s_addr);
  186.  
  187.     if (IN_CLASSA(r)) {
  188.         if ((r & IN_CLASSA_NET) == (d & IN_CLASSA_NET)) {
  189.             if ((r & IN_CLASSA_HOST) == 0)
  190.                 return ((rt->rt_state & RTS_INTERNAL) == 0);
  191.             return (1);
  192.         }
  193.         if (r & IN_CLASSA_HOST)
  194.             return (0);
  195.         return ((rt->rt_state & RTS_INTERNAL) != 0);
  196.     } else if (IN_CLASSB(r)) {
  197.         if ((r & IN_CLASSB_NET) == (d & IN_CLASSB_NET)) {
  198.             if ((r & IN_CLASSB_HOST) == 0)
  199.                 return ((rt->rt_state & RTS_INTERNAL) == 0);
  200.             return (1);
  201.         }
  202.         if (r & IN_CLASSB_HOST)
  203.             return (0);
  204.         return ((rt->rt_state & RTS_INTERNAL) != 0);
  205.     } else {
  206.         if ((r & IN_CLASSC_NET) == (d & IN_CLASSC_NET)) {
  207.             if ((r & IN_CLASSC_HOST) == 0)
  208.                 return ((rt->rt_state & RTS_INTERNAL) == 0);
  209.             return (1);
  210.         }
  211.         if (r & IN_CLASSC_HOST)
  212.             return (0);
  213.         return ((rt->rt_state & RTS_INTERNAL) != 0);
  214.     }
  215. }
  216.