-1

I am new to IOS develop.

And I want to turn on the Hotspot via UI Setting , and get the IP address and MAC address of client which connected to this Hotspot via objective-c.

I reference the link forHow do I query the ARP table on iPhone?

The code is like the following :

#include <stdio.h>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/sysctl.h>

#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include "if_types.h"
#include "route.h"
#include "if_ether.h"
#include <netinet/in.h>


#include <arpa/inet.h>

#include <err.h>
#include <errno.h>
#include <netdb.h>

#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

-(NSString*) ip2mac: (char*) ip
{



    int expire_time, flags, export_only, doing_proxy, found_entry;



    NSString *mAddr = nil;
    u_long addr = inet_addr(ip);
    int mib[6];
    size_t needed;
    char *host, *lim, *buf, *next;
    struct rt_msghdr *rtm;
    struct sockaddr_inarp *sin;
    struct sockaddr_dl *sdl;
    extern int h_errno;
    struct hostent *hp;

    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, 6, NULL, &needed, NULL, 0) < 0)
        err(1, "route-sysctl-estimate");
        if ((buf = malloc(needed)) == NULL)
            err(1, "malloc");
            if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
                err(1, "actual retrieval of routing table");


                lim = buf + needed;
                for (next = buf; next < lim; next += rtm->rtm_msglen) {
                    rtm = (struct rt_msghdr *)next;
                    sin = (struct sockaddr_inarp *)(rtm + 1);
                    sdl = (struct sockaddr_dl *)(sin + 1);
                    if (addr) {
                        if (addr != sin->sin_addr.s_addr)
                            continue;
                        found_entry = 1;
                    }
                    if (nflag == 0)
                        hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
                                           sizeof sin->sin_addr, AF_INET);
                    else
                        hp = 0;
                    if (hp)
                        host = hp->h_name;
                    else {
                        host = "?";
                        if (h_errno == TRY_AGAIN)
                            nflag = 1;
                    }



                    if (sdl->sdl_alen) {

                        u_char *cp = LLADDR(sdl);

                        mAddr = [NSString stringWithFormat:@"%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]];


                        //  ether_print((u_char *)LLADDR(sdl));
                    }
                    else

                        mAddr = nil;

                }

    if (found_entry == 0) {
        return nil;
    } else {
        return mAddr;
    }
}

Does someone can teach me how to use the above code to get the IP address from ARP table ?

Community
  • 1
  • 1
Martin
  • 2,813
  • 11
  • 43
  • 66
  • You are working on a `C` project with `Objective C` classes. And you declared a method, not a C function. You should migrate your code either to `C` or `Objective C`. So a function is `return_type name_of_function (function_arguments)` – Francesco Apr 08 '14 at 10:10
  • @Martin, you can't declare a ObjC method outside of the scope of a class. – vikingosegundo Apr 08 '14 at 10:15
  • Marting @Francesco is correct. Either you should move this method inside a class or make it a c function and by using below imports you can remove those erros. My answer to your question assumes that this method is inside a class – Inder Kumar Rathore Apr 08 '14 at 10:22
  • Excuse me...I want to use the code in the link to get the `IP address` from `ARP table`. Can you teach me how to use above code get the `IP address` from `ARP table` ? – Martin Apr 08 '14 at 10:58
  • I have edit the content what I want to ask. – Martin Apr 08 '14 at 11:50
  • What does this have to do with the `xcode IDE`???? – Popeye Apr 08 '14 at 12:24
  • It seem can get the IP address and the MAC address from the ARP table. Like the link [How do I query the ARP table on iPhone?](http://stackoverflow.com/questions/2258172/how-do-i-query-the-arp-table-on-iphone) – Martin Apr 08 '14 at 12:27

1 Answers1

4

Assuming that your method -(NSString*) ip2mac: (char*) ip is inside a class

Your includes are fucked up. Please correct those first if you can't then replace the below to remove errors

#include <stdio.h>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <netdb.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/if_ether.h>


First try to remove import/include errors then move to next errors
As you can see in your image if_types.h file is not present, and the correct way to include is #include <net/if_types.h>.

P.S. For why you should prefer #include over #import, please read What is the difference between #import and #include in Objective-C?

enter image description here


Edit

.h file

@interface MyClass : NSObject

@end

.m file

#import "MyClass.h"
#include <stdio.h>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <netdb.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/if_ether.h>


@implementation MyClass {
  BOOL nflag;
}


-(NSString*) ip2mac: (char*) ip
{



  int expire_time, flags, export_only, doing_proxy, found_entry;



  NSString *mAddr = nil;
  u_long addr = inet_addr(ip);
  int mib[6];
  size_t needed;
  char *host, *lim, *buf, *next;
  struct rt_msghdr *rtm;
  struct sockaddr_inarp *sin;
  struct sockaddr_dl *sdl;
  extern int h_errno;
  struct hostent *hp;

  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, 6, NULL, &needed, NULL, 0) < 0)
    err(1, "route-sysctl-estimate");
  if ((buf = malloc(needed)) == NULL)
    err(1, "malloc");
  if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    err(1, "actual retrieval of routing table");


  lim = buf + needed;
  for (next = buf; next < lim; next += rtm->rtm_msglen) {
    rtm = (struct rt_msghdr *)next;
    sin = (struct sockaddr_inarp *)(rtm + 1);
    sdl = (struct sockaddr_dl *)(sin + 1);
    if (addr) {
      if (addr != sin->sin_addr.s_addr)
        continue;
      found_entry = 1;
    }
    if (nflag == 0)
      hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
                         sizeof sin->sin_addr, AF_INET);
    else
      hp = 0;
    if (hp)
      host = hp->h_name;
    else {
      host = "?";
      if (h_errno == TRY_AGAIN)
        nflag = 1;
    }



    if (sdl->sdl_alen) {

      u_char *cp = LLADDR(sdl);

      mAddr = [NSString stringWithFormat:@"%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]];


      //  ether_print((u_char *)LLADDR(sdl));
    }
    else

      mAddr = nil;

  }

  if (found_entry == 0) {
    return nil;
  } else {
    return mAddr;
  }
}

@end
Community
  • 1
  • 1
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • Sorry...I am new to this , what you mean for `-(NSString*) ip2mac: (char*) ip` is inside a class ? The code is what I reference from the link [How do I query the ARP table on iPhone?](http://stackoverflow.com/questions/2258172/how-do-i-query-the-arp-table-on-iphone). – Martin Apr 08 '14 at 10:45
  • I want to use the code in the link to get the `IP address` from `ARP table`. – Martin Apr 08 '14 at 10:56
  • you can comment on the original post for more. – Inder Kumar Rathore Apr 08 '14 at 11:28
  • You are defining an instance method. Instance methods can be declared in an "@interface" and defined in an "@implementation". You should really get a good book that explains the basics of Objective-C. – gnasher729 Apr 08 '14 at 11:45
  • @InderKumarRathore I have edit the content what I want to ask. – Martin Apr 08 '14 at 11:46
  • @Martin I'm sorry I have no idea about your new question – Inder Kumar Rathore Apr 08 '14 at 11:49