Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
mavris committed Jan 27, 2017
2 parents de88db7 + 3635194 commit 0a2ee65
Show file tree
Hide file tree
Showing 11 changed files with 29,526 additions and 69,176 deletions.
27 changes: 2 additions & 25 deletions MMLanScan/MacFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
//

#import <Foundation/Foundation.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 "if_types.h"

#if TARGET_IPHONE_SIMULATOR
#include <net/route.h>
Expand All @@ -23,23 +15,8 @@
#endif

#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>

@interface MacFinder : NSObject;

+(NSString*)ip2mac:(NSString*)strIP;

@end
+(NSString*)ip2mac: (NSString*)strIP;
@end
130 changes: 43 additions & 87 deletions MMLanScan/MacFinder.m
Original file line number Diff line number Diff line change
@@ -1,109 +1,65 @@
//
// MacFinder.m
// MacFinder
//
// Created by Michael Mavris on 08/06/16.
// Copyright © 2016 Miksoft. All rights reserved.
//

#import "MacFinder.h"
#define BUFLEN (sizeof(struct rt_msghdr) + 512)
#define SEQ 9999
#define RTM_VERSION 5 // important, version 2 does not return a mac address!
#define RTM_GET 0x4 // Report Metrics
#define RTF_LLINFO 0x400 // generated by link layer (e.g. ARP)
#define RTF_IFSCOPE 0x1000000 // has valid interface scope
#define RTA_DST 0x1 // destination sockaddr present

@implementation MacFinder

+(NSString*)ip2mac:(NSString*)strIP {
+(NSString*)ip2mac: (NSString*)strIP {

const char *ip = [strIP UTF8String];

int found_entry = 0;
int nflag=0;

NSString *mAddr = nil;
u_long addr = inet_addr(ip);
int mib[6];
size_t needed;
char *host, *lim, *buf, *next;
int sockfd;
unsigned char buf[BUFLEN];
unsigned char buf2[BUFLEN];
ssize_t n;
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 =(u_char*)LLADDR(sdl);

mAddr = [NSString stringWithFormat:@"%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]];
}
else
mAddr = nil;
}
struct sockaddr_in *sin;
memset(buf,0,sizeof(buf));
memset(buf2,0,sizeof(buf2));

sockfd = socket(AF_ROUTE, SOCK_RAW, 0);
rtm = (struct rt_msghdr *) buf;
rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
rtm->rtm_version = RTM_VERSION;
rtm->rtm_type = RTM_GET;
rtm->rtm_addrs = RTA_DST;
rtm->rtm_flags = RTF_LLINFO;
rtm->rtm_pid = 1234;
rtm->rtm_seq = SEQ;

if (!mAddr) {
return nil;
}

//Fixing the issue with 0 in MAC Address (eg. 00 will be displayed as 0. We fix this here).
NSArray *macArray = [mAddr componentsSeparatedByString:@":"];
sin = (struct sockaddr_in *) (rtm + 1);
sin->sin_len = sizeof(struct sockaddr_in);
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = inet_addr(ip);
write(sockfd, rtm, rtm->rtm_msglen);

NSMutableString *mutStr = [[NSMutableString alloc] init];
n = read(sockfd, buf2, BUFLEN);
close(sockfd);

for (int i=0; i < [macArray count]; i++) {
NSString *str = [macArray objectAtIndex:i];
if ([str length]<2) {
[mutStr appendString:@"0"];
if (n != 0) {
int index = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_inarp) + 8;
// savedata("test",buf2,n);
NSString *macAddress =[NSString stringWithFormat:@"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",buf2[index+0], buf2[index+1], buf2[index+2], buf2[index+3], buf2[index+4], buf2[index+5]];
//If macAddress is equal to 00:00.. then mac address not exist in ARP table and returns nil. If it retuns 08:00.. then the mac address not exist because it's not in the same subnet with the device and return nils
if ([macAddress isEqualToString:@"00:00:00:00:00:00"] ||[macAddress isEqualToString:@"08:00:00:00:00:00"] ) {
return nil;
}

NSString *strToAppend = i < [macArray count] - 1 ? [NSString stringWithFormat:@"%@:",str] : str;

[mutStr appendString:strToAppend];
}

if (found_entry == 0) {

return nil;
}
else {

return mutStr;
return macAddress;
}
return nil;
}
@end
@end

Loading

0 comments on commit 0a2ee65

Please sign in to comment.