// File: /workspace/github_c_makefile_star2_O3/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 0x119bf
cmpw $0x0, 0x4(%r12)
je 0x119bf
movq %rcx, %r9
movq %rdx, %rbx
movq %rdi, %r15
cmpw $0x1, 0xa(%rax)
jne 0x119ff
movzwl 0x8(%rax), %ecx
movl $0x2, %r14d
movw %cx, 0x14(%rsp)
cmpw $0x1, %cx
je 0x11963
movl $0xa, %r14d
cmpw $0x1c, 0x14(%rsp)
jne 0x119ff
movq (%rax), %rsi
leaq 0x70(%rsp), %r13
movl $0xff, %edx
movq %r13, %rdi
movq %r9, 0x18(%rsp)
callq 0x31d0
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 0x39f8
movq 0x18(%rsp), %r9
testq %rax, %rax
je 0x119ff
movq -0x8(%rax), %rbp
testq %rbp, %rbp
je 0x119ff
cmpl %r14d, (%rbp)
je 0x11a30
movq 0x18(%rbp), %rbp
jmp 0x119ae
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x11a1b
movzwl 0x4(%r12), %r9d
leaq 0x11d38(%rip), %rsi     # 0x23716
leaq 0x112fe(%rip), %rdx     # 0x22ce3
leaq 0x11dc6(%rip), %r8      # 0x237b2
movl $0x3, %edi
movl $0x98, %ecx
xorl %eax, %eax
callq 0x1771e
jmp 0x11a1b
xorl %r13d, %r13d
testq %rbx, %rbx
je 0x11a1b
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
movl $0x1, %ecx
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
movzwl (%r12), %eax
leaq 0x30(%rsp), %r13
movw %ax, (%r13)
leaq 0x16(%rsp), %rsi
movw $0x101, (%rsi)          # imm = 0x101
movq %r13, %rdi
callq 0x19445
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)
movl $0x4, %eax
movl $0x10, %edx
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 0x176e5
cmpl $0x8, (%rax)
jl 0x11b00
movl (%rbp), %edi
movq %r12, %rsi
callq 0x17e5b
movq %rax, (%rsp)
leaq 0x11c3a(%rip), %rsi     # 0x23716
leaq 0x11200(%rip), %rdx     # 0x22ce3
leaq 0x11ce2(%rip), %r8      # 0x237cc
leaq 0x70(%rsp), %r9
movl $0x8, %edi
movl $0xe0, %ecx
xorl %eax, %eax
callq 0x1771e
testq %rbx, %rbx
je 0x11a1b
leaq 0x30(%rsp), %rdx
xorl %r13d, %r13d
movq %r15, %rdi
xorl %esi, %esi
movl $0x1, %ecx
xorl %r8d, %r8d
movq 0x18(%rsp), %r9
jmp 0x11a19