dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2000, 2001, 2004, 2005, 2007, 2008, 2014, 2016 Internet Systems Consortium, Inc. ("ISC")
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington */
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
d36adf470fe38579fd89cb8186a88a3b21a4be14Automatic Updater/* $Id: lwaddr.c,v 1.10 2008/01/11 23:46:56 tbox Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#include <config.h>
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#include <string.h>
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#include <isc/result.h>
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#include <isc/netaddr.h>
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#include <isc/sockaddr.h>
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#include <lwres/lwres.h>
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#include <named/lwaddr.h>
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*%
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * Convert addresses from lwres to isc format.
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington */
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellingtonisc_result_t
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellingtonlwaddr_netaddr_fromlwresaddr(isc_netaddr_t *na, lwres_addr_t *la) {
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington if (la->family != LWRES_ADDRTYPE_V4 && la->family != LWRES_ADDRTYPE_V6)
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington return (ISC_R_FAMILYNOSUPPORT);
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington if (la->family == LWRES_ADDRTYPE_V4) {
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington struct in_addr ina;
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(&ina.s_addr, la->address, 4);
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington isc_netaddr_fromin(na, &ina);
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington } else {
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington struct in6_addr ina6;
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(&ina6.s6_addr, la->address, 16);
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington isc_netaddr_fromin6(na, &ina6);
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington }
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington return (ISC_R_SUCCESS);
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington}
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellingtonisc_result_t
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellingtonlwaddr_sockaddr_fromlwresaddr(isc_sockaddr_t *sa, lwres_addr_t *la,
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington in_port_t port)
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington{
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington isc_netaddr_t na;
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington isc_result_t result;
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington result = lwaddr_netaddr_fromlwresaddr(&na, la);
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington if (result != ISC_R_SUCCESS)
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington return (result);
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington isc_sockaddr_fromnetaddr(sa, &na, port);
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington return (ISC_R_SUCCESS);
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington}
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*%
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * Convert addresses from isc to lwres format.
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington */
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellingtonisc_result_t
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellingtonlwaddr_lwresaddr_fromnetaddr(lwres_addr_t *la, isc_netaddr_t *na) {
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington if (na->family != AF_INET && na->family != AF_INET6)
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington return (ISC_R_FAMILYNOSUPPORT);
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington if (na->family == AF_INET) {
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington la->family = LWRES_ADDRTYPE_V4;
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington la->length = 4;
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(la->address, &na->type.in, 4);
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington } else {
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington la->family = LWRES_ADDRTYPE_V6;
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington la->length = 16;
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(la->address, &na->type.in6, 16);
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington }
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington return (ISC_R_SUCCESS);
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington}
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellingtonisc_result_t
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellingtonlwaddr_lwresaddr_fromsockaddr(lwres_addr_t *la, isc_sockaddr_t *sa) {
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington isc_netaddr_t na;
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington isc_netaddr_fromsockaddr(&na, sa);
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington return (lwaddr_lwresaddr_fromnetaddr(la, &na));
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington}