Exemplo n.º 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;
}
Exemplo n.º 2
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;
}