// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x170(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3200
leaq -0x180(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3200
leaq -0x1a0(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3200
leaq -0x1a2(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x3200
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x1ac87
movq -0x18(%rbp), %rax
movzwl 0x4(%rax), %eax
cmpl $0x1, %eax
jge 0x1acd2
jmp 0x1ac89
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x1acc4
movq -0x18(%rbp), %rax
movzwl 0x4(%rax), %r9d
movl $0x3, %edi
leaq 0x1c050(%rip), %rsi     # 0x36cfa
leaq 0x1b600(%rip), %rdx     # 0x362b1
movl $0x98, %ecx
leaq 0x1c0d9(%rip), %r8      # 0x36d96
movb $0x0, %al
callq 0x243b0
jmp 0x1acc6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b05b
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movzwl 0xa(%rax), %eax
cmpl $0x1, %eax
je 0x1ace8
jmp 0x1b02e
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movzwl 0x8(%rax), %eax
cmpl $0x1, %eax
jne 0x1ad05
movl $0x2, -0x144(%rbp)
jmp 0x1ad29
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movzwl 0x8(%rax), %eax
cmpl $0x1c, %eax
jne 0x1ad22
movl $0xa, -0x144(%rbp)
jmp 0x1ad27
jmp 0x1b02e
jmp 0x1ad29
leaq -0x140(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rsi
movl $0xff, %edx
callq 0x31f0
movb $0x0, -0x42(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x3180
subq $0x1, %rax
movb $0x0, -0x140(%rbp,%rax)
movq -0x30(%rbp), %rdi
addq $0x58, %rdi
leaq -0x140(%rbp), %rsi
callq 0x3b10
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x1ad85
jmp 0x1b02e
xorl %eax, %eax
movq %rax, %rcx
addq $0x10, %rcx
subq %rcx, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x150(%rbp)
cmpq $0x0, -0x150(%rbp)
je 0x1add9
movq -0x150(%rbp), %rax
movl (%rax), %eax
cmpl -0x144(%rbp), %eax
jne 0x1adc5
jmp 0x1add9
movq -0x150(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x150(%rbp)
jmp 0x1ada8
cmpq $0x0, -0x150(%rbp)
jne 0x1ade8
jmp 0x1b02e
movq -0x18(%rbp), %rax
movw (%rax), %ax
movw %ax, -0x170(%rbp)
movw -0x1a2(%rbp), %ax
andw $-0x2, %ax
orw $0x1, %ax
movw %ax, -0x1a2(%rbp)
movw -0x1a2(%rbp), %ax
andw $-0x1f, %ax
orw $0x0, %ax
movw %ax, -0x1a2(%rbp)
movw -0x1a2(%rbp), %ax
andw $-0x21, %ax
orw $0x0, %ax
movw %ax, -0x1a2(%rbp)
movw -0x1a2(%rbp), %ax
andw $-0x41, %ax
orw $0x0, %ax
movw %ax, -0x1a2(%rbp)
movw -0x1a2(%rbp), %ax
andw $0xfeff, %ax            # imm = 0xFEFF
orw $0x100, %ax             # imm = 0x100
movw %ax, -0x1a2(%rbp)
movw -0x1a2(%rbp), %ax
andw $0xf1ff, %ax            # imm = 0xF1FF
orw $0x0, %ax
movw %ax, -0x1a2(%rbp)
movw -0x1a2(%rbp), %ax
andw $0xfff, %ax             # imm = 0xFFF
orw $0x0, %ax
movw %ax, -0x1a2(%rbp)
leaq -0x170(%rbp), %rdi
leaq -0x1a2(%rbp), %rsi
callq 0x26fd0
movw $0x1, -0x16c(%rbp)
leaq -0x180(%rbp), %rax
movq %rax, -0x160(%rbp)
movw $0x1, -0x16a(%rbp)
leaq -0x1a0(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq %rax, -0x180(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movw 0xa(%rax), %ax
movw %ax, -0x176(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movw 0x8(%rax), %ax
movw %ax, -0x178(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movw -0x178(%rbp), %ax
movw %ax, -0x198(%rbp)
movw -0x176(%rbp), %ax
movw %ax, -0x196(%rbp)
movl $0xe10, -0x194(%rbp)    # imm = 0xE10
movl -0x144(%rbp), %edx
movl $0x10, %eax
movl $0x4, %ecx
cmpl $0x2, %edx
cmoveq %rcx, %rax
movw %ax, -0x190(%rbp)
cmpl $0x2, -0x144(%rbp)
jne 0x1af78
movq -0x150(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x1b0(%rbp)
jmp 0x1af8a
movq -0x150(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x1b0(%rbp)
movq -0x1b0(%rbp), %rax
movq %rax, -0x188(%rbp)
movb $0x0, %al
callq 0x242d0
cmpl $0x8, (%rax)
jl 0x1affc
leaq -0x140(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x150(%rbp), %rax
movl (%rax), %edi
movq -0x150(%rbp), %rsi
addq $0x4, %rsi
callq 0x24fe0
movq -0x1b8(%rbp), %r9
movl $0x8, %edi
leaq 0x1bd1c(%rip), %rsi     # 0x36cfa
leaq 0x1b2cc(%rip), %rdx     # 0x362b1
movl $0xe0, %ecx
leaq 0x1bdbf(%rip), %r8      # 0x36db0
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0x1affe
cmpq $0x0, -0x20(%rbp)
je 0x1b025
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %r9
xorl %r8d, %r8d
leaq -0x170(%rbp), %rdx
movl $0x1, %ecx
movl %r8d, %esi
callq *%rax
movl $0x0, -0x4(%rbp)
jmp 0x1b05b
cmpq $0x0, -0x20(%rbp)
je 0x1b054
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %r9
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movl %ecx, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq *%rax
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
nopw (%rax,%rax)