1 /* $NetBSD: util.c,v 1.15 2008/04/28 20:24:17 martin Exp $ */
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/queue.h>
36 #include <netinet/in.h>
46 #include <netconfig.h>
48 #include <arpa/inet.h>
52 static struct sockaddr_in
*local_in4
;
54 static struct sockaddr_in6
*local_in6
;
57 static int bitmaskcmp(void *, void *, void *, int);
59 static void in6_fillscopeid(struct sockaddr_in6
*);
63 * For all bits set in "mask", compare the corresponding bits in
64 * "dst" and "src", and see if they match.
67 bitmaskcmp(void *dst
, void *src
, void *mask
, int bytelen
)
70 u_int8_t
*p1
= dst
, *p2
= src
, *netmask
= mask
;
73 for (i
= 0; i
< bytelen
; i
++) {
74 for (j
= 0; j
< 8; j
++) {
76 if (!(netmask
[i
] & bitmask
))
78 if ((p1
[i
] & bitmask
) != (p2
[i
] & bitmask
))
87 * Taken from ifconfig.c
91 in6_fillscopeid(struct sockaddr_in6
*sin6
)
93 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
95 ntohs(*(u_int16_t
*)&sin6
->sin6_addr
.s6_addr
[2]);
96 sin6
->sin6_addr
.s6_addr
[2] = sin6
->sin6_addr
.s6_addr
[3] = 0;
102 addrmerge(struct netbuf
*caller
, char *serv_uaddr
, char *clnt_uaddr
,
105 struct ifaddrs
*ifap
, *ifp
, *bestif
;
107 struct sockaddr_in6
*servsin6
, *sin6mask
, *clntsin6
, *ifsin6
, *realsin6
;
108 struct sockaddr_in6
*newsin6
;
110 struct sockaddr_in
*servsin
, *sinmask
, *clntsin
, *newsin
, *ifsin
;
111 struct netbuf
*serv_nbp
, *clnt_nbp
= NULL
, tbuf
;
112 struct sockaddr
*serv_sa
;
113 struct sockaddr
*clnt_sa
;
114 struct sockaddr_storage ss
;
115 struct netconfig
*nconf
;
116 struct sockaddr
*clnt
= caller
->buf
;
120 servsin6
= ifsin6
= newsin6
= NULL
; /* XXXGCC -Wuninitialized */
122 servsin
= newsin
= NULL
; /* XXXGCC -Wuninitialized */
126 fprintf(stderr
, "addrmerge(caller, %s, %s, %s\n", serv_uaddr
,
129 nconf
= getnetconfigent(netid
);
134 * Local merge, just return a duplicate.
136 if (clnt_uaddr
!= NULL
&& strncmp(clnt_uaddr
, "0.0.0.0.", 8) == 0)
137 return strdup(clnt_uaddr
);
139 serv_nbp
= uaddr2taddr(nconf
, serv_uaddr
);
140 if (serv_nbp
== NULL
)
143 serv_sa
= (struct sockaddr
*)serv_nbp
->buf
;
144 if (clnt_uaddr
!= NULL
) {
145 clnt_nbp
= uaddr2taddr(nconf
, clnt_uaddr
);
146 if (clnt_nbp
== NULL
) {
150 clnt_sa
= (struct sockaddr
*)clnt_nbp
->buf
;
151 if (clnt_sa
->sa_family
== AF_LOCAL
) {
155 return strdup(serv_uaddr
);
158 clnt_sa
= (struct sockaddr
*)
159 malloc(sizeof (struct sockaddr_storage
));
160 memcpy(clnt_sa
, clnt
, clnt
->sa_len
);
163 if (getifaddrs(&ifp
) < 0) {
166 if (clnt_nbp
!= NULL
)
172 * Loop through all interfaces. For each interface, see if the
173 * network portion of its address is equal to that of the client.
174 * If so, we have found the interface that we want to use.
176 for (ifap
= ifp
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
177 if (ifap
->ifa_addr
->sa_family
!= clnt
->sa_family
||
178 !(ifap
->ifa_flags
& IFF_UP
))
181 switch (clnt
->sa_family
) {
184 * realsin: address that recvfrom gave us.
185 * ifsin: address of interface being examined.
186 * clntsin: address that client want us to contact
188 * servsin: local address of RPC service.
189 * sinmask: netmask of this interface
190 * newsin: initially a copy of clntsin, eventually
193 servsin
= (struct sockaddr_in
*)serv_sa
;
194 clntsin
= (struct sockaddr_in
*)clnt_sa
;
195 sinmask
= (struct sockaddr_in
*)ifap
->ifa_netmask
;
196 newsin
= (struct sockaddr_in
*)&ss
;
197 ifsin
= (struct sockaddr_in
*)ifap
->ifa_addr
;
198 if (!bitmaskcmp(&ifsin
->sin_addr
, &clntsin
->sin_addr
,
199 &sinmask
->sin_addr
, sizeof (struct in_addr
))) {
206 * realsin6: address that recvfrom gave us.
207 * ifsin6: address of interface being examined.
208 * clntsin6: address that client want us to contact
210 * servsin6: local address of RPC service.
211 * sin6mask: netmask of this interface
212 * newsin6: initially a copy of clntsin, eventually
215 * For v6 link local addresses, if the client contacted
216 * us via a link-local address, and wants us to reply
217 * to one, use the scope id to see which one.
219 realsin6
= (struct sockaddr_in6
*)clnt
;
220 ifsin6
= (struct sockaddr_in6
*)ifap
->ifa_addr
;
221 in6_fillscopeid(ifsin6
);
222 clntsin6
= (struct sockaddr_in6
*)clnt_sa
;
223 servsin6
= (struct sockaddr_in6
*)serv_sa
;
224 sin6mask
= (struct sockaddr_in6
*)ifap
->ifa_netmask
;
225 newsin6
= (struct sockaddr_in6
*)&ss
;
226 if (IN6_IS_ADDR_LINKLOCAL(&ifsin6
->sin6_addr
) &&
227 IN6_IS_ADDR_LINKLOCAL(&realsin6
->sin6_addr
) &&
228 IN6_IS_ADDR_LINKLOCAL(&clntsin6
->sin6_addr
)) {
229 if (ifsin6
->sin6_scope_id
!=
230 realsin6
->sin6_scope_id
)
234 if (!bitmaskcmp(&ifsin6
->sin6_addr
,
235 &clntsin6
->sin6_addr
, &sin6mask
->sin6_addr
,
236 sizeof (struct in6_addr
)))
245 * Didn't find anything. Get the first possibly useful interface,
246 * preferring "normal" interfaces to point-to-point and loopback
250 for (ifap
= ifp
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
251 if (ifap
->ifa_addr
->sa_family
!= clnt
->sa_family
||
252 !(ifap
->ifa_flags
& IFF_UP
))
254 if (!(ifap
->ifa_flags
& IFF_LOOPBACK
) &&
255 !(ifap
->ifa_flags
& IFF_POINTOPOINT
)) {
261 else if ((bestif
->ifa_flags
& IFF_LOOPBACK
) &&
262 !(ifap
->ifa_flags
& IFF_LOOPBACK
))
267 switch (clnt
->sa_family
) {
269 memcpy(newsin
, ifap
->ifa_addr
, clnt_sa
->sa_len
);
270 newsin
->sin_port
= servsin
->sin_port
;
271 tbuf
.len
= clnt_sa
->sa_len
;
272 tbuf
.maxlen
= sizeof (struct sockaddr_storage
);
278 memcpy(newsin6
, ifsin6
, clnt_sa
->sa_len
);
279 newsin6
->sin6_port
= servsin6
->sin6_port
;
280 tbuf
.maxlen
= sizeof (struct sockaddr_storage
);
281 tbuf
.len
= clnt_sa
->sa_len
;
289 ret
= taddr2uaddr(nconf
, &tbuf
);
291 freenetconfigent(nconf
);
296 if (clnt_nbp
!= NULL
)
302 fprintf(stderr
, "addrmerge: returning %s\n", ret
);
311 struct ifaddrs
*ifap
, *ifp
;
312 struct ipv6_mreq mreq6
;
313 unsigned int ifindex
;
317 struct addrinfo hints
, *res
;
319 memset(&hints
, 0, sizeof hints
);
320 hints
.ai_family
= AF_INET
;
321 if ((ecode
= getaddrinfo(NULL
, "sunrpc", &hints
, &res
))) {
323 fprintf(stderr
, "can't get local ip4 address: %s\n",
324 gai_strerror(ecode
));
326 local_in4
= (struct sockaddr_in
*)malloc(sizeof *local_in4
);
327 if (local_in4
== NULL
) {
329 fprintf(stderr
, "can't alloc local ip4 addr\n");
331 memcpy(local_in4
, res
->ai_addr
, sizeof *local_in4
);
335 hints
.ai_family
= AF_INET6
;
336 if ((ecode
= getaddrinfo(NULL
, "sunrpc", &hints
, &res
))) {
338 fprintf(stderr
, "can't get local ip6 address: %s\n",
339 gai_strerror(ecode
));
341 local_in6
= (struct sockaddr_in6
*)malloc(sizeof *local_in6
);
342 if (local_in6
== NULL
) {
344 fprintf(stderr
, "can't alloc local ip6 addr\n");
346 memcpy(local_in6
, res
->ai_addr
, sizeof *local_in6
);
350 * Now join the RPC ipv6 multicast group on all interfaces.
352 if (getifaddrs(&ifp
) < 0)
355 mreq6
.ipv6mr_interface
= 0;
356 inet_pton(AF_INET6
, RPCB_MULTICAST_ADDR
, &mreq6
.ipv6mr_multiaddr
);
358 s
= socket(AF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
);
361 * Loop through all interfaces. For each interface, see if the
362 * network portion of its address is equal to that of the client.
363 * If so, we have found the interface that we want to use.
365 for (ifap
= ifp
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
366 if (ifap
->ifa_addr
->sa_family
!= AF_INET6
||
367 !(ifap
->ifa_flags
& IFF_MULTICAST
))
369 ifindex
= if_nametoindex(ifap
->ifa_name
);
370 if (ifindex
== mreq6
.ipv6mr_interface
)
372 * Already did this one.
375 mreq6
.ipv6mr_interface
= ifindex
;
376 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
, &mreq6
,
379 perror("setsockopt v6 multicast");
391 return (struct sockaddr
*)local_in4
;
394 return (struct sockaddr
*)local_in6
;