// File: /workspace/github_c_makefile_star2_O0/bloomsource[P]dns_update/dns.c
// Function: proc_dns_query
void proc_dns_query( char* data, int len, char* ip, int port )
{
    dns_hdr req, rsp;
    char domain[MAX_DOMAIN+1];
    char buf[1024];
    int write_len;
    int qry_len;
    int qry_type, qry_class;
    int offset = 0;
    unsigned short slen;
    unsigned int   ilen;
    unsigned char bin_ip[4];
    int find;
    dns_node *node;
    
    //write_log( "recv dns data from %s:%d", ip, port );
    //write_log_hex( data, len );
    
    if( len < sizeof(dns_hdr) )
    {
        write_log( "recv dns data from %s:%d", ip, port );
        write_log_hex( data, len );
        write_log( "[WRN] dns pack size small, query abandon." );
        return;
    }
    
    memcpy( &req, data, sizeof(req) );
    req.flags   = ntohs( req.flags );
    req.qry_cnt = ntohs( req.qry_cnt );
    req.ans_cnt = ntohs( req.ans_cnt );
    req.auth_cnt= ntohs( req.auth_cnt );
    req.add_cnt = ntohs( req.add_cnt );
    
    if( req.flags & 0x8000 )
    {
        write_log( "recv dns data from %s:%d", ip, port );
        write_log_hex( data, len );
        write_log( "[WRN] dns query flag is not 1, query abandon." );
        return;
    }
    
    if( req.qry_cnt != 1 )
    {
        write_log( "recv dns data from %s:%d", ip, port );
        write_log_hex( data, len );
        write_log( "[WRN] dns query req cnt is not 1, query abandon." );
        return;
    }
    
    if( req.ans_cnt )
    {
        write_log( "recv dns data from %s:%d", ip, port );
        write_log_hex( data, len );
        write_log( "[WRN] dns ans_cnt not 0, query abandon." );
        return;
    }
    
    memset( domain, 0, sizeof(domain) );
    if( dns_resove_query( data + sizeof(req), len - sizeof(req), domain, &qry_type, &qry_class, &qry_len ) )
    {
        write_log( "recv dns data from %s:%d", ip, port );
        write_log_hex( data, len );
        write_log( "[WRN] dns query resolve domain failed, query abandon." );
        return ;
    }
    strlower( domain );
    
    write_log( "recv dns query from %-15s domain: %-20s type: %-3d class: %-3d", ip, domain, qry_type, qry_class );
    

    
    node = (dns_node*)rbtree_find( &domains, domain );
    if( node )
        find = 1;
    else
        find = 0;
    
    if( find == 0 )
    {
        write_log( "[WRN] domain %s not exist!", domain );
        return;
    }

    if( qry_type != 1 || qry_class != 1 ) //ipv4 only
    {
        write_log( "[WRN] dns query type/class not support." );
        find = 0;
    }
    
    

    rsp.id = req.id;
    rsp.flags = 0;
    rsp.flags |= 0x8000;
    if( !find )
        rsp.flags |= 0x03;
        
    rsp.flags = htons( rsp.flags );
    rsp.qry_cnt = htons( 1 );
    rsp.ans_cnt = find ? htons( 1 ) : 0;
    rsp.auth_cnt = 0;
    rsp.add_cnt = 0;
    
    /*------------ write response header ---------------*/
    memcpy( buf+offset, &rsp, sizeof(rsp) );
    offset+= sizeof(rsp );


    //-------------- write query ------------------------
    //write domain
    if( dns_write_domain( buf+offset, sizeof(buf)-sizeof(rsp), domain, &write_len ) )
        return;
    offset+= write_len;
    
    
    // write type
    slen = htons( qry_type );
    memcpy( buf + offset, &slen, sizeof(slen) );
    offset += sizeof(slen);
    
    //write class
    slen = htons( qry_class );
    memcpy( buf + offset, &slen, sizeof(slen) );
    offset += sizeof(slen);
    

    //--------------- write answer ------------------------
    if( !find )
        goto send_rsp;
        
    //answer name
    slen = htons( 0xc00c );
    memcpy( buf + offset, &slen, sizeof(slen) );
    offset += sizeof(slen);
    
    // write type
    slen = htons( 1 );
    memcpy( buf + offset, &slen, sizeof(slen) );
    offset += sizeof(slen);
    
    //write class
    slen = htons( 1 );
    memcpy( buf + offset, &slen, sizeof(slen) );
    offset += sizeof(slen);
    
    //write ttl
    ilen = htonl( 300 );
    memcpy( buf + offset, &ilen, sizeof(ilen) );
    offset += sizeof(ilen);
    
    //write address len
    slen = htons( 4 );
    memcpy( buf + offset, &slen, sizeof(slen) );
    offset += sizeof(slen);
    
    //write ip
    fill_binary_ip( bin_ip, node->ip );
    memcpy( buf + offset, bin_ip, 4 );
    offset += 4;
    
    
    send_rsp:
    //write_log( "dns response data:" );
    //write_log_hex( buf, offset );
    
    udp_send( dns_fd, ip, port, buf, offset );
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x580, %rsp            # imm = 0x580
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl $0x0, -0x554(%rbp)
movslq -0xc(%rbp), %rax
cmpq $0xc, %rax
jae 0x29c1
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
leaq 0xc7b1(%rip), %rdi      # 0xf14c
movb $0x0, %al
callq 0x3b40
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x3ca0
leaq 0xc7b0(%rip), %rdi      # 0xf165
movb $0x0, %al
callq 0x3b40
jmp 0x2ef8
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x28(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x20(%rbp)
movzwl -0x26(%rbp), %edi
callq 0x2250
movw %ax, -0x26(%rbp)
movzwl -0x24(%rbp), %edi
callq 0x2250
movw %ax, -0x24(%rbp)
movzwl -0x22(%rbp), %edi
callq 0x2250
movw %ax, -0x22(%rbp)
movzwl -0x20(%rbp), %edi
callq 0x2250
movw %ax, -0x20(%rbp)
movzwl -0x1e(%rbp), %edi
callq 0x2250
movw %ax, -0x1e(%rbp)
movzwl -0x26(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x2a55
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
leaq 0xc71d(%rip), %rdi      # 0xf14c
movb $0x0, %al
callq 0x3b40
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x3ca0
leaq 0xc746(%rip), %rdi      # 0xf18f
movb $0x0, %al
callq 0x3b40
jmp 0x2ef8
movzwl -0x24(%rbp), %eax
cmpl $0x1, %eax
je 0x2a92
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
leaq 0xc6e0(%rip), %rdi      # 0xf14c
movb $0x0, %al
callq 0x3b40
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x3ca0
leaq 0xc737(%rip), %rdi      # 0xf1bd
movb $0x0, %al
callq 0x3b40
jmp 0x2ef8
cmpw $0x0, -0x22(%rbp)
je 0x2acd
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
leaq 0xc6a5(%rip), %rdi      # 0xf14c
movb $0x0, %al
callq 0x3b40
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x3ca0
leaq 0xc72d(%rip), %rdi      # 0xf1ee
movb $0x0, %al
callq 0x3b40
jmp 0x2ef8
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x2150
movq -0x8(%rbp), %rdi
addq $0xc, %rdi
movslq -0xc(%rbp), %rax
subq $0xc, %rax
movl %eax, %esi
leaq -0x140(%rbp), %rdx
leaq -0x54c(%rbp), %rcx
leaq -0x550(%rbp), %r8
leaq -0x548(%rbp), %r9
callq 0x33b0
cmpl $0x0, %eax
je 0x2b4c
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
leaq 0xc626(%rip), %rdi      # 0xf14c
movb $0x0, %al
callq 0x3b40
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x3ca0
leaq 0xc6d6(%rip), %rdi      # 0xf216
movb $0x0, %al
callq 0x3b40
jmp 0x2ef8
leaq -0x140(%rbp), %rdi
callq 0x40a0
movq -0x18(%rbp), %rsi
leaq -0x140(%rbp), %rdx
movl -0x54c(%rbp), %ecx
movl -0x550(%rbp), %r8d
leaq 0xc6d5(%rip), %rdi      # 0xf24c
movb $0x0, %al
callq 0x3b40
leaq -0x140(%rbp), %rsi
leaq 0x14a54(%rip), %rdi     # 0x175e0
callq 0xb420
movq %rax, -0x570(%rbp)
cmpq $0x0, -0x570(%rbp)
je 0x2bae
movl $0x1, -0x564(%rbp)
jmp 0x2bb8
movl $0x0, -0x564(%rbp)
cmpl $0x0, -0x564(%rbp)
jne 0x2bdb
leaq -0x140(%rbp), %rsi
leaq 0xc6bc(%rip), %rdi      # 0xf28b
movb $0x0, %al
callq 0x3b40
jmp 0x2ef8
cmpl $0x1, -0x54c(%rbp)
jne 0x2bed
cmpl $0x1, -0x550(%rbp)
je 0x2c05
leaq 0xc6b2(%rip), %rdi      # 0xf2a6
movb $0x0, %al
callq 0x3b40
movl $0x0, -0x564(%rbp)
movw -0x28(%rbp), %ax
movw %ax, -0x34(%rbp)
movw $0x0, -0x32(%rbp)
movzwl -0x32(%rbp), %eax
orl $0x8000, %eax           # imm = 0x8000
movw %ax, -0x32(%rbp)
cmpl $0x0, -0x564(%rbp)
jne 0x2c34
movzwl -0x32(%rbp), %eax
orl $0x3, %eax
movw %ax, -0x32(%rbp)
movzwl -0x32(%rbp), %edi
callq 0x2100
movw %ax, -0x32(%rbp)
movl $0x1, %edi
callq 0x2100
movw %ax, -0x30(%rbp)
cmpl $0x0, -0x564(%rbp)
je 0x2c6d
movl $0x1, %edi
callq 0x2100
movzwl %ax, %eax
movl %eax, -0x574(%rbp)
jmp 0x2c77
xorl %eax, %eax
movl %eax, -0x574(%rbp)
jmp 0x2c77
movl -0x574(%rbp), %eax
movw %ax, -0x2e(%rbp)
movw $0x0, -0x2c(%rbp)
movw $0x0, -0x2a(%rbp)
movslq -0x554(%rbp), %rax
movq -0x34(%rbp), %rcx
movq %rcx, -0x540(%rbp,%rax)
movl -0x2c(%rbp), %ecx
movl %ecx, -0x538(%rbp,%rax)
movslq -0x554(%rbp), %rax
addq $0xc, %rax
movl %eax, -0x554(%rbp)
leaq -0x540(%rbp), %rdi
movslq -0x554(%rbp), %rax
addq %rax, %rdi
leaq -0x140(%rbp), %rdx
movl $0x3f4, %esi            # imm = 0x3F4
leaq -0x544(%rbp), %rcx
callq 0x3620
cmpl $0x0, %eax
je 0x2cee
jmp 0x2ef8
movl -0x544(%rbp), %eax
addl -0x554(%rbp), %eax
movl %eax, -0x554(%rbp)
movl -0x54c(%rbp), %eax
movzwl %ax, %edi
callq 0x2100
movw %ax, -0x556(%rbp)
movslq -0x554(%rbp), %rax
movw -0x556(%rbp), %cx
movw %cx, -0x540(%rbp,%rax)
movslq -0x554(%rbp), %rax
addq $0x2, %rax
movl %eax, -0x554(%rbp)
movl -0x550(%rbp), %eax
movzwl %ax, %edi
callq 0x2100
movw %ax, -0x556(%rbp)
movslq -0x554(%rbp), %rax
movw -0x556(%rbp), %cx
movw %cx, -0x540(%rbp,%rax)
movslq -0x554(%rbp), %rax
addq $0x2, %rax
movl %eax, -0x554(%rbp)
cmpl $0x0, -0x564(%rbp)
jne 0x2d86
jmp 0x2ed8
movl $0xc00c, %edi           # imm = 0xC00C
callq 0x2100
movw %ax, -0x556(%rbp)
movslq -0x554(%rbp), %rax
movw -0x556(%rbp), %cx
movw %cx, -0x540(%rbp,%rax)
movslq -0x554(%rbp), %rax
addq $0x2, %rax
movl %eax, -0x554(%rbp)
movl $0x1, %edi
callq 0x2100
movw %ax, -0x556(%rbp)
movslq -0x554(%rbp), %rax
movw -0x556(%rbp), %cx
movw %cx, -0x540(%rbp,%rax)
movslq -0x554(%rbp), %rax
addq $0x2, %rax
movl %eax, -0x554(%rbp)
movl $0x1, %edi
callq 0x2100
movw %ax, -0x556(%rbp)
movslq -0x554(%rbp), %rax
movw -0x556(%rbp), %cx
movw %cx, -0x540(%rbp,%rax)
movslq -0x554(%rbp), %rax
addq $0x2, %rax
movl %eax, -0x554(%rbp)
movl $0x12c, %edi            # imm = 0x12C
callq 0x2140
movl %eax, -0x55c(%rbp)
movslq -0x554(%rbp), %rax
movl -0x55c(%rbp), %ecx
movl %ecx, -0x540(%rbp,%rax)
movslq -0x554(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x554(%rbp)
movl $0x4, %edi
callq 0x2100
movw %ax, -0x556(%rbp)
movslq -0x554(%rbp), %rax
movw -0x556(%rbp), %cx
movw %cx, -0x540(%rbp,%rax)
movslq -0x554(%rbp), %rax
addq $0x2, %rax
movl %eax, -0x554(%rbp)
leaq -0x560(%rbp), %rdi
movq -0x570(%rbp), %rsi
addq $0x120, %rsi            # imm = 0x120
callq 0x32c0
movslq -0x554(%rbp), %rax
movl -0x560(%rbp), %ecx
movl %ecx, -0x540(%rbp,%rax)
movl -0x554(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x554(%rbp)
movl 0x146fa(%rip), %edi     # 0x175d8
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
leaq -0x540(%rbp), %rcx
movl -0x554(%rbp), %r8d
callq 0x39a0
addq $0x580, %rsp            # imm = 0x580
popq %rbp
retq
nopw %cs:(%rax,%rax)