// File: /workspace/github_c_makefile_star2_O2/GangZhuo[P]dohclient/src/channel_hosts.c
// Function: query
static int query(channel_t *ctx,
	const ns_msg_t *msg,
	channel_query_cb callback, void *state)
{
	hosts_t *c = (hosts_t*)ctx;
	struct rbnode_t *rbn;
	hosts_item_t *item;
	char domain[NS_QNAME_SIZE];
	int family;
	ipnode_t *ip;
	ns_msg_t result = { 0 };
	ns_qr_t qr = { 0 };
	ns_rr_t an = { 0 };
	ns_flags_t flags = { 0 };

	if (!msg->qrs || msg->qdcount < 1) {
		loge("invalid msg (qdcount=%d)\n",
			(int)msg->qdcount);
		return -1;
	}

	if (msg->qrs->qclass != NS_QCLASS_IN) {
		goto error;
	}

	if (msg->qrs->qtype == NS_QTYPE_A) {
		family = AF_INET;
	}
	else if (msg->qrs->qtype == NS_QTYPE_AAAA) {
		family = AF_INET6;
	}
	else {
		goto error;
	}

	strncpy(domain, msg->qrs->qname, sizeof(domain));
	domain[NS_QNAME_SIZE - 1] = '\0';
	domain[strlen(domain) - 1] = '\0'; /* Remove last point */

	rbn = rbtree_lookup(&c->dic, domain);
	if (!rbn) {
		goto error;
	}

	item = rbtree_container_of(rbn, hosts_item_t, node);

	ip = item->iplist;

	while(ip) {
		if (ip->family == family) {
			break;
		}
		ip = ip->next;
	}

	if (!ip) {
		goto error;
	}

	/* TODO: create result */

	result.id = msg->id;

	flags.qr = 1;
	flags.opcode = 0;
	flags.aa = 0;
	flags.tc = 0;
	flags.ra = 1;
	flags.z = 0;
	flags.rcode = 0;
	ns_set_flags(&result, &flags);

	result.qdcount = 1;
	result.qrs = &qr;

	result.ancount = 1;
	result.rrs = &an;

	qr.qname = msg->qrs->qname;
	qr.qclass = msg->qrs->qclass;
	qr.qtype = msg->qrs->qtype;

	an.name = qr.qname;
	an.type = qr.qtype;
	an.cls = qr.qclass;
	an.ttl = 60 * 60; /* 1 hour */
	an.rdlength = family == AF_INET ? sizeof(struct in_addr) : sizeof(struct in6_addr);
	an.rdata = family == AF_INET ? (void*)&ip->ip4 : (void*)&ip->ip6;

	logv("hit hosts: %s - %s\n", domain, get_ipname(ip->family, &ip->ip4));

	if (callback)
		callback(ctx, 0, &result, TRUE, FALSE, state);

	return 0;

error:
	if (callback)
		callback(ctx, -1, NULL, TRUE, FALSE, state);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rsi, %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movq 0x10(%rsi), %rax
testq %rax, %rax
je 0x10f6d
cmpw $0x0, 0x4(%r12)
je 0x10f6d
movq %rcx, %r9
movq %rdx, %rbx
movq %rdi, %r15
cmpw $0x1, 0xa(%rax)
jne 0x1101d
movzwl 0x8(%rax), %ecx
cmpw $0x1, %cx
movw %cx, 0x14(%rsp)
jne 0x10fb0
movq %r9, 0x18(%rsp)
pushq $0x2
jmp 0x10fbd
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %r13
cmpl $0x3, (%rax)
jl 0x11035
movzwl 0x4(%r12), %r9d
leaq 0x10788(%rip), %rsi     # 0x21716
leaq 0xfd4e(%rip), %rdx      # 0x20ce3
leaq 0x10816(%rip), %r8      # 0x217b2
pushq $0x3
popq %rdi
movl $0x98, %ecx
xorl %eax, %eax
callq 0x1662e
jmp 0x11035
cmpw $0x1c, %cx
jne 0x1101d
movq %r9, 0x18(%rsp)
pushq $0xa
popq %r14
movq (%rax), %rsi
leaq 0x70(%rsp), %r13
movl $0xff, %edx
movq %r13, %rdi
callq 0x31e0
xorl %ebp, %ebp
movb %bpl, 0x16e(%rsp)
movq %r13, %rdi
callq 0x3170
movb %bpl, -0x1(%rax,%r13)
leaq 0x58(%r15), %rdi
movq %r13, %rsi
callq 0x38ef
testq %rax, %rax
movq 0x18(%rsp), %r9
je 0x1101d
movq %rax, %rbp
addq $-0x8, %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x1101d
cmpl %r14d, (%rbp)
je 0x1104a
addq $0x18, %rbp
jmp 0x11008
xorl %r13d, %r13d
testq %rbx, %rbx
je 0x11035
pushq $-0x1
popq %rsi
pushq $0x1
popq %rcx
movq %r15, %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq *%rbx
movl %r13d, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r9, %r14
movzwl (%r12), %eax
leaq 0x30(%rsp), %r13
movw %ax, (%r13)
leaq 0x16(%rsp), %rsi
movw $0x101, (%rsi)          # imm = 0x101
movq %r13, %rdi
callq 0x18245
cmpw $0x1, 0x14(%rsp)
movl $0x10001, 0x4(%r13)     # imm = 0x10001
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%r13)
leaq 0x50(%rsp), %rcx
movq %rcx, 0x18(%r13)
movq 0x10(%r12), %rdx
movq (%rdx), %rsi
movq %rsi, (%rax)
movzwl 0xa(%rdx), %edi
movw %di, 0xa(%rax)
movzwl 0x8(%rdx), %edx
movw %dx, 0x8(%rax)
movq %rsi, (%rcx)
movw %dx, 0x8(%rcx)
movw %di, 0xa(%rcx)
pushq $0x4
popq %rax
pushq $0x10
popq %rdx
cmovel %eax, %edx
movl $0xe10, 0xc(%rcx)       # imm = 0xE10
movw %dx, 0x10(%rcx)
leaq 0x4(%rbp), %r12
movq %r12, 0x18(%rcx)
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x165f5
cmpl $0x8, (%rax)
jl 0x11117
movl (%rbp), %edi
movq %r12, %rsi
callq 0x16d6a
movq %rax, (%rsp)
leaq 0x10621(%rip), %rsi     # 0x21716
leaq 0xfbe7(%rip), %rdx      # 0x20ce3
leaq 0x106c9(%rip), %r8      # 0x217cc
pushq $0x8
popq %rdi
leaq 0x70(%rsp), %r9
movl $0xe0, %ecx
xorl %eax, %eax
callq 0x1662e
testq %rbx, %rbx
je 0x11035
leaq 0x30(%rsp), %rdx
pushq $0x1
popq %rcx
xorl %r13d, %r13d
movq %r15, %rdi
xorl %esi, %esi
xorl %r8d, %r8d
movq %r14, %r9
jmp 0x11033