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

  1. /*
  2.  * Copyright (c) 1983 The 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.  * static char sccsid[] = "@(#)if.c    5.1 (Berkeley) 6/4/85"; (routed/if.c)
  34.  */
  35.  
  36. #ifndef lint
  37. static char sccsid[] = "@(#)if.c    5.2 (Berkeley) 6/1/90";
  38. #endif /* not lint */
  39.  
  40. /*
  41.  * Routing Table Management Daemon
  42.  */
  43. #include "defs.h"
  44.  
  45. extern    struct interface *ifnet;
  46.  
  47. /*
  48.  * Find the interface with address addr.
  49.  */
  50. struct interface *
  51. if_ifwithaddr(addr)
  52.     struct sockaddr *addr;
  53. {
  54.     register struct interface *ifp;
  55.  
  56. #define    same(a1, a2) \
  57.     (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0)
  58.     for (ifp = ifnet; ifp; ifp = ifp->int_next) {
  59.         if (ifp->int_flags & IFF_REMOTE)
  60.             continue;
  61.         if (ifp->int_addr.sa_family != addr->sa_family)
  62.             continue;
  63.         if (same(&ifp->int_addr, addr))
  64.             break;
  65.         if ((ifp->int_flags & IFF_BROADCAST) &&
  66.             same(&ifp->int_broadaddr, addr))
  67.             break;
  68.     }
  69.     return (ifp);
  70. }
  71.  
  72. /*
  73.  * Find the point-to-point interface with destination address addr.
  74.  */
  75. struct interface *
  76. if_ifwithdstaddr(addr)
  77.     struct sockaddr *addr;
  78. {
  79.     register struct interface *ifp;
  80.  
  81.     for (ifp = ifnet; ifp; ifp = ifp->int_next) {
  82.         if ((ifp->int_flags & IFF_POINTOPOINT) == 0)
  83.             continue;
  84.         if (same(&ifp->int_dstaddr, addr))
  85.             break;
  86.     }
  87.     return (ifp);
  88. }
  89.  
  90. /*
  91.  * Find the interface on the network 
  92.  * of the specified address.
  93.  */
  94. struct interface *
  95. if_ifwithnet(addr)
  96.     register struct sockaddr *addr;
  97. {
  98.     register struct interface *ifp;
  99.     register int af = addr->sa_family;
  100.     register int (*netmatch)();
  101.  
  102.     if (af >= AF_MAX)
  103.         return (0);
  104.     netmatch = afswitch[af].af_netmatch;
  105.     for (ifp = ifnet; ifp; ifp = ifp->int_next) {
  106.         if (ifp->int_flags & IFF_REMOTE)
  107.             continue;
  108.         if (af != ifp->int_addr.sa_family)
  109.             continue;
  110.         if ((*netmatch)(addr, &ifp->int_addr))
  111.             break;
  112.     }
  113.     return (ifp);
  114. }
  115.  
  116. /*
  117.  * Find an interface from which the specified address
  118.  * should have come from.  Used for figuring out which
  119.  * interface a packet came in on -- for tracing.
  120.  */
  121. struct interface *
  122. if_iflookup(addr)
  123.     struct sockaddr *addr;
  124. {
  125.     register struct interface *ifp, *maybe;
  126.     register int af = addr->sa_family;
  127.     register int (*netmatch)();
  128.  
  129.     if (af >= AF_MAX)
  130.         return (0);
  131.     maybe = 0;
  132.     netmatch = afswitch[af].af_netmatch;
  133.     for (ifp = ifnet; ifp; ifp = ifp->int_next) {
  134.         if (ifp->int_addr.sa_family != af)
  135.             continue;
  136.         if (same(&ifp->int_addr, addr))
  137.             break;
  138.         if ((ifp->int_flags & IFF_BROADCAST) &&
  139.             same(&ifp->int_broadaddr, addr))
  140.             break;
  141.         if (maybe == 0 && (*netmatch)(addr, &ifp->int_addr))
  142.             maybe = ifp;
  143.     }
  144.     if (ifp == 0)
  145.         ifp = maybe;
  146.     return (ifp);
  147. }
  148.