1 | #include "Python.h" |
---|
2 | |
---|
3 | #include <SystemConfiguration/SystemConfiguration.h> |
---|
4 | |
---|
5 | #define SCNR_REACHABLE "SCNetworkIsReachable" |
---|
6 | static CFStringRef SCNR_Reachable = CFSTR(SCNR_REACHABLE); |
---|
7 | static SCNetworkReachabilityContext SCNR_Context = {0, NULL, NULL, NULL, NULL}; |
---|
8 | |
---|
9 | static void |
---|
10 | SCNR_callback(SCNetworkReachabilityRef target, |
---|
11 | SCNetworkConnectionFlags flags, void *tagString); |
---|
12 | |
---|
13 | static Boolean |
---|
14 | SCNR_reachable(SCNetworkConnectionFlags flags) { |
---|
15 | /* not reachable if it requires a non-automatic connection */ |
---|
16 | return ((flags & kSCNetworkFlagsReachable) && |
---|
17 | (!(flags & kSCNetworkFlagsConnectionRequired) || |
---|
18 | (flags & kSCNetworkFlagsConnectionAutomatic))); |
---|
19 | } |
---|
20 | |
---|
21 | /* XXX should have a way to cancel (e.g. on location detach) */ |
---|
22 | |
---|
23 | static void |
---|
24 | SCNR_schedule(CFStringRef tagString, |
---|
25 | SCNetworkReachabilityRef target) { |
---|
26 | SCNR_Context.info = (void *)tagString; |
---|
27 | SCNetworkReachabilitySetCallback(target, SCNR_callback, &SCNR_Context); |
---|
28 | SCNetworkReachabilityScheduleWithRunLoop(target, |
---|
29 | CFRunLoopGetCurrent(), |
---|
30 | kCFRunLoopDefaultMode); |
---|
31 | } |
---|
32 | |
---|
33 | static void |
---|
34 | SCNR_callback(SCNetworkReachabilityRef target, |
---|
35 | SCNetworkConnectionFlags flags, void *tagString) { |
---|
36 | if (SCNR_reachable(flags)) { |
---|
37 | CFNotificationCenterPostNotification(CFNotificationCenterGetLocalCenter(), |
---|
38 | SCNR_Reachable, tagString, NULL, false); |
---|
39 | CFRelease((CFStringRef)tagString); |
---|
40 | } else { |
---|
41 | SCNR_schedule(tagString, target); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | static PyObject * |
---|
46 | SCNR_notify_when_reachable(PyObject *self, PyObject *args) { |
---|
47 | const char *hostname; |
---|
48 | const char *tag; |
---|
49 | if (!PyArg_ParseTuple(args, "ss", &hostname, &tag)) |
---|
50 | return NULL; |
---|
51 | |
---|
52 | SCNetworkConnectionFlags flags; |
---|
53 | if (SCNetworkCheckReachabilityByName(hostname, &flags) && |
---|
54 | SCNR_reachable(flags)) |
---|
55 | return PyBool_FromLong(0); |
---|
56 | |
---|
57 | SCNetworkReachabilityRef target = |
---|
58 | SCNetworkReachabilityCreateWithName(NULL, hostname); |
---|
59 | CFStringRef tagString = |
---|
60 | CFStringCreateWithCString(NULL, tag, kCFStringEncodingUTF8); |
---|
61 | |
---|
62 | SCNR_schedule(tagString, target); |
---|
63 | |
---|
64 | return PyBool_FromLong(1); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | static PyMethodDef SCNetworkReachabilitymodule_methods[] = { |
---|
69 | {"notify_when_reachable", SCNR_notify_when_reachable, METH_VARARGS, |
---|
70 | "notify_when_reachable(hostname, tag) -> bool\n\n" |
---|
71 | "Deliver the SCNetworkIsReachable runloop notification with object tag\n" |
---|
72 | "when the host becomes reachable, or return false if it's currently\n" |
---|
73 | "reachable."}, |
---|
74 | {NULL, NULL, 0, NULL} |
---|
75 | }; |
---|
76 | |
---|
77 | PyMODINIT_FUNC |
---|
78 | initSCNetworkReachability(void) { |
---|
79 | PyObject *module = Py_InitModule("SCNetworkReachability", |
---|
80 | SCNetworkReachabilitymodule_methods); |
---|
81 | PyObject *dict = PyModule_GetDict(module); |
---|
82 | PyObject *reachable = PyString_FromString(SCNR_REACHABLE); |
---|
83 | PyDict_SetItemString(dict, SCNR_REACHABLE, reachable); |
---|
84 | Py_DECREF(reachable); |
---|
85 | } |
---|