// File: /workspace/github_c_makefile_star2_O2/bloomsource[P]dns_update/dns.c
// Function: main
int main( int argc, char* argv[] )
{
    int rc;
    struct pollfd pfds[2];
    char buf[1024];
    char ip[16];
    int  port;
    
    
    strcpy( log_file, "dns.log" );
    
    if( argc < 3 )
    {
        printf( "usage:dns_srv port auth_key [user]\n" );
        return 1;
    }
    
    cmd_port = atoi( argv[1] );
    snprintf( auth_key, sizeof(auth_key), "%s", argv[2] );
    
    dns_fd = udp_listen( NULL, DNS_PORT );
    if( dns_fd == -1 )
    {
        printf( "udp listen on dns port[%d] failed! err:%s\n", DNS_PORT, ERR );
        write_log( "[ERR] udp listen on dns port[%d] failed! err:%s", DNS_PORT, ERR );
        return 1;
    }
    
    cmd_fd = udp_listen( NULL, cmd_port );
    if( dns_fd == -1 )
    {
        printf( "udp listen on dns port[%d] failed! err:%s\n", cmd_port, ERR );
        write_log( "[ERR] udp listen on dns port[%d] failed! err:%s", cmd_port, ERR );
        return 1;
    }
    
    if( argc > 3 )
    {
        if( change_process_user( argv[3] ) )
        {
            printf( "process switch to user '%s' failed!\n", argv[3] );
            write_log( "[ERR] process switch to user '%s' failed!", argv[3] );
            return 1;
        }
    }
    
    signal( SIGINT, sigproc );
    signal( SIGTERM,sigproc );
    
    printf( "dns start up ok!\n" );
    write_log( "dns start up ok!" );
    write_log( "pid: %d", getpid() );
    
    rbtree_init( &domains, domain_node_cmp, domain_value_cmp, domain_node_swap );
    
    
    while( run )
    {
        pfds[0].fd = dns_fd;
        pfds[0].events = POLLIN;
        pfds[0].revents = 0;
        
        pfds[1].fd = cmd_fd;
        pfds[1].events = POLLIN;
        pfds[1].revents = 0;
        
        rc = poll( pfds, 2, 1000 );
        if( rc <= 0 )
            continue;
        
        if( pfds[0].revents )
        {
            rc = udp_recv( dns_fd, buf, sizeof(buf), ip, &port );
            if( rc > 0 )
            {
                proc_dns_query( buf, rc, ip, port );
            }
        }
        
        if( pfds[1].revents )
        {
            rc = udp_recv( cmd_fd, buf, sizeof(buf)-1, ip, &port );
            if( rc > 0 )
            {
                buf[rc] = 0;
                proc_cmd( buf, rc, ip, port );
            }
            
            
            
        }
        
        
    }
    
    write_log( "[WRN] dns stop." );
    
    return 0;
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
leaq 0xfdcc(%rip), %rax      # 0x12200
movabsq $0x676f6c2e736e64, %rcx # imm = 0x676F6C2E736E64
movq %rcx, (%rax)
cmpl $0x2, %edi
jg 0x2457
leaq 0x7115(%rip), %rdi      # 0x9562
callq 0x2070
jmp 0x26c7
movq %rsi, %rbx
movl %edi, %ebp
movq 0x8(%rsi), %rdi
callq 0x2290
leaq 0xfe68(%rip), %r15      # 0x122d4
movl %eax, (%r15)
movq 0x10(%rbx), %rcx
leaq 0xfe66(%rip), %rdi      # 0x122e0
leaq 0x708e(%rip), %rdx      # 0x950f
pushq $0x64
popq %rsi
xorl %eax, %eax
callq 0x2130
pushq $0x35
popq %rsi
xorl %edi, %edi
callq 0x30e8
movl %eax, 0xfd2d(%rip)      # 0x121c8
cmpl $-0x1, %eax
je 0x24f8
movl (%r15), %esi
xorl %edi, %edi
callq 0x30e8
movl %eax, 0xfd1c(%rip)      # 0x121cc
cmpl $-0x1, 0xfd11(%rip)     # 0x121c8
je 0x2684
cmpl $0x3, %ebp
je 0x2532
movq 0x18(%rbx), %rdi
callq 0x367f
testl %eax, %eax
je 0x2532
movq 0x18(%rbx), %rsi
leaq 0x6b85(%rip), %rdi      # 0x905f
xorl %eax, %eax
callq 0x2120
movq 0x18(%rbx), %rsi
leaq 0x6b98(%rip), %rdi      # 0x9084
xorl %eax, %eax
callq 0x3281
jmp 0x26c7
callq 0x2040
movq %rax, %rbx
movl (%rax), %edi
callq 0x22d0
leaq 0x6af6(%rip), %rdi      # 0x9004
pushq $0x35
popq %rbp
movl %ebp, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2120
movl (%rbx), %edi
callq 0x22d0
leaq 0x6b04(%rip), %rdi      # 0x902f
movl %ebp, %esi
jmp 0x26bd
leaq 0x1a5(%rip), %rbx       # 0x26de
pushq $0x2
popq %rdi
movq %rbx, %rsi
callq 0x21a0
pushq $0xf
popq %rdi
movq %rbx, %rsi
callq 0x21a0
leaq 0x6ffb(%rip), %rbx      # 0x9551
movq %rbx, %rdi
callq 0x2070
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3281
callq 0x20d0
leaq 0x6b3a(%rip), %rdi      # 0x90ae
movl %eax, %esi
xorl %eax, %eax
callq 0x3281
leaq 0xfc4c(%rip), %rdi      # 0x121d0
leaq 0x173(%rip), %rsi       # 0x26fe
leaq 0x179(%rip), %rdx       # 0x270b
leaq 0x17b(%rip), %rcx       # 0x2714
callq 0x697c
pushq $0x1
popq %rbp
leaq 0x10(%rsp), %rbx
pushq $0x2
popq %r14
leaq 0x30(%rsp), %r15
leaq 0x20(%rsp), %r12
leaq 0xc(%rsp), %r13
cmpl $0x0, 0xfbe0(%rip)      # 0x121a0
je 0x2672
movl 0xfbfc(%rip), %eax      # 0x121c8
movl %eax, 0x10(%rsp)
movl %ebp, 0x14(%rsp)
movl 0xfbf2(%rip), %eax      # 0x121cc
movl %eax, 0x18(%rsp)
movl %ebp, 0x1c(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x2240
testl %eax, %eax
jle 0x25b9
cmpw $0x0, 0x16(%rsp)
je 0x262c
movl 0xfbc4(%rip), %edi      # 0x121c8
movq %r15, %rsi
movl $0x400, %edx            # imm = 0x400
movq %r12, %rcx
movq %r13, %r8
callq 0x31d1
testl %eax, %eax
jle 0x262c
movl 0xc(%rsp), %ecx
movq %r15, %rdi
movl %eax, %esi
movq %r12, %rdx
callq 0x279f
cmpw $0x0, 0x1e(%rsp)
je 0x25b9
movl 0xfb92(%rip), %edi      # 0x121cc
movq %r15, %rsi
movl $0x3ff, %edx            # imm = 0x3FF
movq %r12, %rcx
movq %r13, %r8
callq 0x31d1
testl %eax, %eax
jle 0x25b9
movl %eax, %ecx
movb $0x0, 0x30(%rsp,%rcx)
movl 0xc(%rsp), %ecx
movq %r15, %rdi
movl %eax, %esi
movq %r12, %rdx
callq 0x2a8c
jmp 0x25b9
leaq 0x6a3d(%rip), %rdi      # 0x90b6
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x3281
jmp 0x26ca
movl (%r15), %ebx
callq 0x2040
movq %rax, %r14
movl (%rax), %edi
callq 0x22d0
leaq 0x6967(%rip), %rdi      # 0x9004
movl %ebx, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2120
movl (%r15), %ebx
movl (%r14), %edi
callq 0x22d0
leaq 0x6974(%rip), %rdi      # 0x902f
movl %ebx, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3281
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq