// File: 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
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movl $0x0, -0x4(%rbp)
movq 0x7d3f(%rip), %rdi      # 0xbfc8
leaq 0x3da6(%rip), %rsi      # 0x8036
callq 0x3270
movq 0x7d44(%rip), %rdi      # 0xbfe0
leaq -0x8(%rbp), %rsi
callq 0x3180
movq 0x7d1c(%rip), %rdi      # 0xbfc8
leaq 0x3daf(%rip), %rsi      # 0x8062
callq 0x3270
movq 0x7d21(%rip), %rdi      # 0xbfe0
leaq -0xc(%rbp), %rsi
callq 0x3180
movq 0x7cf9(%rip), %rdi      # 0xbfc8
movq 0x7cda(%rip), %rsi      # 0xbfb0
callq 0x32b0
movq 0x7ce6(%rip), %rdi      # 0xbfc8
leaq 0x3d93(%rip), %rsi      # 0x807c
callq 0x3270
movq %rax, %rdi
movq 0x7cb8(%rip), %rsi      # 0xbfb0
callq 0x32b0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3090
movl %eax, 0x7f60(%rip)      # 0xc274
cmpl $-0x1, 0x7f59(%rip)     # 0xc274
je 0x431f
jmp 0x433e
leaq 0x3d77(%rip), %rdi      # 0x809d
leaq 0x3d7d(%rip), %rsi      # 0x80aa
movl $0x38, %edx
leaq 0x3d7c(%rip), %rcx      # 0x80b5
callq 0x31b0
leaq -0x1c(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3150
movw $0x2, -0x1c(%rbp)
movl -0x8(%rbp), %eax
movzwl %ax, %edi
callq 0x3070
movw %ax, -0x1a(%rbp)
leaq 0x3d56(%rip), %rdi      # 0x80c0
callq 0x3290
movl %eax, -0x18(%rbp)
movl 0x7efc(%rip), %edi      # 0xc274
leaq -0x1c(%rbp), %rsi
movl $0x10, %edx
callq 0x3230
movl %eax, -0x20(%rbp)
cmpl $-0x1, -0x20(%rbp)
je 0x4391
jmp 0x43b0
leaq 0x3d32(%rip), %rdi      # 0x80ca
leaq 0x3d0b(%rip), %rsi      # 0x80aa
movl $0x43, %edx
leaq 0x3d0a(%rip), %rcx      # 0x80b5
callq 0x31b0
movq 0x7c11(%rip), %rdi      # 0xbfc8
leaq 0x3d16(%rip), %rsi      # 0x80d4
callq 0x3270
movq %rax, %rdi
movq 0x7be3(%rip), %rsi      # 0xbfb0
callq 0x32b0
leaq 0x7e9f(%rip), %rdi      # 0xc278
leaq -0x210(%rip), %rdx      # 0x41d0
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
callq 0x3430
movl -0xc(%rbp), %eax
movl %eax, -0x1ec(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x3410
movq -0x1e8(%rbp), %rdx
leaq 0x3cdb(%rip), %rsi      # 0x80f1
leaq -0x80(%rbp), %rdi
callq 0x5670
jmp 0x4421
movl -0x1ec(%rbp), %esi
movq %rsp, %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movl $0x0, (%rax)
leaq -0x60(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq -0x80(%rbp), %rcx
xorl %r9d, %r9d
movl %r9d, %r8d
callq 0x5720
jmp 0x4458
leaq -0x80(%rbp), %rdi
callq 0x3190
leaq -0x81(%rbp), %rdi
callq 0x32e0
movq 0x7b54(%rip), %rdi      # 0xbfc8
leaq 0x3c7d(%rip), %rsi      # 0x80f8
callq 0x3270
movq %rax, -0x1f8(%rbp)
jmp 0x4489
movq -0x1f8(%rbp), %rdi
movq 0x7b19(%rip), %rsi      # 0xbfb0
callq 0x32b0
jmp 0x449e
movl -0xc(%rbp), %edx
movl -0x8(%rbp), %ecx
leaq -0xb8(%rbp), %rdi
movl $0x2, %esi
leaq -0x60(%rbp), %r8
callq 0x3b20
jmp 0x44bb
movl 0x7db3(%rip), %eax      # 0xc274
movl %eax, -0x204(%rbp)
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x30e0
movq -0x210(%rbp), %rdi
movq %rax, -0x200(%rbp)
callq 0x30e0
movq %rax, %rdi
callq 0x3100
movl -0x204(%rbp), %edi
movq -0x200(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x3130
jmp 0x450e
movq 0x7ab3(%rip), %rdi      # 0xbfc8
movq 0x7a94(%rip), %rsi      # 0xbfb0
callq 0x32b0
jmp 0x4523
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x3150
movl $0xffffffff, -0x144(%rbp) # imm = 0xFFFFFFFF
leaq -0x140(%rbp), %rax
leaq 0x3ae2(%rip), %rcx      # 0x8030
cmpq %rcx, %rax
je 0x4984
movl $0x2, %edi
callq 0x3200
jmp 0x4563
movq 0x7a5e(%rip), %rdi      # 0xbfc8
leaq 0x3ba9(%rip), %rsi      # 0x811a
callq 0x3270
jmp 0x4578
movl $0x0, -0x148(%rbp)
movq 0x7a57(%rip), %rdi      # 0xbfe0
leaq -0x148(%rbp), %rsi
callq 0x3180
jmp 0x4597
leaq -0x168(%rbp), %rdi
callq 0x3360
cmpl $0x1, -0x148(%rbp)
jne 0x4728
movq 0x7a11(%rip), %rdi      # 0xbfc8
leaq 0x3b9f(%rip), %rsi      # 0x815d
callq 0x3270
jmp 0x45c5
movq 0x7a14(%rip), %rdi      # 0xbfe0
leaq -0x144(%rbp), %rsi
callq 0x3180
jmp 0x45da
xorps %xmm0, %xmm0
movaps %xmm0, -0x220(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0x100(%rbp)
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x120(%rbp)
movaps %xmm0, -0x130(%rbp)
movaps %xmm0, -0x140(%rbp)
movq 0x79a5(%rip), %rdi      # 0xbfc8
leaq 0x3b6e(%rip), %rsi      # 0x8198
callq 0x3270
jmp 0x4631
movq 0x79a8(%rip), %rdi      # 0xbfe0
callq 0x3440
jmp 0x463f
movq 0x797a(%rip), %rax      # 0xbfc0
movq (%rax), %rdx
leaq -0x140(%rbp), %rdi
movl $0x80, %esi
callq 0x33b0
jmp 0x465c
movl -0xc(%rbp), %edx
movl -0x144(%rbp), %ecx
leaq -0x188(%rbp), %rdi
movl $0x4, %esi
leaq -0x140(%rbp), %r8
callq 0x3b20
jmp 0x467f
leaq -0x168(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x3320
leaq -0x188(%rbp), %rdi
callq 0x3190
jmp 0x48c6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x46d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x3190
leaq -0x81(%rbp), %rdi
callq 0x32e0
jmp 0x49c7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x49be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x49b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x4976
cmpl $0x2, -0x148(%rbp)
jne 0x48c4
movl $0xffffffff, -0x18c(%rbp) # imm = 0xFFFFFFFF
movq 0x7882(%rip), %rdi      # 0xbfc8
leaq 0x3a62(%rip), %rsi      # 0x81af
callq 0x3270
jmp 0x4754
movq 0x7885(%rip), %rdi      # 0xbfe0
leaq -0x18c(%rbp), %rsi
callq 0x3180
jmp 0x4769
movq 0x7858(%rip), %rdi      # 0xbfc8
movq 0x7839(%rip), %rsi      # 0xbfb0
callq 0x32b0
jmp 0x477e
movq 0x7843(%rip), %rdi      # 0xbfc8
leaq 0x3a34(%rip), %rsi      # 0x81c0
callq 0x3270
jmp 0x4793
movq 0x7846(%rip), %rdi      # 0xbfe0
callq 0x3440
jmp 0x47a1
movq 0x7818(%rip), %rax      # 0xbfc0
movq (%rax), %rdx
leaq -0x140(%rbp), %rdi
movl $0x80, %esi
callq 0x33b0
jmp 0x47be
leaq -0x1b1(%rbp), %rdi
movq %rdi, -0x228(%rbp)
callq 0x3410
movq -0x228(%rbp), %rdx
leaq 0x39f9(%rip), %rsi      # 0x81d8
leaq -0x1b0(%rbp), %rdi
callq 0x5670
jmp 0x47ed
leaq -0x1b1(%rbp), %rdi
callq 0x32e0
leaq -0x1b0(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x3260
jmp 0x480e
movl -0xc(%rbp), %eax
movl %eax, -0x230(%rbp)
movl -0x18c(%rbp), %eax
movl %eax, -0x22c(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x30e0
movl -0x230(%rbp), %edx
movl -0x22c(%rbp), %ecx
movq %rax, %r8
leaq -0x1d8(%rbp), %rdi
movl $0x4, %esi
callq 0x3b20
jmp 0x4851
leaq -0x168(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x3320
leaq -0x1d8(%rbp), %rdi
callq 0x3190
leaq -0x1b0(%rbp), %rdi
callq 0x3190
jmp 0x48c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0x1b1(%rbp), %rdi
callq 0x32e0
jmp 0x4976
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x3190
jmp 0x4976
jmp 0x48c6
movl 0x79a8(%rip), %eax      # 0xc274
movl %eax, -0x23c(%rbp)
leaq -0x168(%rbp), %rdi
movq %rdi, -0x248(%rbp)
callq 0x30e0
movq -0x248(%rbp), %rdi
movq %rax, -0x238(%rbp)
callq 0x31d0
movl -0x23c(%rbp), %edi
movq -0x238(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x3130
jmp 0x4911
leaq -0x140(%rbp), %rdi
leaq 0x3711(%rip), %rsi      # 0x8030
movl $0x3, %edx
callq 0x3120
cmpl $0x0, %eax
jne 0x493a
movl $0x3, -0x1dc(%rbp)
jmp 0x4957
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x3150
movl $0x0, -0x1dc(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x3190
movl -0x1dc(%rbp), %eax
testl %eax, %eax
je 0x4971
jmp 0x496f
jmp 0x4984
jmp 0x4540
leaq -0x168(%rbp), %rdi
callq 0x3190
jmp 0x49b2
movl 0x78ea(%rip), %edi      # 0xc274
callq 0x3470
jmp 0x4991
leaq -0xb8(%rbp), %rdi
callq 0x3190
leaq -0x60(%rbp), %rdi
callq 0x54d0
movl -0x4(%rbp), %eax
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
leaq -0xb8(%rbp), %rdi
callq 0x3190
leaq -0x60(%rbp), %rdi
callq 0x54d0
movq -0x90(%rbp), %rdi
callq 0x3400
nopw %cs:(%rax,%rax)
nopl (%rax)