AnD
AnD

Reputation: 125

Getting ARP table on iPhone/iPad always return nil the MAC

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

Upvotes: 1

Views: 524

Answers (2)

AnD
AnD

Reputation: 125

I found the solution.

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

Upvotes: 1

David Berry
David Berry

Reputation: 41226

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

Upvotes: 0

Related Questions