// File: /workspace/github_c_makefile_star2_O1/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 0xb23f
movq %rdx, %r14
cmpw $0x0, 0x4(%rdx)
je 0xb23f
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0xb261
callq 0x1a29d
movq %rax, %r15
jmp 0xb264
movq (%rbx), %r14
xorl %edi, %edi
callq 0x3310
addq $0xe10, %rax            # imm = 0xE10
cmpq %rax, %r14
jge 0xb372
addq $0x3c, (%rbx)
jmp 0xb372
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x17d59
cmpl $0x6, (%rax)
jl 0xb2a5
movq %r14, %rdi
callq 0x1a4f0
movq %rax, (%rsp)
leaq 0x17725(%rip), %rsi     # 0x229a8
leaq 0x17a1d(%rip), %rdx     # 0x22ca7
leaq 0x17a28(%rip), %r8      # 0x22cb9
movl $0x6, %edi
movl $0xe4, %ecx
movq %r15, %r9
xorl %eax, %eax
callq 0x17d92
xorl %edi, %edi
callq 0x3310
movq %rax, %r15
movq %r14, %rdi
callq 0x19b1f
movl %eax, %eax
addq %r15, %rax
movq %rax, (%rbx)
movzwl 0x6(%r14), %eax
movzwl 0x8(%r14), %ecx
addl %eax, %ecx
je 0xb35e
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 0xb32f
cmpl $0x1, %esi
je 0xb300
addq $0x20, %rdx
cmpq %rdx, %rcx
jne 0xb2e6
jmp 0xb35e
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 0xb35e
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 0xb372
movq %r14, %rdi
callq 0x1a1dd
movq %r14, %rdi
callq 0x3450
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq