Local<Value> GNUtil::convertToJSObj(struct getdns_dict* dict) { if (!dict) { return Nan::Null(); } // try it as an IP char* ipStr = getdns_dict_to_ip_string(dict); if (ipStr) { Local<Value> result = Nan::New<String>(ipStr).ToLocalChecked(); free(ipStr); return result; } getdns_list* names; getdns_dict_get_names(dict, &names); size_t len = 0; Local<Object> result = Nan::New<Object>(); getdns_list_get_length(names, &len); for (size_t i = 0; i < len; ++i) { getdns_bindata* nameBin; getdns_list_get_bindata(names, i, &nameBin); Local<Value> name = Nan::New<String>((char*) nameBin->data).ToLocalChecked(); getdns_data_type type; getdns_dict_get_data_type(dict, (char*)nameBin->data, &type); switch (type) { case t_bindata: { getdns_bindata* data = NULL; getdns_dict_get_bindata(dict, (char*)nameBin->data, &data); result->Set(name, convertBinData(data, (char*) nameBin->data)); break; } case t_int: { uint32_t res = 0; getdns_dict_get_int(dict, (char*)nameBin->data, &res); result->Set(name, Nan::New<Integer>(res)); break; } case t_dict: { getdns_dict* subdict = NULL; getdns_dict_get_dict(dict, (char*)nameBin->data, &subdict); result->Set(name, GNUtil::convertToJSObj(subdict)); break; } case t_list: { getdns_list* list = NULL; getdns_dict_get_list(dict, (char*)nameBin->data, &list); result->Set(name, GNUtil::convertToJSArray(list)); break; } default: break; } } getdns_list_destroy(names); return result; }
Local<Value> GNUtil::convertToJSArray(struct getdns_list* list) { if (!list) { return Nan::Null(); } size_t len; getdns_list_get_length(list, &len); Local<Array> array = Nan::New<Array>(); for (size_t i = 0; i < len; ++i) { getdns_data_type type; getdns_list_get_data_type(list, i, &type); switch (type) { case t_bindata: { getdns_bindata* data = NULL; getdns_list_get_bindata(list, i, &data); array->Set(i, convertBinData(data, NULL)); break; } case t_int: { uint32_t res = 0; getdns_list_get_int(list, i, &res); array->Set(i, Nan::New<Integer>(res)); break; } case t_dict: { getdns_dict* dict = NULL; getdns_list_get_dict(list, i, &dict); array->Set(i, GNUtil::convertToJSObj(dict)); break; } case t_list: { getdns_list* sublist = NULL; getdns_list_get_list(list, i, &sublist); array->Set(i, GNUtil::convertToJSArray(sublist)); break; } default: break; } } return array; }
/** * 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 */