// File: /workspace/github_cpp_makefile_star2_O2/luigix25[P]SSLP/server_files/server.cpp
// Function: main
int main(int argc,char **argv){


	if(argc != 2){
		cout<<"[Error] missing port!"<<endl;
		exit(-1);
	}

	signal(SIGPIPE, SIG_IGN);					//ignoro sigpipe
	signal (SIGINT,close_handler);				//gestisco i ctrl-c

	struct sockaddr_in clientAddress;

	int port = atoi(argv[1]);
	int fdmax;
	unsigned int addrlen;
	int i,status,new_sock;
	int cmd;
	fd_set read_fds;
		
	if(port <= 0 || port >= USHRT_MAX){
		cout<<"[Error] Invalid Port"<<endl;
		exit(-1);
	}


	memset(&clientAddress,0,sizeof(clientAddress));

	server_socket = initialize_server(port);

	cout<<"[LOG] Waiting connections on port "<<port<<endl;

	
	FD_ZERO(&master);	
	FD_ZERO(&read_fds);
	FD_SET(server_socket,&master);
	fdmax = server_socket;

	client_socket = NetSocket();

	bool alreadyConnected = false;


	while(true){
		read_fds = master;
		if(select(fdmax+1,&read_fds,NULL,NULL,NULL) <=0){
			perror("[Error] Select");
			exit(-1);
		}

		for(i = 0; i <= fdmax; i++){
			if(FD_ISSET(i,&read_fds)){	
				if(i == server_socket){ 						//new connection
					memset(&clientAddress,0,sizeof(clientAddress));
					addrlen = sizeof(clientAddress);
					new_sock = accept(server_socket,(struct sockaddr*)&clientAddress,&addrlen);
					
					if(new_sock < 0){
						perror("[Error] accept\n");
						continue;
					}

					if(alreadyConnected){					
						close(new_sock);						//refuse new connections
						continue;
					}

					FD_SET(new_sock,&master);
					if(new_sock > fdmax) 
						fdmax = new_sock;
					
					client_socket.setSocket(new_sock);
					alreadyConnected = true;

					if(!initial_protocol(client_socket)){
						client_socket.closeConnection();
						KeyManager::destroyKeys();
						alreadyConnected = false;
						FD_CLR(new_sock,&master);
					}

					continue;


				} else {

					status = receive_command(cmd);
					if(!status){
						cout<<"[LOG] Client Disconnected"<<endl;
						client_socket.closeConnection();
						KeyManager::destroyKeys();
						alreadyConnected = false;
						FD_CLR(i,&master);							//remove socket from select

					} else {
						if(!select_command(cmd)){
							client_socket.closeConnection();
							KeyManager::destroyKeys();
							alreadyConnected = false;
							FD_CLR(i,&master);
						}
					}

				}
			}

		}	

	}
	
	//close(server_socket);											//no more clients allowed

	return 0;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
cmpl $0x2, %edi
jne 0x5f17
movq %rsi, %r14
pushq $0xd
popq %rdi
pushq $0x1
popq %rbp
movq %rbp, %rsi
callq 0x4560
leaq -0x11aa(%rip), %rsi     # 0x4d3c
pushq $0x2
popq %rdi
callq 0x4560
movq 0x8(%r14), %rdi
callq 0x4730
movl %eax, %r14d
movl $0xffff0001, %eax       # imm = 0xFFFF0001
leal (%r14,%rax), %ecx
cmpl %eax, %ecx
ja 0x5f37
movq 0x90c2(%rip), %rdi      # 0xefd0
leaq 0x4338(%rip), %rsi      # 0xa24d
jmp 0x5f25
movq 0x90b2(%rip), %rdi      # 0xefd0
leaq 0x4312(%rip), %rsi      # 0xa237
callq 0x4440
movq %rax, %rdi
callq 0x4230
jmp 0x6196
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movl %r14d, %edi
callq 0x5435
movl %eax, 0x962b(%rip)      # 0xf578
movq 0x907c(%rip), %rdi      # 0xefd0
leaq 0x4307(%rip), %rsi      # 0xa262
callq 0x4440
movq %rax, %rdi
movl %r14d, %esi
callq 0x47c0
movq %rax, %rdi
callq 0x4230
xorl %eax, %eax
leaq 0x9574(%rip), %rbx      # 0xf4f0
cmpq $0x80, %rax
je 0x5f8f
andq $0x0, (%rax,%rbx)
addq $0x8, %rax
jmp 0x5f7c
xorl %eax, %eax
cmpq $0x10, %rax
je 0x5fa2
andq $0x0, 0x38(%rsp,%rax,8)
incq %rax
jmp 0x5f91
movl 0x95cf(%rip), %r15d     # 0xf578
pushq $0x40
popq %rcx
movl %r15d, %eax
cltd
idivl %ecx
movslq %eax, %rdx
shll $0x6, %eax
movl %r15d, %ecx
subl %eax, %ecx
movq %rbp, %rax
shlq %cl, %rax
orq %rax, (%rbx,%rdx,8)
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x63fc
movb 0x4(%r14), %al
movb %al, 0x9596(%rip)       # 0xf574
movl (%r14), %eax
movl %eax, 0x9589(%rip)      # 0xf570
movq $0x0, 0x10(%rsp)
leaq 0xc(%rsp), %r12
pushq $0x10
popq %rcx
leaq 0x38(%rsp), %rax
movq %rax, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
leal 0x1(%r15), %edi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x4430
testl %eax, %eax
jle 0x618a
xorl %r14d, %r14d
cmpl %r15d, %r14d
jg 0x617e
movl %r14d, %r13d
shrl $0x6, %r13d
movq 0x38(%rsp,%r13,8), %rax
movq %rbp, %rbx
movl %r14d, %ecx
shlq %cl, %rbx
btq %r14, %rax
jae 0x6168
cmpl 0x9525(%rip), %r14d     # 0xf578
jne 0x6099
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movl $0x10, 0x18(%rsp)
movl %r14d, %edi
leaq 0x20(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x4180
testl %eax, %eax
js 0x6170
testb $0x1, 0x10(%rsp)
je 0x60fc
movl %eax, %edi
callq 0x4900
movb $0x1, %al
movq %rax, 0x10(%rsp)
jmp 0x6168
movq %r12, %rdi
callq 0x54f2
notq %rbx
testb %al, %al
je 0x60ba
movl 0xc(%rsp), %edi
callq 0x5417
testb %al, %al
je 0x60d5
jmp 0x6168
movq 0x8f0f(%rip), %rdi      # 0xefd0
leaq 0x41dc(%rip), %rsi      # 0xa2a4
callq 0x4440
movq %rax, %rdi
callq 0x4230
leaq 0x9494(%rip), %rdi      # 0xf570
callq 0x640c
callq 0x913c
leaq 0x9403(%rip), %rax      # 0xf4f0
andq %rbx, (%rax,%r13,8)
movq $0x0, 0x10(%rsp)
jmp 0x6168
movq %rbp, %rbx
movl %eax, %ecx
shlq %cl, %rbx
movl %eax, %r13d
shrl $0x6, %r13d
leaq 0x93de(%rip), %rbp      # 0xf4f0
orq %rbx, (%rbp,%r13,8)
cmpl %r15d, %eax
cmovgl %eax, %r15d
leaq 0x944b(%rip), %r12      # 0xf570
movq %r12, %rdi
movl %eax, %esi
callq 0x6404
movq %r12, %rdi
callq 0x55f1
movb $0x1, %cl
movq %rcx, 0x10(%rsp)
testb %al, %al
jne 0x6160
movq %r12, %rdi
callq 0x640c
callq 0x913c
notq %rbx
andq %rbx, (%rbp,%r13,8)
movq $0x0, 0x10(%rsp)
pushq $0x1
popq %rbp
leaq 0xc(%rsp), %r12
incl %r14d
jmp 0x6024
leaq 0x411d(%rip), %rdi      # 0xa294
callq 0x43e0
jmp 0x6168
leaq 0x936b(%rip), %rbx      # 0xf4f0
jmp 0x5ff5
leaq 0x40f4(%rip), %rdi      # 0xa285
callq 0x43e0
pushq $-0x1
popq %rdi
callq 0x4580