This file is indexed.

/usr/share/pyshared/jarabe/model/adhoc.py is in python-jarabe-0.96 0.96.1-2.1git1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# Copyright (C) 2010 One Laptop per Child
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import logging

import dbus
import gobject

from jarabe.model import network
from jarabe.model.network import Settings
from sugar.util import unique_id
from jarabe.model.network import IP4Config


_adhoc_manager_instance = None


def get_adhoc_manager_instance():
    global _adhoc_manager_instance
    if _adhoc_manager_instance is None:
        _adhoc_manager_instance = AdHocManager()
    return _adhoc_manager_instance


class AdHocManager(gobject.GObject):
    """To mimic the mesh behavior on devices where mesh hardware is
    not available we support the creation of an Ad-hoc network on
    three channels 1, 6, 11. If Sugar sees no "known" network when it
    starts, it does autoconnect to an Ad-hoc network.

    """

    __gsignals__ = {
        'members-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                            ([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT])),
        'state-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                          ([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT])),
    }

    _AUTOCONNECT_TIMEOUT = 60
    _CHANNEL_1 = 1
    _CHANNEL_6 = 6
    _CHANNEL_11 = 11

    def __init__(self):
        gobject.GObject.__init__(self)

        self._bus = dbus.SystemBus()
        self._device = None
        self._idle_source = 0
        self._listening_called = 0
        self._device_state = network.NM_DEVICE_STATE_UNKNOWN

        self._current_channel = None
        self._networks = {self._CHANNEL_1: None,
                          self._CHANNEL_6: None,
                          self._CHANNEL_11: None}

        for channel in (self._CHANNEL_1, self._CHANNEL_6, self._CHANNEL_11):
            if not self._find_connection(channel):
                self._add_connection(channel)

    def start_listening(self, device):
        self._listening_called += 1
        if self._listening_called > 1:
            raise RuntimeError('The start listening method can' \
                                   ' only be called once.')

        self._device = device
        props = dbus.Interface(device, dbus.PROPERTIES_IFACE)
        self._device_state = props.Get(network.NM_DEVICE_IFACE, 'State')

        self._bus.add_signal_receiver(self.__device_state_changed_cb,
                                      signal_name='StateChanged',
                                      path=self._device.object_path,
                                      dbus_interface=network.NM_DEVICE_IFACE)

        self._bus.add_signal_receiver(self.__wireless_properties_changed_cb,
                                      signal_name='PropertiesChanged',
                                      path=self._device.object_path,
                                      dbus_interface=network.NM_WIRELESS_IFACE)

    def stop_listening(self):
        self._listening_called = 0
        self._bus.remove_signal_receiver(self.__device_state_changed_cb,
                                         signal_name='StateChanged',
                                         path=self._device.object_path,
                                         dbus_interface=network.NM_DEVICE_IFACE)
        self._bus.remove_signal_receiver(self.__wireless_properties_changed_cb,
                                         signal_name='PropertiesChanged',
                                         path=self._device.object_path,
                                         dbus_interface=network.NM_WIRELESS_IFACE)

    def __device_state_changed_cb(self, new_state, old_state, reason):
        self._device_state = new_state
        self._update_state()

    def __wireless_properties_changed_cb(self, properties):
        if 'ActiveAccessPoint' in properties and \
                properties['ActiveAccessPoint'] != '/':
            active_ap = self._bus.get_object(network.NM_SERVICE,
                                             properties['ActiveAccessPoint'])
            props = dbus.Interface(active_ap, dbus.PROPERTIES_IFACE)
            props.GetAll(network.NM_ACCESSPOINT_IFACE, byte_arrays=True,
                         reply_handler=self.__get_all_ap_props_reply_cb,
                         error_handler=self.__get_all_ap_props_error_cb)

    def __get_all_ap_props_reply_cb(self, properties):
        if properties['Mode'] == network.NM_802_11_MODE_ADHOC and \
                'Frequency' in properties:
            frequency = properties['Frequency']
            self._current_channel = network.frequency_to_channel(frequency)
        else:
            self._current_channel = None
        self._update_state()

    def __get_all_ap_props_error_cb(self, err):
        logging.error('Error getting the access point properties: %s', err)

    def _update_state(self):
        self.emit('state-changed', self._current_channel, self._device_state)

    def autoconnect(self):
        """Start a timer which basically looks for 30 seconds of inactivity
        on the device, then does autoconnect to an Ad-hoc network.

        This function may be called early on (e.g. when the device is still
        in NM_DEVICE_STATE_UNMANAGED). It is assumed that initialisation
        will complete quickly, and long before the timeout ticks.
        """
        if self._idle_source != 0:
            gobject.source_remove(self._idle_source)
        self._idle_source = gobject.timeout_add_seconds(
            self._AUTOCONNECT_TIMEOUT, self.__idle_check_cb)

    def __idle_check_cb(self):
        if self._device_state == network.NM_DEVICE_STATE_DISCONNECTED:
            logging.debug('Connect to Ad-hoc network due to inactivity.')
            self._autoconnect_adhoc()
        else:
            logging.debug('autoconnect Sugar Ad-hoc: already connected')
        return False

    def _autoconnect_adhoc(self):
        """First we try if there is an Ad-hoc network that is used by other
        learners in the area, if not we default to channel 1.

        """
        if self._networks[self._CHANNEL_1] is not None:
            self.activate_channel(self._CHANNEL_1)
        elif self._networks[self._CHANNEL_6] is not None:
            self.activate_channel(self._CHANNEL_6)
        elif self._networks[self._CHANNEL_11] is not None:
            self.activate_channel(self._CHANNEL_11)
        else:
            self.activate_channel(self._CHANNEL_1)

    def activate_channel(self, channel):
        """Activate a sugar Ad-hoc network.

        Keyword arguments:
        channel -- Channel to connect to (should be 1, 6, 11)

        """
        connection = self._find_connection(channel)
        if connection:
            connection.activate(self._device.object_path)

    @staticmethod
    def _get_connection_id(channel):
        return '%s%d' % (network.ADHOC_CONNECTION_ID_PREFIX, channel)

    def _add_connection(self, channel):
        ssid = 'Ad-hoc Network %d' % (channel,)
        settings = Settings()
        settings.connection.id = self._get_connection_id(channel)
        settings.connection.uuid = unique_id()
        settings.connection.type = '802-11-wireless'
        settings.connection.autoconnect = False
        settings.wireless.ssid = dbus.ByteArray(ssid)
        settings.wireless.band = 'bg'
        settings.wireless.channel = channel
        settings.wireless.mode = 'adhoc'
        settings.ip4_config = IP4Config()
        settings.ip4_config.method = 'link-local'
        network.add_connection(settings)

    def _find_connection(self, channel):
        connection_id = self._get_connection_id(channel)
        return network.find_connection_by_id(connection_id)

    def deactivate_active_channel(self):
        """Deactivate the current active channel."""
        obj = self._bus.get_object(network.NM_SERVICE, network.NM_PATH)
        netmgr = dbus.Interface(obj, network.NM_IFACE)

        netmgr_props = dbus.Interface(netmgr, dbus.PROPERTIES_IFACE)
        netmgr_props.Get(network.NM_IFACE, 'ActiveConnections', \
                reply_handler=self.__get_active_connections_reply_cb,
                error_handler=self.__get_active_connections_error_cb)

    def __get_active_connections_reply_cb(self, active_connections_o):
        for connection_o in active_connections_o:
            obj = self._bus.get_object(network.NM_IFACE, connection_o)
            props = dbus.Interface(obj, dbus.PROPERTIES_IFACE)
            state = props.Get(network.NM_ACTIVE_CONN_IFACE, 'State')
            if state == network.NM_ACTIVE_CONNECTION_STATE_ACTIVATED:
                access_point_o = props.Get(network.NM_ACTIVE_CONN_IFACE,
                                           'SpecificObject')
                if access_point_o != '/':
                    obj = self._bus.get_object(network.NM_SERVICE, network.NM_PATH)
                    netmgr = dbus.Interface(obj, network.NM_IFACE)
                    netmgr.DeactivateConnection(connection_o)

    def __get_active_connections_error_cb(self, err):
        logging.error('Error getting the active connections: %s', err)

    def __activate_reply_cb(self, connection):
        logging.debug('Ad-hoc network created: %s', connection)

    def __activate_error_cb(self, err):
        logging.error('Failed to create Ad-hoc network: %s', err)

    def add_access_point(self, access_point):
        """Add an access point to a network and notify the view to idicate
        the member change.

        Keyword arguments:
        access_point -- Access Point

        """
        if access_point.ssid.endswith(' 1'):
            self._networks[self._CHANNEL_1] = access_point
            self.emit('members-changed', self._CHANNEL_1, True)
        elif access_point.ssid.endswith(' 6'):
            self._networks[self._CHANNEL_6] = access_point
            self.emit('members-changed', self._CHANNEL_6, True)
        elif access_point.ssid.endswith('11'):
            self._networks[self._CHANNEL_11] = access_point
            self.emit('members-changed', self._CHANNEL_11, True)

    def is_sugar_adhoc_access_point(self, ap_object_path):
        """Checks whether an access point is part of a sugar Ad-hoc network.

        Keyword arguments:
        ap_object_path -- Access Point object path

        Return: Boolean

        """
        for access_point in self._networks.values():
            if access_point is not None:
                if access_point.model.object_path == ap_object_path:
                    return True
        return False

    def remove_access_point(self, ap_object_path):
        """Remove an access point from a sugar Ad-hoc network.

        Keyword arguments:
        ap_object_path -- Access Point object path

        """
        for channel in self._networks:
            if self._networks[channel] is not None:
                if self._networks[channel].model.object_path == ap_object_path:
                    self.emit('members-changed', channel, False)
                    self._networks[channel] = None
                    break