mirror of https://github.com/getdnsapi/getdns.git
Generalize str2ipaddr_dict conversion
This commit is contained in:
parent
8318766593
commit
b99b7511ec
|
@ -499,8 +499,23 @@ getdns_return_t parse_args(int argc, char **argv)
|
|||
continue;
|
||||
|
||||
} else if (arg[0] == '@') {
|
||||
getdns_dict *upstream = _getdns_ipaddr_dict(arg + 1);
|
||||
if (upstream) {
|
||||
getdns_dict *upstream;
|
||||
getdns_bindata *address;
|
||||
|
||||
if ((r = getdns_str2dict(arg + 1, &upstream)))
|
||||
fprintf(stderr, "Could not convert \"%s\" to "
|
||||
"an IP dict: %s\n", arg + 1,
|
||||
getdns_get_errorstr_by_id(r));
|
||||
|
||||
else if ((r = getdns_dict_get_bindata(
|
||||
upstream, "address_data", &address))) {
|
||||
|
||||
fprintf(stderr, "\"%s\" did not translate to "
|
||||
"an IP dict: %s\n", arg + 1,
|
||||
getdns_get_errorstr_by_id(r));
|
||||
|
||||
getdns_dict_destroy(upstream);
|
||||
} else {
|
||||
if (!upstream_list &&
|
||||
!(upstream_list =
|
||||
getdns_list_create_with_context(context))){
|
||||
|
@ -521,6 +536,9 @@ getdns_return_t parse_args(int argc, char **argv)
|
|||
continue;
|
||||
}
|
||||
for (c = arg+1; *c; c++) {
|
||||
getdns_dict *downstream;
|
||||
getdns_bindata *address;
|
||||
|
||||
switch (*c) {
|
||||
case 'a':
|
||||
async = 1;
|
||||
|
@ -896,23 +914,33 @@ getdns_return_t parse_args(int argc, char **argv)
|
|||
DEBUG_SERVER("Clear listen list\n");
|
||||
break;
|
||||
}
|
||||
getdns_dict *downstream =
|
||||
_getdns_ipaddr_dict(argv[i]);
|
||||
if (!downstream) {
|
||||
fprintf(stderr, "could not parse "
|
||||
"listen address: %s", argv[i]);
|
||||
|
||||
if ((r = getdns_str2dict(argv[i], &downstream)))
|
||||
fprintf(stderr, "Could not convert \"%s\" to "
|
||||
"an IP dict: %s\n", argv[i],
|
||||
getdns_get_errorstr_by_id(r));
|
||||
|
||||
else if ((r = getdns_dict_get_bindata(
|
||||
downstream, "address_data", &address))) {
|
||||
|
||||
fprintf(stderr, "\"%s\" did not translate to "
|
||||
"an IP dict: %s\n", argv[i],
|
||||
getdns_get_errorstr_by_id(r));
|
||||
|
||||
getdns_dict_destroy(downstream);
|
||||
} else {
|
||||
if (!listen_list &&
|
||||
!(listen_list =
|
||||
getdns_list_create_with_context(context))){
|
||||
fprintf(stderr, "Could not create "
|
||||
"downstream list\n");
|
||||
return GETDNS_RETURN_MEMORY_ERROR;
|
||||
}
|
||||
getdns_list_set_dict(listen_list,
|
||||
listen_count++, downstream);
|
||||
getdns_dict_destroy(downstream);
|
||||
touched_listen_list = 1;
|
||||
}
|
||||
if (!listen_list &&
|
||||
!(listen_list =
|
||||
getdns_list_create_with_context(context))){
|
||||
fprintf(stderr, "Could not create "
|
||||
"downstram list\n");
|
||||
return GETDNS_RETURN_MEMORY_ERROR;
|
||||
}
|
||||
getdns_list_set_dict(listen_list,
|
||||
listen_count++, downstream);
|
||||
getdns_dict_destroy(downstream);
|
||||
touched_listen_list = 1;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Unknown option "
|
||||
|
@ -976,14 +1004,26 @@ getdns_return_t do_the_call(void)
|
|||
{
|
||||
getdns_return_t r;
|
||||
getdns_dict *address = NULL;
|
||||
getdns_bindata *address_bindata;
|
||||
getdns_dict *response = NULL;
|
||||
char *response_str;
|
||||
uint32_t status;
|
||||
|
||||
if (calltype == HOSTNAME &&
|
||||
!(address = _getdns_ipaddr_dict(name))) {
|
||||
fprintf(stderr, "Could not convert \"%s\" "
|
||||
"to an IP address", name);
|
||||
if (calltype != HOSTNAME)
|
||||
; /* pass */
|
||||
|
||||
else if ((r = getdns_str2dict(name, &address))) {
|
||||
|
||||
fprintf(stderr, "Could not convert \"%s\" to an IP dict: %s\n"
|
||||
, name, getdns_get_errorstr_by_id(r));
|
||||
return GETDNS_RETURN_GOOD;
|
||||
|
||||
} else if ((r = getdns_dict_get_bindata(
|
||||
address, "address_data", &address_bindata))) {
|
||||
|
||||
fprintf(stderr, "Could not convert \"%s\" to an IP dict: %s\n"
|
||||
, name, getdns_get_errorstr_by_id(r));
|
||||
getdns_dict_destroy(address);
|
||||
return GETDNS_RETURN_GOOD;
|
||||
}
|
||||
if (async) {
|
||||
|
|
|
@ -233,21 +233,6 @@ static int _jsmn_get_ipdict(struct mem_funcs *mf, const char *js, jsmntok_t *t,
|
|||
return *value != NULL;
|
||||
}
|
||||
|
||||
getdns_dict *
|
||||
_getdns_ipaddr_dict(const char *ipstr)
|
||||
{
|
||||
char value_str[3072];
|
||||
size_t size = strlen(ipstr);
|
||||
|
||||
if (size >= sizeof(value_str))
|
||||
return NULL;
|
||||
|
||||
(void) memcpy(value_str, ipstr, size);
|
||||
value_str[size] = '\0';
|
||||
|
||||
return _getdns_ipaddr_dict_mf(&_getdns_plain_mem_funcs, value_str);
|
||||
}
|
||||
|
||||
static int _jsmn_get_data(struct mem_funcs *mf, const char *js, jsmntok_t *t,
|
||||
getdns_bindata **value)
|
||||
{
|
||||
|
@ -658,8 +643,48 @@ getdns_str2dict(const char *str, getdns_dict **dict)
|
|||
return r;
|
||||
|
||||
else if (item.dtype != t_dict) {
|
||||
uint8_t buf[16];
|
||||
getdns_dict *dict_r;
|
||||
|
||||
if (item.dtype != t_bindata)
|
||||
r = GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
||||
|
||||
else if (item.data.bindata->size == 4 &&
|
||||
inet_pton(AF_INET, str, buf) == 1) {
|
||||
|
||||
if (!(dict_r = getdns_dict_create()))
|
||||
r = GETDNS_RETURN_MEMORY_ERROR;
|
||||
|
||||
else if ((r = getdns_dict_util_set_string(
|
||||
dict_r, "address_type", "IPv4")))
|
||||
getdns_dict_destroy(dict_r);
|
||||
|
||||
else if ((r = getdns_dict_set_bindata(
|
||||
dict_r, "address_data", item.data.bindata)))
|
||||
getdns_dict_destroy(dict_r);
|
||||
else
|
||||
*dict = dict_r;
|
||||
|
||||
} else if (item.data.bindata->size == 16 &&
|
||||
inet_pton(AF_INET6, str, buf) == 1) {
|
||||
|
||||
if (!(dict_r = getdns_dict_create()))
|
||||
r = GETDNS_RETURN_MEMORY_ERROR;
|
||||
|
||||
else if ((r = getdns_dict_util_set_string(
|
||||
dict_r, "address_type", "IPv6")))
|
||||
getdns_dict_destroy(dict_r);
|
||||
|
||||
else if ((r = getdns_dict_set_bindata(
|
||||
dict_r, "address_data", item.data.bindata)))
|
||||
getdns_dict_destroy(dict_r);
|
||||
else
|
||||
*dict = dict_r;
|
||||
} else
|
||||
r = GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
||||
|
||||
_getdns_destroy_item_data(&_getdns_plain_mem_funcs, &item);
|
||||
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
||||
return r;
|
||||
}
|
||||
*dict = item.data.dict;
|
||||
return GETDNS_RETURN_GOOD;
|
||||
|
|
|
@ -29,8 +29,6 @@
|
|||
#define GETDNS_STR2DICT_H_
|
||||
#include "getdns/getdns.h"
|
||||
|
||||
getdns_dict *_getdns_ipaddr_dict(const char *ipstr);
|
||||
|
||||
getdns_return_t getdns_str2dict(const char *str, getdns_dict **dict);
|
||||
getdns_return_t getdns_str2list(const char *str, getdns_list **list);
|
||||
getdns_return_t getdns_str2bindata(const char *str, getdns_bindata **bindata);
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit daa17063c67f0dfe873af25ab6b664641c8cf90c
|
||||
Subproject commit 49024a6e11739c866bce0e9f3617278b98906ad0
|
Loading…
Reference in New Issue