// File: /workspace/github_cpp_makefile_star2_O2/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 0x43fe(%rip), %rbx      # 0x6fc8
leaq 0x1465(%rip), %rsi      # 0x4036
movq %rbx, %rdi
callq 0x21c0
movq 0x4400(%rip), %r14      # 0x6fe0
leaq 0x1c(%rsp), %rsi
movq %r14, %rdi
callq 0x2120
leaq 0x146e(%rip), %rsi      # 0x4062
movq %rbx, %rdi
callq 0x21c0
leaq 0xc(%rsp), %rsi
movq %r14, %rdi
callq 0x2120
movq %rbx, %rdi
callq 0x20d0
leaq 0x1464(%rip), %rsi      # 0x407c
movq %rbx, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x2050
movl %eax, 0x4599(%rip)      # 0x71d4
cmpl $-0x1, %eax
je 0x3057
leaq 0x168(%rsp), %rbx
andl $0x0, 0xc(%rbx)
andq $0x0, 0x4(%rbx)
movw $0x2, (%rbx)
movzwl 0x1c(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%rbx)
leaq 0x1452(%rip), %rdi      # 0x40c0
callq 0x21e0
movl %eax, 0x4(%rbx)
movl 0x4558(%rip), %edi      # 0x71d4
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x2190
cmpl $-0x1, %eax
je 0x3070
movq 0x4331(%rip), %rdi      # 0x6fc8
leaq 0x1436(%rip), %rsi      # 0x40d4
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
leaq 0x4526(%rip), %rdi      # 0x71d8
leaq -0x1b5(%rip), %rdx      # 0x2b04
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x2300
movl 0xc(%rsp), %ebx
leaq 0x1424(%rip), %rsi      # 0x40f1
leaq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x37c0
leaq 0x50(%rsp), %r14
movl %ebx, -0x10(%r14)
orl $-0x1, -0xc(%r14)
andw $0x0, -0x8(%r14)
andl $0x0, -0x4(%r14)
leaq 0x128(%rsp), %rsi
movq %r14, %rdi
callq 0x2250
movb $0x0, 0x70(%rsp)
orq $-0x1, 0x74(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x20c0
movq 0x429e(%rip), %rdi      # 0x6fc8
leaq 0x13c7(%rip), %rsi      # 0x40f8
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
movl 0xc(%rsp), %edx
movl 0x1c(%rsp), %ecx
leaq 0x108(%rsp), %rdi
pushq $0x2
popq %rsi
leaq 0x40(%rsp), %r8
callq 0x2780
movl 0x4473(%rip), %ebx      # 0x71d4
movq 0x108(%rsp), %r14
movq %r14, %rdi
callq 0x20a0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x20e0
movq 0x4241(%rip), %rdi      # 0x6fc8
callq 0x20d0
xorps %xmm0, %xmm0
leaq 0x80(%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)
leaq 0x18(%rsp), %rax
orl $-0x1, (%rax)
movaps %xmm0, (%r14)
movq 0x41fb(%rip), %r12      # 0x6fc8
movq 0x420c(%rip), %rbx      # 0x6fe0
leaq 0x148(%rsp), %rbp
leaq 0x178(%rsp), %r13
leaq 0x20(%rsp), %r15
pushq $0x2
popq %rdi
callq 0x2160
movq %r12, %rdi
leaq 0x131f(%rip), %rsi      # 0x411a
callq 0x21c0
andl $0x0, 0x14(%rsp)
movq %rbx, %rdi
leaq 0x14(%rsp), %rsi
callq 0x2120
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
andq $0x0, 0x28(%rsp)
movb $0x0, 0x30(%rsp)
movl 0x14(%rsp), %eax
cmpl $0x2, %eax
je 0x2ef0
cmpl $0x1, %eax
jne 0x2f9a
movq %r12, %rdi
leaq 0x1316(%rip), %rsi      # 0x415d
callq 0x21c0
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
callq 0x2120
xorps %xmm0, %xmm0
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movq %r12, %rdi
leaq 0x12f2(%rip), %rsi      # 0x4198
callq 0x21c0
movq %rbx, %rdi
callq 0x2310
movq 0x4106(%rip), %rax      # 0x6fc0
movq (%rax), %rdx
movq %r14, %rdi
movl $0x80, %esi
callq 0x22a0
movl 0xc(%rsp), %edx
movl 0x18(%rsp), %ecx
movq %rbp, %rdi
pushq $0x4
popq %rsi
movq %r14, %r8
callq 0x2780
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2230
jmp 0x2f92
orl $-0x1, 0x10(%rsp)
movq %r12, %rdi
leaq 0x12b0(%rip), %rsi      # 0x41af
callq 0x21c0
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2120
movq %r12, %rdi
callq 0x20d0
movq %r12, %rdi
leaq 0x129d(%rip), %rsi      # 0x41c0
callq 0x21c0
movq %rbx, %rdi
callq 0x2310
movq 0x4089(%rip), %rax      # 0x6fc0
movq (%rax), %rdx
movq %r14, %rdi
movl $0x80, %esi
callq 0x22a0
movq %rbp, %rdi
leaq 0x1287(%rip), %rsi      # 0x41d8
movq %r13, %rdx
callq 0x37c0
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2330
movl 0xc(%rsp), %edx
movl 0x10(%rsp), %ecx
movq 0x148(%rsp), %r8
movq %r13, %rdi
pushq $0x4
popq %rsi
callq 0x2780
movq %r15, %rdi
movq %r13, %rsi
callq 0x2230
movq %r13, %rdi
callq 0x20c0
movq %rbp, %rdi
callq 0x20c0
movl 0x4234(%rip), %edi      # 0x71d4
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
xorl %ecx, %ecx
callq 0x20e0
movq %r14, %rdi
leaq 0x1075(%rip), %rsi      # 0x4030
pushq $0x3
popq %rdx
callq 0x2200
testl %eax, %eax
je 0x3017
xorps %xmm0, %xmm0
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movq %r15, %rdi
callq 0x20c0
jmp 0x2de9
leaq 0x20(%rsp), %rdi
callq 0x20c0
movl 0x41ad(%rip), %edi      # 0x71d4
callq 0x2350
leaq 0x108(%rsp), %rdi
callq 0x20c0
leaq 0x50(%rsp), %rdi
callq 0x20c0
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x103f(%rip), %rdi      # 0x409d
leaq 0x1045(%rip), %rsi      # 0x40aa
leaq 0x1049(%rip), %rcx      # 0x40b5
pushq $0x38
jmp 0x3087
leaq 0x1053(%rip), %rdi      # 0x40ca
leaq 0x102c(%rip), %rsi      # 0x40aa
leaq 0x1030(%rip), %rcx      # 0x40b5
pushq $0x43
popq %rdx
callq 0x2130
jmp 0x30c6
jmp 0x30a5
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
jmp 0x30ed
movq %rax, %rbx
jmp 0x30f2
jmp 0x30c6
movq %rax, %rbx
jmp 0x30ea
jmp 0x30ac
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0x20c0
jmp 0x30ce
jmp 0x30cb
jmp 0x30cb
jmp 0x30c6
jmp 0x30cb
movq %rax, %rbx
jmp 0x30d8
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x20c0
leaq 0x50(%rsp), %r14
leaq 0x108(%rsp), %rdi
callq 0x20c0
movq %r14, %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x22e0