121993Sdist /* 221993Sdist * Copyright (c) 1983 Regents of the University of California. 3*33489Sbostic * All rights reserved. 4*33489Sbostic * 5*33489Sbostic * Redistribution and use in source and binary forms are permitted 6*33489Sbostic * provided that this notice is preserved and that due credit is given 7*33489Sbostic * to the University of California at Berkeley. The name of the University 8*33489Sbostic * may not be used to endorse or promote products derived from this 9*33489Sbostic * software without specific prior written permission. This software 10*33489Sbostic * is provided ``as is'' without express or implied warranty. 1121993Sdist */ 1221993Sdist 139015Ssam #ifndef lint 14*33489Sbostic static char sccsid[] = "@(#)if.c 5.4 (Berkeley) 02/16/88"; 15*33489Sbostic #endif /* not lint */ 169015Ssam 179015Ssam /* 189015Ssam * Routing Table Management Daemon 199015Ssam */ 2010245Ssam #include "defs.h" 219015Ssam 229015Ssam extern struct interface *ifnet; 239015Ssam 249015Ssam /* 2517569Skarels * Find the interface with address addr. 269015Ssam */ 279015Ssam struct interface * 289015Ssam if_ifwithaddr(addr) 299015Ssam struct sockaddr *addr; 309015Ssam { 319015Ssam register struct interface *ifp; 329015Ssam 339015Ssam #define same(a1, a2) \ 349015Ssam (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0) 359015Ssam for (ifp = ifnet; ifp; ifp = ifp->int_next) { 369015Ssam if (ifp->int_flags & IFF_REMOTE) 379015Ssam continue; 389015Ssam if (ifp->int_addr.sa_family != addr->sa_family) 399015Ssam continue; 409015Ssam if (same(&ifp->int_addr, addr)) 419015Ssam break; 429015Ssam if ((ifp->int_flags & IFF_BROADCAST) && 439015Ssam same(&ifp->int_broadaddr, addr)) 449015Ssam break; 459015Ssam } 469015Ssam return (ifp); 479015Ssam } 489015Ssam 499015Ssam /* 5017569Skarels * Find the point-to-point interface with destination address addr. 5117569Skarels */ 5217569Skarels struct interface * 5317569Skarels if_ifwithdstaddr(addr) 5417569Skarels struct sockaddr *addr; 5517569Skarels { 5617569Skarels register struct interface *ifp; 5717569Skarels 5817569Skarels for (ifp = ifnet; ifp; ifp = ifp->int_next) { 5917569Skarels if ((ifp->int_flags & IFF_POINTOPOINT) == 0) 6017569Skarels continue; 6117569Skarels if (same(&ifp->int_dstaddr, addr)) 6217569Skarels break; 6317569Skarels } 6417569Skarels return (ifp); 6517569Skarels } 6617569Skarels 6717569Skarels /* 689015Ssam * Find the interface on the network 699015Ssam * of the specified address. 709015Ssam */ 719015Ssam struct interface * 729015Ssam if_ifwithnet(addr) 739015Ssam register struct sockaddr *addr; 749015Ssam { 759015Ssam register struct interface *ifp; 769015Ssam register int af = addr->sa_family; 779015Ssam register int (*netmatch)(); 789015Ssam 7926340Skarels if (af >= af_max) 809015Ssam return (0); 819015Ssam netmatch = afswitch[af].af_netmatch; 829015Ssam for (ifp = ifnet; ifp; ifp = ifp->int_next) { 839015Ssam if (ifp->int_flags & IFF_REMOTE) 849015Ssam continue; 859015Ssam if (af != ifp->int_addr.sa_family) 869015Ssam continue; 879015Ssam if ((*netmatch)(addr, &ifp->int_addr)) 889015Ssam break; 899015Ssam } 909015Ssam return (ifp); 919015Ssam } 929015Ssam 939015Ssam /* 949015Ssam * Find an interface from which the specified address 959015Ssam * should have come from. Used for figuring out which 969015Ssam * interface a packet came in on -- for tracing. 979015Ssam */ 989015Ssam struct interface * 999015Ssam if_iflookup(addr) 1009015Ssam struct sockaddr *addr; 1019015Ssam { 1029015Ssam register struct interface *ifp, *maybe; 1039015Ssam register int af = addr->sa_family; 1049015Ssam register int (*netmatch)(); 1059015Ssam 10626340Skarels if (af >= af_max) 1079015Ssam return (0); 1089015Ssam maybe = 0; 1099015Ssam netmatch = afswitch[af].af_netmatch; 1109015Ssam for (ifp = ifnet; ifp; ifp = ifp->int_next) { 1119015Ssam if (ifp->int_addr.sa_family != af) 1129015Ssam continue; 1139015Ssam if (same(&ifp->int_addr, addr)) 1149015Ssam break; 1159015Ssam if ((ifp->int_flags & IFF_BROADCAST) && 1169015Ssam same(&ifp->int_broadaddr, addr)) 1179015Ssam break; 11827231Skarels if ((ifp->int_flags & IFF_POINTOPOINT) && 11927231Skarels same(&ifp->int_dstaddr, addr)) 12027231Skarels break; 1219015Ssam if (maybe == 0 && (*netmatch)(addr, &ifp->int_addr)) 1229015Ssam maybe = ifp; 1239015Ssam } 1249015Ssam if (ifp == 0) 1259015Ssam ifp = maybe; 1269015Ssam return (ifp); 1279015Ssam } 128