// File: SaadBazaz[P]RIP-Protocol-Simulation/DNS.cpp
// Function: main
int main()
{
    vector<Directory> List = initiateList();
    PrintDirectory(List);
    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);

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

	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")
	{

        cout << "Waiting For Request ...\n";
        recv(sockfd,buff,127,0);
 
            // Splitting the Recieved Message into Substrings
            vector<string> data = tokenizeData(buff,'&');

            /*string domainName;
            for (size_t i = 4; data[3].size(); i++)
            {
                domainName += data[3][i];
            }*/
            string domainName = tokenizeData(data[3],' ')[2];
            cout << "Domain Requested: " <<domainName << endl;
            while (domainName.back() == '\n')
            {
                domainName.pop_back();
            }
            
            int pos = SearchDomain(domainName,List);
            string message;
            if (pos != -1)
            {
                cout << "IP Address to be Delivered : "<< List[pos].IPAddress << endl;
                string Response = "DNS RES " + List[pos].DomainName + " " + List[pos].IPAddress;
                message = constructNewMessage(MESSAGE, myip , stoi(data[1]),(void*)Response.c_str());
                send(sockfd,message.c_str(),message.size(),0);
            }
            else
            {
                /*cout << "Domain Not Found !"<<endl;
                char* notFound = "NOT_FOUND";
                message = constructNewMessage(MESSAGE, myip , stoi(data[1]),(void*)notFound);
                send(sockfd,message.c_str(),message.size(),0);*/
            }
            memset(buff, 0, 128);
        
	}
	close(sockfd);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3b0, %rsp            # imm = 0x3B0
movl $0x0, -0x4(%rbp)
leaq -0x20(%rbp), %rdi
movq %rdi, -0x2f8(%rbp)
callq 0x4200
movq -0x2f8(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x6370
jmp 0x4bb4
leaq -0x38(%rbp), %rdi
callq 0x49d0
jmp 0x4bbf
leaq -0x38(%rbp), %rdi
callq 0x62c0
movq 0xa3f9(%rip), %rdi      # 0xefc8
leaq 0x5860(%rip), %rsi      # 0xa436
callq 0x32a0
jmp 0x4bdd
movq 0xa3fc(%rip), %rdi      # 0xefe0
leaq -0x48(%rbp), %rsi
callq 0x3190
jmp 0x4bef
movq 0xa3d2(%rip), %rdi      # 0xefc8
leaq 0x5865(%rip), %rsi      # 0xa462
callq 0x32a0
jmp 0x4c04
movq 0xa3d5(%rip), %rdi      # 0xefe0
leaq -0x4c(%rbp), %rsi
callq 0x3190
jmp 0x4c16
movq 0xa3ab(%rip), %rdi      # 0xefc8
movq 0xa394(%rip), %rsi      # 0xefb8
callq 0x32e0
jmp 0x4c2b
movq 0xa396(%rip), %rdi      # 0xefc8
leaq 0x5843(%rip), %rsi      # 0xa47c
callq 0x32a0
movq %rax, -0x300(%rbp)
jmp 0x4c47
movq -0x300(%rbp), %rdi
movq 0xa363(%rip), %rsi      # 0xefb8
callq 0x32e0
jmp 0x4c5c
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3090
movl %eax, 0xa615(%rip)      # 0xf288
cmpl $-0x1, 0xa60e(%rip)     # 0xf288
je 0x4c7e
jmp 0x4cc8
leaq 0x5818(%rip), %rdi      # 0xa49d
leaq 0x581e(%rip), %rsi      # 0xa4aa
movl $0x4f, %edx
leaq 0x581a(%rip), %rcx      # 0xa4b2
callq 0x31c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x54b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x62c0
jmp 0x54b0
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movw $0x2, -0x60(%rbp)
movzwl -0x48(%rbp), %edi
callq 0x3060
movw %ax, -0x5e(%rbp)
leaq 0x57d4(%rip), %rdi      # 0xa4bd
callq 0x32c0
movl %eax, -0x5c(%rbp)
movl 0xa591(%rip), %edi      # 0xf288
leaq -0x60(%rbp), %rsi
movl $0x10, %edx
callq 0x3250
movl %eax, -0x304(%rbp)
jmp 0x4d0d
movl -0x304(%rbp), %eax
movl %eax, -0x64(%rbp)
cmpl $-0x1, -0x64(%rbp)
je 0x4d1e
jmp 0x4d3d
leaq 0x57a2(%rip), %rdi      # 0xa4c7
leaq 0x577e(%rip), %rsi      # 0xa4aa
movl $0x5a, %edx
leaq 0x577a(%rip), %rcx      # 0xa4b2
callq 0x31c0
movl -0x4c(%rbp), %eax
movl %eax, -0x314(%rbp)
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x310(%rbp)
callq 0x3450
movq -0x310(%rbp), %rdx
leaq 0x576a(%rip), %rsi      # 0xa4d1
leaq -0xc8(%rbp), %rdi
callq 0x6160
jmp 0x4d75
movl -0x314(%rbp), %esi
movq %rsp, %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movl $0x1, (%rax)
leaq -0xa8(%rbp), %rdi
leaq -0xc8(%rbp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %r9d, %edx
movl %r9d, %r8d
callq 0x6460
jmp 0x4db3
leaq -0xc8(%rbp), %rdi
callq 0x31a0
leaq -0xc9(%rbp), %rdi
callq 0x3310
movq 0xa1f6(%rip), %rdi      # 0xefc8
leaq 0x56ff(%rip), %rsi      # 0xa4d8
callq 0x32a0
movq %rax, -0x320(%rbp)
jmp 0x4de7
movq -0x320(%rbp), %rdi
movq 0xa1c3(%rip), %rsi      # 0xefb8
callq 0x32e0
jmp 0x4dfc
movl -0x4c(%rbp), %edx
movl -0x48(%rbp), %ecx
leaq -0xf0(%rbp), %rdi
movl $0x2, %esi
leaq -0xa8(%rbp), %r8
callq 0x3b50
jmp 0x4e1c
movl 0xa466(%rip), %eax      # 0xf288
movl %eax, -0x32c(%rbp)
leaq -0xf0(%rbp), %rdi
movq %rdi, -0x338(%rbp)
callq 0x30f0
movq -0x338(%rbp), %rdi
movq %rax, -0x328(%rbp)
callq 0x30f0
movq %rax, %rdi
callq 0x3110
movl -0x32c(%rbp), %edi
movq -0x328(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x3130
jmp 0x4e6f
movq 0xa152(%rip), %rdi      # 0xefc8
movq 0xa13b(%rip), %rsi      # 0xefb8
callq 0x32e0
jmp 0x4e84
leaq -0x170(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x3150
movl $0xffffffff, -0x174(%rbp) # imm = 0xFFFFFFFF
leaq -0x170(%rbp), %rax
leaq 0x564b(%rip), %rcx      # 0xa4fa
cmpq %rcx, %rax
je 0x545e
movq 0xa109(%rip), %rdi      # 0xefc8
leaq 0x563a(%rip), %rsi      # 0xa500
callq 0x32a0
jmp 0x4ecd
movl 0xa3b5(%rip), %edi      # 0xf288
leaq -0x170(%rbp), %rsi
movl $0x7f, %edx
xorl %ecx, %ecx
callq 0x30b0
jmp 0x4ee8
leaq -0x1b1(%rbp), %rdi
movq %rdi, -0x340(%rbp)
callq 0x3450
movq -0x340(%rbp), %rdx
leaq -0x1b0(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x6160
jmp 0x4f17
leaq -0x190(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
movl $0x26, %edx
callq 0x3610
jmp 0x4f31
leaq -0x1b0(%rbp), %rdi
callq 0x31a0
leaq -0x1b1(%rbp), %rdi
callq 0x3310
leaq -0x190(%rbp), %rdi
movl $0x3, %esi
callq 0x5ce0
movq %rax, %rsi
leaq -0x210(%rbp), %rdi
callq 0x3100
jmp 0x4f6b
leaq -0x1f0(%rbp), %rdi
leaq -0x210(%rbp), %rsi
movl $0x20, %edx
callq 0x3610
jmp 0x4f85
leaq -0x1f0(%rbp), %rdi
movl $0x2, %esi
callq 0x5ce0
movq %rax, %rsi
leaq -0x1d8(%rbp), %rdi
callq 0x3100
jmp 0x4fa7
leaq -0x1f0(%rbp), %rdi
callq 0x5580
leaq -0x210(%rbp), %rdi
callq 0x31a0
movq 0xa002(%rip), %rdi      # 0xefc8
leaq 0x554c(%rip), %rsi      # 0xa519
callq 0x32a0
movq %rax, -0x348(%rbp)
jmp 0x4fdb
movq -0x348(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x3280
movq %rax, -0x350(%rbp)
jmp 0x4ff7
movq -0x350(%rbp), %rdi
movq 0x9fb3(%rip), %rsi      # 0xefb8
callq 0x32e0
jmp 0x500c
jmp 0x500e
leaq -0x1d8(%rbp), %rdi
callq 0x3200
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0x511d
leaq -0x1d8(%rbp), %rdi
callq 0x3350
jmp 0x500e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x505a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x31a0
leaq -0xc9(%rbp), %rdi
callq 0x3310
jmp 0x54b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x54a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5498
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x50b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x31a0
leaq -0x1b1(%rbp), %rdi
callq 0x3310
jmp 0x5498
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5450
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x50fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0x5580
leaq -0x210(%rbp), %rdi
callq 0x31a0
jmp 0x5450
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5444
leaq -0x238(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x3100
jmp 0x5132
leaq -0x250(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x6370
jmp 0x5144
leaq -0x238(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x4a90
movl %eax, -0x354(%rbp)
jmp 0x515f
leaq -0x250(%rbp), %rdi
callq 0x62c0
leaq -0x238(%rbp), %rdi
callq 0x31a0
movl -0x354(%rbp), %eax
movl %eax, -0x214(%rbp)
leaq -0x270(%rbp), %rdi
callq 0x33a0
cmpl $-0x1, -0x214(%rbp)
je 0x53fa
movq 0x9e25(%rip), %rdi      # 0xefc8
leaq 0x5382(%rip), %rsi      # 0xa52c
callq 0x32a0
movq %rax, -0x360(%rbp)
jmp 0x51b8
movslq -0x214(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0x6350
movq -0x360(%rbp), %rdi
movq %rax, %rsi
addq $0x20, %rsi
callq 0x32a0
movq %rax, -0x368(%rbp)
jmp 0x51e4
movq -0x368(%rbp), %rdi
movq 0x9dc6(%rip), %rsi      # 0xefb8
callq 0x32e0
jmp 0x51f9
movslq -0x214(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0x6350
movq %rax, %rdx
leaq 0x5337(%rip), %rsi      # 0xa54a
leaq -0x2d0(%rbp), %rdi
callq 0x6540
jmp 0x5221
leaq 0x5320(%rip), %rdx      # 0xa548
leaq -0x2b0(%rbp), %rdi
leaq -0x2d0(%rbp), %rsi
callq 0x64f0
jmp 0x523d
movslq -0x214(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0x6350
movq %rax, %rdx
addq $0x20, %rdx
leaq -0x290(%rbp), %rdi
leaq -0x2b0(%rbp), %rsi
callq 0x64f0
jmp 0x5269
leaq -0x2b0(%rbp), %rdi
callq 0x31a0
leaq -0x2d0(%rbp), %rdi
callq 0x31a0
movl -0x4c(%rbp), %eax
movl %eax, -0x370(%rbp)
leaq -0x190(%rbp), %rdi
movl $0x1, %esi
callq 0x5ce0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x6650
movl %eax, -0x36c(%rbp)
jmp 0x52b4
leaq -0x290(%rbp), %rdi
callq 0x30f0
movl -0x370(%rbp), %edx
movl -0x36c(%rbp), %ecx
movq %rax, %r8
leaq -0x2f0(%rbp), %rdi
movl $0x4, %esi
callq 0x3b50
jmp 0x52e2
leaq -0x270(%rbp), %rdi
movq %rdi, -0x388(%rbp)
leaq -0x2f0(%rbp), %rsi
movq %rsi, -0x390(%rbp)
callq 0x3360
movq -0x390(%rbp), %rdi
callq 0x31a0
movq -0x388(%rbp), %rdi
movl 0x9f6c(%rip), %eax      # 0xf288
movl %eax, -0x37c(%rbp)
callq 0x30f0
movq -0x388(%rbp), %rdi
movq %rax, -0x378(%rbp)
callq 0x31e0
movl -0x37c(%rbp), %edi
movq -0x378(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x3130
jmp 0x5353
leaq -0x290(%rbp), %rdi
callq 0x31a0
jmp 0x53fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x538a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x62c0
leaq -0x238(%rbp), %rdi
callq 0x31a0
jmp 0x5444
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5438
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x53d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x2b0(%rbp), %rdi
callq 0x31a0
leaq -0x2d0(%rbp), %rdi
callq 0x31a0
jmp 0x5438
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x290(%rbp), %rdi
callq 0x31a0
jmp 0x5438
jmp 0x53fc
leaq -0x170(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x3150
leaq -0x270(%rbp), %rdi
callq 0x31a0
leaq -0x1d8(%rbp), %rdi
callq 0x31a0
leaq -0x190(%rbp), %rdi
callq 0x5580
jmp 0x4ea1
leaq -0x270(%rbp), %rdi
callq 0x31a0
leaq -0x1d8(%rbp), %rdi
callq 0x31a0
leaq -0x190(%rbp), %rdi
callq 0x5580
jmp 0x5498
movl 0x9e24(%rip), %edi      # 0xf288
callq 0x34a0
jmp 0x546b
leaq -0xf0(%rbp), %rdi
callq 0x31a0
leaq -0xa8(%rbp), %rdi
callq 0x5fc0
leaq -0x20(%rbp), %rdi
callq 0x62c0
movl -0x4(%rbp), %eax
addq $0x3b0, %rsp            # imm = 0x3B0
popq %rbp
retq
leaq -0xf0(%rbp), %rdi
callq 0x31a0
leaq -0xa8(%rbp), %rdi
callq 0x5fc0
leaq -0x20(%rbp), %rdi
callq 0x62c0
movq -0x40(%rbp), %rdi
callq 0x3440
nopw %cs:(%rax,%rax)
nopl (%rax)