// File: /workspace/github_c_makefile_star2_O3/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 0x3c35
leaq 0x88e7(%rip), %rax      # 0xc318
movq (%rax), %rsi
testq %rsi, %rsi
je 0x3a47
movq 0x88c8(%rip), %rdi      # 0xc308
callq 0x3090
jmp 0x3a60
movl $0x80, %edi
callq 0x33a0
movq %rax, 0x88b0(%rip)      # 0xc308
movq %rax, %rdi
callq 0x3f0b
leaq 0x45bf(%rip), %rdi      # 0x8026
callq 0x4a90
xorl %eax, %eax
callq 0x6ee0
xorl %eax, %eax
callq 0x6b44
leaq 0x8867(%rip), %rax      # 0xc2e8
movl (%rax), %edi
callq 0x3330
leaq 0x1cb(%rip), %rsi       # 0x3c5a
movl $0x11, %edi
callq 0x32f0
movq 0x8868(%rip), %rdi      # 0xc308
movl $0x1444, %esi           # imm = 0x1444
callq 0x3d86
movl %eax, %ebx
xorl %eax, %eax
callq 0x7014
leaq 0x8856(%rip), %rax      # 0xc310
movl (%rax), %edx
leaq 0x10(%rsp), %rsi
movl %ebx, %edi
callq 0x41af
cmpl $-0x1, %eax
je 0x3ba5
movl %eax, %r12d
leaq 0x20(%rsp), %r14
leaq 0x8(%rsp), %r15
leaq 0x10(%rsp), %rbp
movq %r14, %rdi
movl %r12d, %esi
callq 0x46d0
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x3360
cmpl $-0x1, %eax
je 0x3c1d
callq 0x3540
testl %eax, %eax
je 0x3baa
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x3c22
testl %r13d, %r13d
jle 0x3b86
movl 0xc(%rsp), %edi
callq 0x3260
movl 0x14(%rsp), %esi
movl %r13d, %edi
callq 0x7092
movl 0x14(%rsp), %edi
xorl %eax, %eax
callq 0x718b
cmpl $-0x2, %eax
je 0x3b69
testl %eax, %eax
jne 0x3b75
movl 0x8(%rsp), %edi
movl $0x1, %edx
leaq 0x455f(%rip), %rsi      # 0x80c1
callq 0x4466
jmp 0x3b75
leaq 0x4553(%rip), %rdi      # 0x80c3
callq 0x30c0
movl %r12d, %edi
callq 0x3260
movl 0x8(%rsp), %edi
callq 0x3260
leaq 0x8783(%rip), %rax      # 0xc310
movl (%rax), %edx
movl %ebx, %edi
movq %rbp, %rsi
callq 0x41af
movl %eax, %r12d
cmpl $-0x1, %eax
jne 0x3ae3
callq 0x35cf
movl $0x1, %esi
movl $0x11, %edi
callq 0x32f0
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 0x440b
movzbl (%rbx), %ebx
movl 0xc(%rsp), %edi
callq 0x3260
cmpl $0x72, %ebx
jne 0x3c27
leaq 0x20(%rsp), %rdi
callq 0x4717
movq 0x8704(%rip), %rdi      # 0xc308
callq 0x3040
xorl %eax, %eax
addq $0xc68, %rsp            # imm = 0xC68
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x35ba
callq 0x35a5
cmpl $0x69, %ebx
jne 0x3c3a
leaq 0x4421(%rip), %rdx      # 0x8054
jmp 0x3c41
callq 0x3580
leaq 0x444d(%rip), %rdx      # 0x808e
leaq 0x20(%rsp), %rdi
movl $0x1a5, %esi            # imm = 0x1A5
callq 0x4cf4
movl $0x1, %edi
callq 0x34e0