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