#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <paths.h>
#include <sysexits.h>
#include <sys/param.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/network/IOEthernetInterface.h>
#include <IOKit/network/IOEthernetController.h>
#include <IOKit/storage/ata/ATASMARTLib.h>
static kern_return_t FindEthernetInterfaces(io_iterator_t *matchingServices);
static kern_return_t GetMACAddress(io_iterator_t intfIterator, UInt8 *MACAddress);
// Returns an iterator across all known Ethernet interfaces. Caller is responsible for
// releasing the iterator when iteration is complete.
static kern_return_t FindEthernetInterfaces(io_iterator_t *matchingServices)
{
kern_return_t kernResult;
mach_port_t masterPort;
CFMutableDictionaryRef classesToMatch;
/*! @function IOMasterPort
@abstract Returns the mach port used to initiate communication with IOKit.
@discussion Functions that don't specify an existing object require the IOKit master port to be passed. This function obtains that port.
@param bootstrapPort Pass MACH_PORT_NULL for the default.
@param masterPort The master port is returned.
@result A kern_return_t error code. */
kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
if (KERN_SUCCESS != kernResult)
printf("IOMasterPort returned %d\n", kernResult);
/*! @function IOServiceMatching
@abstract Create a matching dictionary that specifies an IOService class match.
@discussion A very common matching criteria for IOService is based on its class. IOServiceMatching will create a matching dictionary that specifies any IOService of a class, or its subclasses. The class is specified by C-string name.
@param name The class name, as a const C-string. Class matching is successful on IOService's of this class or any subclass.
@result The matching dictionary created, is returned on success, or zero on failure. The dictionary is commonly passed to IOServiceGetMatchingServices or IOServiceAddNotification which will consume a reference, otherwise it should be released with CFRelease by the caller. */
// Ethernet interfaces are instances of class kIOEthernetInterfaceClass
classesToMatch = IOServiceMatching(kIOEthernetInterfaceClass);
// Note that another option here would be:
// classesToMatch = IOBSDMatching("en0");
// where X is a number from 0 to the number of Ethernet interfaces on the system - 1.
if (classesToMatch == NULL)
printf("IOServiceMatching returned a NULL dictionary.\n");
/*! @function IOServiceGetMatchingServices
@abstract Look up registered IOService objects that match a matching dictionary.
@discussion This is the preferred method of finding IOService objects currently registered by IOKit. IOServiceAddNotification can also supply this information and install a notification of new IOServices. The matching information used in the matching dictionary may vary depending on the class of service being looked up.
@param masterPort The master port obtained from IOMasterPort().
@param matching A CF dictionary containing matching information, of which one reference is consumed by this function. IOKitLib can contruct matching dictionaries for common criteria with helper functions such as IOServiceMatching, IOOpenFirmwarePathMatching.
@param existing An iterator handle is returned on success, and should be released by the caller when the iteration is finished.
@result A kern_return_t error code. */
kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, matchingServices);
if (KERN_SUCCESS != kernResult)
printf("IOServiceGetMatchingServices returned %d\n", kernResult);
return kernResult;
}
// Given an iterator across a set of Ethernet interfaces, return the MAC address of the first one.
// If no interfaces are found the MAC address is set to an empty string.
static kern_return_t GetMACAddress(io_iterator_t intfIterator, UInt8 *MACAddress)
{
io_object_t intfService;
io_object_t controllerService;
kern_return_t kernResult = KERN_FAILURE;
// Initialize the returned address
bzero(MACAddress, kIOEthernetAddressSize);
/*! @function IOIteratorNext
@abstract Returns the next object in an iteration.
@discussion This function returns the next object in an iteration, or zero if no more remain or the iterator is invalid.
@param iterator An IOKit iterator handle.
@result If the iterator handle is valid, the next element in the iteration is returned, otherwise zero is returned. The element should be released by the caller when it is finished. */
while (intfService = IOIteratorNext(intfIterator))
{
CFTypeRef MACAddressAsCFData;
// IONetworkControllers can't be found directly by the IOServiceGetMatchingServices call,
// matching mechanism. So we've found the IONetworkInterface and will get its parent controller
// by asking for it specifically.
kernResult = IORegistryEntryGetParentEntry( intfService,
kIOServicePlane,
&controllerService );
if (KERN_SUCCESS != kernResult)
printf("IORegistryEntryGetParentEntry returned 0x%08x\n", kernResult);
else {
/*! @function IORegistryEntryCreateCFProperty
@abstract Create a CF representation of a registry entry's property.
@discussion This function creates an instantaneous snapshot of a registry entry property, creating a CF container analogue in the caller's task. Not every object available in the kernel is represented as a CF container; currently OSDictionary, OSArray, OSSet, OSSymbol, OSString, OSData, OSNumber, OSBoolean are created as their CF counterparts.
@param entry The registry entry handle whose property to copy.
@param key A CFString specifying the property name.
@param allocator The CF allocator to use when creating the CF container.
@param options No options are currently defined.
@result A CF container is created and returned the caller on success. The caller should release with CFRelease. */
MACAddressAsCFData = IORegistryEntryCreateCFProperty( controllerService,
CFSTR(kIOMACAddress),
kCFAllocatorDefault,
0);
if (MACAddressAsCFData)
{
CFDataGetBytes(MACAddressAsCFData, CFRangeMake(0, kIOEthernetAddressSize), MACAddress);
CFRelease(MACAddressAsCFData);
}
/*! @function IOObjectRelease
@abstract Releases an object handle previously returned by IOKitLib.
@discussion All objects returned by IOKitLib should be released with this function when access to them is no longer needed. Using the object after it has been released may or may not return an error, depending on how many references the task has to the same object in the kernel.
@param object The IOKit object to release.
@result A kern_return_t error code. */
(void) IOObjectRelease(controllerService);
}
// We have sucked this service dry of information so release it now.
(void) IOObjectRelease(intfService);
// We're just interested in the first interface so exit the loop.
break;
}
return kernResult;
}
//To Get the customer serial number of the machine (serial-number + color-code)
bool GetSerialNumber(char serial[40])
{
bool result = false;
mach_port_t iokitPort;
io_registry_entry_t service;
CFStringRef keys[2] = { CFSTR("serial-number"), CFSTR("color-code")};
CFTypeRef values[2] = {NULL, NULL};
int icl;
char *sbytes = NULL, *cbytes = NULL, *sndptr = NULL;
if(IOMasterPort(MACH_PORT_NULL, &iokitPort) != KERN_SUCCESS)
return false;
//Build a dictionary to match with this class
service = IOServiceGetMatchingService(iokitPort, IOServiceMatching("IOPlatformExpertDevice"));
if(service == NULL)
return false;
// Get "serial-number" and "color-code" values
values[0] = IORegistryEntryCreateCFProperty(service, keys[0], nil, nil);
values[1] = IORegistryEntryCreateCFProperty(service, keys[1], nil, nil);
if (values[0] == NULL) goto exit;
sbytes = (char*)CFDataGetBytePtr((CFDataRef)values[0]);
if (values[1] != NULL)
cbytes = (char*)CFDataGetBytePtr((CFDataRef)values[1]);
sndptr = sbytes + strlen(sbytes) + 1;
icl =0;
while(icl < 20 && (*sndptr == 0))
{
icl++;
sndptr++;
}
//format the property values properly to fit with Apple System Profiler
sprintf(serial,"%.30s-%3.3s-%4.4hx", sndptr, sbytes, (cbytes ? *(short*)cbytes : 0xffff));
result = true;
exit:
if (service != NULL)
IOObjectRelease(service);
if (values[0] != NULL)
CFRelease(values[0]);
if (values[1] != NULL)
CFRelease(values[1]);
return result;
}
// To get the "Device Serial" of the first ATA Device.
bool GetDeviceSerial(char deviceserial[40])
{
bool result = false;
mach_port_t iokitPort;
io_registry_entry_t DSservice;
CFStringRef key = CFSTR("device serial");
if(IOMasterPort(MACH_PORT_NULL, &iokitPort) != KERN_SUCCESS)
return false;
// Build a Dictionary to match with _all_ ATADevice
DSservice = IOServiceGetMatchingService(iokitPort, IOServiceMatching("ATADeviceNub"));
if(DSservice == NULL)
return false;
CFStringRef data = NULL;
// Get from the first ATA Device the value of its "device serial" property
// key = "device serial"
data = IORegistryEntryCreateCFProperty(DSservice, key, nil, nil);
if (data == NULL)
goto exit;
//copy raw informations in a character array
CFStringGetBytes(data, CFRangeMake(0,CFStringGetLength(data)), kCFStringEncodingMacRoman,
0, 0, deviceserial, 40, NULL);
result = true;
exit:
if (DSservice != NULL)
IOObjectRelease(DSservice);
if (data != NULL)
CFRelease(data);
return result;
}
int main(int argc, char *argv
)
{
kern_return_t kernResult = KERN_SUCCESS; // on PowerPC this is an int (4 bytes)
/*
* error number layout as follows (see mach/error.h and IOKitLib/IOReturn.h):
*
* hi lo
* | system(6) | subsystem(12) | code(14) |
*/
io_iterator_t intfIterator;
UInt8 MACAddress[ kIOEthernetAddressSize ];
bool testSNFunction;
char serial[40];
char deviceserial[40];
kernResult = FindEthernetInterfaces(&intfIterator);
if (KERN_SUCCESS != kernResult)
printf("FindEthernetInterfaces returned 0x%08x\n", kernResult);
else {
kernResult = GetMACAddress(intfIterator, MACAddress);
if (KERN_SUCCESS != kernResult)
printf("GetMACAddress returned 0x%08x\n", kernResult);
}
printf("Ethernet Address: %02x:%02x:%02x:%02x:%02x:%02x\n", MACAddress[0], MACAddress[1],
MACAddress[2], MACAddress[3],
MACAddress[4], MACAddress[5]);
IOObjectRelease(intfIterator); // Release the iterator.
testSNFunction = GetSerialNumber(serial);
if(testSNFunction == true)
printf("Customer serial number : %s\n", serial);
else
printf("Customer serial number not found\n");
testSNFunction = GetDeviceSerial(deviceserial);
if(testSNFunction == true)
printf("First ATA Device serial: %s\n", deviceserial);
else
printf("ATA Device not found\n");
return kernResult;
}
___________________
Désolé, mais c'est long lol. Tu as en plus l'adresse éthernet et le sn de ton mac