1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)if.c 8.2 (Berkeley) 04/28/95"; 10 #endif /* not lint */ 11 12 /* 13 * Routing Table Management Daemon 14 */ 15 #include "defs.h" 16 17 extern struct interface *ifnet; 18 19 /* 20 * Find the interface with address addr. 21 */ 22 struct interface * 23 if_ifwithaddr(addr) 24 struct sockaddr *addr; 25 { 26 register struct interface *ifp; 27 28 #define same(a1, a2) \ 29 (memcmp((a1)->sa_data, (a2)->sa_data, 14) == 0) 30 for (ifp = ifnet; ifp; ifp = ifp->int_next) { 31 if (ifp->int_flags & IFF_REMOTE) 32 continue; 33 if (ifp->int_addr.sa_family != addr->sa_family) 34 continue; 35 if (same(&ifp->int_addr, addr)) 36 break; 37 if ((ifp->int_flags & IFF_BROADCAST) && 38 same(&ifp->int_broadaddr, addr)) 39 break; 40 } 41 return (ifp); 42 } 43 44 /* 45 * Find the point-to-point interface with destination address addr. 46 */ 47 struct interface * 48 if_ifwithdstaddr(addr) 49 struct sockaddr *addr; 50 { 51 register struct interface *ifp; 52 53 for (ifp = ifnet; ifp; ifp = ifp->int_next) { 54 if ((ifp->int_flags & IFF_POINTOPOINT) == 0) 55 continue; 56 if (same(&ifp->int_dstaddr, addr)) 57 break; 58 } 59 return (ifp); 60 } 61 62 /* 63 * Find the interface on the network 64 * of the specified address. 65 */ 66 struct interface * 67 if_ifwithnet(addr) 68 register struct sockaddr *addr; 69 { 70 register struct interface *ifp; 71 register int af = addr->sa_family; 72 register int (*netmatch)(); 73 74 if (af >= af_max) 75 return (0); 76 netmatch = afswitch[af].af_netmatch; 77 for (ifp = ifnet; ifp; ifp = ifp->int_next) { 78 if (ifp->int_flags & IFF_REMOTE) 79 continue; 80 if (af != ifp->int_addr.sa_family) 81 continue; 82 if ((*netmatch)(addr, &ifp->int_addr)) 83 break; 84 } 85 return (ifp); 86 } 87 88 /* 89 * Find an interface from which the specified address 90 * should have come from. Used for figuring out which 91 * interface a packet came in on -- for tracing. 92 */ 93 struct interface * 94 if_iflookup(addr) 95 struct sockaddr *addr; 96 { 97 register struct interface *ifp, *maybe; 98 register int af = addr->sa_family; 99 register int (*netmatch)(); 100 101 if (af >= af_max) 102 return (0); 103 maybe = 0; 104 netmatch = afswitch[af].af_netmatch; 105 for (ifp = ifnet; ifp; ifp = ifp->int_next) { 106 if (ifp->int_addr.sa_family != af) 107 continue; 108 if (same(&ifp->int_addr, addr)) 109 break; 110 if ((ifp->int_flags & IFF_BROADCAST) && 111 same(&ifp->int_broadaddr, addr)) 112 break; 113 if ((ifp->int_flags & IFF_POINTOPOINT) && 114 same(&ifp->int_dstaddr, addr)) 115 break; 116 if (maybe == 0 && (*netmatch)(addr, &ifp->int_addr)) 117 maybe = ifp; 118 } 119 if (ifp == 0) 120 ifp = maybe; 121 return (ifp); 122 } 123