// File: yahoo[P]UDPing/src/ifinfo.cc
// Function: getIfInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int*, unsigned char*)
int getIfInfo (string ip, int* ifIndex, uint8_t *srcMac) {
    char interface [256];
    int sd;
    int foundInterface = 0;
    struct ifreq ifr;
    struct ifaddrs *ifa;
    struct in_addr srcAddr;

    cout << "Getting interface info for " << ip << endl;

    if (inet_pton (AF_INET, ip.c_str(), &srcAddr) != 1) {
         perror ("inet_pton failed");
         return (-1);
    }

    getifaddrs(&ifa);
    struct ifaddrs *curAddr = ifa;
    while (curAddr) {
        struct sockaddr_in* sin = (struct sockaddr_in *) curAddr->ifa_addr;
        if (sin->sin_addr.s_addr == srcAddr.s_addr) {
            printf ("The interface is named %s\n", curAddr->ifa_name);
            strcpy (interface, curAddr->ifa_name);
            foundInterface = 1;
            break;
        }
        curAddr = curAddr->ifa_next;
    }
    freeifaddrs(ifa);

    if (!foundInterface) {
        cerr << "Couldn't find interface for IP " << ip << endl;
        return (-1);
    }

    if ((sd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) {
        perror ("socket() failed to get socket descriptor for using ioctl() ");
        return (-1);
    }

    // Use ioctl() to look up interface name and get its MAC address.
    memset (&ifr, 0, sizeof (ifr));
    snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), "%s", interface);
    if (ioctl (sd, SIOCGIFHWADDR, &ifr) < 0) {
        perror ("ioctl() failed to get source MAC address ");
        return (-1);
    }
    close (sd);

    // Copy source MAC address.
    memcpy (srcMac, ifr.ifr_hwaddr.sa_data, 6 * sizeof (uint8_t));
    
    // Report source MAC address to stdout.
    printf ("MAC address is ");
    for (int i=0; i<5; i++) {
        printf ("%02x:", srcMac[i]);
    }
    printf ("%02x\n", srcMac[5]);
    
    // Find interface index from interface name and store index in
    // struct sockaddr_ll device, which will be used as an argument of sendto().
    if ((*ifIndex = if_nametoindex (interface)) == 0) {
        perror ("if_nametoindex() failed to obtain interface index ");
        return (-1);
    }
    printf ("Index is %i\n", *ifIndex);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x180(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x0, -0x128(%rbp)
movq 0x38b1(%rip), %rdi      # 0x9fd0
leaq 0x1122(%rip), %rsi      # 0x7848
callq 0x22f0
movq -0x180(%rbp), %rsi
movq %rax, %rdi
callq 0x2260
movq %rax, %rdi
movq 0x3874(%rip), %rsi      # 0x9fb8
callq 0x2310
movq -0x180(%rbp), %rdi
callq 0x2150
movq %rax, %rsi
movl $0x2, %edi
leaq -0x15c(%rbp), %rdx
callq 0x2480
cmpl $0x1, %eax
je 0x6786
leaq 0x10ef(%rip), %rdi      # 0x7864
callq 0x2240
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x69e1
leaq -0x158(%rbp), %rdi
callq 0x2470
movq -0x158(%rbp), %rax
movq %rax, -0x168(%rbp)
cmpq $0x0, -0x168(%rbp)
je 0x681d
movq -0x168(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x15c(%rbp), %eax
jne 0x680a
movq -0x168(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x1095(%rip), %rdi      # 0x7875
movb $0x0, %al
callq 0x2050
leaq -0x120(%rbp), %rdi
movq -0x168(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x22a0
movl $0x1, -0x128(%rbp)
jmp 0x681d
movq -0x168(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x168(%rbp)
jmp 0x67a0
movq -0x158(%rbp), %rdi
callq 0x2410
cmpl $0x0, -0x128(%rbp)
jne 0x686f
movq 0x37b7(%rip), %rdi      # 0x9ff0
leaq 0x1050(%rip), %rsi      # 0x7890
callq 0x22f0
movq -0x180(%rbp), %rsi
movq %rax, %rdi
callq 0x2260
movq %rax, %rdi
movq 0x375a(%rip), %rsi      # 0x9fb8
callq 0x2310
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x69e1
movl $0x3, %edi
callq 0x2060
movzwl %ax, %edx
movl $0x11, %edi
movl $0x3, %esi
callq 0x20d0
movl %eax, -0x124(%rbp)
cmpl $0x0, %eax
jge 0x68ae
leaq 0x1013(%rip), %rdi      # 0x78b0
callq 0x2240
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x69e1
leaq -0x150(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x21a0
leaq -0x150(%rbp), %rdi
leaq -0x120(%rbp), %rcx
movl $0x10, %esi
leaq 0x1011(%rip), %rdx      # 0x78ec
movb $0x0, %al
callq 0x23f0
movl -0x124(%rbp), %edi
movl $0x8927, %esi           # imm = 0x8927
leaq -0x150(%rbp), %rdx
movb $0x0, %al
callq 0x23a0
cmpl $0x0, %eax
jge 0x6918
leaq 0xfe8(%rip), %rdi       # 0x78ef
callq 0x2240
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x69e1
movl -0x124(%rbp), %edi
callq 0x2490
movq -0x20(%rbp), %rax
movl -0x13e(%rbp), %ecx
movl %ecx, (%rax)
movw -0x13a(%rbp), %cx
movw %cx, 0x4(%rax)
leaq 0xfd8(%rip), %rdi       # 0x7919
movb $0x0, %al
callq 0x2050
movl $0x0, -0x174(%rbp)
cmpl $0x5, -0x174(%rbp)
jge 0x6989
movq -0x20(%rbp), %rax
movslq -0x174(%rbp), %rcx
movzbl (%rax,%rcx), %esi
leaq 0xfb8(%rip), %rdi       # 0x7929
movb $0x0, %al
callq 0x2050
movl -0x174(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
jmp 0x6952
movq -0x20(%rbp), %rax
movzbl 0x5(%rax), %esi
leaq 0xf97(%rip), %rdi       # 0x792f
movb $0x0, %al
callq 0x2050
leaq -0x120(%rbp), %rdi
callq 0x20b0
movq -0x18(%rbp), %rcx
movl %eax, (%rcx)
cmpl $0x0, %eax
jne 0x69cb
leaq 0xf78(%rip), %rdi       # 0x7935
callq 0x2240
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x69e1
movq -0x18(%rbp), %rax
movl (%rax), %esi
leaq 0xf90(%rip), %rdi       # 0x7968
movb $0x0, %al
callq 0x2050
ud2
movl -0x4(%rbp), %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq