Commit | Line | Data |
---|---|---|
5ff67f98 DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
0eb85d71 KB |
3 | * All rights reserved. |
4 | * | |
d60d530a | 5 | * %sccs.include.redist.c% |
5ff67f98 DF |
6 | */ |
7 | ||
7ab5124a | 8 | #ifndef lint |
d60d530a | 9 | static char sccsid[] = "@(#)if.c 5.6 (Berkeley) %G%"; |
0eb85d71 | 10 | #endif /* not lint */ |
7ab5124a SL |
11 | |
12 | /* | |
13 | * Routing Table Management Daemon | |
14 | */ | |
7fe7fe74 | 15 | #include "defs.h" |
7ab5124a SL |
16 | |
17 | extern struct interface *ifnet; | |
18 | ||
19 | /* | |
2d55efad | 20 | * Find the interface with address addr. |
7ab5124a SL |
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 | (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((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 | ||
2d55efad MK |
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 | ||
7ab5124a SL |
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 | ||
17fe297f | 74 | if (af >= af_max) |
7ab5124a SL |
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 | ||
17fe297f | 101 | if (af >= af_max) |
7ab5124a SL |
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; | |
4fad5a6e MK |
113 | if ((ifp->int_flags & IFF_POINTOPOINT) && |
114 | same(&ifp->int_dstaddr, addr)) | |
115 | break; | |
7ab5124a SL |
116 | if (maybe == 0 && (*netmatch)(addr, &ifp->int_addr)) |
117 | maybe = ifp; | |
118 | } | |
119 | if (ifp == 0) | |
120 | ifp = maybe; | |
121 | return (ifp); | |
122 | } |