// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x133a3
cmpq $0x0, -0x20(%rbp)
je 0x133a3
movq -0x20(%rbp), %rax
movzwl 0x4(%rax), %eax
cmpl $0x1, %eax
jge 0x133f0
movq -0x38(%rbp), %rax
movq 0x118(%rax), %rax
movq %rax, -0x80(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x3330
movq %rax, %rcx
movq -0x80(%rbp), %rax
addq $0xe10, %rcx            # imm = 0xE10
cmpq %rcx, %rax
jge 0x133e4
movq -0x38(%rbp), %rax
movq 0x118(%rax), %rcx
addq $0x3c, %rcx
movq %rcx, 0x118(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x13614
movq -0x20(%rbp), %rdi
callq 0x13a00
movq %rax, -0x40(%rbp)
movb $0x0, %al
callq 0x242d0
cmpl $0x6, (%rax)
jl 0x1344e
movq -0x40(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x20(%rbp), %rdi
callq 0x27de0
movq -0x88(%rbp), %r9
movl $0x6, %edi
leaq 0x231d3(%rip), %rsi     # 0x36603
leaq 0x232a9(%rip), %rdx     # 0x366e0
movl $0xa9, %ecx
leaq 0x22e4c(%rip), %r8      # 0x3628f
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0x13450
xorl %eax, %eax
movl %eax, %edi
callq 0x3330
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rdi
callq 0x27120
movq -0x90(%rbp), %rcx
movl %eax, %eax
addq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x118(%rax)
movq -0x38(%rbp), %rax
movzwl 0x90(%rax), %eax
cmpl $0x2, %eax
jne 0x134a1
movq -0x38(%rbp), %rax
movw 0x92(%rax), %ax
movw %ax, -0x52(%rbp)
jmp 0x134b0
movq -0x38(%rbp), %rax
movw 0x92(%rax), %ax
movw %ax, -0x52(%rbp)
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %eax
movq -0x20(%rbp), %rcx
movzwl 0x8(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jge 0x135ee
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x44(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movzwl 0x8(%rax), %eax
cmpl $0x1, %eax
jne 0x13562
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
addq $0x90, %rax
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rdi
addq $0x90, %rdi
xorl %esi, %esi
movl $0x88, %edx
callq 0x3200
movq -0x38(%rbp), %rax
movl $0x10, 0x110(%rax)
movq -0x68(%rbp), %rax
movw $0x2, (%rax)
movw -0x52(%rbp), %cx
movq -0x68(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x4(%rax)
jmp 0x135ee
movq -0x50(%rbp), %rax
movzwl 0x8(%rax), %eax
cmpl $0x1c, %eax
jne 0x135dc
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rax
addq $0x90, %rax
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rdi
addq $0x90, %rdi
xorl %esi, %esi
movl $0x88, %edx
callq 0x3200
movq -0x38(%rbp), %rax
movl $0x1c, 0x110(%rax)
movq -0x78(%rbp), %rax
movw $0x2, (%rax)
movw -0x52(%rbp), %cx
movq -0x78(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x10(%rax)
jmp 0x135ee
jmp 0x135de
jmp 0x135e0
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x134cc
cmpq $0x0, -0x20(%rbp)
je 0x1360d
cmpl $0x0, -0x24(%rbp)
jne 0x1360d
movq -0x20(%rbp), %rdi
callq 0x27870
movq -0x20(%rbp), %rdi
callq 0x3460
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq