if.c revision 22997
133965Sjdp/* 233965Sjdp * Copyright (c) 1983, 1993 333965Sjdp * The Regents of the University of California. All rights reserved. 433965Sjdp * 533965Sjdp * Copyright (c) 1995 John Hay. All rights reserved. 633965Sjdp * 733965Sjdp * Redistribution and use in source and binary forms, with or without 833965Sjdp * modification, are permitted provided that the following conditions 933965Sjdp * are met: 1033965Sjdp * 1. Redistributions of source code must retain the above copyright 1133965Sjdp * notice, this list of conditions and the following disclaimer. 1233965Sjdp * 2. Redistributions in binary form must reproduce the above copyright 1333965Sjdp * notice, this list of conditions and the following disclaimer in the 1433965Sjdp * documentation and/or other materials provided with the distribution. 1533965Sjdp * 3. All advertising materials mentioning features or use of this software 1633965Sjdp * must display the following acknowledgement: 1733965Sjdp * This product includes software developed by the University of 1833965Sjdp * California, Berkeley and its contributors. 1933965Sjdp * 4. Neither the name of the University nor the names of its contributors 2033965Sjdp * may be used to endorse or promote products derived from this software 2133965Sjdp * without specific prior written permission. 2233965Sjdp * 2333965Sjdp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2433965Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2533965Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2633965Sjdp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2733965Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2833965Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2933965Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3033965Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3133965Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3233965Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3333965Sjdp * SUCH DAMAGE. 3433965Sjdp * 3533965Sjdp * static char sccsid[] = "@(#)if.c 5.1 (Berkeley) 6/4/85"; (routed/if.c) 3633965Sjdp * 3733965Sjdp * $Id$ 3833965Sjdp */ 3933965Sjdp 4033965Sjdp#ifndef lint 4133965Sjdpstatic char sccsid[] = "@(#)if.c 8.1 (Berkeley) 6/5/93"; 4233965Sjdp#endif /* not lint */ 4333965Sjdp 4433965Sjdp/* 4533965Sjdp * Routing Table Management Daemon 4633965Sjdp */ 4733965Sjdp#include "defs.h" 4833965Sjdp 4933965Sjdpextern struct interface *ifnet; 5033965Sjdp 5133965Sjdp/* 5233965Sjdp * Find the interface with address addr. 53 */ 54struct interface * 55if_ifwithaddr(addr) 56 struct sockaddr *addr; 57{ 58 register struct interface *ifp; 59 60#define same(a1, a2) \ 61 (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0) 62 for (ifp = ifnet; ifp; ifp = ifp->int_next) { 63 if (ifp->int_flags & IFF_REMOTE) 64 continue; 65 if (ifp->int_addr.sa_family != addr->sa_family) 66 continue; 67 if (same(&ifp->int_addr, addr)) 68 break; 69 if ((ifp->int_flags & IFF_BROADCAST) && 70 same(&ifp->int_broadaddr, addr)) 71 break; 72 } 73 return (ifp); 74} 75 76/* 77 * Find the point-to-point interface with destination address addr. 78 */ 79struct interface * 80if_ifwithdstaddr(addr) 81 struct sockaddr *addr; 82{ 83 register struct interface *ifp; 84 85 for (ifp = ifnet; ifp; ifp = ifp->int_next) { 86 if ((ifp->int_flags & IFF_POINTOPOINT) == 0) 87 continue; 88 if (same(&ifp->int_dstaddr, addr)) 89 break; 90 } 91 return (ifp); 92} 93 94/* 95 * Find the interface on the network 96 * of the specified address. 97 */ 98struct interface * 99if_ifwithnet(addr) 100 register struct sockaddr *addr; 101{ 102 register struct interface *ifp; 103 register int af = addr->sa_family; 104 register int (*netmatch)(); 105 106 if (af >= AF_MAX) 107 return (0); 108 netmatch = afswitch[af].af_netmatch; 109 for (ifp = ifnet; ifp; ifp = ifp->int_next) { 110 if (ifp->int_flags & IFF_REMOTE) 111 continue; 112 if (af != ifp->int_addr.sa_family) 113 continue; 114 if ((*netmatch)(addr, &ifp->int_addr)) 115 break; 116 } 117 return (ifp); 118} 119 120/* 121 * Find an interface from which the specified address 122 * should have come from. Used for figuring out which 123 * interface a packet came in on -- for tracing. 124 */ 125struct interface * 126if_iflookup(addr) 127 struct sockaddr *addr; 128{ 129 register struct interface *ifp, *maybe; 130 register int af = addr->sa_family; 131 register int (*netmatch)(); 132 133 if (af >= AF_MAX) 134 return (0); 135 maybe = 0; 136 netmatch = afswitch[af].af_netmatch; 137 for (ifp = ifnet; ifp; ifp = ifp->int_next) { 138 if (ifp->int_addr.sa_family != af) 139 continue; 140 if (same(&ifp->int_addr, addr)) 141 break; 142 if ((ifp->int_flags & IFF_BROADCAST) && 143 same(&ifp->int_broadaddr, addr)) 144 break; 145 if (maybe == 0 && (*netmatch)(addr, &ifp->int_addr)) 146 maybe = ifp; 147 } 148 if (ifp == 0) 149 ifp = maybe; 150 return (ifp); 151} 152