1 /* $NetBSD: lwaddr.c,v 1.5 2014/12/10 04:37:51 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2008, 2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: lwaddr.c,v 1.10 2008/01/11 23:46:56 tbox Exp */
28 #include <isc/result.h>
29 #include <isc/netaddr.h>
30 #include <isc/sockaddr.h>
32 #include <lwres/lwres.h>
34 #include <named/lwaddr.h>
37 * Convert addresses from lwres to isc format.
40 lwaddr_netaddr_fromlwresaddr(isc_netaddr_t
*na
, lwres_addr_t
*la
) {
41 if (la
->family
!= LWRES_ADDRTYPE_V4
&& la
->family
!= LWRES_ADDRTYPE_V6
)
42 return (ISC_R_FAMILYNOSUPPORT
);
44 if (la
->family
== LWRES_ADDRTYPE_V4
) {
46 memmove(&ina
.s_addr
, la
->address
, 4);
47 isc_netaddr_fromin(na
, &ina
);
50 memmove(&ina6
.s6_addr
, la
->address
, 16);
51 isc_netaddr_fromin6(na
, &ina6
);
53 return (ISC_R_SUCCESS
);
57 lwaddr_sockaddr_fromlwresaddr(isc_sockaddr_t
*sa
, lwres_addr_t
*la
,
63 result
= lwaddr_netaddr_fromlwresaddr(&na
, la
);
64 if (result
!= ISC_R_SUCCESS
)
66 isc_sockaddr_fromnetaddr(sa
, &na
, port
);
67 return (ISC_R_SUCCESS
);
71 * Convert addresses from isc to lwres format.
75 lwaddr_lwresaddr_fromnetaddr(lwres_addr_t
*la
, isc_netaddr_t
*na
) {
76 if (na
->family
!= AF_INET
&& na
->family
!= AF_INET6
)
77 return (ISC_R_FAMILYNOSUPPORT
);
79 if (na
->family
== AF_INET
) {
80 la
->family
= LWRES_ADDRTYPE_V4
;
82 memmove(la
->address
, &na
->type
.in
, 4);
84 la
->family
= LWRES_ADDRTYPE_V6
;
86 memmove(la
->address
, &na
->type
.in6
, 16);
88 return (ISC_R_SUCCESS
);
92 lwaddr_lwresaddr_fromsockaddr(lwres_addr_t
*la
, isc_sockaddr_t
*sa
) {
94 isc_netaddr_fromsockaddr(&na
, sa
);
95 return (lwaddr_lwresaddr_fromnetaddr(la
, &na
));