// File: /workspace/github_c_makefile_star2_O2/ulisses[P]ftp-client-server/servidorc2.c
// Function: aceitaLigacao
ConState* aceitaLigacao(int s, struct sockaddr_in *client, int clientLen){
	int try, timeout = 10;
	struct timeval t;
	fd_set fds;	
	try = 0;
	
	char *buffer;
	int n, r, conected = 0;
	Datagrama *recebido, *enviar = initDatagrama();
	ConState *con = (ConState *)malloc(sizeof(struct con_estado));
	
	/*Construir um segmento com um SYN ACK para enviar ao cliente*/
	/*Coloca tambem no campo win_size o tamanho do buffer*/
	enviar->flags = (FL_SYN | FL_ACK);
	enviar->win_size = TAM2;
	buffer = muxDatagrama(enviar);
	
	n = sendto(s,buffer,strlen(buffer), 0, (struct sockaddr *)client, clientLen);
	if (n  < 0) {
		perror("send");
		free(buffer);
		free(client);
		exit(1);
		}
	/*Esperar por um ack usando o select de forma a que o recvfrom nao bloqueie
	Desta forma pode-se obter o valor do RTT uma vez que quando o select retorna 
	devido á escrita em um dos fds ele coloca no timeval o tempo restante*/
	
	
	FD_ZERO(&fds); FD_SET(s, &fds); //inicializa fds e coloca sock em fds
	t.tv_sec = timeout; t.tv_usec = 0; //inicializa o timeval
	
        r = 1;
        
	while(!conected && try < 10 ){
	
	r = select(s + 1, &fds, NULL, NULL, &t);
	if(r < 0) {
	perror("select");
	exit(0);
	}
	if(r && FD_ISSET(s, &fds)){
		n = recvfrom(s,buffer,TAM2,0, (struct sockaddr *) client, (socklen_t*) &clientLen);
		if(n < 0){ //Nao foi lido nada
			try++;
		}
		else {
			recebido = demuxDatagrama(buffer);
			if(recebido && recebido->flags & FL_ACK){
			conected = 1;
			/*three way hand shake completo*/
			/*retiro neste ultimo segmento recevido o tamanho do buffer do cliente e tambem o rtt*/
			con->clientBuffer = recebido->win_size;
			con->rtt.tv_sec = (timeout - t.tv_sec);
			con->rtt.tv_usec = t.tv_usec;
			printConState(con);
			conected = 1;
			}
			}
		}
	try++;
	}
	if(FD_ISSET(s, &fds)) FD_CLR(s, &fds);
	if(conected) return con; else{free(con); return NULL;} 
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %edx, %ebx
movq %rsi, %r15
movl %edi, %r13d
movl %edx, -0x3c(%rbp)
callq 0x15ea
movq %rax, %r14
pushq $0x18
popq %rdi
callq 0x1100
movq %rax, %r12
movabsq $0x300000100, %rax      # imm = 0x300000100
movq %rax, 0xc(%r14)
movq %r14, %rdi
callq 0x16b5
movq %rax, %r14
movq %rax, %rdi
callq 0x1070
movq %r13, -0x50(%rbp)
movl %r13d, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq %r15, -0x48(%rbp)
movq %r15, %r8
movl %ebx, %r9d
callq 0x10a0
testl %eax, %eax
js 0x20ff
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1fa8
andq $0x0, -0xf8(%rbp,%rax,8)
incq %rax
jmp 0x1f94
movq %r14, -0x58(%rbp)
movq %r12, -0x38(%rbp)
pushq $0x40
popq %rcx
movq -0x50(%rbp), %rsi
movl %esi, %eax
cltd
idivl %ecx
movslq %eax, %rdx
shll $0x6, %eax
movl %esi, %ecx
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
movq %rdx, -0x68(%rbp)
movq %rax, -0x60(%rbp)
orq %rax, -0xf8(%rbp,%rdx,8)
leaq -0x78(%rbp), %r15
movq $0xa, (%r15)
andq $0x0, 0x8(%r15)
leal 0x1(%rsi), %r14d
xorl %r13d, %r13d
leaq -0xf8(%rbp), %r12
xorl %ebx, %ebx
movl %r14d, -0x2c(%rbp)
testl %r13d, %r13d
jne 0x20c4
cmpl $0x9, %ebx
jg 0x20c4
movl %r14d, %edi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x10f0
testl %eax, %eax
js 0x20ec
xorl %r13d, %r13d
testl %eax, %eax
je 0x20bd
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
testq %rcx, -0xf8(%rbp,%rax,8)
je 0x20bd
movq %r12, %r14
movq %r15, %r12
movl $0x100, %edx            # imm = 0x100
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %r15
movq %r15, %rsi
xorl %ecx, %ecx
movq -0x48(%rbp), %r8
leaq -0x3c(%rbp), %r9
callq 0x1120
testl %eax, %eax
js 0x20b1
movq %r15, %rdi
callq 0x160b
testq %rax, %rax
je 0x20b3
testb $0x2, 0x10(%rax)
movq %r12, %r15
je 0x20b6
movl 0xc(%rax), %eax
pushq $0xa
popq %rcx
subq -0x78(%rbp), %rcx
movq -0x38(%rbp), %rdi
movl %eax, (%rdi)
movq %rcx, 0x8(%rdi)
movq -0x70(%rbp), %rax
movq %rax, 0x10(%rdi)
callq 0x1909
pushq $0x1
popq %rax
movl %eax, %r13d
jmp 0x20b6
incl %ebx
movq %r12, %r15
movq %r14, %r12
movl -0x2c(%rbp), %r14d
incl %ebx
jmp 0x2000
testl %r13d, %r13d
je 0x20cf
movq -0x38(%rbp), %rax
jmp 0x20da
movq -0x38(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x100f(%rip), %rdi      # 0x3102
callq 0x1160
xorl %edi, %edi
callq 0x11a0
leaq 0xff7(%rip), %rdi       # 0x30fd
callq 0x1160
movq %r14, %rdi
callq 0x1030
movq -0x48(%rbp), %rdi
callq 0x1030
pushq $0x1
popq %rdi
callq 0x11a0