// File: /workspace/github_cpp_makefile_star2_O1/SaadBazaz[P]RIP-Protocol-Simulation/Client.cpp
// Function: main
int main()
{
	int router_port;
	int myip;
	cout << ">> Which server do you want to connect to? ";
	cin >> router_port;

	cout << ">> Enter your unique IP: ";
	cin >> myip;

	cout << endl;

	cout << "Starting connection to server..." << endl;
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	assert(sockfd != -1);

	//Set Address Information
	struct sockaddr_in saddr;
	memset(&saddr, 0, sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(router_port);
	saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	//Link to router
	int res = connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
	assert(res != -1);

	cout << "Creating listening thread..." << endl;
	pthread_create(&listener_thread, NULL, messageListener, (void *)NULL);

	TableRow myself(myip, -1, "CLIENT");

	cout << "Sending my unique IP to server..." << endl;
	string init_msg = constructNewMessage(UPDATE, myip, router_port, (void *)&myself);
	send(sockfd, init_msg.c_str(), strlen(init_msg.c_str()), 0);

	cout << endl;

	char buff[128] = {0};

	int receiver = -1;
	while (buff != "close")
	{
		sleep(2);
		cout << "Press 1 for Sending a message\nPress 2 for resolving a Domain Name\n";
		int option = 0;
		cin >> option;
		string message ;
		if(option == 1){
			cout<<">> Enter the unique IP of the client you wish to talk to: ";
			cin>>receiver;

			memset(buff,0,128);
			cout<<">> Type you message : ";
			cin.ignore();
			fgets(buff,128,stdin);
			message = constructNewMessage(MESSAGE, myip, receiver, (void*)buff );
		}
		else if(option == 2){
			int DNS_IP = -1;
			cout << "Enter DNS IP  : ";
			cin >> DNS_IP;
			cout << endl;
			cout << "Enter Domain Name : \n>>";
			cin.ignore();
			fgets(buff,128,stdin);
			string finalMessage = "DNS REQ ";
			finalMessage += buff;
			message = constructNewMessage(MESSAGE, myip, DNS_IP, (void*)finalMessage.c_str() );
		}
		
		send(sockfd,message.c_str(),message.size(),0);

		if (strncmp(buff, "close", 3) == 0)
		{
			break;
		}

		memset(buff, 0, 128);

	}
	close(sockfd);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq 0x4c63(%rip), %rbx      # 0x7fd0
leaq 0x2cc2(%rip), %rsi      # 0x6036
movl $0x2b, %edx
movq %rbx, %rdi
callq 0x2230
movq 0x4c58(%rip), %r14      # 0x7fe0
leaq 0x1c(%rsp), %rsi
movq %r14, %rdi
callq 0x2120
leaq 0x2cc6(%rip), %rsi      # 0x6062
movl $0x19, %edx
movq %rbx, %rdi
callq 0x2230
leaq 0xc(%rsp), %rsi
movq %r14, %rdi
callq 0x2120
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
leaq 0x2c98(%rip), %rsi      # 0x607c
movl $0x20, %edx
movq %rbx, %rdi
callq 0x2230
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2060
movl %eax, 0x4da5(%rip)      # 0x81d4
cmpl $-0x1, %eax
je 0x39d9
xorps %xmm0, %xmm0
leaq 0x180(%rsp), %r14
movaps %xmm0, (%r14)
movw $0x2, (%r14)
movzwl 0x1c(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r14)
leaq 0x2c5e(%rip), %rdi      # 0x60c0
callq 0x21f0
movl %eax, 0x4(%r14)
movl 0x4d63(%rip), %edi      # 0x81d4
movq %r14, %rsi
movl $0x10, %edx
callq 0x21a0
cmpl $-0x1, %eax
je 0x39f8
leaq 0x2c46(%rip), %rsi      # 0x60d4
movl $0x1c, %edx
movq %rbx, %rdi
callq 0x2230
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
leaq 0x4d0f(%rip), %rdi      # 0x81d8
leaq -0x21d(%rip), %rdx      # 0x32b3
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x2300
movl 0xc(%rsp), %ebp
leaq 0xd0(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2c01(%rip), %rsi      # 0x60f1
leaq 0x2c00(%rip), %rdx      # 0x60f7
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
callq 0x5348
leaq 0x70(%rsp), %rdi
movl %ebp, -0x10(%rdi)
movl $0xffffffff, -0xc(%rdi) # imm = 0xFFFFFFFF
movw $0x0, -0x8(%rdi)
movl $0x0, -0x4(%rdi)
leaq 0x80(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
callq 0x52b8
movb $0x0, 0x90(%rsp)
movq $-0x1, 0x94(%rsp)
movq 0xc0(%rsp), %rdi
cmpq %r14, %rdi
je 0x356f
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x4a5a(%rip), %rbx      # 0x7fd0
leaq 0x2b7b(%rip), %rsi      # 0x60f8
movl $0x21, %edx
movq %rbx, %rdi
callq 0x2230
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq 0x4a28(%rip), %rbx      # 0x7fd0
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movl 0xc(%rsp), %edx
movl 0x1c(%rsp), %ecx
leaq 0xa0(%rsp), %rdi
leaq 0x60(%rsp), %r8
movl $0x2, %esi
callq 0x28eb
movl 0x4bf7(%rip), %ebp      # 0x81d4
movq 0xa0(%rsp), %r14
movq %r14, %rdi
callq 0x20d0
movl %ebp, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x20e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq 0x49b6(%rip), %rbx      # 0x7fd0
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
xorps %xmm0, %xmm0
leaq 0xe0(%rsp), %r14
movaps %xmm0, 0x70(%r14)
movaps %xmm0, 0x60(%r14)
movaps %xmm0, 0x50(%r14)
movaps %xmm0, 0x40(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
leaq 0x18(%rsp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x4972(%rip), %r13      # 0x7fe0
leaq 0x40(%rsp), %r15
leaq 0x160(%rsp), %r12
movl $0x2, %edi
callq 0x2170
movl $0x42, %edx
movq %rbx, %rdi
leaq 0x2a86(%rip), %rsi      # 0x611a
callq 0x2230
movl $0x0, 0x14(%rsp)
movq %r13, %rdi
leaq 0x14(%rsp), %rsi
callq 0x2120
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movb $0x0, 0x30(%rsp)
movl 0x14(%rsp), %eax
cmpl $0x2, %eax
je 0x379d
cmpl $0x1, %eax
jne 0x38b3
movl $0x3a, %edx
movq %rbx, %rdi
leaq 0x2a72(%rip), %rsi      # 0x615d
callq 0x2230
movq %r13, %rdi
leaq 0x18(%rsp), %rsi
callq 0x2120
xorps %xmm0, %xmm0
movaps %xmm0, 0x150(%rsp)
movaps %xmm0, 0x140(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movl $0x16, %edx
movq %rbx, %rdi
leaq 0x2a49(%rip), %rsi      # 0x6198
callq 0x2230
movq %r13, %rdi
callq 0x2310
movq 0x4865(%rip), %rax      # 0x7fc8
movq (%rax), %rdx
movq %r14, %rdi
movl $0x80, %esi
callq 0x22b0
movl 0xc(%rsp), %edx
movl 0x18(%rsp), %ecx
movq %r15, %rdi
movl $0x4, %esi
movq %r14, %r8
callq 0x28eb
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x2250
jmp 0x3897
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movl $0x10, %edx
movq %rbx, %rdi
leaq 0x29fb(%rip), %rsi      # 0x61af
callq 0x2230
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2120
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movl $0x17, %edx
movq %rbx, %rdi
leaq 0x29c4(%rip), %rsi      # 0x61c0
callq 0x2230
movq %r13, %rdi
callq 0x2310
movq 0x47b8(%rip), %rax      # 0x7fc8
movq (%rax), %rdx
movq %r14, %rdi
movl $0x80, %esi
callq 0x22b0
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %r15, %rdi
leaq 0x29a4(%rip), %rsi      # 0x61d8
leaq 0x29a5(%rip), %rdx      # 0x61e0
callq 0x5348
movq %r15, %rdi
movq %r14, %rsi
callq 0x2340
movl 0xc(%rsp), %edx
movl 0x10(%rsp), %ecx
movq 0x40(%rsp), %r8
movq %r12, %rdi
movl $0x4, %esi
callq 0x28eb
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x2250
movq 0x160(%rsp), %rdi
leaq 0x170(%rsp), %rax
cmpq %rax, %rdi
je 0x3897
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x38b3
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2200
movl 0x491b(%rip), %edi      # 0x81d4
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
xorl %ecx, %ecx
callq 0x20e0
movq %r13, %rbx
movq %r12, %r13
movq %r14, %r12
movq %r15, %r14
movl 0xe0(%rsp), %eax
movl $0x6c63, %ecx           # imm = 0x6C63
xorl %ecx, %eax
movzbl 0xe2(%rsp), %ecx
xorl $0x6f, %ecx
xorl %ebp, %ebp
orw %ax, %cx
setne %r15b
je 0x393d
xorps %xmm0, %xmm0
movaps %xmm0, 0x150(%rsp)
movaps %xmm0, 0x140(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3959
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2200
movb %r15b, %bpl
testl %ebp, %ebp
movq %r14, %r15
movq %r12, %r14
movq %r13, %r12
movq %rbx, %r13
movq 0x465f(%rip), %rbx      # 0x7fd0
jne 0x367b
movl 0x4857(%rip), %edi      # 0x81d4
callq 0x2350
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x39a3
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x39c5
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2200
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x26bd(%rip), %rdi      # 0x609d
leaq 0x26c3(%rip), %rsi      # 0x60aa
leaq 0x26c7(%rip), %rcx      # 0x60b5
movl $0x38, %edx
callq 0x2130
leaq 0x26cb(%rip), %rdi      # 0x60ca
leaq 0x26a4(%rip), %rsi      # 0x60aa
leaq 0x26a8(%rip), %rcx      # 0x60b5
movl $0x43, %edx
callq 0x2130
jmp 0x3a67
jmp 0x3a39
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
movq %r14, %rax
cmpq %r14, %rdi
jne 0x3ac0
jmp 0x3acb
jmp 0x3a67
movq %rax, %rbx
jmp 0x3aae
jmp 0x3a42
jmp 0x3a6e
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3a71
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x3a71
jmp 0x3a6e
jmp 0x3a67
movq %rax, %rbx
jmp 0x3a8d
jmp 0x3a6e
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3a8d
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2200
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3aae
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x2200
leaq 0x80(%rsp), %rax
movq 0x70(%rsp), %rdi
cmpq %rax, %rdi
je 0x3acb
movq (%rax), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x22e0
nop