close

Please fill the <prog_name> with "getMacAddr" in this Makefile[Generic makefile for single-target] first.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * IPAddress.h
 *
 * Created by Chris Whiteford on 2009-02-20.
 * Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#define MAXADDRS 32

extern char *if_names[MAXADDRS];
extern char *ip_names[MAXADDRS];
extern char *hw_addrs[MAXADDRS];
extern unsigned long ip_addrs[MAXADDRS];

// Function prototypes

extern void InitAddresses();
extern void FreeAddresses();
extern void GetIPAddresses();
extern void GetHWAddresses();

 

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * IPAddress.c
 *
 * Created by Chris Whiteford on 2009-02-20.
 * Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/ethernet.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <errno.h>
#include <net/if_dl.h>

#include "IPAddress.h"

#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))

#define BUFFERSIZE 4000

char *if_names[MAXADDRS];
char *ip_names[MAXADDRS];
char *hw_addrs[MAXADDRS];
unsigned long ip_addrs[MAXADDRS];

static int	nextAddr = 0;

void InitAddresses()
{
	int i;
	for (i=0; i<MAXADDRS; ++i)
	{
		if_names[i] = ip_names[i] = hw_addrs[i] = NULL;
		ip_addrs[i] = 0;
	}
}

void FreeAddresses()
{
	int i;
	for (i=0; i<MAXADDRS; ++i)
	{
		if (if_names[i] != 0) free(if_names[i]);
		if (ip_names[i] != 0) free(ip_names[i]);
		if (hw_addrs[i] != 0) free(hw_addrs[i]);
		ip_addrs[i] = 0;
	}
	InitAddresses();
}

void GetIPAddresses()
{
	int					i, len, flags;
	char				buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
	struct ifconf		ifc;
	struct ifreq		*ifr, ifrcopy;
	struct sockaddr_in	*sin;

	char temp[80];

	int sockfd;

	for (i=0; i<MAXADDRS; ++i)
	{
		if_names[i] = ip_names[i] = NULL;
		ip_addrs[i] = 0;
	}

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0)
	{
		perror("socket failed");
		return;
	}
	
	ifc.ifc_len = BUFFERSIZE;
	ifc.ifc_buf = buffer;
	
	if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
	{
		perror("ioctl error");
		return;
	}
	
	lastname[0] = 0;
	
	for (ptr = buffer; ptr < buffer + ifc.ifc_len; )
	{
		ifr = (struct ifreq *)ptr;
		len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);
		ptr += sizeof(ifr->ifr_name) + len;	// for next one in buffer
	
		if (ifr->ifr_addr.sa_family != AF_INET)
		{
			continue;	// ignore if not desired address family
		}
	
		if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL)
		{
			*cptr = 0;		// replace colon will null
		}
	
		if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0)
		{
			continue;	/* already processed this interface */
		}
	
		memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
	
		ifrcopy = *ifr;
		ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);
		flags = ifrcopy.ifr_flags;
		if ((flags & IFF_UP) == 0)
		{
			continue;	// ignore if interface not up
		}
	
		if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1);
		if (if_names[nextAddr] == NULL)
		{
			return;
		}
		strcpy(if_names[nextAddr], ifr->ifr_name);
	
		sin = (struct sockaddr_in *)&ifr->ifr_addr;
		strcpy(temp, inet_ntoa(sin->sin_addr));
	
		ip_names[nextAddr] = (char *)malloc(strlen(temp)+1);
		if (ip_names[nextAddr] == NULL)
		{
			return;
		}
		strcpy(ip_names[nextAddr], temp);

		ip_addrs[nextAddr] = sin->sin_addr.s_addr;

		++nextAddr;
	}
	
	close(sockfd);
}

void GetHWAddresses()
{
	struct ifconf ifc;
	struct ifreq *ifr;
	int i, sockfd;
	char buffer[BUFFERSIZE], *cp, *cplim;
	char temp[80];

	for (i=0; i<MAXADDRS; ++i)
	{
		hw_addrs[i] = NULL;
	}

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0)
	{
		perror("socket failed");
		return;
	}

	ifc.ifc_len = BUFFERSIZE;
	ifc.ifc_buf = buffer;

	if (ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0)
	{
		perror("ioctl error");
		close(sockfd);
		return;
	}

	ifr = ifc.ifc_req;

	cplim = buffer + ifc.ifc_len;

	for (cp=buffer; cp < cplim; )
	{
		ifr = (struct ifreq *)cp;
		if (ifr->ifr_addr.sa_family == AF_LINK)
		{
			struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
			int a,b,c,d,e,f;
			int i;

			strcpy(temp, (char *)ether_ntoa((struct ether_addr*)LLADDR(sdl)));
			sscanf(temp, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);
			sprintf(temp, "%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);

			for (i=0; i<MAXADDRS; ++i)
			{
				if ((if_names[i] != NULL) && (strcmp(ifr->ifr_name, if_names[i]) == 0))
				{
					if (hw_addrs[i] == NULL)
					{
						hw_addrs[i] = (char *)malloc(strlen(temp)+1);
						strcpy(hw_addrs[i], temp);
						break;
					}
				}
			}
		}
		cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);
	}

	close(sockfd);
}

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * Device.h
 *
 * Created by Jackie Xie on 2011-07-06.
 * Copyright 2011 Jackie Xie. All rights reserved.
 *
 */
#import <Foundation/NSObject.h>

@interface Device : NSObject {
	NSString *deviceInfoStr;
	NSString *ifname;
	NSString *ipaddr;
	NSString *macaddr;
}

@property (copy) NSString* deviceInfoStr;
@property (copy) NSString* ifname;
@property (copy) NSString* ipaddr;
@property (copy) NSString* macaddr;
-(id) initWithDeviceInfo;
- (NSString *)DeviceInfo;
- (NSString *)getDeviceInfo;
- (NSString *)getDeviceName;
- (NSString *)getIPAddr;
- (NSString *)getMacAddr;
+(void) log:(NSString *)msg;

@end

 

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Device.m
 *
 * Created by Jackie Xie on 2011-07-06.
 * Copyright 2011 Jackie Xie. All rights reserved.
 *
 */
#import <Foundation/Foundation.h>
#import "IPAddress.h"
#import "Device.h"
#import "stdio.h"
#import "string.h"

@implementation Device

@synthesize deviceInfoStr;
@synthesize ifname;
@synthesize ipaddr;
@synthesize macaddr;

// 預設建構子
-(id) init{
	return self;
}

// 建構子
-(id) initWithDeviceInfo {
	ifname = ipaddr = macaddr = nil;
	return [self init];
}

- (NSString *)DeviceInfo {
	int i;
	NSString *deviceInfo = nil;
	NSString *deviceName = nil;
	NSString *deviceIP = nil;
	NSString *deviceMac = nil;
	NSString *tmp = nil;
	NSString *delimiter=@"; ";
	char tmpstr[200];
	
	InitAddresses();
	GetIPAddresses();
	GetHWAddresses();
	
	for (i=0; i<MAXADDRS; ++i)
	{
		static unsigned long localHost = 0x7F000001;		// 127.0.0.1
		unsigned long theAddr;
	
		theAddr = ip_addrs[i];
	
		if (theAddr == 0) break;
		if (theAddr == localHost) continue;
		if (strncmp(if_names[i], "lo", 2) != 0)				
		{
			tmp = [[NSString alloc] initWithUTF8String:(const char *)if_names[i]];
			if(ifname != nil){
				deviceName = [ifname copy];
				ifname = [[deviceName autorelease]stringByAppendingString:[tmp autorelease]];
			}
			else
			{
				ifname = [tmp copy];
			}
			deviceName = [ifname copy];
			ifname = [[deviceName autorelease] stringByAppendingString:delimiter];

			tmp = [[NSString alloc] initWithUTF8String:(const char *)ip_names[i]];
			if(ipaddr != nil){
				deviceIP =[ipaddr copy];
				ipaddr = [[deviceIP autorelease]stringByAppendingString:[tmp autorelease]];
			}
			else
			{
				ipaddr = [tmp copy];
			}
			deviceIP = [ipaddr copy];
			ipaddr = [[deviceIP autorelease] stringByAppendingString:delimiter];

			tmp = [[NSString alloc] initWithUTF8String:(const char *)hw_addrs[i]];
			if(macaddr != nil){
				deviceMac = [macaddr copy];
				macaddr = [[deviceMac autorelease]stringByAppendingString:[tmp autorelease]];
			}
			else
			{
				macaddr = [tmp copy];
			}
			deviceMac = [macaddr copy];
			macaddr = [[deviceMac autorelease] stringByAppendingString:delimiter];

			sprintf(tmpstr , "Adapter en has a IP of Interface:%s IP:%s MAC:%s\n", if_names[i], ip_names[i], hw_addrs[i]);
			NSLog(@"%s", tmpstr);
			tmp = [[NSString alloc] initWithUTF8String:(const char *)tmpstr];
			if(deviceInfoStr != nil){
				deviceInfo = [deviceInfoStr copy];
				deviceInfoStr = [[deviceInfo autorelease]stringByAppendingString:[tmp autorelease]];
			}
			else
			{
				deviceInfoStr = [tmp copy];
			}
			deviceInfo = [deviceInfoStr copy];
			deviceInfoStr = [[deviceInfo autorelease] stringByAppendingString:@";"];
		}
	}

	FreeAddresses();
	return deviceInfoStr;
}

- (NSString *)getDeviceInfo {
	deviceInfoStr = self.DeviceInfo;
	return deviceInfoStr;
}


- (NSString *)getDeviceName {
	return ifname;
}

- (NSString *)getIPAddr {
	return ipaddr;
}

- (NSString *)getMacAddr {
	return macaddr;
}

+(void) log:(NSString *)msg{
	NSLog(@"%@",msg);
}
@end

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * getMacAddr.m
 *
 * Created by Jackie Xie on 2011-07-06.
 * Copyright 2011 Jackie Xie. All rights reserved.
 *
 */
#import <Foundation/Foundation.h>
#import <Foundation/NSObject.h>
#import <Cocoa/Cocoa.h>
#import "IPAddress.h"
#import "Device.h"

int main( int argc, const char *argv[] )
{
	int i;
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	Device *device = [[Device alloc] initWithDeviceInfo];
	NSString *deviceInfo = [device getDeviceInfo];
	NSString *deviceName = [device getDeviceName];
	NSString *deviceIP = [device getIPAddr];
	NSString *deviceMac = [device getMacAddr];
	
	[Device log:deviceInfo];
	[Device log:deviceName];
	[Device log:deviceIP];
	[Device log:deviceMac];

	[deviceMac release];
	[deviceIP release];
	[deviceName release];
	[deviceInfo release];
	[device release];
	[pool drain];
	return 0;
}

Then build the target "getMacAddr" by make in console/terminal.

[ jackie localhost ~/test/objc/getMacAddr ]# ls
Device.h  Device.m  IPAddress.c  IPAddress.h  Makefile  getMacAddr.m
[ jackie localhost ~/test/objc/getMacAddr ]# make
cc  -MD  -c -o Device.o Device.m
cc  -MD  -c -o getMacAddr.o getMacAddr.m
gcc -Wno-import  -MD  -c -o IPAddress.o IPAddress.c
gcc -o getMacAddr -L/System/Library/Frameworks/Foundation.framework/ -lobjc -framework Foundation Device.o getMacAddr.o IPAddress.o 
[ jackie localhost ~/test/objc/getMacAddr ]# ./getMacAddr 
2011-07-07 11:41:31.517 getMacAddr[2979:903] Adapter en has a IP of Interface:en0 IP:192.168.1.2 MAC:58:B0:35:FD:25:7C
2011-07-07 11:41:31.520 getMacAddr[2979:903] Adapter en has a IP of Interface:en1 IP:192.168.0.2 MAC:F8:1E:DF:EB:4D:DC
2011-07-07 11:41:31.521 getMacAddr[2979:903] Adapter en has a IP of Interface:en2 IP:10.211.55.2 MAC:00:1C:42:00:00:08
2011-07-07 11:41:31.524 getMacAddr[2979:903] Adapter en has a IP of Interface:en3 IP:10.37.129.2 MAC:00:1C:42:00:00:09
2011-07-07 11:41:31.524 getMacAddr[2979:903] Adapter en has a IP of Interface:vmnet1 IP:192.168.176.1 MAC:00:50:56:C0:00:01
2011-07-07 11:41:31.525 getMacAddr[2979:903] Adapter en has a IP of Interface:vmnet8 IP:172.16.206.1 MAC:00:50:56:C0:00:08
2011-07-07 11:41:31.525 getMacAddr[2979:903] [en0 192.168.1.2 58:B0:35:FD:25:7C] ;[en1 192.168.0.2 F8:1E:DF:EB:4D:DC] ;[en2 10.211.55.2 00:1C:42:00:00:08] ;[en3 10.37.129.2 00:1C:42:00:00:09] ;[vmnet1 192.168.176.1 00:50:56:C0:00:01] ;[vmnet8 172.16.206.1 00:50:56:C0:00:08] ;
2011-07-07 11:41:31.526 getMacAddr[2979:903] en0; en1; en2; en3; vmnet1; vmnet8; 
2011-07-07 11:41:31.526 getMacAddr[2979:903] 192.168.1.2; 192.168.0.2; 10.211.55.2; 10.37.129.2; 192.168.176.1; 172.16.206.1; 
2011-07-07 11:41:31.529 getMacAddr[2979:903] 58:B0:35:FD:25:7C; F8:1E:DF:EB:4D:DC; 00:1C:42:00:00:08; 00:1C:42:00:00:09; 00:50:56:C0:00:01; 00:50:56:C0:00:08; 
[ jackie localhost ~/test/objc/getMacAddr ]# 

Above codes could be porting to iOS devices, such as iPhone、iPod Touch、iPad⋯etc.:

1
2
3
4
5
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    ipAddress.text = [self deviceIPAdress];
}

 

arrow
arrow
    全站熱搜

    Bluelove1968 發表在 痞客邦 留言(0) 人氣()