Re: System Configuration Framework API
Re: System Configuration Framework API
- Subject: Re: System Configuration Framework API
- From: Quinn <email@hidden>
- Date: Mon, 3 Dec 2007 11:07:25 +0000
At 12:27 +0800 3/12/07, hector.chou(é¸åöâ) wrote:
I spent so much time on surveying equivalent functions or APIs for Mac OS X,
the command line utility "ifconfig" is out, I need a function call,
the BSD library function "getifaddrs" is fine,
but it doesn't contain enough information I list
above.
Starting with getifaddrs is fine; notably, it
will return information about interfaces that are
not currently in use.
For example, the function "SCNetworkInterfaceGetInterface",
it returns a reference to target interface, called "SCNetworkInterfaceRef",
but how do I use this reference to extract
information, and put it into C data types like
character array, integers or something like that?
The type definition of "SCNetworkInterfaceRef" is:
typedef const struct __SCNetworkInterface * SCNetworkInterfaceRef;
but I can't find the definition of "struct __SCNetworkInterface",
I even try to use Xcode IDE to get this information, but it doesn't work.
So, is there any way (or is it right) to work these APIs with C program?
Yes. SCNetworkInterfaceRef is an opaque type; to
get information out of if, you need to call
various functions declared in the same header.
For example, SCNetworkInterfaceGetBSDName will
return the BSD name of the interface, and you can
use that to match up the information from the
System Configuration framework to the information
returned by getifaddrs.
[SCNetworkInterfaceGetBSDName returns a CFString.
To get a C string, call CFStringGetCString; in
this case, pass kCFStringEncodingUTF8 to the
encoding parameter. Alternatively, create a
CFString from the C string returned by getifaddrs
using CFStringCreateWithCString, again specifying
the kCFStringEncodingUTF8 encoding.]
However, I don't think that SCNetworkInterfaceRef
has all of the information you're looking for.
For example, it's a fine way to get the
user-visible name of the interface, but it won't
return you information about the current DHCP
state. To do that, you need to rummaging around
in the System Configuration framework's dynamic
store. To start, check out the
<x-man-page://8/scutil> command line tool, which
lets you explore the dynamic store without
writing any code.
$ scutil
list
subKey [0] = Plugin:IPConfiguration
subKey [1] = Plugin:InterfaceNamer
subKey [2] = Setup:
subKey [3] = Setup:/
subKey [4] = Setup:/Network/Global/IPv4
subKey [5] = Setup:/Network/HostNames
subKey [6] = Setup:/Network/Service/473A2A5B-3583-4E22-BF14-8C6F2B1641AD
subKey [7] = Setup:/Network/Service/473A2A5B-3583-4E22-BF14-8C6F2B1641AD/IPv4
subKey [8] = Setup:/Network/Service/473A2A5B-3583-4E22-BF14-8C6F2B1641AD/IPv6
[...]
subKey [35] =
State:/Network/Service/8F28FF39-E1AE-41A2-AC5B-E6940730E8E7/DHCP
subKey [36] = State:/Network/Service/8F28FF39-E1AE-41A2-AC5B-E6940730E8E7/DNS
subKey [37] =
State:/Network/Service/8F28FF39-E1AE-41A2-AC5B-E6940730E8E7/IPv4
subKey [38] = State:/Users/ConsoleUser
subKey [39] =
com.apple.DirectoryService.NotifyTypeStandard:DirectoryNodeAdded
subKey [40] = com.apple.network.identification
show State:/Network/Service/8F28FF39-E1AE-41A2-AC5B-E6940730E8E7/IPv4
<dictionary> {
Router : 10.0.40.1
Addresses : <array> {
0 : 10.0.40.15
}
SubnetMasks : <array> {
0 : 255.255.255.0
}
InterfaceName : en0
NetworkSignature :
IPv4.Router=10.0.40.1;IPv4.RouterHardwareAddress=00:10:db:05:c3:e0
}
show State:/Network/Service/8F28FF39-E1AE-41A2-AC5B-E6940730E8E7/DHCP
<dictionary> {
Option_1 : <data> 0xffffff00
Option_54 : <data> 0x0a002801
Option_51 : <data> 0xffffffff
LeaseStartTime : 12/03/2007 10:15:22 Europe/London
Option_15 : <data> 0x6170706c652e636f6d
Option_3 : <data> 0x0a002801
Option_53 : <data> 0x05
Option_6 : <data> 0x118064091180640c
}
^D
You can use the dynamic store APIs to find all of
the active service IDs (the UUIDs in the above
text). Specifically, create a SCDynamicStoreRef
and then
SCDynamicStoreCopyDHCPInfo, then create a regular
expression that matches the service ID, then
search for services using
SCDynamicStoreCopyMultiple. Once you have the
service ID, you can get the DHCP info using the
routines in
<SystemConfiguration/SCDynamicStoreCopyDHCPInfo.h>.
S+E
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden