2 * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
7 * Copyright (c) 2000 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Frank van der Linden.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/queue.h>
39 #include <netinet/in.h>
48 #include <netconfig.h>
50 #include <arpa/inet.h>
54 static struct sockaddr_in *local_in4;
56 static struct sockaddr_in6 *local_in6;
59 static int bitmaskcmp(void *, void *, void *, int);
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
67 bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
70 u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
72 for (i = 0; i < bytelen; i++)
73 if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
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).
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.
89 addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
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;
102 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
103 clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
105 caller_sa = caller->buf;
106 if ((nconf = rpcbind_get_conf(netid)) == NULL)
108 if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
112 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
113 * address family is different from that of the caller.
116 if (clnt_uaddr != NULL) {
117 hint_uaddr = clnt_uaddr;
118 if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
120 hint_sa = hint_nbp->buf;
122 if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
123 hint_uaddr = caller_uaddr;
124 hint_sa = caller->buf;
129 fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
131 /* Local caller, just return the server address. */
132 if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
133 strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
134 ret = strdup(serv_uaddr);
138 if (getifaddrs(&ifp) < 0)
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
151 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
152 ifsa = ifap->ifa_addr;
153 ifmasksa = ifap->ifa_netmask;
155 if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
156 !(ifap->ifa_flags & IFF_UP))
159 if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
162 switch (hint_sa->sa_family) {
165 * If the hint address matches this interface
166 * address/netmask, then we're done.
168 if (!bitmaskcmp(&SA2SINADDR(ifsa),
169 &SA2SINADDR(hint_sa), &SA2SINADDR(ifmasksa),
170 sizeof(struct in_addr))) {
178 * For v6 link local addresses, if the caller is on
179 * a link-local address then use the scope id to see
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) {
190 } else if (!bitmaskcmp(&SA2SIN6ADDR(ifsa),
191 &SA2SIN6ADDR(hint_sa), &SA2SIN6ADDR(ifmasksa),
192 sizeof(struct in6_addr))) {
203 * Remember the first possibly useful interface, preferring
204 * "normal" to point-to-point and loopback ones.
206 if (bestif == NULL ||
207 (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) &&
208 (bestif->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))))
216 * Construct the new address using the address from
217 * `bestif', and the port number from `serv_uaddr'.
219 serv_nbp = uaddr2taddr(nconf, serv_uaddr);
220 if (serv_nbp == NULL)
222 serv_sa = serv_nbp->buf;
224 memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
225 switch (ss.ss_family) {
227 SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
231 SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
235 tbuf.len = ss.ss_len;
236 tbuf.maxlen = sizeof(ss);
238 ret = taddr2uaddr(nconf, &tbuf);
241 if (caller_uaddr != NULL)
243 if (hint_nbp != NULL) {
247 if (serv_nbp != NULL) {
256 fprintf(stderr, "addrmerge: returning %s\n", ret);
265 struct ifaddrs *ifap, *ifp;
266 struct ipv6_mreq mreq6;
267 unsigned int ifindex;
271 struct addrinfo hints, *res;
273 memset(&hints, 0, sizeof hints);
274 hints.ai_family = AF_INET;
275 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
277 fprintf(stderr, "can't get local ip4 address: %s\n",
278 gai_strerror(ecode));
280 local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
281 if (local_in4 == NULL) {
283 fprintf(stderr, "can't alloc local ip4 addr\n");
285 memcpy(local_in4, res->ai_addr, sizeof *local_in4);
289 hints.ai_family = AF_INET6;
290 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
292 fprintf(stderr, "can't get local ip6 address: %s\n",
293 gai_strerror(ecode));
295 local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
296 if (local_in6 == NULL) {
298 fprintf(stderr, "can't alloc local ip6 addr\n");
300 memcpy(local_in6, res->ai_addr, sizeof *local_in6);
304 * Now join the RPC ipv6 multicast group on all interfaces.
306 if (getifaddrs(&ifp) < 0)
309 mreq6.ipv6mr_interface = 0;
310 inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
312 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
315 * Loop through all interfaces. For each IPv6 multicast-capable
316 * interface, join the RPC multicast group on that interface.
318 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
319 if (ifap->ifa_addr->sa_family != AF_INET6 ||
320 !(ifap->ifa_flags & IFF_MULTICAST))
322 ifindex = if_nametoindex(ifap->ifa_name);
323 if (ifindex == mreq6.ipv6mr_interface)
325 * Already did this one.
328 mreq6.ipv6mr_interface = ifindex;
329 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
332 perror("setsockopt v6 multicast");
344 return (struct sockaddr *)local_in4;
347 return (struct sockaddr *)local_in6;