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