Deleted Added
sdiff udiff text old ( 243187 ) new ( 293229 )
full compact
1/*
2 * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
3 * $FreeBSD: head/usr.sbin/rpcbind/util.c 243187 2012-11-17 20:19:00Z hrs $
4 */
5
6/*-
7 * Copyright (c) 2000 The NetBSD Foundation, Inc.
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Frank van der Linden.

--- 39 unchanged lines hidden (view full) ---

51
52#include "rpcbind.h"
53
54static struct sockaddr_in *local_in4;
55#ifdef INET6
56static struct sockaddr_in6 *local_in6;
57#endif
58
59static int bitmaskcmp(void *, void *, void *, int);
60
61/*
62 * For all bits set in "mask", compare the corresponding bits in
63 * "dst" and "src", and see if they match. Returns 0 if the addresses
64 * match.
65 */
66static int
67bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
68{
69 int i;
70 u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
71
72 for (i = 0; i < bytelen; i++)
73 if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
74 return (1);
75 return (0);
76}
77
78/*
79 * Find a server address that can be used by `caller' to contact
80 * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
81 * non-NULL, it is used instead of `caller' as a hint suggesting
82 * the best address (e.g. the `r_addr' field of an rpc, which
83 * contains the rpcbind server address that the caller used).
84 *
85 * Returns the best server address as a malloc'd "universal address"
86 * string which should be freed by the caller. On error, returns NULL.
87 */
88char *
89addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
90 char *netid)
91{
92 struct ifaddrs *ifap, *ifp = NULL, *bestif;
93 struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
94 struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
95 struct sockaddr_storage ss;
96 struct netconfig *nconf;
97 char *caller_uaddr = NULL, *hint_uaddr = NULL;
98 char *ret = NULL;
99
100#ifdef ND_DEBUG
101 if (debugging)
102 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
103 clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
104#endif
105 caller_sa = caller->buf;
106 if ((nconf = rpcbind_get_conf(netid)) == NULL)

--- 27 unchanged lines hidden (view full) ---

134 ret = strdup(serv_uaddr);
135 goto freeit;
136 }
137
138 if (getifaddrs(&ifp) < 0)
139 goto freeit;
140
141 /*
142 * Loop through all interfaces. For each interface, see if it
143 * is either the loopback interface (which we always listen
144 * on) or is one of the addresses the program bound to (the
145 * wildcard by default, or a subset if -h is specified) and
146 * the network portion of its address is equal to that of the
147 * client. If so, we have found the interface that we want to
148 * use.
149 */
150 bestif = NULL;
151 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
152 ifsa = ifap->ifa_addr;
153 ifmasksa = ifap->ifa_netmask;
154
155 if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
156 !(ifap->ifa_flags & IFF_UP))
157 continue;
158
159 if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
160 continue;
161
162 switch (hint_sa->sa_family) {
163 case AF_INET:
164 /*
165 * If the hint address matches this interface
166 * address/netmask, then we're done.
167 */
168 if (!bitmaskcmp(&SA2SINADDR(ifsa),
169 &SA2SINADDR(hint_sa), &SA2SINADDR(ifmasksa),
170 sizeof(struct in_addr))) {
171 bestif = ifap;
172 goto found;
173 }
174 break;
175#ifdef INET6
176 case AF_INET6:
177 /*
178 * For v6 link local addresses, if the caller is on
179 * a link-local address then use the scope id to see
180 * which one.
181 */
182 if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
183 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
184 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
185 if (SA2SIN6(ifsa)->sin6_scope_id ==
186 SA2SIN6(caller_sa)->sin6_scope_id) {
187 bestif = ifap;
188 goto found;
189 }
190 } else if (!bitmaskcmp(&SA2SIN6ADDR(ifsa),
191 &SA2SIN6ADDR(hint_sa), &SA2SIN6ADDR(ifmasksa),
192 sizeof(struct in6_addr))) {
193 bestif = ifap;
194 goto found;
195 }
196 break;
197#endif
198 default:
199 continue;
200 }
201
202 /*
203 * Remember the first possibly useful interface, preferring
204 * "normal" to point-to-point and loopback ones.
205 */
206 if (bestif == NULL ||
207 (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) &&
208 (bestif->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))))
209 bestif = ifap;
210 }
211 if (bestif == NULL)
212 goto freeit;
213
214found:
215 /*
216 * Construct the new address using the address from

--- 136 unchanged lines hidden ---