// File: /workspace/github_c_makefile_star2_O1/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 0xd3b3
movq %rdx, %r14
cmpw $0x0, 0x4(%rdx)
je 0xd3b3
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0xd3dd
callq 0x1a29d
movq %rax, %r15
jmp 0xd3e0
movq 0x118(%rbx), %r14
xorl %edi, %edi
callq 0x3310
addq $0xe10, %rax            # imm = 0xE10
cmpq %rax, %r14
jge 0xd50a
addq $0x3c, 0x118(%rbx)
jmp 0xd50a
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x17d59
cmpl $0x6, (%rax)
jl 0xd421
movq %r14, %rdi
callq 0x1a4f0
movq %rax, (%rsp)
leaq 0x15c02(%rip), %rsi     # 0x23001
leaq 0x15cd8(%rip), %rdx     # 0x230de
leaq 0x158ac(%rip), %r8      # 0x22cb9
movl $0x6, %edi
movl $0xa9, %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, 0x118(%rbx)
movzwl 0x6(%r14), %eax
movzwl 0x8(%r14), %ecx
addl %eax, %ecx
je 0xd4f6
leaq 0x90(%rbx), %rdi
movzwl 0x92(%rbx), %r15d
movq 0x18(%r14), %rax
movl %ecx, %ecx
shlq $0x5, %rcx
xorl %edx, %edx
movzwl 0x8(%rax,%rdx), %esi
cmpl $0x1c, %esi
je 0xd4be
cmpl $0x1, %esi
je 0xd486
addq $0x20, %rdx
cmpq %rdx, %rcx
jne 0xd46c
jmp 0xd4f6
movq 0x18(%rax,%rdx), %r12
movl $0x88, %edx
xorl %esi, %esi
callq 0x31e0
movl $0x10, 0x110(%rbx)
movw $0x2, 0x90(%rbx)
movw %r15w, 0x92(%rbx)
movl (%r12), %eax
movl %eax, 0x94(%rbx)
jmp 0xd4f6
movq 0x18(%rax,%rdx), %r12
movl $0x88, %edx
xorl %esi, %esi
callq 0x31e0
movl $0x1c, 0x110(%rbx)
movw $0x2, 0x90(%rbx)
movw %r15w, 0x92(%rbx)
movups (%r12), %xmm0
movups %xmm0, 0x98(%rbx)
testl %ebp, %ebp
jne 0xd50a
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