// File: /workspace/github_c_makefile_star2_O3/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 0xae5e
movq %rdx, %r14
cmpw $0x0, 0x4(%rdx)
je 0xae5e
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0xae80
callq 0x19ba4
movq %rax, %r15
jmp 0xae83
movq (%rbx), %r14
xorl %edi, %edi
callq 0x3310
addq $0xe10, %rax            # imm = 0xE10
cmpq %rax, %r14
jge 0xaf91
addq $0x3c, (%rbx)
jmp 0xaf91
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x176e5
cmpl $0x6, (%rax)
jl 0xaec4
movq %r14, %rdi
callq 0x19df7
movq %rax, (%rsp)
leaq 0x17b0e(%rip), %rsi     # 0x229b0
leaq 0x17e06(%rip), %rdx     # 0x22caf
leaq 0x17e11(%rip), %r8      # 0x22cc1
movl $0x6, %edi
movl $0xe4, %ecx
movq %r15, %r9
xorl %eax, %eax
callq 0x1771e
xorl %edi, %edi
callq 0x3310
movq %rax, %r15
movq %r14, %rdi
callq 0x194a3
movl %eax, %eax
addq %r15, %rax
movq %rax, (%rbx)
movzwl 0x6(%r14), %eax
movzwl 0x8(%r14), %ecx
addl %eax, %ecx
je 0xaf7d
leaq 0x8(%rbx), %rdi
movzwl 0xa(%rbx), %r15d
movq 0x18(%r14), %rax
movl %ecx, %ecx
shlq $0x5, %rcx
xorl %edx, %edx
movzwl 0x8(%rax,%rdx), %esi
cmpl $0x1c, %esi
je 0xaf4e
cmpl $0x1, %esi
je 0xaf1f
addq $0x20, %rdx
cmpq %rdx, %rcx
jne 0xaf05
jmp 0xaf7d
movq 0x18(%rax,%rdx), %r12
movl $0x88, %edx
xorl %esi, %esi
callq 0x31e0
movl $0x10, 0x88(%rbx)
movw $0x2, 0x8(%rbx)
movw %r15w, 0xa(%rbx)
movl (%r12), %eax
movl %eax, 0xc(%rbx)
jmp 0xaf7d
movq 0x18(%rax,%rdx), %r12
movl $0x88, %edx
xorl %esi, %esi
callq 0x31e0
movl $0x1c, 0x88(%rbx)
movw $0x2, 0x8(%rbx)
movw %r15w, 0xa(%rbx)
movups (%r12), %xmm0
movups %xmm0, 0x10(%rbx)
testl %ebp, %ebp
jne 0xaf91
movq %r14, %rdi
callq 0x19ae0
movq %r14, %rdi
callq 0x3450
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq