// File: /workspace/github_cpp_makefile_star2_O1/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 $0x288, %rsp            # imm = 0x288
leaq 0x128(%rsp), %rbx
movq %rbx, %rdi
callq 0x32a3
leaq 0x158(%rsp), %rdi
movq %rbx, %rsi
callq 0x5b3c
leaq 0x158(%rsp), %rdi
callq 0x36ee
leaq 0x158(%rsp), %rdi
callq 0x5ada
movq 0x6694(%rip), %rdi      # 0x9fd0
leaq 0x3af3(%rip), %rsi      # 0x7436
movl $0x2b, %edx
callq 0x2230
movq 0x668c(%rip), %rdi      # 0x9fe0
leaq 0x4(%rsp), %rsi
callq 0x2130
movq 0x666b(%rip), %rdi      # 0x9fd0
leaq 0x3af6(%rip), %rsi      # 0x7462
movl $0x19, %edx
callq 0x2230
movq 0x6663(%rip), %rdi      # 0x9fe0
movq %rsp, %rsi
callq 0x2130
movq 0x6644(%rip), %rdi      # 0x9fd0
callq 0x20e0
movq 0x6638(%rip), %rdi      # 0x9fd0
leaq 0x3add(%rip), %rsi      # 0x747c
movl $0x20, %edx
callq 0x2230
movq 0x6620(%rip), %rdi      # 0x9fd0
callq 0x20e0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2060
movl %eax, 0x67ec(%rip)      # 0xa1b8
cmpl $-0x1, %eax
jne 0x39f0
leaq 0x3ac5(%rip), %rdi      # 0x749d
leaq 0x3acb(%rip), %rsi      # 0x74aa
leaq 0x3acc(%rip), %rcx      # 0x74b2
movl $0x4f, %edx
callq 0x2140
xorps %xmm0, %xmm0
leaq 0x270(%rsp), %rbx
movaps %xmm0, (%rbx)
movw $0x2, (%rbx)
movzwl 0x4(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%rbx)
leaq 0x3aa6(%rip), %rdi      # 0x74bd
callq 0x21f0
movl %eax, 0x4(%rbx)
movl 0x6793(%rip), %edi      # 0xa1b8
movq %rbx, %rsi
movl $0x10, %edx
callq 0x21a0
cmpl $-0x1, %eax
jne 0x3a56
leaq 0x3a89(%rip), %rdi      # 0x74c7
leaq 0x3a65(%rip), %rsi      # 0x74aa
leaq 0x3a66(%rip), %rcx      # 0x74b2
movl $0x5a, %edx
callq 0x2140
movl (%rsp), %ebx
leaq 0x118(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x3a65(%rip), %rsi      # 0x74d1
leaq 0x3a64(%rip), %rdx      # 0x74d7
leaq 0x108(%rsp), %rdi
callq 0x5d68
leaq 0x1a0(%rsp), %rdi
movl %ebx, -0x10(%rdi)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xc(%rdi)
movw $0xffff, -0x8(%rdi)     # imm = 0xFFFF
movl %eax, -0x4(%rdi)
leaq 0x1b0(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x108(%rsp), %rsi
movq 0x110(%rsp), %rdx
addq %rsi, %rdx
callq 0x5cd8
movb $0x0, 0x1c0(%rsp)
movq $-0x1, 0x1c4(%rsp)
movq 0x108(%rsp), %rdi
cmpq %r14, %rdi
je 0x3af1
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x64d8(%rip), %rdi      # 0x9fd0
leaq 0x39d9(%rip), %rsi      # 0x74d8
movl $0x21, %edx
callq 0x2230
movq 0x64c0(%rip), %rdi      # 0x9fd0
callq 0x20e0
movl (%rsp), %edx
movl 0x4(%rsp), %ecx
leaq 0x170(%rsp), %rdi
leaq 0x190(%rsp), %r8
movl $0x2, %esi
callq 0x28db
movl 0x667c(%rip), %ebx      # 0xa1b8
movq 0x170(%rsp), %r14
movq %r14, %rdi
callq 0x20d0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x20f0
movq 0x646e(%rip), %rdi      # 0x9fd0
callq 0x20e0
xorps %xmm0, %xmm0
leaq 0x1d0(%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 0x38(%rsp), %r14
leaq 0x18(%rsp), %r13
leaq 0x78(%rsp), %rbp
leaq 0x28(%rsp), %r12
movl $0x18, %edx
movq 0x641f(%rip), %rdi      # 0x9fd0
leaq 0x3942(%rip), %rsi      # 0x74fa
callq 0x2230
leaq 0x1d0(%rsp), %r15
movl 0x65ed(%rip), %edi      # 0xa1b8
movl $0x7f, %edx
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x2090
leaq 0xf8(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq %r15, %rdi
callq 0x20d0
leaq (%rsp,%rax), %rdx
addq $0x1d0, %rdx            # imm = 0x1D0
leaq 0xe8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5d68
leaq 0x140(%rsp), %rdi
movq %rbx, %rsi
movl $0x26, %edx
callq 0x249c
movq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rax
cmpq %rax, %rdi
je 0x3c4a
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x140(%rsp), %rax
leaq 0xd8(%rsp), %rcx
movq %rcx, 0xc8(%rsp)
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
addq %rsi, %rdx
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x5cd8
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl $0x20, %edx
callq 0x249c
movq 0x8(%rsp), %rax
movq %r14, 0x28(%rsp)
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x5cd8
leaq 0x8(%rsp), %rdi
callq 0x4346
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x3cdb
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x2200
movl $0x12, %edx
movq 0x62e9(%rip), %rdi      # 0x9fd0
leaq 0x3825(%rip), %rsi      # 0x7513
callq 0x2230
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x62cc(%rip), %rdi      # 0x9fd0
callq 0x2230
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
jmp 0x3d3d
movq %r12, %rdi
callq 0x2250
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
cmpb $0xa, -0x1(%rsi,%rdx)
je 0x3d35
addq %rsi, %rdx
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x5cd8
leaq 0x258(%rsp), %rbx
movq %rbx, %rdi
leaq 0x128(%rsp), %rsi
callq 0x5b3c
leaq 0x88(%rsp), %rdi
movq %rbx, %rsi
callq 0x37d8
movl %eax, %r15d
movq %rbx, %rdi
callq 0x5ada
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x3dc6
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2200
movq %r13, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
cmpl $-0x1, %r15d
je 0x404c
movl $0x1d, %edx
movq 0x61e1(%rip), %rbx      # 0x9fd0
movq %rbx, %rdi
leaq 0x372d(%rip), %rsi      # 0x7526
callq 0x2230
movslq %r15d, %rax
movq 0x128(%rsp), %rcx
leaq (%rax,%rax,4), %r14
shlq $0x5, %r14
leaq (%rcx,%r14), %r15
addq $0x20, %r15
movq %r15, %rdi
callq 0x20d0
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
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 0x2150
movq 0x128(%rsp), %rdx
addq %r14, %rdx
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x36d1(%rip), %rsi      # 0x7544
callq 0x5bd8
leaq 0x58(%rsp), %r15
movq %rbx, %rdi
leaq 0x36bb(%rip), %rsi      # 0x7542
callq 0x2320
movq %r15, 0x48(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x3eaf
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x3eb6
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rdx, 0x50(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x128(%rsp), %rax
leaq (%rax,%r14), %rsi
addq $0x20, %rsi
leaq 0x48(%rsp), %rdi
callq 0x2320
movq %rbp, 0x68(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x3f0b
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
jmp 0x3f12
movups (%rcx), %xmm0
movups %xmm0, (%rbp)
movq 0x8(%rax), %rdx
movq %rdx, 0x70(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x3f41
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x3f66
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2200
movl (%rsp), %r15d
movq 0x140(%rsp), %rax
movq 0x20(%rax), %r14
callq 0x2040
movq %rax, %r13
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r14, %rdi
leaq 0xa8(%rsp), %rsi
movl $0xa, %edx
callq 0x2270
cmpq %r14, 0xa8(%rsp)
je 0x40cf
movslq %eax, %rcx
cmpq %rax, %rcx
leaq 0x48(%rsp), %rbx
jne 0x40db
movl (%r13), %ecx
cmpl $0x22, %ecx
je 0x40db
testl %ecx, %ecx
jne 0x3fcf
movl %ebp, (%r13)
movq 0x68(%rsp), %r8
movq %rbx, %rdi
movl $0x4, %esi
movl %r15d, %edx
movl %eax, %ecx
callq 0x28db
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x2260
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
leaq 0x38(%rsp), %r14
leaq 0x18(%rsp), %r13
leaq 0x78(%rsp), %rbp
je 0x401e
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2200
movl 0x6194(%rip), %edi      # 0xa1b8
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0x20f0
movq 0x68(%rsp), %rdi
cmpq %rbp, %rdi
je 0x404c
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2200
xorps %xmm0, %xmm0
movaps %xmm0, 0x240(%rsp)
movaps %xmm0, 0x230(%rsp)
movaps %xmm0, 0x220(%rsp)
movaps %xmm0, 0x210(%rsp)
movaps %xmm0, 0x200(%rsp)
movaps %xmm0, 0x1f0(%rsp)
movaps %xmm0, 0x1e0(%rsp)
movaps %xmm0, 0x1d0(%rsp)
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x40a6
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0x40bd
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2200
leaq 0x140(%rsp), %rdi
callq 0x4346
jmp 0x3ba5
leaq 0x3477(%rip), %rdi      # 0x754d
callq 0x2120
leaq 0x346b(%rip), %rdi      # 0x754d
callq 0x2280
jmp 0x40e9
movq %rax, %rbx
jmp 0x41fc
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x4110
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x4110
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x421f
movq 0xb8(%rsp), %rsi
jmp 0x4210
jmp 0x421c
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
callq 0x5ada
jmp 0x4150
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x4240
movq 0x98(%rsp), %rsi
jmp 0x4233
jmp 0x423d
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4346
jmp 0x418d
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x425c
movq 0xd8(%rsp), %rsi
jmp 0x4254
movq %rax, %rbx
jmp 0x425c
movq %rax, %rbx
movq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rax
cmpq %rax, %rdi
je 0x426e
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x426e
jmp 0x426b
movq %rax, %rbx
cmpl $0x0, (%r13)
jne 0x41fc
movl %ebp, (%r13)
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x421f
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x421f
jmp 0x426b
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x4240
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x4240
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x425c
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2200
leaq 0x140(%rsp), %rdi
callq 0x4346
jmp 0x426e
movq %rax, %rbx
leaq 0x180(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4296
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x4296
jmp 0x4293
movq %rax, %rbx
movq 0x1a0(%rsp), %rdi
leaq 0x1b0(%rsp), %rax
cmpq %rax, %rdi
je 0x42f2
movq 0x1b0(%rsp), %rsi
jmp 0x42cd
movq %rax, %rbx
movq 0x108(%rsp), %rdi
cmpq %r14, %rdi
je 0x42f2
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x42f2
jmp 0x42ef
jmp 0x42ef
jmp 0x42ef
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x5ada
jmp 0x42f2
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x5ada
movq %rbx, %rdi
callq 0x22e0
nop