Exemplo n.º 1
0
getdns_list* GNUtil::convertToList(Local<Array> array) {
    uint32_t len = array->Length();
    getdns_list* result = getdns_list_create();
    for (uint32_t i = 0; i < len; ++i) {
        size_t idx = getdns_list_get_length(result, &idx);
        Local<Value> val = array->Get(i);
        GetdnsType type = getGetdnsType(val);
        switch (type) {
            case IntType:
                getdns_list_set_int(result, idx, val->ToUint32()->Value());
                break;
            case BoolType:
                if (val->IsTrue()) {
                    getdns_list_set_int(result, idx, GETDNS_EXTENSION_TRUE);
                } else {
                    getdns_list_set_int(result, idx, GETDNS_EXTENSION_FALSE);
                }
                break;
            case StringType:
                {
                    struct getdns_bindata strdata;
                    String::Utf8Value utf8Str(val->ToString());
                    int len = utf8Str.length();
                    strdata.data = (uint8_t*) *utf8Str;
                    strdata.size = len;
                    getdns_list_set_bindata(result, idx, &strdata);
                }
                break;
            case BinDataType:
                {
                    struct getdns_bindata bdata;
                    bdata.data = (uint8_t*) node::Buffer::Data(val);
                    bdata.size = node::Buffer::Length(val);
                    getdns_list_set_bindata(result, idx, &bdata);
                }
                break;
            case ListType:
                {
                    Local<Array> subArray = Local<Array>::Cast(val);
                    struct getdns_list* sublist = GNUtil::convertToList(subArray);
                    getdns_list_set_list(result, idx, sublist);
                    getdns_list_destroy(sublist);
                }
                break;
            case DictType:
                {
                    Local<Object> subObj = val->ToObject();
                    struct getdns_dict* subdict = GNUtil::convertToDict(subObj);
                    if (subdict) {
                        getdns_list_set_dict(result, idx, subdict);
                        getdns_dict_destroy(subdict);
                    }
                }
                break;
            default:
                break;
        }
    }
    return result;
}
static getdns_return_t set_cookie(getdns_dict *exts, char *cookie)
{
	uint8_t data[40];
	size_t i;
	getdns_return_t r = GETDNS_RETURN_GENERIC_ERROR;
	getdns_bindata bindata;

	getdns_dict *opt_parameters = getdns_dict_create();
	getdns_list *options = getdns_list_create();
	getdns_dict *option = getdns_dict_create();

	if (*cookie == '=')
		cookie++;

	for (i = 0; i < 40 && *cookie; i++) {
		if (*cookie >= '0' && *cookie <= '9')
			data[i] = (uint8_t)(*cookie - '0') << 4;
		else if (*cookie >= 'a' && *cookie <= 'f')
			data[i] = (uint8_t)(*cookie - 'a' + 10) << 4;
		else if (*cookie >= 'A' && *cookie <= 'F')
			data[i] = (uint8_t)(*cookie - 'A' + 10) << 4;
		else
			goto done;
		cookie++;
		if (*cookie >= '0' && *cookie <= '9')
			data[i] |= (uint8_t)(*cookie - '0');
		else if (*cookie >= 'a' && *cookie <= 'f')
			data[i] |= (uint8_t)(*cookie - 'a' + 10);
		else if (*cookie >= 'A' && *cookie <= 'F')
			data[i] |= (uint8_t)(*cookie - 'A' + 10);
		else
			goto done;
		cookie++;;
	}
	bindata.data = data;
	bindata.size = i;
	if ((r = getdns_dict_set_int(option, "option_code", 65001)))
		goto done;
	if ((r = getdns_dict_set_bindata(option, "option_data", &bindata)))
		goto done;
	if ((r = getdns_list_set_dict(options, 0, option)))
		goto done;
	if ((r = getdns_dict_set_list(opt_parameters, "options", options)))
		goto done;
	r = getdns_dict_set_dict(exts, "add_opt_parameters", opt_parameters);
done:
	getdns_dict_destroy(option);
	getdns_list_destroy(options);
	getdns_dict_destroy(opt_parameters);
	return r;
}
Exemplo n.º 3
0
static getdns_return_t validate_chain(getdns_dict *response)
{
	getdns_return_t r;
	getdns_list *validation_chain;
	getdns_list *replies_tree;
	getdns_dict *reply;
	getdns_list *to_validate;
	getdns_list *trust_anchor;
	size_t i;
	int s;
	
	if (!(to_validate = getdns_list_create()))
		return GETDNS_RETURN_MEMORY_ERROR;

	trust_anchor = getdns_root_trust_anchor(NULL);

	if ((r = getdns_dict_get_list(
	    response, "validation_chain", &validation_chain)))
		goto error;

	if ((r = getdns_dict_get_list(
	    response, "replies_tree", &replies_tree)))
		goto error;

	fprintf(stdout, "replies_tree dnssec_status: ");
	switch ((s = getdns_validate_dnssec(
	    replies_tree, validation_chain, trust_anchor))) {

	case GETDNS_DNSSEC_SECURE:
		fprintf(stdout, "GETDNS_DNSSEC_SECURE\n");
		break;
	case GETDNS_DNSSEC_BOGUS:
		fprintf(stdout, "GETDNS_DNSSEC_BOGUS\n");
		break;
	case GETDNS_DNSSEC_INDETERMINATE:
		fprintf(stdout, "GETDNS_DNSSEC_INDETERMINATE\n");
		break;
	case GETDNS_DNSSEC_INSECURE:
		fprintf(stdout, "GETDNS_DNSSEC_INSECURE\n");
		break;
	case GETDNS_DNSSEC_NOT_PERFORMED:
		fprintf(stdout, "GETDNS_DNSSEC_NOT_PERFORMED\n");
		break;
	default:
		fprintf(stdout, "%d\n", (int)s);
	}

	i = 0;
	while (!(r = getdns_list_get_dict(replies_tree, i++, &reply))) {

		if ((r = getdns_list_set_dict(to_validate, 0, reply)))
			goto error;

		fprintf( stdout
		       , "reply %zu, dnssec_status: ", i);
		switch ((s = getdns_validate_dnssec(
		    to_validate, validation_chain, trust_anchor))) {

		case GETDNS_DNSSEC_SECURE:
			fprintf(stdout, "GETDNS_DNSSEC_SECURE\n");
			break;
		case GETDNS_DNSSEC_BOGUS:
			fprintf(stdout, "GETDNS_DNSSEC_BOGUS\n");
			break;
		case GETDNS_DNSSEC_INDETERMINATE:
			fprintf(stdout, "GETDNS_DNSSEC_INDETERMINATE\n");
			break;
		case GETDNS_DNSSEC_INSECURE:
			fprintf(stdout, "GETDNS_DNSSEC_INSECURE\n");
			break;
		case GETDNS_DNSSEC_NOT_PERFORMED:
			fprintf(stdout, "GETDNS_DNSSEC_NOT_PERFORMED\n");
			break;
		default:
			fprintf(stdout, "%d\n", (int)s);
		}
	}
	if (r == GETDNS_RETURN_NO_SUCH_LIST_ITEM)
		r = GETDNS_RETURN_GOOD;
error:
	getdns_list_destroy(trust_anchor);
	getdns_list_destroy(to_validate);

	return GETDNS_RETURN_GOOD;
}
Exemplo n.º 4
0
/**
 * test the list get and set routines 
 */
void
tst_bindatasetget(void)
{
	char msg[TSTMSGBUF];
	size_t index = 0;
	getdns_return_t retval;
	struct getdns_list *list = NULL;
	struct getdns_bindata *new_bindata = NULL;
	struct getdns_bindata *ans_bindata = NULL;

	tstmsg_case_begin("tst_bindatasetget");

	list = getdns_list_create();

	/* test get function against empty list and with bogus params */

	tstmsg_case_msg("getdns_list_get_bindata() empty list");
	retval = getdns_list_get_bindata(NULL, index, &ans_bindata);
	snprintf(msg, sizeof(msg),
	    "getdns_list_get_bindata(NULL, index, &ans_bindata),retval = %d",
	    retval);
	tstmsg_case_msg(msg);

	retval = getdns_list_get_bindata(list, index, NULL);
	snprintf(msg, sizeof(msg), "getdns_list_get_bindata(list, index, NULL),retval = %d",
	    retval);
	tstmsg_case_msg(msg);

	tstmsg_case_msg("getdns_list_get_bindata(list, 0, &ans_bindata)");
	retval = getdns_list_get_bindata(list, 0, &ans_bindata);
	snprintf(msg, sizeof(msg), "getdns_list_get_bindata,retval = %d", retval);
	tstmsg_case_msg(msg);

	tstmsg_case_msg("getdns_list_get_bindata(list, 1, &ans_bindata)");
	retval = getdns_list_get_bindata(list, 1, &ans_bindata);
	snprintf(msg, sizeof(msg), "getdns_list_get_bindata,retval = %d", retval);
	tstmsg_case_msg(msg);

	/* test set function against empty list with bogus params */

	tstmsg_case_msg("getdns_list_set_bindata(list, -1, ans_bindata)");
	retval = getdns_list_set_bindata(list, -1, NULL);
	snprintf(msg, sizeof(msg), "getdns_list_set_bindata,retval = %d", retval);
	tstmsg_case_msg(msg);

	tstmsg_case_msg("getdns_list_set_bindata(list, 1, ans_bindata)");
	retval = getdns_list_set_bindata(list, 1, NULL);
	snprintf(msg, sizeof(msg), "getdns_list_set_bindata,retval = %d", retval);
	tstmsg_case_msg(msg);

	/* test set and get legitimate use case */

	new_bindata =
	    (struct getdns_bindata *) malloc(sizeof(struct getdns_bindata));
	new_bindata->size = strlen("foobar") + 1;
	new_bindata->data = (uint8_t *) "foobar";

	getdns_list_set_bindata(list, index, new_bindata);
	retval = getdns_list_get_bindata(list, index, &ans_bindata);
	snprintf(msg, sizeof(msg),
	    "getdns_list_set/get_bindata,retval = %d, bindata->data = %d,%s",
	    retval, (int) ans_bindata->size, (char *) ans_bindata->data);
	tstmsg_case_msg(msg);

	getdns_list_destroy(list);

	tstmsg_case_end();

	return;
}				/* tst_bindatasetget */
Exemplo n.º 5
0
/**
 * test the create, destroy and allocation functions
 */
void
tst_create(void)
{
	char msg[TSTMSGBUF];
	size_t index;
	int i;
	getdns_return_t retval;
	struct getdns_list *list = NULL;

	/* make sure we can do a simple create/destroy first */

	tstmsg_case_begin("tst_create");

	tstmsg_case_msg("getdns_list_create");
	list = getdns_list_create();

	if (list != NULL) {
		tstmsg_case_msg("getdns_list_destroy(list)");
		getdns_list_destroy(list);
	}

	tstmsg_case_msg("getdns_list_destroy(NULL)");
	getdns_list_destroy(NULL);

	/* add items until we force it to allocate more storage */

	tstmsg_case_msg("getdns_list_set_int(list, i) past block size");
	list = getdns_list_create();
	for (i = 0; i < GETDNS_LIST_BLOCKSZ + 2; i++) {
		retval = getdns_list_set_int(list, i, i);
		if (retval != GETDNS_RETURN_GOOD) {
			snprintf(msg, sizeof(msg), "getdns_list_set_int,i=%d,retval = %d",
			    i, retval);
			tstmsg_case_msg(msg);
		}
	}

	tstmsg_case_msg("getdns_list_get_length(list)");
	retval = getdns_list_get_length(list, &index);
	snprintf(msg, sizeof(msg), "list length = %d", (int) index);
	tstmsg_case_msg(msg);

	tstmsg_case_msg("getdns_list_get_length()");
	retval = getdns_list_get_length(NULL, &index);
	snprintf(msg, sizeof(msg), "NUll, %i, retval = %d", (int)index, retval);
	tstmsg_case_msg(msg);

	retval = getdns_list_get_length(NULL, NULL);
	snprintf(msg, sizeof(msg), "NUll, NULL, retval = %d", retval);
	tstmsg_case_msg(msg);

	retval = getdns_list_get_length(list, NULL);
	snprintf(msg, sizeof(msg), "list, NULL, retval = %d", retval);
	tstmsg_case_msg(msg);

	getdns_list_destroy(list);

	tstmsg_case_end();

	return;
}				/* tst_create */
Exemplo n.º 6
0
/**
 * test the int get and set routines 
 */
void
tst_intsetget(void)
{
	char msg[TSTMSGBUF];
	size_t index = 0;
	uint32_t ans_int;
	getdns_return_t retval;
	struct getdns_list *list = NULL;

	tstmsg_case_begin("tst_intsetget");

	list = getdns_list_create();

	/* test int get function against empty list and with bogus params */

	tstmsg_case_msg("getdns_list_get_int() empty list");
	retval = getdns_list_get_int(NULL, index, &ans_int);
	snprintf(msg, sizeof(msg), "getdns_list_get_int(NULL, index, &ans_int),retval = %d",
	    retval);
	tstmsg_case_msg(msg);

	retval = getdns_list_get_int(list, index, NULL);
	snprintf(msg, sizeof(msg), "getdns_list_get_int(list, index, NULL),retval = %d",
	    retval);
	tstmsg_case_msg(msg);

	tstmsg_case_msg("getdns_list_get_int(list, 0, &ans_int)");
	retval = getdns_list_get_int(list, 0, &ans_int);
	snprintf(msg, sizeof(msg), "getdns_list_get_int,retval = %d", retval);
	tstmsg_case_msg(msg);

	tstmsg_case_msg("getdns_list_get_int(list, 1, &ans_int)");
	retval = getdns_list_get_int(list, 1, &ans_int);
	snprintf(msg, sizeof(msg), "getdns_list_get_int,retval = %d", retval);
	tstmsg_case_msg(msg);

	/* test int set function against empty list with bogus params */

	tstmsg_case_msg("getdns_list_set_int(list, -1, ans_int)");
	retval = getdns_list_set_int(list, -1, ans_int);
	snprintf(msg, sizeof(msg), "getdns_list_set_int,retval = %d", retval);
	tstmsg_case_msg(msg);

	tstmsg_case_msg("getdns_list_set_int(list, 1, ans_int)");
	retval = getdns_list_set_int(list, 1, ans_int);
	snprintf(msg, sizeof(msg), "getdns_list_set_int,retval = %d", retval);
	tstmsg_case_msg(msg);

	/* test set and get legitimate use case */

	getdns_list_set_int(list, index, 42);
	retval = getdns_list_get_int(list, index, &ans_int);
	snprintf(msg, sizeof(msg), "getdns_list_set/get_int,retval = %d, ans = %d", retval,
	    ans_int);
	tstmsg_case_msg(msg);

	getdns_list_destroy(list);

	tstmsg_case_end();

	return;
}				/* tst_intsetget */
Exemplo n.º 7
0
/**
 * test the list get and set routines 
 */
void
tst_listsetget(void)
{
	char msg[TSTMSGBUF];
	char key[20];
	size_t index;
	uint32_t int1;
	uint32_t int2;
	getdns_return_t retval;
	struct getdns_list *newlist;
	struct getdns_list *anslist;
	struct getdns_dict *dict = NULL;

	tstmsg_case_begin("tst_listsetget");

	dict = getdns_dict_create();

	/* test get function against empty list and with bogus params */

	strcpy(key, "foo");

	tstmsg_case_msg("getdns_dict_get_list() empty dict");
	retval = getdns_dict_get_list(NULL, key, &anslist);
	snprintf(msg, sizeof(msg),
	    "test 13: getdns_dict_get_list(NULL, key, &anslist),retval = %d",
	    retval);
	tstmsg_case_msg(msg);

	retval = getdns_dict_get_list(dict, key, NULL);
	snprintf(msg, sizeof(msg),
	    "test 14: getdns_dict_get_list(dict, key, NULL),retval = %d",
	    retval);
	tstmsg_case_msg(msg);

	tstmsg_case_msg("getdns_dict_get_list(dict, NULL, &anslist)");
	retval = getdns_dict_get_list(dict, NULL, &anslist);
	snprintf(msg, sizeof(msg), "test 15: getdns_dict_get_list,retval = %d", retval);
	tstmsg_case_msg(msg);

	tstmsg_case_msg("getdns_dict_get_list(dict, key, &anslist)");
	retval = getdns_dict_get_list(dict, key, &anslist);
	snprintf(msg, sizeof(msg), "test 16: getdns_list_get_list,retval = %d", retval);
	tstmsg_case_msg(msg);

	getdns_dict_destroy(dict);

	/* TODO: test getdns_dict_set functions with bogus params */

	/* test set and get legitimate use case */

	dict = getdns_dict_create();

	strcpy(key, "foo");
	newlist = getdns_list_create();
	getdns_list_add_item(newlist, &index);
	getdns_list_set_int(newlist, index, 42);
	getdns_list_add_item(newlist, &index);
	getdns_list_set_int(newlist, index, 52);

	tstmsg_case_msg("getdns_dict_set_list(dict, key, newlist)");
	retval = getdns_dict_set_list(dict, key, newlist);
	snprintf(msg, sizeof(msg), "test 17: getdns_dict_set_list,retval=%d,key=%s",
	    retval, key);
	tstmsg_case_msg(msg);
	getdns_list_destroy(newlist);

	tstmsg_case_msg("getdns_dict_get_list(dict, key, &anslist)");
	retval = getdns_dict_get_list(dict, key, &anslist);
	getdns_list_get_int(anslist, 0, &int1);
	getdns_list_get_int(anslist, 1, &int2);
	snprintf(msg, sizeof(msg),
	    "test 18: getdns_dict_get_list,retval=%d,key=%s,int1=%d,int2=%d",
	    retval, key, int1, int2);
	tstmsg_case_msg(msg);

	getdns_dict_destroy(dict);

	tstmsg_case_end();

	return;
}				/* tst_listsetget */