// File: /workspace/github_c_makefile_star2_O0/icysunlight[P]miniftpd/main.c
// Function: main
int main()
{
    if ( 0 != getuid() )
    {
        fprintf(stderr,"miniftpd should start from root.\n");
        exit(EXIT_FAILURE);
    }

    if ( tunable_listen_address == NULL) 
    {
        //绑定到eth0对应的ip
        listenip = (char*)malloc(sizeof(char) * 128);
        getlocalip(listenip);
    }
    else
        strcpy(listenip,tunable_listen_address);

    parseconf_load_file(CONFFILE);
    printconf();
    hash_test();

    umask(tunable_local_umask);

    signal(SIGCHLD,handle_sigchld);
    int listenfd = tcp_srv(listenip,5188);
    
    pid_t pid;
    int conn;
    

    clntlmt_init();

    //daemon(0,0);

    while (1)
    {
        struct sockaddr_in addr;
        conn = accept_timeout(listenfd,&addr,tunable_connect_timeout);
        if ( -1 == conn )
            ERR_EXIT("connect error");

        
        session_t sessioninf;
        init_session(&sessioninf,conn);

        int lmt_fd[2];
        if ( -1 == socketpair(AF_UNIX,SOCK_STREAM,0,lmt_fd) )
            ERR_EXIT("socketpair");

        pid = fork();
        if (-1 == pid)
            ERR_EXIT("fork");


        if (0 == pid)
        {
            signal(SIGCHLD,SIG_IGN);
            close(listenfd);
            close(lmt_fd[0]);
            char cmd = '\0';
            readn(lmt_fd[1],&cmd,1);
            if ( cmd == 'r' )
            {
                close(lmt_fd[1]);
            }
            else if ( cmd == 'i' )
            {
                close(lmt_fd[1]);
                ftp_reply(&sessioninf,FTP_TOO_MANY_USERS,"There are too many connections from your internet address");
                exit(EXIT_FAILURE);    
            }
            else 
            {
                close(lmt_fd[1]);
                ftp_reply(&sessioninf,FTP_TOO_MANY_USERS,"There are too many connected users, please try later");
                exit(EXIT_FAILURE);    
            }
            begin_session(&sessioninf);
            break;
        }
        if (pid > 0)
        {
            close(lmt_fd[1]);
            //检查是否满足limit限制
            clntlmt_add(pid,addr.sin_addr.s_addr);
            int ret = clntlmt_check(addr.sin_addr.s_addr);
            if ( 0 == ret )
            {
                writen(lmt_fd[0],"r",1);
            }
            if ( -1 == ret )
            {
            }
            else if ( -2 == ret )
                printf("max clients total.\n");    
            close(conn);
            close(lmt_fd[0]);
        }
    }
        

    free(listenip);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc80, %rsp            # imm = 0xC80
movl $0x0, -0x4(%rbp)
callq 0x3180
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x36a1
movq 0x9972(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x5974(%rip), %rsi      # 0x9004
movb $0x0, %al
callq 0x3310
movl $0x1, %edi
callq 0x34e0
leaq 0x9ff0(%rip), %rax      # 0xd698
cmpq $0x0, (%rax)
jne 0x36cd
movl $0x80, %edi
callq 0x33a0
movq %rax, 0x9fc9(%rip)      # 0xd688
movq 0x9fc2(%rip), %rdi      # 0xd688
callq 0x3e30
jmp 0x36e3
movq 0x9fb4(%rip), %rdi      # 0xd688
leaq 0x9fbd(%rip), %rax      # 0xd698
movq (%rax), %rsi
callq 0x3080
leaq 0x593c(%rip), %rdi      # 0x9026
callq 0x51e0
movb $0x0, %al
callq 0x8a50
movb $0x0, %al
callq 0x8390
leaq 0x9bdc(%rip), %rax      # 0xd2e0
movl (%rax), %edi
callq 0x3330
movl $0x11, %edi
leaq 0x259(%rip), %rsi       # 0x3970
callq 0x32f0
movq 0x9f65(%rip), %rdi      # 0xd688
movl $0x1444, %esi           # imm = 0x1444
callq 0x3b90
movl %eax, -0x8(%rbp)
movb $0x0, %al
callq 0x8b90
movl -0x8(%rbp), %edi
leaq 0x9f4f(%rip), %rax      # 0xd690
movl (%rax), %edx
leaq -0x20(%rbp), %rsi
callq 0x4270
movl %eax, -0x10(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl -0x10(%rbp), %eax
jne 0x3771
jmp 0x375b
leaq 0x58d4(%rip), %rdi      # 0x9036
callq 0x3470
movl $0x1, %edi
callq 0x34e0
movl -0x10(%rbp), %esi
leaq -0xc68(%rbp), %rdi
callq 0x4b20
leaq -0xc70(%rbp), %rcx
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x3360
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x37b8
jmp 0x37a2
leaq 0x589b(%rip), %rdi      # 0x9044
callq 0x3470
movl $0x1, %edi
callq 0x34e0
callq 0x3530
movl %eax, -0xc(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl -0xc(%rbp), %eax
jne 0x37e2
jmp 0x37cc
leaq 0x587c(%rip), %rdi      # 0x904f
callq 0x3470
movl $0x1, %edi
callq 0x34e0
xorl %eax, %eax
cmpl -0xc(%rbp), %eax
jne 0x38bd
movl $0x11, %edi
movl $0x1, %esi
callq 0x32f0
movl -0x8(%rbp), %edi
callq 0x3260
movl -0xc70(%rbp), %edi
callq 0x3260
movb $0x0, -0xc71(%rbp)
movl -0xc6c(%rbp), %edi
leaq -0xc71(%rbp), %rsi
movl $0x1, %edx
callq 0x4600
movsbl -0xc71(%rbp), %eax
cmpl $0x72, %eax
jne 0x3846
movl -0xc6c(%rbp), %edi
callq 0x3260
jmp 0x38ac
movsbl -0xc71(%rbp), %eax
cmpl $0x69, %eax
jne 0x387f
movl -0xc6c(%rbp), %edi
callq 0x3260
leaq -0xc68(%rbp), %rdi
movl $0x1a5, %esi            # imm = 0x1A5
leaq 0x57e4(%rip), %rdx      # 0x9054
callq 0x5600
movl $0x1, %edi
callq 0x34e0
movl -0xc6c(%rbp), %edi
callq 0x3260
leaq -0xc68(%rbp), %rdi
movl $0x1a5, %esi            # imm = 0x1A5
leaq 0x57f1(%rip), %rdx      # 0x908e
callq 0x5600
movl $0x1, %edi
callq 0x34e0
leaq -0xc68(%rbp), %rdi
callq 0x4bb0
jmp 0x3952
cmpl $0x0, -0xc(%rbp)
jle 0x394d
movl -0xc6c(%rbp), %edi
callq 0x3260
movl -0xc(%rbp), %edi
movl -0x1c(%rbp), %esi
callq 0x8c40
movl -0x1c(%rbp), %edi
movb $0x0, %al
callq 0x8db0
movl %eax, -0xc78(%rbp)
xorl %eax, %eax
cmpl -0xc78(%rbp), %eax
jne 0x390e
movl -0xc70(%rbp), %edi
leaq 0x57bd(%rip), %rsi      # 0x90c1
movl $0x1, %edx
callq 0x46a0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl -0xc78(%rbp), %eax
jne 0x391d
jmp 0x393a
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpl -0xc78(%rbp), %eax
jne 0x3938
leaq 0x5792(%rip), %rdi      # 0x90c3
movb $0x0, %al
callq 0x31b0
jmp 0x393a
movl -0x10(%rbp), %edi
callq 0x3260
movl -0xc70(%rbp), %edi
callq 0x3260
jmp 0x3737
movq 0x9d2f(%rip), %rdi      # 0xd688
callq 0x3030
xorl %eax, %eax
addq $0xc80, %rsp            # imm = 0xC80
popq %rbp
retq
nopl (%rax)