1

everyone , I need help... I'm trying to recover mac following these links Getting ARP table on iPhone/iPad How do I query the ARP table on iPhone? My test was correct only once..... my code was not changed, only I included cocoaPods for AFNetworking. when I tested again, always fails...the mac always is nil...mi ip is correct my code is :

const char *c = [ipAddress UTF8String];
u_long addr = inet_addr(c);
NSString *mac = [self ip2mac:addr ];



- (NSString*)ip2mac:(in_addr_t)addr{

NSString *ret = nil;

    size_t needed;
    char *buf, *next;

    struct rt_msghdr *rtm;
    struct sockaddr_inarp *sin;
    struct sockaddr_dl *sdl;

    int mib[6];

    mib[0] = CTL_NET;
    mib[1] = PF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_INET;
    mib[4] = NET_RT_FLAGS;
    mib[5] = RTF_LLINFO;

    if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &needed, NULL, 0) < 0)
        err(1, "route-sysctl-estimate");

    if ((buf = (char*)malloc(needed)) == NULL)
        err(1, "malloc");

    if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &needed, NULL, 0) < 0)
        err(1, "retrieval of routing table");

    for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {

        rtm = (struct rt_msghdr *)next;
        sin = (struct sockaddr_inarp *)(rtm + 1);
        sdl = (struct sockaddr_dl *)(sin + 1);

        if (addr != sin->sin_addr.s_addr || sdl->sdl_alen < 6)
            continue;

        u_char *cp = (u_char*)LLADDR(sdl);

        ret = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
               cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]];

        break;
    }

    free(buf);

    return ret;
}

only comes once in... for (next = buf; next < buf + needed; next += rtm->rtm_msglen)

size needed always is 128.... if put assignment ret = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]]; before if (addr != sin->sin_addr.s_addr || sdl->sdl_alen < 6) continue; return a value mac incorrect...It is wrong, I have no such device with this MAC

thanks everyone

Community
  • 1
  • 1
AnD
  • 125
  • 1
  • 8

2 Answers2

1

I found the solution.

u_long addr = inet_addr(c); change in_addr_t addr = inet_addr(c);

AnD
  • 125
  • 1
  • 8
  • Do you mind providing the full code snippet? I have this problem now too, but the above line is not a complete solution. Thanks in advance! – mdizzle Apr 15 '15 at 21:30
  • const char *c = [ipAddress UTF8String]; u_long addr = inet_addr(c);// change in_addr_t addr = inet_addr(c); NSString *mac = [self ip2mac:addr ]; // call method.... ip2mac method code is stated above... if you need anything, let me know I will try to help you – AnD Apr 16 '15 at 21:55
0

This was changed as of iOS 8 so that the MAC address was no longer accessible.

David Berry
  • 40,941
  • 12
  • 84
  • 95
  • Hi David, yesterday when I tried it the first time...works well... before including cocoaPods...later did not return to work.And my device has version 8.2 – AnD Apr 14 '15 at 07:49