~ubuntuone-control-tower/ubuntu-sso-client/trunk

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# -*- coding: utf-8 -*-
#
# Copyright 2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
"""Network state detection on OS X.

is_machine_connected(): (deferred) returns connected state as bool
NetworkManagerState: class with listening thread, calls back with state changes
"""

from twisted.internet import defer

from threading import Thread

from ubuntu_sso.networkstate import NetworkFailException
from ubuntu_sso.networkstate.networkstates import (ONLINE, OFFLINE, UNKNOWN)
from ubuntu_sso.logger import setup_logging
logger = setup_logging("ubuntu_sso.networkstate")

HOSTNAME_TO_CHECK = 'login.ubuntu.com'

from ctypes import (
    CDLL,
    POINTER,
    CFUNCTYPE,
    Structure,
    pointer,
    c_bool,
    c_long,
    c_void_p,
    c_uint32)

from ctypes.util import find_library

# pylint: disable=C0103

# Functions and constants below are from
# /System/Library/CoreFoundation.framework/
CoreFoundationPath = find_library("CoreFoundation")
CoreFoundation = CDLL(CoreFoundationPath)

# CFRunLoopRef CFRunLoopGetCurrent()
CFRunLoopGetCurrent = CoreFoundation.CFRunLoopGetCurrent
CFRunLoopGetCurrent.restype = c_void_p
CFRunLoopGetCurrent.argtypes = []

# void CFRelease(CFTypeRef)
CFRelease = CoreFoundation.CFRelease
CFRelease.restype = None
CFRelease.argtypes = [c_void_p]

# void CFRunLoopRun()
CFRunLoopRun = CoreFoundation.CFRunLoopRun

# const CFStringRef kCFRunLoopDefaultMode
# pylint: disable=E1101
kCFRunLoopDefaultMode = c_void_p.in_dll(CoreFoundation,
                                        "kCFRunLoopDefaultMode")


# Functions and constants below are from
# /System/Library/SystemConfiguration.framework/
SystemConfigurationPath = find_library("SystemConfiguration")

# SystemConfiguration abbreviated as "SC" below:
SC = CDLL(SystemConfigurationPath)

# "SCNetworkReachability" functions abbreviated to "SCNR*" here.

# SCNetworkReachabilityRef
# SCNetworkReachabilityCreateWithName(CFAllocatorRef, const char *)
SCNRCreateWithName = SC.SCNetworkReachabilityCreateWithName
SCNRCreateWithName.restype = c_void_p

# Boolean SCNetworkReachabilityGetFlags(SCNetworkReachabilityRef,
#                                       SCNetworkReachabilityFlags)
SCNRGetFlags = SC.SCNetworkReachabilityGetFlags
SCNRGetFlags.restype = c_bool
SCNRGetFlags.argtypes = [c_void_p,
                         POINTER(c_uint32)]

SCNRScheduleWithRunLoop = SC.SCNetworkReachabilityScheduleWithRunLoop
SCNRScheduleWithRunLoop.restype = c_bool
SCNRScheduleWithRunLoop.argtypes = [c_void_p,
                                    c_void_p,
                                    c_void_p]

# ctypes callback type to match SCNetworkReachabilityCallback
# void (*SCNetworkReachabilityCallback) (SCNetworkReachabilityRef,
#                                        SCNetworkReachabilityFlags,
#                                        void *)
SCNRCallbackType = CFUNCTYPE(None, c_void_p, c_uint32, c_void_p)
# NOTE: need to keep this reference alive as long as a callback might occur.

# Boolean SCNetworkReachabilitySetCallback(SCNetworkReachabilityRef,
#                                          SCNetworkReachabilityCallback,
#                                          SCNetworkReachabilityContext)
SCNRSetCallback = SC.SCNetworkReachabilitySetCallback
SCNRSetCallback.restype = c_bool
SCNRSetCallback.argtypes = [c_void_p,
                            SCNRCallbackType,
                            c_void_p]
# pylint: enable=E1101


def check_connected_state():
    """Calls Synchronous SCNR API, returns bool."""
    target = SCNRCreateWithName(None, HOSTNAME_TO_CHECK)
    if target is None:
        logger.error("Error creating network reachability reference.")
        raise NetworkFailException()

    flags = c_uint32(0)
    ok = SCNRGetFlags(target, pointer(flags))
    CFRelease(target)

    if not ok:
        logger.error("Error getting reachability status of '%s'" %
                     HOSTNAME_TO_CHECK)
        raise NetworkFailException()

    return flags_say_reachable(flags.value)


def flags_say_reachable(flags):
    """Check flags returned from SCNetworkReachability API. Returns bool.

    Requires some logic:
    reachable_flag isn't enough on its own.

    A down wifi will return flags = 7, or reachable_flag and
    connection_required_flag, meaning that the host *would be*
    reachable, but you need a connection first.  (And then you'd
    presumably be best off checking again.)
    """
    # values from SCNetworkReachability.h
    reachable_flag = 1 << 1
    connection_required_flag = 1 << 2

    if flags & connection_required_flag:
        return False
    elif flags & reachable_flag:
        return True
    else:
        return False


class SCNRContext(Structure):

    """A struct to send as SCNetworkReachabilityContext to SCNRSetCallback.

    We don't use the fields currently.
    """

    _fields_ = [("version", c_long),
                ("info", c_void_p),
                ("retain", c_void_p),           # func ptr
                ("release", c_void_p),          # func ptr
                ("copyDescription", c_void_p)]  # func ptr


class NetworkManagerState(object):

    """Probe Network State and receive callbacks on changes.

    This class uses both synchronous and async API from the
    SystemConfiguration framework.

    To use: Initialize with a callback function, then call
    find_online_state. The callback will be called once immediately
    with the current state and then only on state changes.

    Any exceptions in checking state will result in the callback being
    called with UNKNOWN. At this point the listening thread is no
    longer runing, and a new NetworkManagerState should be created.

    NOTE: the callback will be called from the separate listening
    thread, except for the first call.
    """

    def __init__(self, result_cb):
        """Initialize and save result callback function.

        result_cb should take one argument, a networkstate object.

        The callback will be called with one of ONLINE, OFFLINE, or
        UNKNOWN, as defined in networkstates.py.
        """
        self.result_cb = result_cb
        self.listener_thread = None

    def _state_changed(self, flags):
        """Testable callback called by reachability_state_changed_cb.

        Used because reachability_state_changed_cb has to have a
        particular method signature.

        Clients should not call this method.
        """
        if flags_say_reachable(flags):
            self.result_cb(ONLINE)
        else:
            self.result_cb(OFFLINE)

    def _listen_on_separate_thread(self):
        """In separate thread, setup callback and listen for changes.

        On error, calls result_cb(UNKNOWN) and returns.
        """

        def reachability_state_changed_cb(targetref, flags, info):
            """Callback for SCNetworkReachability API

            This callback is passed to the SCNetworkReachability API,
            so its method signature has to be exactly this. Therefore,
            we declare it here and just call _state_changed with
            flags."""
            self._state_changed(flags)

        c_callback = SCNRCallbackType(reachability_state_changed_cb)
        context = SCNRContext(0, None, None, None, None)

        target = SCNRCreateWithName(None, HOSTNAME_TO_CHECK)
        if target is None:
            logger.error("Error creating SCNetworkReachability target")
            self.result_cb(UNKNOWN)
            return

        ok = SCNRSetCallback(target, c_callback, pointer(context))
        if not ok:
            logger.error("error setting SCNetworkReachability callback")
            CFRelease(target)
            self.result_cb(UNKNOWN)
            return

        ok = SCNRScheduleWithRunLoop(target,
                                     CFRunLoopGetCurrent(),
                                     kCFRunLoopDefaultMode)
        if not ok:
            logger.error("error scheduling on runloop: SCNetworkReachability")
            CFRelease(target)
            self.result_cb(UNKNOWN)
            return

        CFRunLoopRun()

        CFRelease(target)               # won't happen

    def _start_listening_thread(self):
        """Start the separate listener thread.

        Currently will not start one more than once.
        Should be OK because we don't expect errors the listen method.

        To add more error handling support, you could either add a
        call to join and re-start, or client could just create a new
        NetworkManagerState.
        """

        if self.listener_thread is None:
            self.listener_thread = Thread(
                target=self._listen_on_separate_thread,
                name="Ubuntu SSO Network Connection Monitor")
            self.listener_thread.daemon = True
            self.listener_thread.start()

    def find_online_state(self):
        """Calls callback with current state. Starts listening thread."""
        try:
            if check_connected_state():
                self.result_cb(ONLINE)
            else:
                self.result_cb(OFFLINE)

        except Exception:               # pylint: disable=W0703
            logger.exception("Getting state from SCNetworkReachability")
            self.result_cb(UNKNOWN)
            return                      # don't start thread on error

        self._start_listening_thread()


def is_machine_connected():
    """Return a deferred that when fired, returns online state as a bool.

    Raises NetworkFailException for errors.
    """
    try:
        return defer.succeed(check_connected_state())
    except Exception as e:                # pylint: disable=W0703
        logger.exception("Exception calling check_connected_state:")
        return defer.fail(NetworkFailException(e))