-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
42 lines (41 loc) · 1.17 KB
/
main.cpp
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
#include <stdio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <net/if.h>
#include <sys/kern_event.h>
int main(int argc,char** argv) {
// create a socket of type PF_SYSTEM to listen for events
int s = socket(PF_SYSTEM, SOCK_RAW, SYSPROTO_EVENT);
// make sure we get receive the correct events
kev_request key;
key.vendor_code = KEV_VENDOR_APPLE;
key.kev_class = KEV_NETWORK_CLASS;
key.kev_subclass = KEV_ANY_SUBCLASS;
//
int code = ioctl(s, SIOCSKEVFILT, &key);
kern_event_msg msg;
// endless loop
while(1) {
// get notification
code = recv(s, &msg, sizeof(msg), 0);
// check type of event
switch(msg.event_code) {
case KEV_DL_IF_DETACHED:
// interface is detached
break;
case KEV_DL_IF_ATTACHED:
// interface is attached
break;
case KEV_DL_LINK_OFF:
// interface is turned off
printf("link off\n");
break;
case KEV_DL_LINK_ON:
// interface is turned on
printf("link on\n");
break;
}
}
return 0;
}