date and time created 89/04/02 14:46:44 by bostic
[unix-history] / usr / src / sbin / routed / if.c
CommitLineData
5ff67f98
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
0eb85d71
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
b8c620d6
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
5ff67f98
DF
16 */
17
7ab5124a 18#ifndef lint
b8c620d6 19static char sccsid[] = "@(#)if.c 5.5 (Berkeley) %G%";
0eb85d71 20#endif /* not lint */
7ab5124a
SL
21
22/*
23 * Routing Table Management Daemon
24 */
7fe7fe74 25#include "defs.h"
7ab5124a
SL
26
27extern struct interface *ifnet;
28
29/*
2d55efad 30 * Find the interface with address addr.
7ab5124a
SL
31 */
32struct interface *
33if_ifwithaddr(addr)
34 struct sockaddr *addr;
35{
36 register struct interface *ifp;
37
38#define same(a1, a2) \
39 (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0)
40 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
41 if (ifp->int_flags & IFF_REMOTE)
42 continue;
43 if (ifp->int_addr.sa_family != addr->sa_family)
44 continue;
45 if (same(&ifp->int_addr, addr))
46 break;
47 if ((ifp->int_flags & IFF_BROADCAST) &&
48 same(&ifp->int_broadaddr, addr))
49 break;
50 }
51 return (ifp);
52}
53
2d55efad
MK
54/*
55 * Find the point-to-point interface with destination address addr.
56 */
57struct interface *
58if_ifwithdstaddr(addr)
59 struct sockaddr *addr;
60{
61 register struct interface *ifp;
62
63 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
64 if ((ifp->int_flags & IFF_POINTOPOINT) == 0)
65 continue;
66 if (same(&ifp->int_dstaddr, addr))
67 break;
68 }
69 return (ifp);
70}
71
7ab5124a
SL
72/*
73 * Find the interface on the network
74 * of the specified address.
75 */
76struct interface *
77if_ifwithnet(addr)
78 register struct sockaddr *addr;
79{
80 register struct interface *ifp;
81 register int af = addr->sa_family;
82 register int (*netmatch)();
83
17fe297f 84 if (af >= af_max)
7ab5124a
SL
85 return (0);
86 netmatch = afswitch[af].af_netmatch;
87 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
88 if (ifp->int_flags & IFF_REMOTE)
89 continue;
90 if (af != ifp->int_addr.sa_family)
91 continue;
92 if ((*netmatch)(addr, &ifp->int_addr))
93 break;
94 }
95 return (ifp);
96}
97
98/*
99 * Find an interface from which the specified address
100 * should have come from. Used for figuring out which
101 * interface a packet came in on -- for tracing.
102 */
103struct interface *
104if_iflookup(addr)
105 struct sockaddr *addr;
106{
107 register struct interface *ifp, *maybe;
108 register int af = addr->sa_family;
109 register int (*netmatch)();
110
17fe297f 111 if (af >= af_max)
7ab5124a
SL
112 return (0);
113 maybe = 0;
114 netmatch = afswitch[af].af_netmatch;
115 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
116 if (ifp->int_addr.sa_family != af)
117 continue;
118 if (same(&ifp->int_addr, addr))
119 break;
120 if ((ifp->int_flags & IFF_BROADCAST) &&
121 same(&ifp->int_broadaddr, addr))
122 break;
4fad5a6e
MK
123 if ((ifp->int_flags & IFF_POINTOPOINT) &&
124 same(&ifp->int_dstaddr, addr))
125 break;
7ab5124a
SL
126 if (maybe == 0 && (*netmatch)(addr, &ifp->int_addr))
127 maybe = ifp;
128 }
129 if (ifp == 0)
130 ifp = maybe;
131 return (ifp);
132}