// File: /workspace/github_c_makefile_star2_O2/GangZhuo[P]dohclient/src/channel_chndoh.c
// Function: query_doh_addr_cb
static int query_doh_addr_cb(channel_t* ctx,
	int status,
	ns_msg_t* result,
	int fromcache,
	int trust,
	void* state)
{
	channel_chndoh_t* c = (channel_chndoh_t*)ctx;
	doh_server_t* doh = (doh_server_t*)state;
	const char* key;
	int i, rrcount;
	ns_rr_t* rr;
	uint16_t port;

	/* ignore failed result */
	if (status || !result || result->qdcount < 1) {
		if (doh->addr_expire < time(NULL) + 1 * 60 * 60) {
			doh->addr_expire += 60;
		}
		return 0;
	}

	key = msg_key(result);

	logi("new DoH server's IP: %s - %s\n",
		key, msg_answers(result));
	
	doh->addr_expire = time(NULL) + ns_get_ttl(result);

	if (doh->addr->addr.ss_family == AF_INET) {
		port = ((struct sockaddr_in*)&doh->addr->addr)->sin_port;
	}
	else {
		port = ((struct sockaddr_in6*)&doh->addr->addr)->sin6_port;
	}

	rrcount = result->ancount + result->nscount;
	for (i = 0; i < rrcount; i++) {
		rr = result->rrs + i;
		if (rr->type == NS_QTYPE_A) {
			struct in_addr* in = (struct in_addr*)rr->rdata;
			struct sockaddr_in* addr = (struct sockaddr_in*)&doh->addr->addr;
			memset(&doh->addr, 0, sizeof(sockaddr_t));
			doh->addr->addrlen = sizeof(struct sockaddr_in);
			addr->sin_family = AF_INET;
			addr->sin_port = port;
			memcpy(&addr->sin_addr, in, sizeof(struct in_addr));
			break;
		}
		else if (rr->type == NS_QTYPE_AAAA) {
			struct in6_addr* in6 = (struct in6_addr*)rr->rdata;
			struct sockaddr_in6* addr = (struct sockaddr_in6*)&doh->addr->addr;
			memset(&doh->addr, 0, sizeof(sockaddr_t));
			doh->addr->addrlen = sizeof(struct sockaddr_in6);
			addr->sin6_family = AF_INET;
			addr->sin6_port = port;
			memcpy(&addr->sin6_addr, in6, sizeof(struct in6_addr));
			break;
		}
	}

	if (result && !fromcache) {
		ns_msg_free(result);
		free(result);
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %r9, %rbx
movl %ecx, %ebp
testl %esi, %esi
setne %al
testq %rdx, %rdx
sete %cl
orb %al, %cl
jne 0xa95c
movq %rdx, %r14
cmpw $0x0, 0x4(%rdx)
je 0xa95c
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0xa97e
callq 0x1897e
movq %rax, %r15
jmp 0xa981
movq (%rbx), %r14
xorl %edi, %edi
callq 0x3320
addq $0xe10, %rax            # imm = 0xE10
cmpq %rax, %r14
jge 0xaa86
addq $0x3c, (%rbx)
jmp 0xaa86
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x165f5
cmpl $0x6, (%rax)
jl 0xa9c0
movq %r14, %rdi
callq 0x18bcb
movq %rax, (%rsp)
leaq 0x16010(%rip), %rsi     # 0x209b0
leaq 0x16308(%rip), %rdx     # 0x20caf
leaq 0x16313(%rip), %r8      # 0x20cc1
pushq $0x6
popq %rdi
movl $0xe4, %ecx
movq %r15, %r9
xorl %eax, %eax
callq 0x1662e
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x3320
movq %rax, %r15
movq %r14, %rdi
callq 0x182a3
movl %eax, %eax
addq %r15, %rax
movq %rax, (%rbx)
leaq 0x8(%rbx), %rdi
movzwl 0xa(%rbx), %r15d
movzwl 0x6(%r14), %ecx
movzwl 0x8(%r14), %eax
addl %ecx, %eax
shll $0x5, %eax
cmpq %r12, %rax
je 0xaa72
movq 0x18(%r14), %rcx
movzwl 0x8(%rcx,%r12), %edx
cmpl $0x1c, %edx
je 0xaa43
cmpl $0x1, %edx
je 0xaa14
addq $0x20, %r12
jmp 0xa9f5
movq 0x18(%rcx,%r12), %r12
movl $0x88, %edx
xorl %esi, %esi
callq 0x31f0
movl $0x10, 0x88(%rbx)
movw $0x2, 0x8(%rbx)
movw %r15w, 0xa(%rbx)
movl (%r12), %eax
movl %eax, 0xc(%rbx)
jmp 0xaa72
movq 0x18(%rcx,%r12), %r12
movl $0x88, %edx
xorl %esi, %esi
callq 0x31f0
movl $0x1c, 0x88(%rbx)
movw $0x2, 0x8(%rbx)
movw %r15w, 0xa(%rbx)
movups (%r12), %xmm0
movups %xmm0, 0x10(%rbx)
testl %ebp, %ebp
jne 0xaa86
movq %r14, %rdi
callq 0x188ce
movq %r14, %rdi
callq 0x3460
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq