2013-07-15 17:43:30 -05:00
|
|
|
/**
|
|
|
|
*
|
2014-01-28 08:30:01 -06:00
|
|
|
* \file convert.c
|
|
|
|
* @brief getdns label conversion functions
|
2013-07-15 17:43:30 -05:00
|
|
|
*
|
|
|
|
*/
|
2014-01-28 08:30:01 -06:00
|
|
|
|
|
|
|
/*
|
2014-02-25 07:12:33 -06:00
|
|
|
* Copyright (c) 2013, NLnet Labs, Verisign, Inc.
|
2014-01-28 08:30:01 -06:00
|
|
|
* All rights reserved.
|
2013-10-17 18:45:25 -05:00
|
|
|
*
|
2014-01-28 08:30:01 -06:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2014-02-25 07:23:19 -06:00
|
|
|
* * Neither the names of the copyright holders nor the
|
2014-01-28 08:30:01 -06:00
|
|
|
* names of its contributors may be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
2013-10-17 18:45:25 -05:00
|
|
|
*
|
2014-01-28 08:30:01 -06:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2013-07-15 17:43:30 -05:00
|
|
|
*/
|
|
|
|
|
2013-08-09 16:04:25 -05:00
|
|
|
#include <stdio.h>
|
2014-06-02 10:40:58 -05:00
|
|
|
#include <string.h>
|
2013-11-25 19:40:38 -06:00
|
|
|
#include <locale.h>
|
2015-08-27 07:38:23 -05:00
|
|
|
#include "config.h"
|
2015-11-22 21:38:13 -06:00
|
|
|
#ifndef USE_WINSOCK
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
2015-08-27 07:24:01 -05:00
|
|
|
#ifdef HAVE_LIBIDN
|
2015-10-29 13:13:39 -05:00
|
|
|
#include <stringprep.h>
|
2013-11-25 19:40:38 -06:00
|
|
|
#include <idna.h>
|
2015-08-27 07:24:01 -05:00
|
|
|
#endif
|
2014-05-19 08:50:34 -05:00
|
|
|
#include "getdns/getdns.h"
|
2015-04-02 08:33:10 -05:00
|
|
|
#include "getdns/getdns_extra.h"
|
2014-05-19 08:50:34 -05:00
|
|
|
#include "util-internal.h"
|
2015-03-02 05:21:29 -06:00
|
|
|
#include "gldns/wire2str.h"
|
|
|
|
#include "gldns/str2wire.h"
|
2015-12-09 05:04:00 -06:00
|
|
|
#include "dict.h"
|
2015-12-22 11:37:24 -06:00
|
|
|
#include "list.h"
|
|
|
|
#include "convert.h"
|
2013-07-15 17:43:30 -05:00
|
|
|
|
|
|
|
/* stuff to make it compile pedantically */
|
|
|
|
#define UNUSED_PARAM(x) ((void)(x))
|
|
|
|
|
2014-02-18 08:19:32 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_convert_dns_name_to_fqdn(
|
2015-03-02 05:21:29 -06:00
|
|
|
const getdns_bindata *dns_name_wire_fmt, char **fqdn_as_string)
|
2013-12-20 06:25:55 -06:00
|
|
|
{
|
2015-03-02 05:21:29 -06:00
|
|
|
char *r;
|
|
|
|
|
|
|
|
if (!dns_name_wire_fmt || !fqdn_as_string)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (!(r = gldns_wire2str_dname(
|
|
|
|
dns_name_wire_fmt->data, dns_name_wire_fmt->size)))
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
|
|
|
*fqdn_as_string = r;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-05 14:03:44 -06:00
|
|
|
}
|
2013-07-15 17:43:30 -05:00
|
|
|
|
2014-02-18 08:19:32 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_convert_fqdn_to_dns_name(
|
2015-03-02 05:21:29 -06:00
|
|
|
const char *fqdn_as_string, getdns_bindata **dns_name_wire_fmt)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-03-02 05:21:29 -06:00
|
|
|
getdns_bindata *r;
|
|
|
|
uint8_t *dname;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (!fqdn_as_string || !dns_name_wire_fmt)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (!(r = malloc(sizeof(getdns_bindata))))
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
|
|
|
|
if (!(dname = gldns_str2wire_dname(fqdn_as_string, &len))) {
|
|
|
|
free(r);
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
2014-02-18 08:19:32 -06:00
|
|
|
}
|
2015-03-02 05:21:29 -06:00
|
|
|
r->size = len;
|
|
|
|
r->data = dname;
|
|
|
|
*dns_name_wire_fmt = r;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-05 14:03:44 -06:00
|
|
|
}
|
2013-07-15 17:43:30 -05:00
|
|
|
|
2013-11-25 19:40:38 -06:00
|
|
|
/*---------------------------------------- getdns_convert_alabel_to_ulabel */
|
|
|
|
/**
|
|
|
|
* Convert UTF-8 string into an ACE-encoded domain
|
|
|
|
* It is the application programmer's responsibility to free()
|
|
|
|
* the returned buffer after use
|
|
|
|
*
|
|
|
|
* @param ulabel the UTF-8-encoded domain name to convert
|
|
|
|
* @return pointer to ACE-encoded string
|
|
|
|
* @return NULL if conversion fails
|
|
|
|
*/
|
|
|
|
|
2013-07-15 17:43:30 -05:00
|
|
|
char *
|
2014-01-14 10:25:23 -06:00
|
|
|
getdns_convert_ulabel_to_alabel(const char *ulabel)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-08-27 07:24:01 -05:00
|
|
|
#ifdef HAVE_LIBIDN
|
2013-11-25 19:40:38 -06:00
|
|
|
int ret;
|
|
|
|
char *buf;
|
|
|
|
char *prepped;
|
2014-02-04 02:49:12 -06:00
|
|
|
char *prepped2;
|
2013-11-25 19:40:38 -06:00
|
|
|
|
2014-01-13 16:11:28 -06:00
|
|
|
if (ulabel == NULL)
|
2014-02-04 02:49:12 -06:00
|
|
|
return 0;
|
|
|
|
prepped2 = malloc(BUFSIZ);
|
|
|
|
if(!prepped2)
|
|
|
|
return 0;
|
2013-11-25 19:40:38 -06:00
|
|
|
setlocale(LC_ALL, "");
|
2014-02-04 02:49:12 -06:00
|
|
|
if ((prepped = stringprep_locale_to_utf8(ulabel)) == 0) {
|
|
|
|
/* convert to utf8 fails, which it can, but continue anyway */
|
|
|
|
if(strlen(ulabel)+1 > BUFSIZ) {
|
|
|
|
free(prepped2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(prepped2, ulabel, strlen(ulabel)+1);
|
|
|
|
} else {
|
|
|
|
if(strlen(prepped)+1 > BUFSIZ) {
|
|
|
|
free(prepped);
|
|
|
|
free(prepped2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(prepped2, prepped, strlen(prepped)+1);
|
|
|
|
free(prepped);
|
|
|
|
}
|
|
|
|
if ((ret = stringprep(prepped2, BUFSIZ, 0, stringprep_nameprep)) != STRINGPREP_OK) {
|
|
|
|
free(prepped2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((ret = idna_to_ascii_8z(prepped2, &buf, 0)) != IDNA_SUCCESS) {
|
|
|
|
free(prepped2);
|
|
|
|
return 0;
|
2013-11-25 19:40:38 -06:00
|
|
|
}
|
2014-02-04 02:49:12 -06:00
|
|
|
free(prepped2);
|
2013-11-25 19:40:38 -06:00
|
|
|
return buf;
|
2015-08-27 07:24:01 -05:00
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2013-11-05 14:03:44 -06:00
|
|
|
}
|
2013-07-15 17:43:30 -05:00
|
|
|
|
2013-11-25 19:40:38 -06:00
|
|
|
/*---------------------------------------- getdns_convert_alabel_to_ulabel */
|
|
|
|
/**
|
|
|
|
* Convert ACE-encoded domain name into a UTF-8 string.
|
|
|
|
* It is the application programmer's responsibility to free()
|
|
|
|
* the returned buffer after use
|
|
|
|
*
|
|
|
|
* @param alabel the ACE-encoded domain name to convert
|
|
|
|
* @return pointer to UTF-8 string
|
|
|
|
* @return NULL if conversion fails
|
|
|
|
*/
|
|
|
|
|
2013-07-15 17:43:30 -05:00
|
|
|
char *
|
2014-01-14 10:25:23 -06:00
|
|
|
getdns_convert_alabel_to_ulabel(const char *alabel)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-08-27 07:24:01 -05:00
|
|
|
#ifdef HAVE_LIBIDN
|
2013-11-25 19:40:38 -06:00
|
|
|
int ret; /* just in case we might want to use it someday */
|
|
|
|
char *buf;
|
|
|
|
|
2014-01-13 16:11:28 -06:00
|
|
|
if (alabel == NULL)
|
|
|
|
return 0;
|
2013-11-25 19:40:38 -06:00
|
|
|
if ((ret = idna_to_unicode_8z8z(alabel, &buf, 0)) != IDNA_SUCCESS) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return buf;
|
2015-08-27 07:24:01 -05:00
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2013-11-05 14:03:44 -06:00
|
|
|
}
|
2013-07-15 17:43:30 -05:00
|
|
|
|
2013-11-25 19:40:38 -06:00
|
|
|
|
2013-07-15 17:43:30 -05:00
|
|
|
char *
|
2014-01-14 10:25:23 -06:00
|
|
|
getdns_display_ip_address(const struct getdns_bindata
|
2013-11-05 14:03:44 -06:00
|
|
|
*bindata_of_ipv4_or_ipv6_address)
|
2013-10-17 18:45:25 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
char buff[256];
|
|
|
|
if (!bindata_of_ipv4_or_ipv6_address ||
|
|
|
|
bindata_of_ipv4_or_ipv6_address->size == 0 ||
|
|
|
|
!bindata_of_ipv4_or_ipv6_address->data) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (bindata_of_ipv4_or_ipv6_address->size == 4) {
|
|
|
|
const char *ipStr = inet_ntop(AF_INET,
|
|
|
|
bindata_of_ipv4_or_ipv6_address->data,
|
|
|
|
buff,
|
|
|
|
256);
|
|
|
|
if (ipStr) {
|
|
|
|
return strdup(ipStr);
|
|
|
|
}
|
|
|
|
} else if (bindata_of_ipv4_or_ipv6_address->size == 16) {
|
|
|
|
const char *ipStr = inet_ntop(AF_INET6,
|
|
|
|
bindata_of_ipv4_or_ipv6_address->data,
|
|
|
|
buff,
|
|
|
|
256);
|
|
|
|
if (ipStr) {
|
|
|
|
return strdup(ipStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2013-10-17 18:45:25 -05:00
|
|
|
}
|
2013-07-15 17:43:30 -05:00
|
|
|
|
2013-07-19 15:19:22 -05:00
|
|
|
getdns_return_t
|
|
|
|
getdns_strerror(getdns_return_t err, char *buf, size_t buflen)
|
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
const char *err_str = getdns_get_errorstr_by_id(err);
|
2013-07-19 15:19:22 -05:00
|
|
|
|
2015-04-02 08:33:10 -05:00
|
|
|
(void) snprintf(buf, buflen, "%s",
|
2015-04-02 08:56:13 -05:00
|
|
|
err_str ? err_str : "/* <unknown getdns value> */");
|
2013-07-19 15:19:22 -05:00
|
|
|
|
2015-04-02 08:33:10 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_strerror */
|
2013-07-19 15:19:22 -05:00
|
|
|
|
2015-12-22 09:04:43 -06:00
|
|
|
|
|
|
|
/* --------------------- rr_dict, wire, str conversions --------------------- */
|
|
|
|
|
|
|
|
|
2015-12-09 05:04:00 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_rr_dict2wire(
|
2015-12-22 09:04:43 -06:00
|
|
|
const getdns_dict *rr_dict, uint8_t **wire, size_t *wire_sz)
|
|
|
|
{
|
|
|
|
uint8_t buf_spc[4096], *buf;
|
|
|
|
size_t buf_len = sizeof(buf_spc);
|
|
|
|
getdns_return_t r = getdns_rr_dict2wire_buf(
|
|
|
|
rr_dict, buf_spc, &buf_len);
|
|
|
|
|
|
|
|
if (r != GETDNS_RETURN_GOOD && r != GETDNS_RETURN_NEED_MORE_SPACE)
|
|
|
|
return r;
|
|
|
|
|
2016-04-29 05:00:17 -05:00
|
|
|
if (!(buf = malloc(buf_len ? buf_len : 1)))
|
2015-12-22 09:04:43 -06:00
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
memcpy(buf, buf_spc, buf_len);
|
|
|
|
|
|
|
|
else if ((r = getdns_rr_dict2wire_buf(rr_dict, buf, &buf_len))) {
|
|
|
|
free(buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
*wire = buf;
|
|
|
|
*wire_sz = buf_len;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_rr_dict2wire_buf(
|
2015-12-09 05:04:00 -06:00
|
|
|
const getdns_dict *rr_dict, uint8_t *wire, size_t *wire_sz)
|
2015-12-22 09:04:43 -06:00
|
|
|
{
|
2015-12-24 08:28:12 -06:00
|
|
|
int my_wire_sz;
|
2015-12-22 09:04:43 -06:00
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if (!wire_sz)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
else
|
|
|
|
my_wire_sz = *wire_sz;
|
|
|
|
|
|
|
|
r = getdns_rr_dict2wire_scan(rr_dict, &wire, &my_wire_sz);
|
|
|
|
if (r == GETDNS_RETURN_GOOD || r == GETDNS_RETURN_NEED_MORE_SPACE)
|
|
|
|
*wire_sz -= my_wire_sz;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_rr_dict2wire_scan(
|
2015-12-24 08:28:12 -06:00
|
|
|
const getdns_dict *rr_dict, uint8_t **wire, int *wire_sz)
|
2015-12-09 05:04:00 -06:00
|
|
|
{
|
|
|
|
getdns_return_t r;
|
|
|
|
gldns_buffer gbuf;
|
|
|
|
|
2015-12-22 09:04:43 -06:00
|
|
|
if (!rr_dict || !wire || !*wire || !wire_sz)
|
2015-12-09 05:04:00 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
|
2015-12-22 09:04:43 -06:00
|
|
|
gldns_buffer_init_frm_data(&gbuf, *wire, *wire_sz);
|
|
|
|
if ((r = _getdns_rr_dict2wire(rr_dict, &gbuf)))
|
2015-12-09 05:04:00 -06:00
|
|
|
return r;
|
|
|
|
|
2015-12-22 09:04:43 -06:00
|
|
|
if (gldns_buffer_position(&gbuf) == 0)
|
2015-12-09 05:04:00 -06:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
2015-12-22 09:04:43 -06:00
|
|
|
*wire += gldns_buffer_position(&gbuf);
|
|
|
|
*wire_sz -= gldns_buffer_position(&gbuf);
|
|
|
|
if (gldns_buffer_position(&gbuf) > gldns_buffer_limit(&gbuf))
|
|
|
|
return GETDNS_RETURN_NEED_MORE_SPACE;
|
|
|
|
else
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2015-12-09 05:04:00 -06:00
|
|
|
}
|
|
|
|
|
2015-12-22 11:37:24 -06:00
|
|
|
static struct mem_funcs _getdns_plain_mem_funcs = {
|
|
|
|
MF_PLAIN, .mf.pln = { malloc, realloc, free }
|
|
|
|
};
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
_getdns_wire2rr_dict(struct mem_funcs *mf,
|
|
|
|
const uint8_t *wire, size_t wire_len, getdns_dict **rr_dict)
|
|
|
|
{
|
|
|
|
return _getdns_wire2rr_dict_scan(mf, &wire, &wire_len, rr_dict);
|
|
|
|
}
|
2015-12-09 05:04:00 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_wire2rr_dict(
|
|
|
|
const uint8_t *wire, size_t wire_len, getdns_dict **rr_dict)
|
2015-12-22 09:04:43 -06:00
|
|
|
{
|
2015-12-22 11:37:24 -06:00
|
|
|
return _getdns_wire2rr_dict(
|
|
|
|
&_getdns_plain_mem_funcs, wire, wire_len, rr_dict);
|
2015-12-22 09:04:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
2015-12-22 11:37:24 -06:00
|
|
|
_getdns_wire2rr_dict_buf(struct mem_funcs *mf,
|
2015-12-22 09:04:43 -06:00
|
|
|
const uint8_t *wire, size_t *wire_len, getdns_dict **rr_dict)
|
|
|
|
{
|
|
|
|
size_t my_wire_len;
|
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if (!wire_len)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
else
|
|
|
|
my_wire_len = *wire_len;
|
|
|
|
|
2015-12-22 11:37:24 -06:00
|
|
|
if ((r = _getdns_wire2rr_dict_scan(mf, &wire, &my_wire_len, rr_dict)))
|
2015-12-22 09:04:43 -06:00
|
|
|
return r;
|
|
|
|
|
|
|
|
*wire_len -= my_wire_len;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
2015-12-22 11:37:24 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_wire2rr_dict_buf(
|
|
|
|
const uint8_t *wire, size_t *wire_len, getdns_dict **rr_dict)
|
|
|
|
{
|
|
|
|
return _getdns_wire2rr_dict_buf(
|
|
|
|
&_getdns_plain_mem_funcs, wire, wire_len, rr_dict);
|
|
|
|
}
|
2015-12-22 09:04:43 -06:00
|
|
|
|
|
|
|
getdns_return_t
|
2015-12-22 11:37:24 -06:00
|
|
|
_getdns_wire2rr_dict_scan(struct mem_funcs *mf,
|
2015-12-22 09:04:43 -06:00
|
|
|
const uint8_t **wire, size_t *wire_len, getdns_dict **rr_dict)
|
2015-12-09 05:04:00 -06:00
|
|
|
{
|
|
|
|
_getdns_rr_iter rr_iter_spc, *rr_iter;
|
|
|
|
|
2015-12-22 09:04:43 -06:00
|
|
|
if (!wire || !*wire || !wire_len || !rr_dict)
|
2015-12-09 05:04:00 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (!(rr_iter = _getdns_single_rr_iter_init(
|
2015-12-22 09:04:43 -06:00
|
|
|
&rr_iter_spc, *wire, *wire_len)))
|
2015-12-09 05:04:00 -06:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
2015-12-22 11:37:24 -06:00
|
|
|
if (!(*rr_dict = _getdns_rr_iter2rr_dict(mf, rr_iter)))
|
2015-12-09 05:04:00 -06:00
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
|
2015-12-22 09:04:43 -06:00
|
|
|
*wire_len -= (rr_iter->nxt - rr_iter->pos);
|
2015-12-22 11:37:24 -06:00
|
|
|
*wire = rr_iter->nxt;
|
2015-12-22 09:04:43 -06:00
|
|
|
|
2015-12-09 05:04:00 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
2015-12-22 11:37:24 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_wire2rr_dict_scan(
|
|
|
|
const uint8_t **wire, size_t *wire_len, getdns_dict **rr_dict)
|
|
|
|
{
|
|
|
|
return _getdns_wire2rr_dict_scan(
|
|
|
|
&_getdns_plain_mem_funcs, wire, wire_len, rr_dict);
|
|
|
|
}
|
2015-12-09 05:04:00 -06:00
|
|
|
|
2015-12-22 09:04:43 -06:00
|
|
|
|
2015-12-09 05:04:00 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_rr_dict2str(
|
|
|
|
const getdns_dict *rr_dict, char **str)
|
2015-12-22 09:04:43 -06:00
|
|
|
{
|
|
|
|
char buf_spc[4096], *buf;
|
|
|
|
size_t buf_len = sizeof(buf_spc) - 1;
|
|
|
|
getdns_return_t r = getdns_rr_dict2str_buf(
|
|
|
|
rr_dict, buf_spc, &buf_len);
|
|
|
|
|
|
|
|
if (r != GETDNS_RETURN_GOOD && r != GETDNS_RETURN_NEED_MORE_SPACE)
|
|
|
|
return r;
|
2015-12-23 10:38:36 -06:00
|
|
|
|
|
|
|
buf_len += 1;
|
|
|
|
if (!(buf = malloc(buf_len)))
|
2015-12-22 09:04:43 -06:00
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
memcpy(buf, buf_spc, buf_len);
|
|
|
|
|
|
|
|
else if ((r = getdns_rr_dict2str_buf(rr_dict, buf, &buf_len))) {
|
|
|
|
free(buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
*str = buf;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_rr_dict2str_buf(
|
|
|
|
const getdns_dict *rr_dict, char *str, size_t *str_len)
|
|
|
|
{
|
2015-12-24 08:28:12 -06:00
|
|
|
int my_str_len;
|
2015-12-22 09:04:43 -06:00
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if (!str_len)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
else
|
|
|
|
my_str_len = *str_len;
|
|
|
|
|
|
|
|
r = getdns_rr_dict2str_scan(rr_dict, &str, &my_str_len);
|
|
|
|
if (r == GETDNS_RETURN_GOOD || r == GETDNS_RETURN_NEED_MORE_SPACE)
|
|
|
|
*str_len -= my_str_len;
|
|
|
|
return r;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_rr_dict2str_scan(
|
2015-12-24 08:28:12 -06:00
|
|
|
const getdns_dict *rr_dict, char **str, int *str_len)
|
2015-12-09 05:04:00 -06:00
|
|
|
{
|
|
|
|
getdns_return_t r;
|
|
|
|
gldns_buffer gbuf;
|
2015-12-22 09:04:43 -06:00
|
|
|
uint8_t buf_spc[4096], *buf = buf_spc, *scan_buf;
|
|
|
|
size_t sz, scan_sz;
|
2015-12-24 08:28:12 -06:00
|
|
|
int prev_str_len;
|
2015-12-22 09:04:43 -06:00
|
|
|
char *prev_str;
|
|
|
|
int sz_needed;
|
2015-12-09 05:04:00 -06:00
|
|
|
|
2015-12-22 09:04:43 -06:00
|
|
|
if (!rr_dict || !str || !*str || !str_len)
|
2015-12-09 05:04:00 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
gldns_buffer_init_frm_data(&gbuf, buf, sizeof(buf_spc));
|
|
|
|
r = _getdns_rr_dict2wire(rr_dict, &gbuf);
|
|
|
|
if (gldns_buffer_position(&gbuf) > sizeof(buf_spc)) {
|
|
|
|
if (!(buf = GETDNS_XMALLOC(
|
|
|
|
rr_dict->mf, uint8_t, (sz = gldns_buffer_position(&gbuf))))) {
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
}
|
|
|
|
gldns_buffer_init_frm_data(&gbuf, buf, sz);
|
|
|
|
r = _getdns_rr_dict2wire(rr_dict, &gbuf);
|
|
|
|
}
|
|
|
|
if (r) {
|
|
|
|
if (buf != buf_spc)
|
|
|
|
GETDNS_FREE(rr_dict->mf, buf);
|
|
|
|
return r;
|
|
|
|
}
|
2015-12-22 09:04:43 -06:00
|
|
|
scan_buf = gldns_buffer_begin(&gbuf);
|
|
|
|
scan_sz = gldns_buffer_position(&gbuf);
|
|
|
|
prev_str = *str;
|
|
|
|
prev_str_len = *str_len;
|
|
|
|
sz = (size_t)*str_len;
|
|
|
|
sz_needed = gldns_wire2str_rr_scan(
|
|
|
|
&scan_buf, &scan_sz, str, &sz, NULL, 0);
|
|
|
|
|
|
|
|
if (sz_needed > prev_str_len) {
|
|
|
|
*str = prev_str + sz_needed;
|
|
|
|
*str_len = prev_str_len - sz_needed;
|
|
|
|
r = GETDNS_RETURN_NEED_MORE_SPACE;
|
2015-12-23 10:15:45 -06:00
|
|
|
} else {
|
2015-12-22 09:04:43 -06:00
|
|
|
*str_len = sz;
|
2015-12-23 10:38:36 -06:00
|
|
|
**str = 0;
|
2015-12-23 10:15:45 -06:00
|
|
|
}
|
2015-12-09 05:04:00 -06:00
|
|
|
if (buf != buf_spc)
|
|
|
|
GETDNS_FREE(rr_dict->mf, buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-12-22 09:04:43 -06:00
|
|
|
|
2015-12-09 05:04:00 -06:00
|
|
|
getdns_return_t
|
2015-12-22 11:37:24 -06:00
|
|
|
_getdns_str2rr_dict(struct mem_funcs *mf,
|
2015-12-09 05:04:00 -06:00
|
|
|
const char *str, getdns_dict **rr_dict, const char *origin, uint32_t default_ttl)
|
|
|
|
{
|
|
|
|
uint8_t wire_spc[4096], *wire = wire_spc;
|
|
|
|
uint8_t origin_spc[256], *origin_wf;
|
|
|
|
size_t origin_len = sizeof(origin_spc), wire_len = sizeof(wire_spc);
|
|
|
|
int e;
|
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if (!str || !rr_dict)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (!origin)
|
|
|
|
origin_wf = NULL;
|
|
|
|
|
|
|
|
else if (gldns_str2wire_dname_buf(origin, origin_spc, &origin_len))
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
else
|
|
|
|
origin_wf = origin_spc;
|
|
|
|
|
|
|
|
e = gldns_str2wire_rr_buf(str, wire, &wire_len,
|
|
|
|
NULL, default_ttl, origin_wf, origin_len, NULL, 0);
|
|
|
|
if (GLDNS_WIREPARSE_ERROR(e) == GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL) {
|
2015-12-22 11:37:24 -06:00
|
|
|
|
|
|
|
if (!(wire = GETDNS_XMALLOC(
|
|
|
|
*mf, uint8_t, (wire_len = GLDNS_RR_BUF_SIZE))))
|
2015-12-09 05:04:00 -06:00
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
e = gldns_str2wire_rr_buf(str, wire, &wire_len,
|
|
|
|
NULL, default_ttl, origin_wf, origin_len, NULL, 0);
|
|
|
|
}
|
|
|
|
if (e) {
|
|
|
|
if (wire != wire_spc)
|
2015-12-22 11:37:24 -06:00
|
|
|
GETDNS_FREE(*mf, wire);
|
2015-12-09 05:04:00 -06:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
2015-12-22 11:37:24 -06:00
|
|
|
r = _getdns_wire2rr_dict(mf, wire, wire_len, rr_dict);
|
2015-12-09 05:04:00 -06:00
|
|
|
if (wire != wire_spc)
|
2015-12-22 11:37:24 -06:00
|
|
|
GETDNS_FREE(*mf, wire);
|
2015-12-09 05:04:00 -06:00
|
|
|
return r;
|
|
|
|
}
|
2015-12-22 11:37:24 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_str2rr_dict(
|
|
|
|
const char *str, getdns_dict **rr_dict, const char *origin, uint32_t default_ttl)
|
|
|
|
{
|
|
|
|
return _getdns_str2rr_dict(
|
|
|
|
&_getdns_plain_mem_funcs, str, rr_dict, origin, default_ttl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
_getdns_fp2rr_list(struct mem_funcs *mf,
|
|
|
|
FILE *in, getdns_list **rr_list, const char *origin, uint32_t default_ttl)
|
|
|
|
{
|
|
|
|
struct gldns_file_parse_state pst;
|
|
|
|
getdns_list *rrs;
|
|
|
|
getdns_return_t r = GETDNS_RETURN_GOOD;
|
|
|
|
uint8_t *rr;
|
|
|
|
size_t len, dname_len;
|
|
|
|
getdns_dict *rr_dict;
|
|
|
|
|
|
|
|
if (!in || !rr_list)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (!origin) {
|
|
|
|
*pst.origin = 0;
|
2015-12-23 05:06:09 -06:00
|
|
|
pst.origin_len = 1;
|
2015-12-22 11:37:24 -06:00
|
|
|
|
|
|
|
} else if (gldns_str2wire_dname_buf(origin,pst.origin,&pst.origin_len))
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
|
|
|
*pst.prev_rr = 0;
|
|
|
|
pst.prev_rr_len = 1;
|
|
|
|
pst.default_ttl = default_ttl;
|
|
|
|
pst.lineno = 1;
|
|
|
|
|
|
|
|
if (!(rrs = _getdns_list_create_with_mf(mf)))
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
|
|
|
|
|
|
|
|
if (!(rr = GETDNS_XMALLOC(*mf, uint8_t, GLDNS_RR_BUF_SIZE)))
|
|
|
|
r = GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
|
|
|
|
else while (r == GETDNS_RETURN_GOOD && !feof(in)) {
|
|
|
|
len = GLDNS_RR_BUF_SIZE;
|
|
|
|
dname_len = 0;
|
|
|
|
if (gldns_fp2wire_rr_buf(in, rr, &len, &dname_len, &pst))
|
|
|
|
break;
|
2015-12-23 05:06:09 -06:00
|
|
|
if (dname_len && dname_len < sizeof(pst.prev_rr)) {
|
|
|
|
memcpy(pst.prev_rr, rr, dname_len);
|
|
|
|
pst.prev_rr_len = dname_len;
|
|
|
|
}
|
2015-12-22 11:37:24 -06:00
|
|
|
if (len == 0)
|
|
|
|
continue;
|
|
|
|
if ((r = _getdns_wire2rr_dict(mf, rr, len, &rr_dict)))
|
|
|
|
break;
|
2016-03-21 08:56:09 -05:00
|
|
|
if ((r = _getdns_list_append_this_dict(rrs, rr_dict)))
|
|
|
|
getdns_dict_destroy(rr_dict);
|
2015-12-22 11:37:24 -06:00
|
|
|
}
|
|
|
|
if (rr)
|
|
|
|
GETDNS_FREE(*mf, rr);
|
|
|
|
if (r)
|
|
|
|
getdns_list_destroy(rrs);
|
|
|
|
else
|
|
|
|
*rr_list = rrs;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_fp2rr_list(
|
|
|
|
FILE *in, getdns_list **rr_list, const char *origin, uint32_t default_ttl)
|
|
|
|
{
|
|
|
|
return _getdns_fp2rr_list(
|
|
|
|
&_getdns_plain_mem_funcs, in, rr_list, origin, default_ttl);
|
|
|
|
}
|
2015-12-09 05:04:00 -06:00
|
|
|
|
2016-03-14 04:02:01 -05:00
|
|
|
#define SET_WIRE_INT(X,Y) if (getdns_dict_set_int(header, #X , (int) \
|
|
|
|
GLDNS_ ## Y ## _WIRE(*wire))) goto error
|
|
|
|
#define SET_WIRE_BIT(X,Y) if (getdns_dict_set_int(header, #X , \
|
|
|
|
GLDNS_ ## Y ## _WIRE(*wire) ? 1 : 0)) goto error
|
|
|
|
#define SET_WIRE_CNT(X,Y) if (getdns_dict_set_int(header, #X , (int) \
|
|
|
|
GLDNS_ ## Y (*wire))) goto error
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
_getdns_wire2msg_dict_scan(struct mem_funcs *mf,
|
|
|
|
const uint8_t **wire, size_t *wire_len, getdns_dict **msg_dict)
|
|
|
|
{
|
|
|
|
getdns_return_t r = GETDNS_RETURN_GOOD;
|
|
|
|
getdns_dict *result = NULL, *header = NULL, *rr_dict = NULL;
|
|
|
|
_getdns_rr_iter rr_iter_storage, *rr_iter;
|
2016-06-07 09:52:10 -05:00
|
|
|
_getdns_section section;
|
|
|
|
getdns_list *sections[16] = { NULL, NULL, NULL, NULL
|
|
|
|
, NULL, NULL, NULL, NULL
|
|
|
|
, NULL, NULL, NULL, NULL
|
|
|
|
, NULL, NULL, NULL, NULL };
|
2016-03-14 04:02:01 -05:00
|
|
|
const uint8_t *eop; /* end of packet */
|
|
|
|
|
|
|
|
if (!wire || !*wire || !wire_len || !msg_dict)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (!(result = _getdns_dict_create_with_mf(mf)) ||
|
|
|
|
!(header = _getdns_dict_create_with_mf(mf)) ||
|
2016-06-07 09:52:10 -05:00
|
|
|
!(sections[SECTION_ANSWER]
|
2016-03-14 04:02:01 -05:00
|
|
|
= _getdns_list_create_with_mf(mf)) ||
|
2016-06-07 09:52:10 -05:00
|
|
|
!(sections[SECTION_AUTHORITY]
|
2016-03-14 04:02:01 -05:00
|
|
|
= _getdns_list_create_with_mf(mf)) ||
|
2016-06-07 09:52:10 -05:00
|
|
|
!(sections[SECTION_ADDITIONAL]
|
2016-03-14 04:02:01 -05:00
|
|
|
= _getdns_list_create_with_mf(mf))) {
|
|
|
|
r = GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
SET_WIRE_INT(id, ID);
|
|
|
|
SET_WIRE_BIT(qr, QR);
|
|
|
|
SET_WIRE_BIT(aa, AA);
|
|
|
|
SET_WIRE_BIT(tc, TC);
|
|
|
|
SET_WIRE_BIT(rd, RD);
|
|
|
|
SET_WIRE_BIT(cd, CD);
|
|
|
|
SET_WIRE_BIT(ra, RA);
|
|
|
|
SET_WIRE_BIT(ad, AD);
|
|
|
|
SET_WIRE_INT(opcode, OPCODE);
|
|
|
|
SET_WIRE_INT(rcode, RCODE);
|
|
|
|
SET_WIRE_BIT(z, Z);
|
|
|
|
|
|
|
|
SET_WIRE_CNT(qdcount, QDCOUNT);
|
|
|
|
SET_WIRE_CNT(ancount, ANCOUNT);
|
|
|
|
SET_WIRE_CNT(nscount, NSCOUNT);
|
|
|
|
SET_WIRE_CNT(arcount, ARCOUNT);
|
|
|
|
|
|
|
|
/* header */
|
2016-03-21 05:55:21 -05:00
|
|
|
if ((r = _getdns_dict_set_this_dict(result, "header", header)))
|
2016-03-14 04:02:01 -05:00
|
|
|
goto error;
|
|
|
|
header = NULL;
|
|
|
|
eop = *wire + 12;
|
|
|
|
|
|
|
|
for ( rr_iter = _getdns_rr_iter_init(&rr_iter_storage,*wire,*wire_len)
|
|
|
|
; rr_iter
|
|
|
|
; rr_iter = _getdns_rr_iter_next(rr_iter)) {
|
|
|
|
|
|
|
|
if (rr_iter->nxt > eop)
|
|
|
|
eop = rr_iter->nxt;
|
|
|
|
|
|
|
|
if (!(rr_dict = _getdns_rr_iter2rr_dict(mf, rr_iter)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch ((section = _getdns_rr_iter_section(rr_iter))) {
|
2016-06-07 09:52:10 -05:00
|
|
|
case SECTION_QUESTION:
|
2016-03-21 05:55:21 -05:00
|
|
|
if ((r = _getdns_dict_set_this_dict(
|
2016-03-14 04:02:01 -05:00
|
|
|
result, "question", rr_dict)))
|
|
|
|
goto error;
|
|
|
|
break;
|
2016-06-07 09:52:10 -05:00
|
|
|
case SECTION_ANSWER:
|
|
|
|
case SECTION_AUTHORITY:
|
|
|
|
case SECTION_ADDITIONAL:
|
2016-03-21 08:56:09 -05:00
|
|
|
if ((r = _getdns_list_append_this_dict(
|
2016-03-14 04:02:01 -05:00
|
|
|
sections[section], rr_dict)))
|
|
|
|
goto error;
|
|
|
|
break;
|
2016-04-29 05:00:17 -05:00
|
|
|
default:
|
|
|
|
r = GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
goto error;
|
2016-03-14 04:02:01 -05:00
|
|
|
}
|
|
|
|
rr_dict = NULL;
|
|
|
|
}
|
2016-03-21 06:50:43 -05:00
|
|
|
if (!(r = _getdns_dict_set_this_list(result, "answer",
|
2016-06-07 09:52:10 -05:00
|
|
|
sections[SECTION_ANSWER])))
|
|
|
|
sections[SECTION_ANSWER] = NULL;
|
2016-03-21 06:50:43 -05:00
|
|
|
else goto error;
|
|
|
|
|
|
|
|
if (!(r = _getdns_dict_set_this_list(result, "authority",
|
2016-06-07 09:52:10 -05:00
|
|
|
sections[SECTION_AUTHORITY])))
|
|
|
|
sections[SECTION_AUTHORITY] = NULL;
|
2016-03-21 06:50:43 -05:00
|
|
|
else goto error;
|
|
|
|
|
|
|
|
if (!(r = _getdns_dict_set_this_list(result, "additional",
|
2016-06-07 09:52:10 -05:00
|
|
|
sections[SECTION_ADDITIONAL])))
|
|
|
|
sections[SECTION_ADDITIONAL] = NULL;
|
2016-03-21 06:50:43 -05:00
|
|
|
else goto error;
|
2016-03-14 04:02:01 -05:00
|
|
|
|
|
|
|
*wire_len -= (eop - *wire);
|
|
|
|
*wire = eop;
|
|
|
|
error:
|
|
|
|
getdns_dict_destroy(rr_dict);
|
2016-06-07 09:52:10 -05:00
|
|
|
getdns_list_destroy(sections[SECTION_ADDITIONAL]);
|
|
|
|
getdns_list_destroy(sections[SECTION_AUTHORITY]);
|
|
|
|
getdns_list_destroy(sections[SECTION_ANSWER]);
|
2016-03-14 04:02:01 -05:00
|
|
|
getdns_dict_destroy(header);
|
|
|
|
if (r)
|
|
|
|
getdns_dict_destroy(result);
|
|
|
|
else
|
|
|
|
*msg_dict = result;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
_getdns_wire2msg_dict(struct mem_funcs *mf,
|
|
|
|
const uint8_t *wire, size_t wire_len, getdns_dict **msg_dict)
|
|
|
|
{
|
|
|
|
return _getdns_wire2msg_dict_scan(mf, &wire, &wire_len, msg_dict);
|
|
|
|
}
|
|
|
|
getdns_return_t
|
|
|
|
getdns_wire2msg_dict(
|
|
|
|
const uint8_t *wire, size_t wire_len, getdns_dict **msg_dict)
|
|
|
|
{
|
|
|
|
return _getdns_wire2msg_dict(
|
|
|
|
&_getdns_plain_mem_funcs, wire, wire_len, msg_dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
_getdns_wire2msg_dict_buf(struct mem_funcs *mf,
|
|
|
|
const uint8_t *wire, size_t *wire_len, getdns_dict **msg_dict)
|
|
|
|
{
|
|
|
|
size_t my_wire_len;
|
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if (!wire_len)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
else
|
|
|
|
my_wire_len = *wire_len;
|
|
|
|
|
|
|
|
if ((r = _getdns_wire2msg_dict_scan(mf, &wire, &my_wire_len, msg_dict)))
|
|
|
|
return r;
|
|
|
|
|
|
|
|
*wire_len -= my_wire_len;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
getdns_return_t
|
|
|
|
getdns_wire2msg_dict_buf(
|
|
|
|
const uint8_t *wire, size_t *wire_len, getdns_dict **msg_dict)
|
|
|
|
{
|
|
|
|
return _getdns_wire2msg_dict_buf(
|
|
|
|
&_getdns_plain_mem_funcs, wire, wire_len, msg_dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_wire2msg_dict_scan(
|
|
|
|
const uint8_t **wire, size_t *wire_len, getdns_dict **msg_dict)
|
|
|
|
{
|
|
|
|
return _getdns_wire2msg_dict_scan(
|
|
|
|
&_getdns_plain_mem_funcs, wire, wire_len, msg_dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SET_HEADER_INT(X,Y) \
|
|
|
|
if (!getdns_dict_get_int(reply, "/header/" #X, &n)) \
|
|
|
|
GLDNS_ ## Y ## _SET(header, n);
|
|
|
|
#define SET_HEADER_BIT(X,Y) \
|
2016-04-26 08:54:58 -05:00
|
|
|
if (!getdns_dict_get_int(reply, "/header/" #X, &n)) { \
|
|
|
|
if (n) GLDNS_ ## Y ## _SET(header); \
|
|
|
|
else GLDNS_ ## Y ## _CLR(header); \
|
|
|
|
}
|
2016-03-14 04:02:01 -05:00
|
|
|
|
2016-04-25 10:21:04 -05:00
|
|
|
getdns_return_t
|
|
|
|
_getdns_reply_dict2wire(
|
2016-04-26 08:54:58 -05:00
|
|
|
const getdns_dict *reply, gldns_buffer *buf, int reuse_header)
|
2016-03-14 04:02:01 -05:00
|
|
|
{
|
2016-04-25 10:21:04 -05:00
|
|
|
uint8_t header_spc[GLDNS_HEADER_SIZE], *header;
|
2016-03-14 04:02:01 -05:00
|
|
|
uint32_t n, qtype, qclass = GETDNS_RRCLASS_IN;
|
|
|
|
size_t pkt_start, i;
|
|
|
|
getdns_list *section;
|
|
|
|
getdns_dict *rr_dict;
|
|
|
|
getdns_bindata *qname;
|
|
|
|
|
2016-04-25 10:21:04 -05:00
|
|
|
pkt_start = gldns_buffer_position(buf);
|
|
|
|
if (reuse_header) {
|
|
|
|
if (gldns_buffer_remaining(buf) < GLDNS_HEADER_SIZE)
|
|
|
|
return GETDNS_RETURN_NEED_MORE_SPACE;
|
|
|
|
header = gldns_buffer_current(buf);
|
|
|
|
gldns_buffer_skip(buf, GLDNS_HEADER_SIZE);
|
|
|
|
} else
|
|
|
|
(void) memset((header = header_spc), 0, GLDNS_HEADER_SIZE);
|
|
|
|
|
2016-03-14 04:02:01 -05:00
|
|
|
SET_HEADER_INT(id, ID);
|
|
|
|
SET_HEADER_BIT(qr, QR);
|
|
|
|
SET_HEADER_BIT(aa, AA);
|
|
|
|
SET_HEADER_BIT(tc, TC);
|
|
|
|
SET_HEADER_BIT(rd, RD);
|
|
|
|
SET_HEADER_BIT(cd, CD);
|
|
|
|
SET_HEADER_BIT(ra, RA);
|
|
|
|
SET_HEADER_BIT(ad, AD);
|
|
|
|
SET_HEADER_INT(opcode, OPCODE);
|
|
|
|
SET_HEADER_INT(rcode, RCODE);
|
|
|
|
SET_HEADER_BIT(z, Z);
|
|
|
|
|
2016-04-25 10:21:04 -05:00
|
|
|
if (!reuse_header)
|
2016-04-26 08:54:58 -05:00
|
|
|
gldns_buffer_write(buf, header, GLDNS_HEADER_SIZE);
|
2016-03-14 04:02:01 -05:00
|
|
|
|
|
|
|
if (!getdns_dict_get_bindata(reply, "/question/qname", &qname) &&
|
|
|
|
!getdns_dict_get_int(reply, "/question/qtype", &qtype)) {
|
|
|
|
(void)getdns_dict_get_int(reply, "/question/qclass", &qclass);
|
|
|
|
gldns_buffer_write(buf, qname->data, qname->size);
|
|
|
|
gldns_buffer_write_u16(buf, (uint16_t)qtype);
|
|
|
|
gldns_buffer_write_u16(buf, (uint16_t)qclass);
|
|
|
|
gldns_buffer_write_u16_at(buf, pkt_start+GLDNS_QDCOUNT_OFF, 1);
|
2016-04-25 10:21:04 -05:00
|
|
|
if (reuse_header) {
|
|
|
|
gldns_buffer_write_u16_at(
|
|
|
|
buf, pkt_start+GLDNS_ANCOUNT_OFF, 0);
|
|
|
|
gldns_buffer_write_u16_at(
|
|
|
|
buf, pkt_start+GLDNS_NSCOUNT_OFF, 0);
|
|
|
|
gldns_buffer_write_u16_at(
|
|
|
|
buf, pkt_start+GLDNS_ARCOUNT_OFF, 0);
|
|
|
|
}
|
2016-03-14 04:02:01 -05:00
|
|
|
}
|
|
|
|
if (!getdns_dict_get_list(reply, "answer", §ion)) {
|
|
|
|
for ( n = 0, i = 0
|
|
|
|
; !getdns_list_get_dict(section, i, &rr_dict); i++) {
|
|
|
|
|
|
|
|
if (!_getdns_rr_dict2wire(rr_dict, buf))
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
gldns_buffer_write_u16_at(buf, pkt_start+GLDNS_ANCOUNT_OFF, n);
|
|
|
|
}
|
|
|
|
if (!getdns_dict_get_list(reply, "authority", §ion)) {
|
|
|
|
for ( n = 0, i = 0
|
|
|
|
; !getdns_list_get_dict(section, i, &rr_dict); i++) {
|
|
|
|
|
|
|
|
if (!_getdns_rr_dict2wire(rr_dict, buf))
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
gldns_buffer_write_u16_at(buf, pkt_start+GLDNS_NSCOUNT_OFF, n);
|
|
|
|
}
|
|
|
|
if (!getdns_dict_get_list(reply, "additional", §ion)) {
|
|
|
|
for ( n = 0, i = 0
|
|
|
|
; !getdns_list_get_dict(section, i, &rr_dict); i++) {
|
|
|
|
|
|
|
|
if (!_getdns_rr_dict2wire(rr_dict, buf))
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
gldns_buffer_write_u16_at(buf, pkt_start+GLDNS_ARCOUNT_OFF, n);
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
2016-04-25 10:21:04 -05:00
|
|
|
getdns_return_t
|
2016-03-14 04:02:01 -05:00
|
|
|
_getdns_msg_dict2wire_buf(const getdns_dict *msg_dict, gldns_buffer *gbuf)
|
|
|
|
{
|
|
|
|
getdns_return_t r;
|
|
|
|
getdns_list *replies;
|
|
|
|
getdns_dict *reply;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if ((r = getdns_dict_get_list(msg_dict, "replies_tree", &replies))) {
|
|
|
|
if (r != GETDNS_RETURN_NO_SUCH_DICT_NAME)
|
|
|
|
return r;
|
2016-04-25 10:21:04 -05:00
|
|
|
return _getdns_reply_dict2wire(msg_dict, gbuf, 0);
|
2016-03-14 04:02:01 -05:00
|
|
|
}
|
|
|
|
for (i = 0; r == GETDNS_RETURN_GOOD; i++) {
|
|
|
|
if (!(r = getdns_list_get_dict(replies, i, &reply)))
|
2016-04-25 10:21:04 -05:00
|
|
|
r = _getdns_reply_dict2wire(reply, gbuf, 0);
|
2016-03-14 04:02:01 -05:00
|
|
|
}
|
|
|
|
return r == GETDNS_RETURN_NO_SUCH_LIST_ITEM ? GETDNS_RETURN_GOOD : r;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_msg_dict2wire(
|
|
|
|
const getdns_dict *msg_dict, uint8_t **wire, size_t *wire_sz)
|
|
|
|
{
|
|
|
|
uint8_t buf_spc[4096], *buf;
|
|
|
|
size_t buf_len = sizeof(buf_spc);
|
|
|
|
getdns_return_t r = getdns_msg_dict2wire_buf(
|
|
|
|
msg_dict, buf_spc, &buf_len);
|
|
|
|
|
|
|
|
if (r != GETDNS_RETURN_GOOD && r != GETDNS_RETURN_NEED_MORE_SPACE)
|
|
|
|
return r;
|
|
|
|
|
2016-04-29 05:00:17 -05:00
|
|
|
if (!(buf = malloc(buf_len ? buf_len : 1)))
|
2016-03-14 04:02:01 -05:00
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
memcpy(buf, buf_spc, buf_len);
|
|
|
|
|
|
|
|
else if ((r = getdns_msg_dict2wire_buf(msg_dict, buf, &buf_len))) {
|
|
|
|
free(buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
*wire = buf;
|
|
|
|
*wire_sz = buf_len;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_msg_dict2wire_buf(
|
|
|
|
const getdns_dict *msg_dict, uint8_t *wire, size_t *wire_sz)
|
|
|
|
{
|
|
|
|
int my_wire_sz;
|
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if (!wire_sz)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
else
|
|
|
|
my_wire_sz = *wire_sz;
|
|
|
|
|
|
|
|
r = getdns_msg_dict2wire_scan(msg_dict, &wire, &my_wire_sz);
|
|
|
|
if (r == GETDNS_RETURN_GOOD || r == GETDNS_RETURN_NEED_MORE_SPACE)
|
|
|
|
*wire_sz -= my_wire_sz;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_msg_dict2wire_scan(
|
|
|
|
const getdns_dict *msg_dict, uint8_t **wire, int *wire_sz)
|
|
|
|
{
|
|
|
|
getdns_return_t r;
|
|
|
|
gldns_buffer gbuf;
|
|
|
|
|
2016-04-25 10:21:04 -05:00
|
|
|
if (!msg_dict || !wire || !wire_sz || (!*wire && *wire_sz))
|
2016-03-14 04:02:01 -05:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
gldns_buffer_init_frm_data(&gbuf, *wire, *wire_sz);
|
|
|
|
if ((r = _getdns_msg_dict2wire_buf(msg_dict, &gbuf)))
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (gldns_buffer_position(&gbuf) == 0)
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
|
|
|
*wire += gldns_buffer_position(&gbuf);
|
|
|
|
*wire_sz -= gldns_buffer_position(&gbuf);
|
|
|
|
if (gldns_buffer_position(&gbuf) > gldns_buffer_limit(&gbuf))
|
|
|
|
return GETDNS_RETURN_NEED_MORE_SPACE;
|
|
|
|
else
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_msg_dict2str(
|
|
|
|
const getdns_dict *msg_dict, char **str)
|
|
|
|
{
|
|
|
|
char buf_spc[4096], *buf;
|
|
|
|
size_t buf_len = sizeof(buf_spc) - 1;
|
|
|
|
getdns_return_t r = getdns_msg_dict2str_buf(
|
|
|
|
msg_dict, buf_spc, &buf_len);
|
|
|
|
|
|
|
|
if (r != GETDNS_RETURN_GOOD && r != GETDNS_RETURN_NEED_MORE_SPACE)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
buf_len += 1;
|
|
|
|
if (!(buf = malloc(buf_len)))
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
memcpy(buf, buf_spc, buf_len);
|
|
|
|
|
|
|
|
else if ((r = getdns_msg_dict2str_buf(msg_dict, buf, &buf_len))) {
|
|
|
|
free(buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
*str = buf;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_msg_dict2str_buf(
|
|
|
|
const getdns_dict *msg_dict, char *str, size_t *str_len)
|
|
|
|
{
|
|
|
|
int my_str_len;
|
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if (!str_len)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
else
|
|
|
|
my_str_len = *str_len;
|
|
|
|
|
|
|
|
r = getdns_msg_dict2str_scan(msg_dict, &str, &my_str_len);
|
|
|
|
if (r == GETDNS_RETURN_GOOD || r == GETDNS_RETURN_NEED_MORE_SPACE)
|
|
|
|
*str_len -= my_str_len;
|
|
|
|
return r;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_msg_dict2str_scan(
|
|
|
|
const getdns_dict *msg_dict, char **str, int *str_len)
|
|
|
|
{
|
|
|
|
getdns_return_t r;
|
|
|
|
gldns_buffer gbuf;
|
|
|
|
uint8_t buf_spc[4096], *buf = buf_spc, *scan_buf;
|
|
|
|
size_t sz, scan_sz;
|
|
|
|
int prev_str_len;
|
|
|
|
char *prev_str;
|
|
|
|
int sz_needed;
|
|
|
|
|
|
|
|
if (!msg_dict || !str || !*str || !str_len)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
gldns_buffer_init_frm_data(&gbuf, buf, sizeof(buf_spc));
|
|
|
|
r = _getdns_msg_dict2wire_buf(msg_dict, &gbuf);
|
|
|
|
if (gldns_buffer_position(&gbuf) > sizeof(buf_spc)) {
|
|
|
|
if (!(buf = GETDNS_XMALLOC(
|
|
|
|
msg_dict->mf, uint8_t, (sz = gldns_buffer_position(&gbuf))))) {
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
}
|
|
|
|
gldns_buffer_init_frm_data(&gbuf, buf, sz);
|
|
|
|
r = _getdns_msg_dict2wire_buf(msg_dict, &gbuf);
|
|
|
|
}
|
|
|
|
if (r) {
|
|
|
|
if (buf != buf_spc)
|
|
|
|
GETDNS_FREE(msg_dict->mf, buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
scan_buf = gldns_buffer_begin(&gbuf);
|
|
|
|
scan_sz = gldns_buffer_position(&gbuf);
|
|
|
|
prev_str = *str;
|
|
|
|
prev_str_len = *str_len;
|
|
|
|
sz = (size_t)*str_len;
|
|
|
|
sz_needed = gldns_wire2str_pkt_scan(
|
|
|
|
&scan_buf, &scan_sz, str, &sz);
|
|
|
|
|
|
|
|
if (sz_needed > prev_str_len) {
|
|
|
|
*str = prev_str + sz_needed;
|
|
|
|
*str_len = prev_str_len - sz_needed;
|
|
|
|
r = GETDNS_RETURN_NEED_MORE_SPACE;
|
|
|
|
} else {
|
|
|
|
*str_len = sz;
|
|
|
|
**str = 0;
|
|
|
|
}
|
|
|
|
if (buf != buf_spc)
|
|
|
|
GETDNS_FREE(msg_dict->mf, buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-28 08:30:01 -06:00
|
|
|
/* convert.c */
|