getdns/src/convert.c

165 lines
4.5 KiB
C
Raw Normal View History

2013-07-15 17:43:30 -05:00
/**
*
* /brief getdns core functions
2013-10-17 18:45:25 -05:00
*
2013-07-15 17:43:30 -05:00
* This is the meat of the API
* Originally taken from the getdns API description pseudo implementation.
*
*/
/* The MIT License (MIT)
* Copyright (c) 2013 Verisign, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2013-10-17 18:45:25 -05:00
*
2013-07-15 17:43:30 -05:00
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2013-10-17 18:45:25 -05:00
*
2013-07-15 17:43:30 -05:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2013-08-15 09:16:15 -05:00
#include <getdns/getdns.h>
2013-10-18 12:55:31 -05:00
#include <getdns/getdns_error.h>
#include <stdio.h>
2013-10-17 18:45:25 -05:00
#include <arpa/inet.h>
#include <util-internal.h>
#include <locale.h>
#include <stringprep.h>
#include <idna.h>
#include <ldns/ldns.h>
2013-07-15 17:43:30 -05:00
/* stuff to make it compile pedantically */
#define UNUSED_PARAM(x) ((void)(x))
char *
getdns_convert_dns_name_to_fqdn(char *name_from_dns_response)
{
char *str;
ldns_rdf *rdf = ldns_rdf_new_frm_data( LDNS_RDF_TYPE_DNAME
, strlen(name_from_dns_response)+1
, name_from_dns_response
);
if (!rdf) return NULL;
str = ldns_rdf2str(rdf);
ldns_rdf_free(rdf);
return str;
}
2013-07-15 17:43:30 -05:00
char *
getdns_convert_fqdn_to_dns_name(char *fqdn_as_string)
{
UNUSED_PARAM(fqdn_as_string);
return NULL;
}
2013-07-15 17:43:30 -05: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 *
getdns_convert_ulabel_to_alabel(char *ulabel)
{
int ret;
char *buf;
char *prepped;
setlocale(LC_ALL, "");
if ((prepped = stringprep_locale_to_utf8(ulabel)) == 0)
return 0;
if ((ret = stringprep(prepped, BUFSIZ, 0, stringprep_nameprep)) != STRINGPREP_OK)
return 0;
if ((ret = idna_to_ascii_8z(prepped, &buf, 0)) != IDNA_SUCCESS) {
return 0;
}
return buf;
}
2013-07-15 17:43:30 -05: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 *
getdns_convert_alabel_to_ulabel(char *alabel)
{
int ret; /* just in case we might want to use it someday */
char *buf;
if ((ret = idna_to_unicode_8z8z(alabel, &buf, 0)) != IDNA_SUCCESS) {
return NULL;
}
return buf;
}
2013-07-15 17:43:30 -05:00
2013-07-15 17:43:30 -05:00
char *
getdns_display_ip_address(struct getdns_bindata
*bindata_of_ipv4_or_ipv6_address)
2013-10-17 18:45:25 -05: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
getdns_return_t
getdns_strerror(getdns_return_t err, char *buf, size_t buflen)
{
getdns_return_t retval = GETDNS_RETURN_GOOD;
const char *err_str = getdns_get_errorstr_by_id(err);
if (!err_str) {
return GETDNS_RETURN_GENERIC_ERROR;
}
snprintf(buf, buflen, "%s", err_str);
return retval;
} /* getdns_strerror */
2013-07-15 17:43:30 -05:00
/* getdns_core_only.c */