Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / bin / named / lwaddr.c
blob44cfc9211ae3add125fd37b6d54118092a66e08f
1 /*
2 * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: lwaddr.c,v 1.8.130.2 2008/01/11 23:46:27 tbox Exp $ */
20 /*! \file */
22 #include <config.h>
24 #include <string.h>
26 #include <isc/result.h>
27 #include <isc/netaddr.h>
28 #include <isc/sockaddr.h>
30 #include <lwres/lwres.h>
32 #include <named/lwaddr.h>
34 /*%
35 * Convert addresses from lwres to isc format.
37 isc_result_t
38 lwaddr_netaddr_fromlwresaddr(isc_netaddr_t *na, lwres_addr_t *la) {
39 if (la->family != LWRES_ADDRTYPE_V4 && la->family != LWRES_ADDRTYPE_V6)
40 return (ISC_R_FAMILYNOSUPPORT);
42 if (la->family == LWRES_ADDRTYPE_V4) {
43 struct in_addr ina;
44 memcpy(&ina.s_addr, la->address, 4);
45 isc_netaddr_fromin(na, &ina);
46 } else {
47 struct in6_addr ina6;
48 memcpy(&ina6.s6_addr, la->address, 16);
49 isc_netaddr_fromin6(na, &ina6);
51 return (ISC_R_SUCCESS);
54 isc_result_t
55 lwaddr_sockaddr_fromlwresaddr(isc_sockaddr_t *sa, lwres_addr_t *la,
56 in_port_t port)
58 isc_netaddr_t na;
59 isc_result_t result;
61 result = lwaddr_netaddr_fromlwresaddr(&na, la);
62 if (result != ISC_R_SUCCESS)
63 return (result);
64 isc_sockaddr_fromnetaddr(sa, &na, port);
65 return (ISC_R_SUCCESS);
68 /*%
69 * Convert addresses from isc to lwres format.
72 isc_result_t
73 lwaddr_lwresaddr_fromnetaddr(lwres_addr_t *la, isc_netaddr_t *na) {
74 if (na->family != AF_INET && na->family != AF_INET6)
75 return (ISC_R_FAMILYNOSUPPORT);
77 if (na->family == AF_INET) {
78 la->family = LWRES_ADDRTYPE_V4;
79 la->length = 4;
80 memcpy(la->address, &na->type.in, 4);
81 } else {
82 la->family = LWRES_ADDRTYPE_V6;
83 la->length = 16;
84 memcpy(la->address, &na->type.in6, 16);
86 return (ISC_R_SUCCESS);
89 isc_result_t
90 lwaddr_lwresaddr_fromsockaddr(lwres_addr_t *la, isc_sockaddr_t *sa) {
91 isc_netaddr_t na;
92 isc_netaddr_fromsockaddr(&na, sa);
93 return (lwaddr_lwresaddr_fromnetaddr(la, &na));