// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc68, %rsp            # imm = 0xC68
callq 0x31a0
testl %eax, %eax
jne 0x3c25
leaq 0x88e7(%rip), %rax      # 0xc308
movq (%rax), %rsi
testq %rsi, %rsi
je 0x3a37
movq 0x88c8(%rip), %rdi      # 0xc2f8
callq 0x3090
jmp 0x3a50
movl $0x80, %edi
callq 0x3390
movq %rax, 0x88b0(%rip)      # 0xc2f8
movq %rax, %rdi
callq 0x3eef
leaq 0x45cf(%rip), %rdi      # 0x8026
callq 0x4ae8
xorl %eax, %eax
callq 0x6fbc
xorl %eax, %eax
callq 0x6bfc
leaq 0x886f(%rip), %rax      # 0xc2e0
movl (%rax), %edi
callq 0x3320
leaq 0x1cb(%rip), %rsi       # 0x3c4a
movl $0x11, %edi
callq 0x32e0
movq 0x8868(%rip), %rdi      # 0xc2f8
movl $0x1444, %esi           # imm = 0x1444
callq 0x3d76
movl %eax, %ebx
xorl %eax, %eax
callq 0x70f0
leaq 0x8856(%rip), %rax      # 0xc300
movl (%rax), %edx
leaq 0x10(%rsp), %rsi
movl %ebx, %edi
callq 0x4193
cmpl $-0x1, %eax
je 0x3b95
movl %eax, %r12d
leaq 0x20(%rsp), %r14
leaq 0x8(%rsp), %r15
leaq 0x10(%rsp), %rbp
movq %r14, %rdi
movl %r12d, %esi
callq 0x4710
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x3350
cmpl $-0x1, %eax
je 0x3c0d
callq 0x3530
testl %eax, %eax
je 0x3b9a
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x3c12
testl %r13d, %r13d
jle 0x3b76
movl 0xc(%rsp), %edi
callq 0x3260
movl 0x14(%rsp), %esi
movl %r13d, %edi
callq 0x716e
movl 0x14(%rsp), %edi
xorl %eax, %eax
callq 0x7267
cmpl $-0x2, %eax
je 0x3b59
testl %eax, %eax
jne 0x3b65
movl 0x8(%rsp), %edi
movl $0x1, %edx
leaq 0x456f(%rip), %rsi      # 0x80c1
callq 0x4465
jmp 0x3b65
leaq 0x4563(%rip), %rdi      # 0x80c3
callq 0x30c0
movl %r12d, %edi
callq 0x3260
movl 0x8(%rsp), %edi
callq 0x3260
leaq 0x8783(%rip), %rax      # 0xc300
movl (%rax), %edx
movl %ebx, %edi
movq %rbp, %rsi
callq 0x4193
movl %eax, %r12d
cmpl $-0x1, %eax
jne 0x3ad3
callq 0x35bf
movl $0x1, %esi
movl $0x11, %edi
callq 0x32e0
movl %ebx, %edi
callq 0x3260
movl 0x8(%rsp), %edi
callq 0x3260
leaq 0x7(%rsp), %rbx
movb $0x0, (%rbx)
movl 0xc(%rsp), %edi
movl $0x1, %edx
movq %rbx, %rsi
callq 0x440a
movzbl (%rbx), %ebx
movl 0xc(%rsp), %edi
callq 0x3260
cmpl $0x72, %ebx
jne 0x3c17
leaq 0x20(%rsp), %rdi
callq 0x475b
movq 0x8704(%rip), %rdi      # 0xc2f8
callq 0x3040
xorl %eax, %eax
addq $0xc68, %rsp            # imm = 0xC68
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x35aa
callq 0x3595
cmpl $0x69, %ebx
jne 0x3c2a
leaq 0x4431(%rip), %rdx      # 0x8054
jmp 0x3c31
callq 0x3570
leaq 0x445d(%rip), %rdx      # 0x808e
leaq 0x20(%rsp), %rdi
movl $0x1a5, %esi            # imm = 0x1A5
callq 0x4d4c
movl $0x1, %edi
callq 0x34d0