// File: /workspace/github_c_makefile_star2_O2/GangZhuo[P]dohclient/src/channel_doh.c
// Function: http_addr_query_cb
static int http_addr_query_cb(channel_t* ctx,
	int status,
	ns_msg_t* result,
	int fromcache,
	int trust,
	void* state)
{
	channel_doh_t* c = (channel_doh_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 (c->http_addr_expire < time(NULL) + 1 * 60 * 60) {
			c->http_addr_expire += 60;
		}
		return 0;
	}

	key = msg_key(result);

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

	if (c->http_addr.addr.ss_family == AF_INET) {
		port = ((struct sockaddr_in*)&c->http_addr.addr)->sin_port;
	}
	else {
		port = ((struct sockaddr_in6*)&c->http_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*)&c->http_addr.addr;
			memset(&c->http_addr, 0, sizeof(sockaddr_t));
			c->http_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*)&c->http_addr.addr;
			memset(&c->http_addr, 0, sizeof(sockaddr_t));
			c->http_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 0xc945
movq %rdx, %r14
cmpw $0x0, 0x4(%rdx)
je 0xc945
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0xc96f
callq 0x1897e
movq %rax, %r15
jmp 0xc972
movq 0x118(%rbx), %r14
xorl %edi, %edi
callq 0x3320
addq $0xe10, %rax            # imm = 0xE10
cmpq %rax, %r14
jge 0xca97
addq $0x3c, 0x118(%rbx)
jmp 0xca97
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x165f5
cmpl $0x6, (%rax)
jl 0xc9b1
movq %r14, %rdi
callq 0x18bcb
movq %rax, (%rsp)
leaq 0x14678(%rip), %rsi     # 0x21009
leaq 0x1474e(%rip), %rdx     # 0x210e6
leaq 0x14322(%rip), %r8      # 0x20cc1
pushq $0x6
popq %rdi
movl $0xa9, %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, 0x118(%rbx)
leaq 0x90(%rbx), %rdi
movzwl 0x92(%rbx), %r15d
movzwl 0x6(%r14), %ecx
movzwl 0x8(%r14), %eax
addl %ecx, %eax
shll $0x5, %eax
cmpq %r12, %rax
je 0xca83
movq 0x18(%r14), %rcx
movzwl 0x8(%rcx,%r12), %edx
cmpl $0x1c, %edx
je 0xca4b
cmpl $0x1, %edx
je 0xca13
addq $0x20, %r12
jmp 0xc9f0
movq 0x18(%rcx,%r12), %r12
movl $0x88, %edx
xorl %esi, %esi
callq 0x31f0
movl $0x10, 0x110(%rbx)
movw $0x2, 0x90(%rbx)
movw %r15w, 0x92(%rbx)
movl (%r12), %eax
movl %eax, 0x94(%rbx)
jmp 0xca83
movq 0x18(%rcx,%r12), %r12
movl $0x88, %edx
xorl %esi, %esi
callq 0x31f0
movl $0x1c, 0x110(%rbx)
movw $0x2, 0x90(%rbx)
movw %r15w, 0x92(%rbx)
movups (%r12), %xmm0
movups %xmm0, 0x98(%rbx)
testl %ebp, %ebp
jne 0xca97
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