示例#1
0
static jdns_string_t *reg_readString(HKEY hk, const char *subkey)
{
	char *buf;
	DWORD bufsize;
	int ret;
	jdns_string_t *str = 0;

	bufsize = 1024;
	buf = (char *)jdns_alloc((int)bufsize);
	if(!buf)
		return 0;
	ret = RegQueryValueExA(hk, subkey, 0, 0, (LPBYTE)buf, &bufsize);
	if(ret == ERROR_MORE_DATA)
	{
		buf = (char *)jdns_realloc(buf, bufsize);
		if(!buf)
		{
			jdns_free(buf);
			return 0;
		}
		ret = RegQueryValueExA(hk, subkey, 0, 0, (LPBYTE)buf, &bufsize);
	}
	if(ret == ERROR_SUCCESS)
	{
		str = jdns_string_new();
		jdns_string_set_cstr(str, (char *)buf);
	}
	jdns_free(buf);
	return str;
}
示例#2
0
static unsigned char *string_getnextword(unsigned char *in, int size, int pos, int *newpos)
{
	int n;
	int at;
	int len;
	unsigned char *out;

	at = pos;

	// skip any space at the start
	while(at < size && char_isspace(in[at]))
		++at;

	// all space?  no word then
	if(at >= size)
		return 0;

	// skip until a space or end
	n = at;
	while(n < size && !char_isspace(in[n]))
		++n;
	len = n - at;

	// allocate length + zero byte
	out = (unsigned char *)jdns_alloc(len + 1);
	if(!out)
		return 0;
	memcpy(out, in + at, len);
	out[len] = 0;
	*newpos = at + len;
	return out;
}
示例#3
0
static jdns_dnsparams_t *dnsparams_get_winsys()
{
	jdns_dnsparams_t *params;
	GetNetworkParamsFunc myGetNetworkParams;
	DWORD ret;
	HINSTANCE lib;
	jdns_address_t *addr;
	jdns_string_t *str;
	IP_ADDR_STRING *ipstr;

	lib = LoadLibraryA("iphlpapi");
	if(!lib)
		return 0;

	params = 0;
	myGetNetworkParams = (GetNetworkParamsFunc)GetProcAddress(lib, "GetNetworkParams");
	if(myGetNetworkParams)
	{
		ULONG bufsize = 0;
		ret = myGetNetworkParams(0, &bufsize);
		if(ret == ERROR_BUFFER_OVERFLOW)
		{
			FIXED_INFO *info = (FIXED_INFO *)jdns_alloc((int)bufsize);
			ret = myGetNetworkParams(info, &bufsize);
			if(ret == ERROR_SUCCESS)
			{
				params = jdns_dnsparams_new();
				ipstr = &info->DnsServerList;
				while(ipstr)
				{
					addr = jdns_address_new();
					if(jdns_address_set_cstr(addr, (char *)ipstr->IpAddress.String))
						jdns_dnsparams_append_nameserver(params, addr, JDNS_UNICAST_PORT);
					jdns_address_delete(addr);
					ipstr = ipstr->Next;
				}
				str = jdns_string_new();
				jdns_string_set_cstr(str, info->DomainName);
				if(str->size > 0)
					jdns_dnsparams_append_domain(params, str);
				jdns_string_delete(str);
			}
			jdns_free(info);
		}
	}
	FreeLibrary(lib);
	return params;
}
示例#4
0
static void apply_hosts_var_filepath(jdns_dnsparams_t *a, const char *envvar, const char *path)
{
	jdns_string_t *e;
	char *str;
	int elen, plen;

	e = jdns_getenv(envvar);
	if(!e)
		return;
	elen = strlen((char *)e->data);
	plen = strlen(path);
	str = (char *)jdns_alloc(elen + plen + 1);
	memcpy(str, e->data, elen);
	jdns_string_delete(e);

	jdns_strcpy(str + elen, path);
	apply_hosts_file(a, str);
	jdns_free(str);
}
示例#5
0
static jdns_string_t *file_nextline(FILE *f)
{
	int at, size;
	unsigned char *buf;
	jdns_string_t *str;

	size = 1023;
	buf = (unsigned char *)jdns_alloc(size);
	at = 0;
	while(1)
	{
		unsigned char c = (unsigned char)fgetc(f); // cast is intentional
		if(feof(f)) // checking EOF here
		{
			if(at > 0)
			{
				// if we read at least one char, take it as a
				//   line
				break;
			}
			else
			{
				jdns_free(buf);
				return 0;
			}
		}
		if(c == '\n')
			break;
		if(c == '\r')
			continue;
		if(at < 1023)
			buf[at++] = c;
	}

	str = jdns_string_new();
	jdns_string_set(str, buf, at);
	jdns_free(buf);
	return str;
}
示例#6
0
static jdns_string_t *string_simplify(const jdns_string_t *in)
{
	int n;
	int pos;
	int total;
	unsigned char *out;
	int outlen;
	jdns_string_t *outstr;
	jdns_stringlist_t *wordlist;

	// gather words and total of lengths
	pos = 0;
	total = 0;
	wordlist = jdns_stringlist_new();
	while(1)
	{
		jdns_string_t *word;
		unsigned char *str = string_getnextword(in->data, in->size, pos, &pos);
		if(!str)
			break;
		word = jdns_string_new();
		jdns_string_set_cstr(word, (char *)str);
		jdns_free(str);
		jdns_stringlist_append(wordlist, word);
		total += word->size;
		jdns_string_delete(word);
	}

	if(total == 0)
	{
		jdns_stringlist_delete(wordlist);

		outstr = jdns_string_new();
		jdns_string_set_cstr(outstr, "");
		return outstr;
	}

	// we need to allocate space for total lengths and wordcount-1 spaces
	outlen = total + (wordlist->count - 1);
	out = (unsigned char *)jdns_alloc(outlen);

	// lay out the words
	pos = 0;
	for(n = 0; n < wordlist->count; ++n)
	{
		unsigned char *data = wordlist->item[n]->data;
		int size = wordlist->item[n]->size;
		memcpy(out + pos, data, size);
		pos += size;

		// if this is not the last word, append a space
		if(n + 1 < wordlist->count)
			out[pos++] = ' ';
	}
	jdns_stringlist_delete(wordlist);

	outstr = jdns_string_new();
	jdns_string_set(outstr, out, outlen);
	jdns_free(out);
	return outstr;
}
示例#7
0
int jdns_packet_export(jdns_packet_t *a, int maxsize)
{
	unsigned char *block = 0;
	unsigned char *buf, *last;
	unsigned char c;
	int size;
	jdns_list_t *lookup = 0; // to hold jdns_packet_label_t

	// clear out any existing raw data before we begin
	if(a->raw_data)
	{
		jdns_free(a->raw_data);
		a->raw_data = 0;
		a->raw_size = 0;
	}

	// preallocate
	size = maxsize;
	block = (unsigned char *)jdns_alloc(size);
	memset(block, 0, size);

	buf = block;
	last = block + size;

	if(size < 12)
		goto error;

	short2net(a->id, &buf);
	if(a->opts.qr)
		buf[0] |= 0x80;
	c = (unsigned char)a->opts.opcode;
	buf[0] |= c << 3;
	if(a->opts.aa)
		buf[0] |= 0x04;
	if(a->opts.tc)
		buf[0] |= 0x02;
	if(a->opts.rd)
		buf[0] |= 0x01;
	if(a->opts.ra)
		buf[1] |= 0x80;
	c = (unsigned char)a->opts.z;
	buf[1] |= c << 4;
	c = (unsigned char)a->opts.rcode;
	buf[1] |= c;
	buf += 2;
	short2net((unsigned short int)a->questions->count, &buf);
	short2net((unsigned short int)a->answerRecords->count, &buf);
	short2net((unsigned short int)a->authorityRecords->count, &buf);
	short2net((unsigned short int)a->additionalRecords->count, &buf);

	// append sections
	lookup = jdns_list_new();
	lookup->autoDelete = 1;

	if(!append_qsection(a->questions, buf - block, last - buf, &buf, lookup))
		goto error;
	if(!append_rrsection(a->answerRecords, buf - block, last - buf, &buf, lookup))
		goto error;
	if(!append_rrsection(a->authorityRecords, buf - block, last - buf, &buf, lookup))
		goto error;
	if(!append_rrsection(a->additionalRecords, buf - block, last - buf, &buf, lookup))
		goto error;

	// done with all sections
	jdns_list_delete(lookup);

	// condense
	size = buf - block;
	block = (unsigned char *)jdns_realloc(block, size);

	// finalize
	a->qdcount = a->questions->count;
	a->ancount = a->answerRecords->count;
	a->nscount = a->authorityRecords->count;
	a->arcount = a->additionalRecords->count;
	a->raw_data = block;
	a->raw_size = size;

	return 1;

error:
	jdns_list_delete(lookup);
	if(block)
		jdns_free(block);
	return 0;
}