// File: /workspace/github_c_makefile_star2_O3/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 0xedcc(%rip), %rax      # 0x11200
movabsq $0x676f6c2e736e64, %rcx # imm = 0x676F6C2E736E64
movq %rcx, (%rax)
cmpl $0x2, %edi
jg 0x2457
leaq 0x70f3(%rip), %rdi      # 0x9540
callq 0x2070
jmp 0x26d6
movq %rsi, %rbx
movl %edi, %ebp
movq 0x8(%rsi), %rdi
callq 0x2290
leaq 0xee68(%rip), %r15      # 0x112d4
movl %eax, (%r15)
movq 0x10(%rbx), %rcx
leaq 0xee66(%rip), %rdi      # 0x112e0
leaq 0x706c(%rip), %rdx      # 0x94ed
movl $0x64, %esi
xorl %eax, %eax
callq 0x2130
xorl %edi, %edi
movl $0x35, %esi
callq 0x3109
movl %eax, 0xed29(%rip)      # 0x111c8
cmpl $-0x1, %eax
je 0x24fc
movl (%r15), %esi
xorl %edi, %edi
callq 0x3109
movl %eax, 0xed18(%rip)      # 0x111cc
cmpl $-0x1, 0xed0d(%rip)     # 0x111c8
je 0x2693
cmpl $0x3, %ebp
je 0x2539
movq 0x18(%rbx), %rdi
callq 0x371d
testl %eax, %eax
je 0x2539
movq 0x18(%rbx), %rsi
leaq 0x6b81(%rip), %rdi      # 0x905f
xorl %eax, %eax
callq 0x2120
movq 0x18(%rbx), %rsi
leaq 0x6b94(%rip), %rdi      # 0x9084
xorl %eax, %eax
callq 0x32b8
jmp 0x26d6
callq 0x2040
movq %rax, %rbx
movl (%rax), %edi
callq 0x22d0
leaq 0x6af2(%rip), %rdi      # 0x9004
movl $0x35, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2120
movl (%rbx), %edi
callq 0x22d0
leaq 0x6b00(%rip), %rdi      # 0x902f
movl $0x35, %esi
jmp 0x26cc
leaq 0x1af(%rip), %rbx       # 0x26ef
movl $0x2, %edi
movq %rbx, %rsi
callq 0x21a0
movl $0xf, %edi
movq %rbx, %rsi
callq 0x21a0
leaq 0x6fce(%rip), %r14      # 0x952f
movq %r14, %rdi
callq 0x2070
xorl %ebx, %ebx
movq %r14, %rdi
xorl %eax, %eax
callq 0x32b8
callq 0x20d0
leaq 0x6b2d(%rip), %rdi      # 0x90ae
movl %eax, %esi
xorl %eax, %eax
callq 0x32b8
leaq 0xec3f(%rip), %rdi      # 0x111d0
leaq 0x17a(%rip), %rsi       # 0x2712
leaq 0x180(%rip), %rdx       # 0x271f
leaq 0x182(%rip), %rcx       # 0x2728
callq 0x70cc
cmpl $0x0, 0xebee(%rip)      # 0x111a0
je 0x2683
movl $0x1, %ebp
leaq 0x10(%rsp), %r14
leaq 0x30(%rsp), %r15
leaq 0x20(%rsp), %r12
leaq 0xc(%rsp), %r13
movl 0xebf1(%rip), %eax      # 0x111c8
movl %eax, 0x10(%rsp)
movl %ebp, 0x14(%rsp)
movl 0xebe7(%rip), %eax      # 0x111cc
movl %eax, 0x18(%rsp)
movl %ebp, 0x1c(%rsp)
movl $0x2, %esi
movq %r14, %rdi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x2240
testl %eax, %eax
jle 0x2676
cmpw $0x0, 0x16(%rsp)
je 0x2639
movl 0xebb7(%rip), %edi      # 0x111c8
movq %r15, %rsi
movl $0x400, %edx            # imm = 0x400
movq %r12, %rcx
movq %r13, %r8
callq 0x3206
testl %eax, %eax
jle 0x2639
movl 0xc(%rsp), %ecx
movq %r15, %rdi
movl %eax, %esi
movq %r12, %rdx
callq 0x27b3
cmpw $0x0, 0x1e(%rsp)
je 0x2676
movl 0xeb85(%rip), %edi      # 0x111cc
movq %r15, %rsi
movl $0x3ff, %edx            # imm = 0x3FF
movq %r12, %rcx
movq %r13, %r8
callq 0x3206
testl %eax, %eax
jle 0x2676
movl %eax, %ecx
movb $0x0, 0x30(%rsp,%rcx)
movl 0xc(%rsp), %ecx
movq %r15, %rdi
movl %eax, %esi
movq %r12, %rdx
callq 0x2aa3
cmpl $0x0, 0xeb23(%rip)      # 0x111a0
jne 0x25d1
leaq 0x6a2c(%rip), %rdi      # 0x90b6
xorl %eax, %eax
callq 0x32b8
jmp 0x26db
movl (%r15), %ebx
callq 0x2040
movq %rax, %r14
movl (%rax), %edi
callq 0x22d0
leaq 0x6958(%rip), %rdi      # 0x9004
movl %ebx, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2120
movl (%r15), %ebx
movl (%r14), %edi
callq 0x22d0
leaq 0x6965(%rip), %rdi      # 0x902f
movl %ebx, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32b8
movl $0x1, %ebx
movl %ebx, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq