Objective C / C. I get wrong Value when i try to get Mac Adress on IOS device - ios

I'm trying to use a C code to obtain mac adress on any IOS device.
I'm trying it on a iPhone 6 plus but it seems not work.
My output will look as follows:
2015-02-15 15:37:37.947 MyDemo[438:223963] Mac Address: 02:00:00:00:00:00
Anyone can help me for this please ?
Thanks.
This is the GetMacAdress.m
Original source code courtesy John from iOSDeveloperTips.com
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
...
- (NSString *)getMacAddress
{
int mgmtInfoBase[6];
char *msgBuffer = NULL;
size_t length;
unsigned char macAddress[6];
struct if_msghdr *interfaceMsgStruct;
struct sockaddr_dl *socketStruct;
NSString *errorFlag = NULL;
// Setup the management Information Base (mib)
mgmtInfoBase[0] = CTL_NET; // Request network subsystem
mgmtInfoBase[1] = AF_ROUTE; // Routing table info
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // Request link layer information
mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces
// With all configured interfaces requested, get handle index
if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
errorFlag = #"if_nametoindex failure";
else
{
// Get the size of the data available (store in len)
if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
errorFlag = #"sysctl mgmtInfoBase failure";
else
{
// Alloc memory based on above call
if ((msgBuffer = malloc(length)) == NULL)
errorFlag = #"buffer allocation failure";
else
{
// Get system information, store in buffer
if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
errorFlag = #"sysctl msgBuffer failure";
}
}
}
// Befor going any further...
if (errorFlag != NULL)
{
NSLog(#"Error: %#", errorFlag);
return errorFlag;
}
// Map msgbuffer to interface message structure
interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
// Map to link-level socket structure
socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
// Copy link layer address data in socket structure to an array
memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
// Read from char array into a string object, into traditional Mac address format
NSString *macAddressString = [NSString stringWithFormat:#"%02X:%02X:%02X:%02X:%02X:%02X",
macAddress[0], macAddress[1], macAddress[2],
macAddress[3], macAddress[4], macAddress[5]];
NSLog(#"Mac Address: %#", macAddressString);
// Release the buffer memory
free(msgBuffer);
return macAddressString;
}

That is some pretty nasty low-level code.
I personally think that anyone who writes code of the form:
if ((a=b)==c)
...should be shot. We're not writing assembler here - Don't write code that looks like a typo to save 1 statement. (That rant is directed at the original author, not you.)
I'm not familiar enough with the low-level system functions to be able to tell what's wrong without a lot of digging.
However, stepping back from why your code isn't working, WHY do you need the MAC address? Does it have to be the actual network MAC address?
Apple no longer allows you to get that information because it allows you to uniquely track the user's device. If you do find a way to do it, your app will be rejected. (Not really Apple's fault; there was a big stink in the industry and all the device vendors had to block providing this info.)
#mad_mask's suggestion of using identifierForVendor is probably the best solution. That gives you an ID that's unique for that device for your company.

It is not possible to get the MAC address for an apple device any longer. This was added to prevent applications from using the MAC address as a unique identifier for tracking. It always comes out as 02:00:00:00:00:00 regardless of the route you take to finding it.
The vendor and marketing Ids you have to use instead are not the MAC address but something else.
I have written a network scanner and as far as I have been able to tell, there is no way round this unless you can talk to an external device on the same LAN segment which has visibility of the device on the network and can send it to you.

Related

Stm32f4 dma m2m

I'm using STM32F407VG Discovery Board and I've issue with DMA memory to memory transfer. I want to copy 32 bytes of data from one place in memory to other using DMA by writing copy_dma() function. In while loop i'm checking Transfer Complete flag but DMA never returns it. I want to ask where i'm making mistake? Maybe something in configuration is wrong. I'm using Standart Peripheral Libraries. Here's my code.
#include "stm32f4xx.h"
#define BUFFER_SIZE 32
uint8_t src_buffer[BUFFER_SIZE];
uint8_t dst_buffer[BUFFER_SIZE];
void copy_dma(void);
int main(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
int i;
DMA_InitTypeDef dma;
DMA_DeInit(DMA1_Stream4);
DMA_StructInit(&dma);
dma.DMA_Channel = DMA_Channel_1;
dma.DMA_PeripheralBaseAddr = (uint32_t)src_buffer;
dma.DMA_PeripheralInc = DMA_PeripheralInc_Enable;
dma.DMA_Memory0BaseAddr = (uint32_t)dst_buffer;
dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma.DMA_BufferSize = BUFFER_SIZE;
dma.DMA_DIR = DMA_DIR_MemoryToMemory;
dma.DMA_FIFOMode = DMA_FIFOMode_Disable;
dma.DMA_MemoryBurst = DMA_MemoryBurst_Single;
dma.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
dma.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
dma.DMA_Mode = DMA_Mode_Normal;
dma.DMA_Priority = DMA_Priority_High;
DMA_Init(DMA1_Stream4, &dma);
for (i = 0; i < BUFFER_SIZE; i++) {
src_buffer[i] = 100 + i;
}
copy_dma();
while(1) {
}
}
void copy_dma(void) {
DMA_Cmd(DMA1_Stream4, ENABLE);
while (DMA_GetFlagStatus(DMA1_Stream4, DMA_FLAG_TCIF4) == RESET);
}
In app note "Using the STM32F2 and STM32F4 DMA controller"(http://stm32.eefocus.com/download/index.php?act=down&id=6312)
is mentioned:
"Memory to memory (only DMA2 is able to do such transfer, in this mode, the circular and direct modes are not allowed.)"
So, try to use DMA2.
In addition to Mariusz Górka's awnser:
When using the DMA you need to know which memory region you are using. The stm32f4 has a memory section called Core Coupled Memory (CCM). The DMA does not have access to this region.
Check your map file and make sure your buffers are not in the region 0x10000000 - 0x1000FFFF.

Is there any way to register a notification when ip address changed on device?

I want to use NSStream to send send and receive information to server.
I thought if I can obtain the ip address the server can send information back in order to replace the silent push notification.
Is is possible to register a notification when ip address changed?
P.S.
I'm currently using reachability class to register a notification when network changed but if there is a better way it'll be great!
I think there is not straight way to achieve this but Use
`[[NSHost currentHost] address];`
save value into userDefault and every time when you connect check with stored value and get notify
i found this on SO hope this work for you
#include <ifaddrs.h>
#include <arpa/inet.h>
- (NSString *)getIPAddress {
NSString *address = #"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:#"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}

iOS - saving in app purchase data in plist files?

NOTE ---- The answer below uses iOS6- methods, Apple has since then removed developer's access to MAC addresses. If you are developing for iOS7+ disregaurd first answer and just encrypt your IAP unlock data based on other variables that will be unique to each device (like the date the app was first launched)
I have features that need tone unlocked so I store them in my plist files.... a feature like a new avatar in a chat room could have the id "13891" and if it is unlocked I might assign it some key like "93" and if it's locked it might have any other key "37" for example....
So the plist will say: "13891" = "93"
My question is can jailbroken phones edit the plist files easily and unlock features for themselves?
What's a better way of storing this data?
I don't want to have to check Apple's servers every time, it takes too long with low internet connection.
Edit: Current Answer:
4 measures to take:
1) Store it in the keychain. (Or plist I guess now that I've added measure #4)
2) Check Apple's servers every time but if you are worried about the lag that follows just check it in the background and in the meantime let the user use the app if it says they can.
3) Store your variables as encrypted keys in the keychain... don't store "FishingRod = unlocked" store "3dhk34D#HT% = d3tD##".
4) Encrypt each key with the devices MAC address (these MAC addresses do NOT change and are available with or without WiFi connection... code below). That way if a user downloads a plist off of the internet and tries to use it, it won't work because when you decrypt it using their device ID you will can't random nonsense instead of the unlock key (in my examples case that would be "d3tD##".)!!! -- Mac Address can no longer be accessed as of iOS7+, instead encrypt with other device unique things, such as the date the app was first launched
MAC address code (Just stick it in the view controllers ViewDidAppear... in the .H import )
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
-(void)viewDidAppear:(BOOL)animated {
int mgmtInfoBase[6];
char *msgBuffer = NULL;
NSString *errorFlag = NULL;
size_t length;
// Setup the management Information Base (mib)
mgmtInfoBase[0] = CTL_NET; // Request network subsystem
mgmtInfoBase[1] = AF_ROUTE; // Routing table info
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // Request link layer information
mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces
// With all configured interfaces requested, get handle index
if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
errorFlag = #"if_nametoindex failure";
// Get the size of the data available (store in len)
else if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
errorFlag = #"sysctl mgmtInfoBase failure";
// Alloc memory based on above call
else if ((msgBuffer = malloc(length)) == NULL)
errorFlag = #"buffer allocation failure";
// Get system information, store in buffer
else if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
{
free(msgBuffer);
errorFlag = #"sysctl msgBuffer failure";
}
else
{
// Map msgbuffer to interface message structure
struct if_msghdr *interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
// Map to link-level socket structure
struct sockaddr_dl *socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
// Copy link layer address data in socket structure to an array
unsigned char macAddress[6];
memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
// Read from char array into a string object, into traditional Mac address format
NSString *macAddressString = [NSString stringWithFormat:#"%02X:%02X:%02X:%02X:%02X:%02X",
macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]];
NSLog(#"Mac Address: %#", macAddressString);
// Release the buffer memory
free(msgBuffer);
//return macAddressString;
NSLog(#"MAC: %#", macAddressString);
//F0:DC:E2:1D:EB:50
}
// Error...
NSLog(#"Error: %#", errorFlag);
}
Note: I got the MAC address code from a friend... I'm not claiming that I wrote the code... I don't know if he wrote it or got it from someone else as well.
4 measures to take:
1) Store it in the keychain. (Or plist I guess now that I've added measure #4)
2) Check Apple's servers every time but if you are worried about the lag that follows just check it in the background and in the meantime let the user use the app if it says they can.
3) Store your variables as encrypted keys in the keychain... don't store "FishingRod = unlocked" store "3dhk34D#HT% = d3tD##".
4) Encrypt each key with the devices MAC address (these MAC addresses do NOT change and are available with or without WiFi connection... code below). That way if a user downloads a plist off of the internet and tries to use it, it won't work because when you decrypt it using their device ID you will can't random nonsense instead of the unlock key (in my examples case that would be "d3tD##".)!!! -- Mac Address can no longer be accessed as of iOS7+, instead encrypt with other device unique things, such as the date the app was first launched
MAC address code (Just stick it in the view controllers ViewDidAppear... in the .H import )
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
-(void)viewDidAppear:(BOOL)animated {
int mgmtInfoBase[6];
char *msgBuffer = NULL;
NSString *errorFlag = NULL;
size_t length;
// Setup the management Information Base (mib)
mgmtInfoBase[0] = CTL_NET; // Request network subsystem
mgmtInfoBase[1] = AF_ROUTE; // Routing table info
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // Request link layer information
mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces
// With all configured interfaces requested, get handle index
if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
errorFlag = #"if_nametoindex failure";
// Get the size of the data available (store in len)
else if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
errorFlag = #"sysctl mgmtInfoBase failure";
// Alloc memory based on above call
else if ((msgBuffer = malloc(length)) == NULL)
errorFlag = #"buffer allocation failure";
// Get system information, store in buffer
else if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
{
free(msgBuffer);
errorFlag = #"sysctl msgBuffer failure";
}
else
{
// Map msgbuffer to interface message structure
struct if_msghdr *interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
// Map to link-level socket structure
struct sockaddr_dl *socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
// Copy link layer address data in socket structure to an array
unsigned char macAddress[6];
memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
// Read from char array into a string object, into traditional Mac address format
NSString *macAddressString = [NSString stringWithFormat:#"%02X:%02X:%02X:%02X:%02X:%02X",
macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]];
NSLog(#"Mac Address: %#", macAddressString);
// Release the buffer memory
free(msgBuffer);
//return macAddressString;
NSLog(#"MAC: %#", macAddressString);
//F0:DC:E2:1D:EB:50
}
// Error...
NSLog(#"Error: %#", errorFlag);
}
You don't even need to jailbreak. Wherever you store a writable file, an application like iPhone Explorer can let the user grab and modify that file then write it back out to the device. It would only take one person purchasing to send the unlocked plist file out to the internet at large.
What I would do is store the unlocked items in the keychain on the device (just to obscure it a tiny bit more), and trust that on launch - but then also every time try to contact the Apple servers in the background to verify that the user really should have those unlocked items. That way they may have the items unlocked for a short time even if they can forge the keychain entries, but the ability will be removed if the device is connected to the internet at all while the app runs. Having to remember to disconnect a device from internet connectivity before each run is probably too annoying to make the stolen unlock worth it for the forger.
Since the keychain persists even across application deletion, you may also want to write out a plist file on first launch, if you do not detect that initially created plist file on later launches clear out the keychain.
If anything though, the risk of someone fiddling and unlocking things in your app is probably low. Always err on the side of giving user access when the situation is murky so you do not cause problems for real users.
can jailbroken phones edit the plist files easily and unlock features for themselves?
Yes, exactly.
What's a better way of storing this data?
Maybe the keychain, but that can be altered too on jailbroken devices.
I don't want to have to check Apple's servers every time, it takes too long with low internet connection.
Too bad. If you want to be secure at least to some extent, you better check Apple's server, or even better, your own server as well. (That's also not a 100% guarantee that your game won't be hacked on a jailbroken phone, since the behavior of the app can be modified as one wishes using MobileSubstrate, but at least it's a bit more secure.)

Save BIO into char* (from SMIME_write_CMS)

I want to save (pipe/copy) a BIO into a char array.
When I know the size it works, but otherwise not.
For example, I can store the content of my char* into a BIO using this
const unsigned char* data = ...
myBio = BIO_new_mem_buf((void*)data, strlen(data));
But when I try to use SMIME_write_CMS which takes a BIO (what I've created before) for the output it doesn't work.
const int SIZE = 50000;
unsigned char *temp = malloc(SIZE);
memset(temp, 0, SIZE);
out = BIO_new_mem_buf((void*)temp, SIZE);
if (!out) {
NSLog(#"Couldn't create new file!");
assert(false);
}
int finished = SMIME_write_CMS(out, cms, in, flags);
if (!finished) {
NSLog(#"SMIME write CMS didn't succeed!");
assert(false);
}
printf("cms encrypted: %s\n", temp);
NSLog(#"All succeeded!");
The OpenSSL reference uses a direct file output with the BIO.
This works but I can't use BIO_new_file() in objective-c... :-/
out = BIO_new_file("smencr.txt", "w");
if (!out)
goto err;
/* Write out S/MIME message */
if (!SMIME_write_CMS(out, cms, in, flags))
goto err;
Do you guys have any suggestion?
I would suggest trying to use SIZE-1, that way you are guaranteed that it is NULL terminated. Otherwise, it is possible that it is just over running the buffer.
out = BIO_new_mem_buf((void*)temp, SIZE-1);
Let me know if that helps.
Edit:
When using BIO_new_mem_buf() it is a read only buffer, so you cannot write to it. If you want to write to memory use:
BIO *bio = BIO_new(BIO_s_mem());

Getting Device ID or Mac Address in iOS [duplicate]

This question already has answers here:
How can I programmatically get the MAC address of an iphone
(12 answers)
Closed 9 years ago.
I have an application that uses rest to communicate to a server, i would like to obtain the iphones either mac address or device ID for uniqueness validation, how can this be done?
[[UIDevice currentDevice] uniqueIdentifier] is guaranteed to be unique to each device.
uniqueIdentifier (Deprecated in iOS 5.0. Instead, create a unique identifier specific to your app.)
The docs recommend use of CFUUIDCreate instead of [[UIDevice currentDevice] uniqueIdentifier]
So here is how you generate an unique id in your app
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef);
CFRelease(uuidRef);
Note that you have to save the uuidString in user defaults or in other place because you can not generate the same uuidString again.
You can use UIPasteboard to store your generated uuid. And if the app will be deleted and reinstalled you can read from UIPasteboard the old uuid. The paste board will be wiped out when the device will be erased.
In iOS 6 they have introduced the NSUUID Class that is designed to create UUIDs strings
Also they added in iOS 6 #property(nonatomic, readonly, retain) NSUUID *identifierForVendor to the UIDevice class
The value of this property is the same for apps that come from the
same vendor running on the same device. A different value is returned
for apps on the same device that come from different vendors, and for
apps on different devices regardless of vendor.
The value of this property may be nil if the app is running in the
background, before the user has unlocked the device the first time
after the device has been restarted. If the value is nil, wait and get
the value again later.
Also in iOS 6 you can use ASIdentifierManager class from AdSupport.framework. There you have
#property(nonatomic, readonly) NSUUID *advertisingIdentifier
Discussion Unlike the identifierForVendor property of the UIDevice,
the same value is returned to all vendors. This identifier may
change—for example, if the user erases the device—so you should not
cache it.
The value of this property may be nil if the app is running in the
background, before the user has unlocked the device the first time
after the device has been restarted. If the value is nil, wait and get
the value again later.
Edit:
Pay attention that the advertisingIdentifier may return
00000000-0000-0000-0000-000000000000
because there seems to be a bug in iOS. Related question: The advertisingIdentifier and identifierForVendor return "00000000-0000-0000-0000-000000000000"
For a Mac Adress you could use
#import <Foundation/Foundation.h>
#interface MacAddressHelper : NSObject
+ (NSString *)getMacAddress;
#end
implentation
#import "MacAddressHelper.h"
#import <sys/socket.h>
#import <sys/sysctl.h>
#import <net/if.h>
#import <net/if_dl.h>
#implementation MacAddressHelper
+ (NSString *)getMacAddress
{
int mgmtInfoBase[6];
char *msgBuffer = NULL;
size_t length;
unsigned char macAddress[6];
struct if_msghdr *interfaceMsgStruct;
struct sockaddr_dl *socketStruct;
NSString *errorFlag = NULL;
// Setup the management Information Base (mib)
mgmtInfoBase[0] = CTL_NET; // Request network subsystem
mgmtInfoBase[1] = AF_ROUTE; // Routing table info
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // Request link layer information
mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces
// With all configured interfaces requested, get handle index
if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
errorFlag = #"if_nametoindex failure";
else
{
// Get the size of the data available (store in len)
if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
errorFlag = #"sysctl mgmtInfoBase failure";
else
{
// Alloc memory based on above call
if ((msgBuffer = malloc(length)) == NULL)
errorFlag = #"buffer allocation failure";
else
{
// Get system information, store in buffer
if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
errorFlag = #"sysctl msgBuffer failure";
}
}
}
// Befor going any further...
if (errorFlag != NULL)
{
NSLog(#"Error: %#", errorFlag);
return errorFlag;
}
// Map msgbuffer to interface message structure
interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
// Map to link-level socket structure
socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
// Copy link layer address data in socket structure to an array
memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
// Read from char array into a string object, into traditional Mac address format
NSString *macAddressString = [NSString stringWithFormat:#"%02X:%02X:%02X:%02X:%02X:%02X",
macAddress[0], macAddress[1], macAddress[2],
macAddress[3], macAddress[4], macAddress[5]];
//NSLog(#"Mac Address: %#", macAddressString);
// Release the buffer memory
free(msgBuffer);
return macAddressString;
}
#end
Use:
NSLog(#"MAC address: %#",[MacAddressHelper getMacAddress]);
Use this:
NSUUID *id = [[UIDevice currentDevice] identifierForVendor];
NSLog(#"ID: %#", id);
In IOS 5 [[UIDevice currentDevice] uniqueIdentifier] is deprecated.
It's better to use -identifierForVendor or -identifierForAdvertising.
A lot of useful information can be found here:
iOS6 UDID - What advantages does identifierForVendor have over identifierForAdvertising?
Here, We can find mac address for IOS device using Asp.net C# Code...
.aspx.cs
-
var UserDeviceInfo = HttpContext.Current.Request.UserAgent.ToLower(); // User's Iphone/Ipad Info.
var UserMacAdd = HttpContext.Current.Request.UserHostAddress; // User's Iphone/Ipad Mac Address
GetMacAddressfromIP macadd = new GetMacAddressfromIP();
if (UserDeviceInfo.Contains("iphone;"))
{
// iPhone
Label1.Text = UserDeviceInfo;
Label2.Text = UserMacAdd;
string Getmac = macadd.GetMacAddress(UserMacAdd);
Label3.Text = Getmac;
}
else if (UserDeviceInfo.Contains("ipad;"))
{
// iPad
Label1.Text = UserDeviceInfo;
Label2.Text = UserMacAdd;
string Getmac = macadd.GetMacAddress(UserMacAdd);
Label3.Text = Getmac;
}
else
{
Label1.Text = UserDeviceInfo;
Label2.Text = UserMacAdd;
string Getmac = macadd.GetMacAddress(UserMacAdd);
Label3.Text = Getmac;
}
.class File
public string GetMacAddress(string ipAddress)
{
string macAddress = string.Empty;
if (!IsHostAccessible(ipAddress)) return null;
try
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
Process process = new Process();
processStartInfo.FileName = "arp";
processStartInfo.RedirectStandardInput = false;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.Arguments = "-a " + ipAddress;
processStartInfo.UseShellExecute = false;
process = Process.Start(processStartInfo);
int Counter = -1;
while (Counter <= -1)
{
Counter = macAddress.Trim().ToLower().IndexOf("mac address", 0);
if (Counter > -1)
{
break;
}
macAddress = process.StandardOutput.ReadLine();
if (macAddress != "")
{
string[] mac = macAddress.Split(' ');
if (Array.IndexOf(mac, ipAddress) > -1)
{
if (mac[11] != "")
{
macAddress = mac[11].ToString();
break;
}
}
}
}
process.WaitForExit();
macAddress = macAddress.Trim();
}
catch (Exception e)
{
Console.WriteLine("Failed because:" + e.ToString());
}
return macAddress;
}

Resources