PageRenderTime 57ms CodeModel.GetById 49ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/bind9/bin/named/lwaddr.c

https://bitbucket.org/freebsd/freebsd-head/
C | 94 lines | 55 code | 15 blank | 24 comment | 16 complexity | e422a69962abf9bf541ea98e8e9b49cf MD5 | raw file
 1/*
 2 * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
 3 * Copyright (C) 2000, 2001  Internet Software Consortium.
 4 *
 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.
 8 *
 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.
16 */
17
18/* $Id: lwaddr.c,v 1.10 2008/01/11 23:46:56 tbox Exp $ */
19
20/*! \file */
21
22#include <config.h>
23
24#include <string.h>
25
26#include <isc/result.h>
27#include <isc/netaddr.h>
28#include <isc/sockaddr.h>
29
30#include <lwres/lwres.h>
31
32#include <named/lwaddr.h>
33
34/*%
35 * Convert addresses from lwres to isc format.
36 */
37isc_result_t
38lwaddr_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);
41
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);
50	}
51	return (ISC_R_SUCCESS);
52}
53
54isc_result_t
55lwaddr_sockaddr_fromlwresaddr(isc_sockaddr_t *sa, lwres_addr_t *la,
56			      in_port_t port)
57{
58	isc_netaddr_t na;
59	isc_result_t result;
60
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);
66}
67
68/*%
69 * Convert addresses from isc to lwres format.
70 */
71
72isc_result_t
73lwaddr_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);
76
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);
85	}
86	return (ISC_R_SUCCESS);
87}
88
89isc_result_t
90lwaddr_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));
94}