// File: luigix25[P]SSLP/client_files/client.cpp
// Function: main
int main(int argc,char **argv){

	if(argc < 3){
		cout<<"[Error] IP and Port are mandatory!"<<endl;
		exit(-1);
	}

	signal(SIGPIPE, SIG_IGN);
	signal (SIGINT,close_handler);

	int status,portServer;
	struct sockaddr_in serverAddress;

	int i,fdmax;
	fd_set read_fds;

	portServer = atoi(argv[2]);

	if(portServer <= 0 || portServer >= USHRT_MAX){
		cout<<"[Error] Invalid Port"<<endl;
		exit(-1);
	}


	socket_tcp = socket(AF_INET,SOCK_STREAM,0);
	if(socket_tcp < 0){
		perror("[Error] socket\n");
		return -1;
	}

	memset(&serverAddress,0,sizeof(serverAddress));
	
	serverAddress.sin_family = AF_INET;
	serverAddress.sin_port = htons(portServer);
	if(inet_pton(AF_INET,argv[1],&serverAddress.sin_addr) <=0){
		cout<<"[Error] invalid IP"<<endl;
		exit(-1);
	}
	

	status = connect(socket_tcp, (struct sockaddr*)&serverAddress,sizeof(serverAddress));
	if(status < 0){
		perror("[Error] connect\n");
		return -1;
	}
	
	cout<<endl<<"Successfully connected to the server "<<argv[1]<<" : "<<portServer<<endl;
	server_socket = NetSocket(socket_tcp);

	if(!initial_protocol(server_socket)){
		cout<<"Something Strange Occurred"<<endl;
		exit(-1);
		//handle error
	}

	cmd_help();

	FD_ZERO(&master);	
	FD_ZERO(&read_fds);


	FD_SET(socket_tcp,&master);
	FD_SET(0,&master);		// 0 è stdin

	fdmax = socket_tcp;

	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 == 0){						//stdin
					if(!read_input()){
						KeyManager::destroyKeys();
						server_socket.closeConnection();
						exit(-1);
					}
					//continue;
				} else if(i == socket_tcp) {			//server tcp
					//non dovrei mai arrivare qui	
					cout<<"Protocolar Error!"<<endl;
					KeyManager::destroyKeys();
					server_socket.closeConnection();
					exit(-1);
				} 

			}

		}
		
	}

	
	return 0;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
jge 0x727b
movq 0xbd62(%rip), %rdi      # 0x12fb8
leaq 0x7100(%rip), %rsi      # 0xe35d
callq 0x44a0
movq %rax, %rdi
movq 0xbd3c(%rip), %rsi      # 0x12fa8
callq 0x4500
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4640
movl $0xd, %edi
movl $0x1, %esi
callq 0x4620
movl $0x2, %edi
leaq -0x2246(%rip), %rsi     # 0x5050
callq 0x4620
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x4830
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jle 0x72ba
cmpl $0xffff, -0x18(%rbp)    # imm = 0xFFFF
jl 0x72e6
movq 0xbcf7(%rip), %rdi      # 0x12fb8
leaq 0x70b8(%rip), %rsi      # 0xe380
callq 0x44a0
movq %rax, %rdi
movq 0xbcd1(%rip), %rsi      # 0x12fa8
callq 0x4500
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4640
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x4120
movl %eax, 0xc303(%rip)      # 0x13600
cmpl $0x0, 0xc2fc(%rip)      # 0x13600
jge 0x731e
leaq 0x7088(%rip), %rdi      # 0xe395
callq 0x4430
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x76c1
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x4270
movw $0x2, -0x28(%rbp)
movl -0x18(%rbp), %eax
movzwl %ax, %edi
callq 0x4080
movw %ax, -0x26(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x28(%rbp), %rdx
addq $0x4, %rdx
movl $0x2, %edi
callq 0x4990
cmpl $0x0, %eax
jg 0x738e
movq 0xbc4f(%rip), %rdi      # 0x12fb8
leaq 0x7035(%rip), %rsi      # 0xe3a5
callq 0x44a0
movq %rax, %rdi
movq 0xbc29(%rip), %rsi      # 0x12fa8
callq 0x4500
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4640
movl 0xc26c(%rip), %edi      # 0x13600
leaq -0x28(%rbp), %rsi
movl $0x10, %edx
callq 0x4420
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x73c3
leaq 0x7006(%rip), %rdi      # 0xe3b8
callq 0x4430
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x76c1
movq 0xbbee(%rip), %rdi      # 0x12fb8
movq 0xbbd7(%rip), %rsi      # 0x12fa8
callq 0x4500
movq %rax, %rdi
leaq 0x6fe9(%rip), %rsi      # 0xe3c9
callq 0x44a0
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x44a0
movq %rax, %rdi
leaq 0x6ff0(%rip), %rsi      # 0xe3ef
callq 0x44a0
movq %rax, %rdi
movl -0x18(%rbp), %esi
callq 0x48d0
movq %rax, %rdi
movq 0xbb8f(%rip), %rsi      # 0x12fa8
callq 0x4500
movl 0xc1dc(%rip), %esi      # 0x13600
leaq -0xb8(%rbp), %rdi
callq 0x7df0
movl -0xb8(%rbp), %eax
movl %eax, 0xc1e4(%rip)      # 0x13620
movb -0xb4(%rbp), %al
movb %al, 0xc1dc(%rip)       # 0x13624
leaq 0xc1d1(%rip), %rdi      # 0x13620
callq 0x62c0
testb $0x1, %al
jne 0x7484
movq 0xbb59(%rip), %rdi      # 0x12fb8
leaq 0x6f8d(%rip), %rsi      # 0xe3f3
callq 0x44a0
movq %rax, %rdi
movq 0xbb33(%rip), %rsi      # 0x12fa8
callq 0x4500
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4640
callq 0x5190
leaq 0xc0f0(%rip), %rax      # 0x13580
movq %rax, -0xc8(%rbp)
movl $0x0, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
cmpq $0x10, %rax
jae 0x74d3
movq -0xc8(%rbp), %rax
movl -0xbc(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x74a1
jmp 0x74d5
jmp 0x74d7
leaq -0xb0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl $0x0, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
cmpq $0x10, %rax
jae 0x7521
movq -0xd8(%rbp), %rax
movl -0xcc(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x74ef
jmp 0x7523
movl 0xc0d7(%rip), %eax      # 0x13600
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0xe0(%rbp)
movl 0xc0b6(%rip), %eax      # 0x13600
movl $0x40, %ecx
cltd
idivl %ecx
movq -0xe0(%rbp), %rdx
movslq %eax, %rcx
leaq 0xc01d(%rip), %rax      # 0x13580
orq (%rax,%rcx,8), %rdx
leaq 0xc012(%rip), %rax      # 0x13580
movq %rdx, (%rax,%rcx,8)
movq 0xc007(%rip), %rax      # 0x13580
orq $0x1, %rax
movq %rax, 0xbffc(%rip)      # 0x13580
movl 0xc076(%rip), %eax      # 0x13600
movl %eax, -0x30(%rbp)
leaq -0xb0(%rbp), %rdi
leaq 0xbfe5(%rip), %rsi      # 0x13580
movl $0x80, %edx
callq 0x4410
movl -0x30(%rbp), %edi
addl $0x1, %edi
leaq -0xb0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rdx
movq %r8, %rcx
callq 0x4490
cmpl $0x0, %eax
jg 0x75dd
leaq 0x6e40(%rip), %rdi      # 0xe40e
callq 0x4430
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4640
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jg 0x76bc
movl -0x2c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xb0(%rbp,%rax,8), %rax
movq %rax, -0xe8(%rbp)
movl -0x2c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0xe8(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x76ac
cmpl $0x0, -0x2c(%rbp)
jne 0x7660
callq 0x6210
testb $0x1, %al
jne 0x765e
callq 0xd3c0
leaq 0xbfd1(%rip), %rdi      # 0x13620
callq 0x7e50
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4640
jmp 0x76aa
movl -0x2c(%rbp), %eax
cmpl 0xbf97(%rip), %eax      # 0x13600
jne 0x76a8
movq 0xb946(%rip), %rdi      # 0x12fb8
leaq 0x6da4(%rip), %rsi      # 0xe41d
callq 0x44a0
movq %rax, %rdi
movq 0xb920(%rip), %rsi      # 0x12fa8
callq 0x4500
callq 0xd3c0
leaq 0xbf87(%rip), %rdi      # 0x13620
callq 0x7e50
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4640
jmp 0x76aa
jmp 0x76ac
jmp 0x76ae
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x75e4
jmp 0x758d
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax)