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.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/queue.h>
46 #include <netinet/in.h>
55 #include <netconfig.h>
57 #include <arpa/inet.h>
61 #define SA2SIN(sa) ((struct sockaddr_in *)(sa))
62 #define SA2SINADDR(sa) (SA2SIN(sa)->sin_addr)
64 #define SA2SIN6(sa) ((struct sockaddr_in6 *)(sa))
65 #define SA2SIN6ADDR(sa) (SA2SIN6(sa)->sin6_addr)
68 static struct sockaddr_in
*local_in4
;
70 static struct sockaddr_in6
*local_in6
;
73 static int bitmaskcmp(void *, void *, void *, int);
75 static void in6_fillscopeid(struct sockaddr_in6
*);
79 * For all bits set in "mask", compare the corresponding bits in
80 * "dst" and "src", and see if they match. Returns 0 if the addresses
84 bitmaskcmp(void *dst
, void *src
, void *mask
, int bytelen
)
87 u_int8_t
*p1
= dst
, *p2
= src
, *netmask
= mask
;
89 for (i
= 0; i
< bytelen
; i
++)
90 if ((p1
[i
] & netmask
[i
]) != (p2
[i
] & netmask
[i
]))
96 * Similar to code in ifconfig.c. Fill in the scope ID for link-local
97 * addresses returned by getifaddrs().
101 in6_fillscopeid(struct sockaddr_in6
*sin6
)
105 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
106 ifindex
= ntohs(*(u_int16_t
*)&sin6
->sin6_addr
.s6_addr
[2]);
107 if (sin6
->sin6_scope_id
== 0 && ifindex
!= 0) {
108 sin6
->sin6_scope_id
= ifindex
;
109 *(u_int16_t
*)&sin6
->sin6_addr
.s6_addr
[2] = 0;
116 * Find a server address that can be used by `caller' to contact
117 * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
118 * non-NULL, it is used instead of `caller' as a hint suggesting
119 * the best address (e.g. the `r_addr' field of an rpc, which
120 * contains the rpcbind server address that the caller used).
122 * Returns the best server address as a malloc'd "universal address"
123 * string which should be freed by the caller. On error, returns NULL.
126 addrmerge(struct netbuf
*caller
, char *serv_uaddr
, char *clnt_uaddr
,
129 struct ifaddrs
*ifap
, *ifp
= NULL
, *bestif
;
130 struct netbuf
*serv_nbp
= NULL
, *hint_nbp
= NULL
, tbuf
;
131 struct sockaddr
*caller_sa
, *hint_sa
, *ifsa
, *ifmasksa
, *serv_sa
;
132 struct sockaddr_storage ss
;
133 struct netconfig
*nconf
;
134 char *caller_uaddr
= NULL
, *hint_uaddr
= NULL
;
139 fprintf(stderr
, "addrmerge(caller, %s, %s, %s\n", serv_uaddr
,
140 clnt_uaddr
== NULL
? "NULL" : clnt_uaddr
, netid
);
142 caller_sa
= caller
->buf
;
143 if ((nconf
= rpcbind_get_conf(netid
)) == NULL
)
145 if ((caller_uaddr
= taddr2uaddr(nconf
, caller
)) == NULL
)
149 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
150 * address family is different from that of the caller.
153 if (clnt_uaddr
!= NULL
) {
154 hint_uaddr
= clnt_uaddr
;
155 if ((hint_nbp
= uaddr2taddr(nconf
, clnt_uaddr
)) == NULL
)
157 hint_sa
= hint_nbp
->buf
;
159 if (hint_sa
== NULL
|| hint_sa
->sa_family
!= caller_sa
->sa_family
) {
160 hint_uaddr
= caller_uaddr
;
161 hint_sa
= caller
->buf
;
166 fprintf(stderr
, "addrmerge: hint %s\n", hint_uaddr
);
168 /* Local caller, just return the server address. */
169 if (strncmp(caller_uaddr
, "0.0.0.0.", 8) == 0 ||
170 strncmp(caller_uaddr
, "::.", 3) == 0 || caller_uaddr
[0] == '/') {
171 ret
= strdup(serv_uaddr
);
175 if (getifaddrs(&ifp
) < 0)
179 * Loop through all interfaces. For each interface, see if the
180 * network portion of its address is equal to that of the client.
181 * If so, we have found the interface that we want to use.
184 for (ifap
= ifp
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
185 ifsa
= ifap
->ifa_addr
;
186 ifmasksa
= ifap
->ifa_netmask
;
188 if (ifsa
== NULL
|| ifsa
->sa_family
!= hint_sa
->sa_family
||
189 !(ifap
->ifa_flags
& IFF_UP
))
192 switch (hint_sa
->sa_family
) {
195 * If the hint address matches this interface
196 * address/netmask, then we're done.
198 if (!bitmaskcmp(&SA2SINADDR(ifsa
),
199 &SA2SINADDR(hint_sa
), &SA2SINADDR(ifmasksa
),
200 sizeof(struct in_addr
))) {
208 * For v6 link local addresses, if the caller is on
209 * a link-local address then use the scope id to see
212 in6_fillscopeid(SA2SIN6(ifsa
));
213 if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa
)) &&
214 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa
)) &&
215 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa
))) {
216 if (SA2SIN6(ifsa
)->sin6_scope_id
==
217 SA2SIN6(caller_sa
)->sin6_scope_id
) {
221 } else if (!bitmaskcmp(&SA2SIN6ADDR(ifsa
),
222 &SA2SIN6ADDR(hint_sa
), &SA2SIN6ADDR(ifmasksa
),
223 sizeof(struct in6_addr
))) {
234 * Remember the first possibly useful interface, preferring
235 * "normal" to point-to-point and loopback ones.
237 if (bestif
== NULL
||
238 (!(ifap
->ifa_flags
& (IFF_LOOPBACK
| IFF_POINTOPOINT
)) &&
239 (bestif
->ifa_flags
& (IFF_LOOPBACK
| IFF_POINTOPOINT
))))
247 * Construct the new address using the the address from
248 * `bestif', and the port number from `serv_uaddr'.
250 serv_nbp
= uaddr2taddr(nconf
, serv_uaddr
);
251 if (serv_nbp
== NULL
)
253 serv_sa
= serv_nbp
->buf
;
255 memcpy(&ss
, bestif
->ifa_addr
, bestif
->ifa_addr
->sa_len
);
256 switch (ss
.ss_family
) {
258 SA2SIN(&ss
)->sin_port
= SA2SIN(serv_sa
)->sin_port
;
262 SA2SIN6(&ss
)->sin6_port
= SA2SIN6(serv_sa
)->sin6_port
;
266 tbuf
.len
= ss
.ss_len
;
267 tbuf
.maxlen
= sizeof(ss
);
269 ret
= taddr2uaddr(nconf
, &tbuf
);
272 if (caller_uaddr
!= NULL
)
274 if (hint_nbp
!= NULL
) {
278 if (serv_nbp
!= NULL
) {
287 fprintf(stderr
, "addrmerge: returning %s\n", ret
);
296 struct ifaddrs
*ifap
, *ifp
;
297 struct ipv6_mreq mreq6
;
298 unsigned int ifindex
;
302 struct addrinfo hints
, *res
;
304 memset(&hints
, 0, sizeof hints
);
305 hints
.ai_family
= AF_INET
;
306 if ((ecode
= getaddrinfo(NULL
, "sunrpc", &hints
, &res
))) {
308 fprintf(stderr
, "can't get local ip4 address: %s\n",
309 gai_strerror(ecode
));
311 local_in4
= (struct sockaddr_in
*)malloc(sizeof *local_in4
);
312 if (local_in4
== NULL
) {
314 fprintf(stderr
, "can't alloc local ip4 addr\n");
316 memcpy(local_in4
, res
->ai_addr
, sizeof *local_in4
);
320 hints
.ai_family
= AF_INET6
;
321 if ((ecode
= getaddrinfo(NULL
, "sunrpc", &hints
, &res
))) {
323 fprintf(stderr
, "can't get local ip6 address: %s\n",
324 gai_strerror(ecode
));
326 local_in6
= (struct sockaddr_in6
*)malloc(sizeof *local_in6
);
327 if (local_in6
== NULL
) {
329 fprintf(stderr
, "can't alloc local ip6 addr\n");
331 memcpy(local_in6
, res
->ai_addr
, sizeof *local_in6
);
335 * Now join the RPC ipv6 multicast group on all interfaces.
337 if (getifaddrs(&ifp
) < 0)
340 mreq6
.ipv6mr_interface
= 0;
341 inet_pton(AF_INET6
, RPCB_MULTICAST_ADDR
, &mreq6
.ipv6mr_multiaddr
);
343 s
= socket(AF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
);
346 * Loop through all interfaces. For each IPv6 multicast-capable
347 * interface, join the RPC multicast group on that interface.
349 for (ifap
= ifp
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
350 if (ifap
->ifa_addr
->sa_family
!= AF_INET6
||
351 !(ifap
->ifa_flags
& IFF_MULTICAST
))
353 ifindex
= if_nametoindex(ifap
->ifa_name
);
354 if (ifindex
== mreq6
.ipv6mr_interface
)
356 * Already did this one.
359 mreq6
.ipv6mr_interface
= ifindex
;
360 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
, &mreq6
,
363 perror("setsockopt v6 multicast");
375 return (struct sockaddr
*)local_in4
;
378 return (struct sockaddr
*)local_in6
;