-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscnotify.c
118 lines (94 loc) · 2.58 KB
/
scnotify.c
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
/*
* Copyright 2006 James Peach
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <CoreFoundation/CoreFoundation.h>
#include <SystemConfiguration/SystemConfiguration.h>
const char PROGNAME[] = "scnotify";
CFStringRef cfstring(const char * str)
{
return CFStringCreateWithCStringNoCopy(NULL,
str, kCFStringEncodingASCII, kCFAllocatorNull);
}
const char * utf8string(CFStringRef cfstr)
{
return CFStringGetCStringPtr(cfstr, CFStringGetFastestEncoding(cfstr));
}
void sc_perror(const char * msg)
{
fprintf(stderr, "%s: %s: %s\n",
PROGNAME, msg, SCErrorString(SCError()));
}
void sc_notify(SCDynamicStoreRef ds, CFArrayRef changes, void * ctx)
{
}
CFRange array_range(CFArrayRef array)
{
return CFRangeMake(0, CFArrayGetCount(array));
}
void print_key(const void * data, void * context)
{
CFStringRef key = (CFStringRef)data;
printf("\t%s\n", utf8string(key));
}
void array_print(CFArrayRef array)
{
CFArrayApplyFunction (array, array_range(array), print_key, NULL);
}
CFArrayRef sc_list_keys(SCDynamicStoreRef sc, const char * key)
{
CFArrayRef keys;
CFStringRef cfkey;
cfkey = cfstring(key);
keys = SCDynamicStoreCopyKeyList(sc, cfkey);
if (!keys) {
sc_perror(key);
CFRelease(cfkey);
return NULL;
}
CFRelease(cfkey);
return keys;
}
void print_addresses(SCDynamicStoreRef sc)
{
CFArrayRef links;
links = sc_list_keys(sc, "State:/Network/Interface/.*/Link");
if (links == NULL) {
return;
}
array_print(links);
CFRelease(links);
}
SCDynamicStoreRef sc_connect(void)
{
SCDynamicStoreRef ref;
SCDynamicStoreContext context = { 0 /* version */,
NULL /* retain */, NULL /* release */, NULL /* copy */};
ref = SCDynamicStoreCreate(NULL /* default allocator */,
cfstring(PROGNAME), sc_notify, &context);
if (ref == NULL) {
sc_perror("SCDynamicStoreCreate");
}
return ref;
}
int main(int argc, const char ** argv)
{
SCDynamicStoreRef sc;
sc = sc_connect();
print_addresses(sc);
CFRelease(sc);
return 0;
}