// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x2ac4
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x3c5a
leaq 0x58(%rsp), %rdi
callq 0x2db0
leaq 0x58(%rsp), %rdi
callq 0x3c38
movq 0x5092(%rip), %rdi      # 0x7fc8
leaq 0x24f9(%rip), %rsi      # 0x5436
callq 0x21c0
movq 0x5097(%rip), %rdi      # 0x7fe0
leaq 0x4(%rsp), %rsi
callq 0x2120
movq 0x506e(%rip), %rdi      # 0x7fc8
leaq 0x2501(%rip), %rsi      # 0x5462
callq 0x21c0
movq 0x5073(%rip), %rdi      # 0x7fe0
movq %rsp, %rsi
callq 0x2120
movq 0x504c(%rip), %rdi      # 0x7fc8
callq 0x20d0
movq 0x5040(%rip), %rdi      # 0x7fc8
leaq 0x24ed(%rip), %rsi      # 0x547c
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x2050
movl %eax, 0x5201(%rip)      # 0x81b0
cmpl $-0x1, %eax
jne 0x2fd1
leaq 0x24e2(%rip), %rdi      # 0x549d
leaq 0x24e8(%rip), %rsi      # 0x54aa
leaq 0x24e9(%rip), %rcx      # 0x54b2
pushq $0x4f
popq %rdx
callq 0x2130
leaq 0x268(%rsp), %rbx
andl $0x0, 0xc(%rbx)
andq $0x0, 0x4(%rbx)
movw $0x2, (%rbx)
movzwl 0x4(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%rbx)
leaq 0x24c2(%rip), %rdi      # 0x54bd
callq 0x21e0
movl %eax, 0x4(%rbx)
movl 0x51a7(%rip), %edi      # 0x81b0
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x2180
cmpl $-0x1, %eax
jne 0x3032
leaq 0x24a7(%rip), %rdi      # 0x54c7
leaq 0x2483(%rip), %rsi      # 0x54aa
leaq 0x2484(%rip), %rcx      # 0x54b2
pushq $0x5a
jmp 0x2fcb
movl (%rsp), %ebx
leaq 0x2495(%rip), %rsi      # 0x54d1
leaq 0xb0(%rsp), %rdi
leaq 0x150(%rsp), %rdx
callq 0x3bcc
leaq 0x1f8(%rsp), %rdi
movl %ebx, -0x10(%rdi)
orl $-0x1, -0xc(%rdi)
orw $-0x1, -0x8(%rdi)
orl $-0x1, -0x4(%rdi)
leaq 0xb0(%rsp), %rsi
callq 0x2250
movb $0x0, 0x218(%rsp)
orq $-0x1, 0x21c(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x20c0
movq 0x4f2d(%rip), %rdi      # 0x7fc8
leaq 0x2436(%rip), %rsi      # 0x54d8
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
movl (%rsp), %edx
movl 0x4(%rsp), %ecx
leaq 0x130(%rsp), %rdi
pushq $0x2
popq %rsi
leaq 0x1e8(%rsp), %r8
callq 0x2740
movl 0x50dc(%rip), %ebx      # 0x81b0
movq 0x130(%rsp), %r14
movq %r14, %rdi
callq 0x20a0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x20e0
movq 0x4ece(%rip), %rdi      # 0x7fc8
callq 0x20d0
xorps %xmm0, %xmm0
leaq 0x150(%rsp), %rax
movaps %xmm0, 0x70(%rax)
movaps %xmm0, 0x60(%rax)
movaps %xmm0, 0x50(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
leaq 0x90(%rsp), %rbx
leaq 0x8(%rsp), %r14
leaq 0x1d0(%rsp), %r15
leaq 0x228(%rsp), %rbp
movq 0x4e7b(%rip), %rdi      # 0x7fc8
leaq 0x23a6(%rip), %rsi      # 0x54fa
callq 0x21c0
leaq 0x150(%rsp), %r13
movl 0x5049(%rip), %edi      # 0x81b0
movq %r13, %rsi
pushq $0x7f
popq %rdx
xorl %ecx, %ecx
callq 0x2070
leaq 0x248(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x3bcc
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
pushq $0x26
popq %rdx
callq 0x244c
movq %r12, %rdi
callq 0x20c0
movq 0x40(%rsp), %rsi
addq $0x60, %rsi
leaq 0x110(%rsp), %r12
movq %r12, %rdi
callq 0x2250
movq %r14, %rdi
movq %r12, %rsi
pushq $0x20
popq %rdx
callq 0x244c
movq 0x8(%rsp), %rsi
addq $0x40, %rsi
movq %rbx, %rdi
callq 0x2250
movq %r14, %rdi
callq 0x3532
leaq 0x110(%rsp), %rdi
callq 0x20c0
movq 0x4dd2(%rip), %rdi      # 0x7fc8
leaq 0x2316(%rip), %rsi      # 0x5513
callq 0x21c0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21b0
movq %rax, %rdi
callq 0x20d0
movq 0x90(%rsp), %rax
movq 0x98(%rsp), %rcx
cmpb $0xa, -0x1(%rax,%rcx)
jne 0x3236
movq %rbx, %rdi
callq 0x2220
jmp 0x3215
leaq 0xf0(%rsp), %rdi
movq %rbx, %rsi
callq 0x2250
movq %r15, %rdi
leaq 0x28(%rsp), %rsi
callq 0x3c5a
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x2e46
movl %eax, %r13d
movq %r15, %rdi
callq 0x3c38
movq %r12, %rdi
callq 0x20c0
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
andq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
cmpl $-0x1, %r13d
je 0x3392
movq 0x4d29(%rip), %rdi      # 0x7fc8
leaq 0x2280(%rip), %rsi      # 0x5526
callq 0x21c0
movslq %r13d, %rcx
movq 0x28(%rsp), %rdx
imulq $0xa0, %rcx, %r13
leaq (%rdx,%r13), %rsi
addq $0x20, %rsi
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
movq 0x28(%rsp), %rdx
addq %r13, %rdx
leaq 0xd0(%rsp), %r12
movq %r12, %rdi
leaq 0x2258(%rip), %rsi      # 0x5544
callq 0x3cd5
movq %rbp, %rdi
movq %r12, %rsi
leaq 0x2244(%rip), %rdx      # 0x5542
callq 0x3cb6
movq 0x28(%rsp), %rax
leaq (%rax,%r13), %rdx
addq $0x20, %rdx
leaq 0x70(%rsp), %rdi
movq %rbp, %rsi
callq 0x3cb6
movq %rbp, %rdi
callq 0x20c0
leaq 0xd0(%rsp), %rdi
callq 0x20c0
movl (%rsp), %r13d
movq 0x40(%rsp), %rdi
addq $0x20, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x3d4b
movq 0x70(%rsp), %r8
movq %rbp, %rdi
pushq $0x4
popq %rsi
movl %r13d, %edx
movl %eax, %ecx
callq 0x2740
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2230
movq %rbp, %rdi
callq 0x20c0
movl 0x4e39(%rip), %edi      # 0x81b0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0x20e0
leaq 0x70(%rsp), %rdi
callq 0x20c0
xorps %xmm0, %xmm0
movaps %xmm0, 0x1c0(%rsp)
movaps %xmm0, 0x1b0(%rsp)
movaps %xmm0, 0x1a0(%rsp)
movaps %xmm0, 0x190(%rsp)
movaps %xmm0, 0x180(%rsp)
movaps %xmm0, 0x170(%rsp)
movaps %xmm0, 0x160(%rsp)
movaps %xmm0, 0x150(%rsp)
movq %r14, %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x20c0
leaq 0x40(%rsp), %rdi
callq 0x3532
jmp 0x3146
jmp 0x346d
movq %rax, %rbx
leaq 0x228(%rsp), %rdi
callq 0x20c0
jmp 0x340b
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
jmp 0x3475
jmp 0x347c
movq %rax, %rbx
leaq 0x1d0(%rsp), %rdi
callq 0x3c38
jmp 0x342c
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
jmp 0x3484
jmp 0x348d
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x3532
jmp 0x344a
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
jmp 0x3498
movq %rax, %rbx
jmp 0x349d
movq %rax, %rbx
leaq 0x248(%rsp), %rdi
callq 0x20c0
jmp 0x34ac
jmp 0x34a9
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x20c0
jmp 0x347f
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x20c0
jmp 0x3490
jmp 0x34a9
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x20c0
leaq 0x40(%rsp), %rdi
callq 0x3532
jmp 0x34ac
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x20c0
jmp 0x34c0
jmp 0x34bd
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
jmp 0x34d5
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x20c0
jmp 0x34f4
jmp 0x34f1
jmp 0x34f1
jmp 0x34f1
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x3c38
jmp 0x34f4
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x3c38
movq %rbx, %rdi
callq 0x22d0