summaryrefslogtreecommitdiffstats
path: root/BitTorrent/IPC.py
blob: 9c1ef0c6a7711bd080a02c1dc05e9f5633c485f9 (plain)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# The contents of this file are subject to the BitTorrent Open Source License
# Version 1.1 (the License).  You may not copy or use this file, in either
# source code or executable form, except in compliance with the License.  You
# may obtain a copy of the License at http://www.bittorrent.com/license/.
#
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
# for the specific language governing rights and limitations under the
# License.

# Written by Greg Hazel
# based on code by Uoti Urpala
from __future__ import generators

import os
import socket
import sys
import traceback
if os.name == 'nt':
    import win32api
    import win32event
    import winerror
    import win32ui
    import dde
    import pywin.mfc.object

from binascii import b2a_hex

from BitTorrent.RawServer_magic import RawServer, Handler
from BitTorrent.platform import get_home_dir, get_config_dir
from BitTorrent import INFO, WARNING, ERROR, CRITICAL, BTFailure, app_name

def toint(s):
    return int(b2a_hex(s), 16)

def tobinary(i):
    return (chr(i >> 24) + chr((i >> 16) & 0xFF) +
        chr((i >> 8) & 0xFF) + chr(i & 0xFF))

CONTROL_SOCKET_PORT = 46881

class ControlsocketListener(Handler):

    def __init__(self, callback):
        self.callback = callback

    def connection_made(self, connection):
        connection.handler = MessageReceiver(self.callback)


class MessageReceiver(Handler):

    def __init__(self, callback):
        self.callback = callback
        self._buffer = []
        self._buffer_len = 0
        self._reader = self._read_messages()
        self._next_len = self._reader.next()

    def _read_messages(self):
        while True:
            yield 4
            l = toint(self._message)
            yield l
            action = self._message
            
            if action in ('no-op',):
                self.callback(action, None)
            else:
                yield 4
                l = toint(self._message)
                yield l
                data = self._message
                if action in ('show_error',):
                    self.callback(action, data)
                else:
                    yield 4
                    l = toint(self._message)
                    yield l
                    path = self._message
                    if action in ('start_torrent'):
                        self.callback(action, data, path)

    # copied from Connecter.py
    def data_came_in(self, conn, s):
        while True:
            i = self._next_len - self._buffer_len
            if i > len(s):
                self._buffer.append(s)
                self._buffer_len += len(s)
                return
            m = s[:i]
            if self._buffer_len > 0:
                self._buffer.append(m)
                m = ''.join(self._buffer)
                self._buffer = []
                self._buffer_len = 0
            s = s[i:]
            self._message = m
            try:
                self._next_len = self._reader.next()
            except StopIteration:
                self._reader = None
                conn.close()
                return

    def connection_lost(self, conn):
        self._reader = None
        pass

    def connection_flushed(self, conn):
        pass

class IPC(object):
    def __init__(self, config, log):
        self.config = config
        self.log = log
        self.rawserver = None
        self.callback = None        

    def create(self):
        pass

    def start(self, callback):
        self.callback = callback

    def send_command(self, command, *args):
        pass

    def handle_command(self, command, *args):
        if callable(self.callback):
            return self.callback(command, *args)
        self.log(WARNING, _("Unhandled command: %s %s"  % (str(command), str(args))))

    def set_rawserver(self, rawserver):
        self.rawserver = rawserver

    def stop(self):
        pass

class IPCSocketBase(IPC):

    def __init__(self, *args):
        IPC.__init__(self, *args)
        self.port = CONTROL_SOCKET_PORT

        self.controlsocket = None        

    def start(self, callback):
        IPC.start(self, callback)
        self.rawserver.start_listening(self.controlsocket,
                                       ControlsocketListener(self.handle_command))

    def stop(self):
        # safe double-stop, since TorrentQueue seems to be prone to do so
        if self.controlsocket:
            # it's possible we're told to stop after controlsocket creation but
            # before rawserver registration
            if self.rawserver:
                self.rawserver.stop_listening(self.controlsocket)
            self.controlsocket.close()
            self.controlsocket = None
        
class IPCUnixSocket(IPCSocketBase):

    def __init__(self, *args):
        IPCSocketBase.__init__(self, *args)
        self.socket_filename = os.path.join(self.config['data_dir'], 'ui_socket')
        
    def create(self):
        filename = self.socket_filename
        if os.path.exists(filename):
            try:
                self.send_command('no-op')
            except BTFailure:
                pass
            else:
                raise BTFailure(_("Could not create control socket: already in use"))

            try:
                os.unlink(filename)
            except OSError, e:
                raise BTFailure(_("Could not remove old control socket filename:")
                                + str(e))
        try:
            controlsocket = RawServer.create_unixserversocket(filename)
        except socket.error, e:
            raise BTFailure(_("Could not create control socket: ")+str(e))

        self.controlsocket = controlsocket

    # blocking version without rawserver
    def send_command(self, command, *args):
        s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        filename = self.socket_filename
        try:
            s.connect(filename)
            s.send(tobinary(len(command)))
            s.send(command)
            for arg in args:
                s.send(tobinary(len(arg)))
                s.send(arg)
            s.close()
        except socket.error, e:
            s.close()
            raise BTFailure(_("Could not send command: ") + str(e))


class IPCWin32Socket(IPCSocketBase):
    def __init__(self, *args):
        IPCSocketBase.__init__(self, *args)
        self.socket_filename = os.path.join(self.config['data_dir'], 'ui_socket')
        self.mutex = None
        self.master = 0

    def _get_sic_path(self):
        directory = get_config_dir()
        configdir = os.path.join(directory, '.bittorrent')
        filename = os.path.join(configdir, ".btcontrol")
        return filename

    def create(self):
        obtain_mutex = 1
        mutex = win32event.CreateMutex(None, obtain_mutex, app_name)

        # prevent the PyHANDLE from going out of scope, ints are fine
        self.mutex = int(mutex)
        mutex.Detach()

        lasterror = win32api.GetLastError()
        
        if lasterror == winerror.ERROR_ALREADY_EXISTS:
            takeover = 0

            try:
                # if the mutex already exists, discover which port to connect to.
                # if something goes wrong with that, tell us to take over the
                # role of master
                takeover = self.discover_sic_socket()
            except:
                pass
            
            if not takeover:
                raise BTFailure(_("Global mutex already created."))

        self.master = 1

        # lazy free port code
        port_limit = 50000
        while self.port < port_limit:
            try:
                controlsocket = RawServer.create_serversocket(self.port,
                                                              '127.0.0.1', reuse=True)
                self.controlsocket = controlsocket
                break
            except socket.error, e:
                self.port += 1

        if self.port >= port_limit:
            raise BTFailure(_("Could not find an open port!"))

        filename = self._get_sic_path()
        (path, name) = os.path.split(filename)
        try:
            os.makedirs(path)
        except OSError, e:
            # 17 is dir exists
            if e.errno != 17:
                BTFailure(_("Could not create application data directory!"))
        f = open(filename, "w")
        f.write(str(self.port))
        f.close()
        
        # we're done writing the control file, release the mutex so other instances can lock it and read the file
        # but don't destroy the handle until the application closes, so that the named mutex is still around
        win32event.ReleaseMutex(self.mutex)

    def discover_sic_socket(self):
        takeover = 0
        
        # mutex exists and has been opened (not created, not locked).
        # wait for it so we can read the file
        r = win32event.WaitForSingleObject(self.mutex, win32event.INFINITE)

        # WAIT_OBJECT_0 means the mutex was obtained
        # WAIT_ABANDONED means the mutex was obtained, and it had previously been abandoned
        if (r != win32event.WAIT_OBJECT_0) and (r != win32event.WAIT_ABANDONED):
            raise BTFailure(_("Could not acquire global mutex lock for controlsocket file!"))

        filename = self._get_sic_path()
        try:
            f = open(filename, "r")
            self.port = int(f.read())
            f.close()
        except:
            if (r == win32event.WAIT_ABANDONED):
                self.log(WARNING, _("A previous instance of BT was not cleaned up properly. Continuing."))
                # take over the role of master
                takeover = 1
            else:
                self.log(WARNING, (_("Another instance of BT is running, but \"%s\" does not exist.\n") % filename)+
                                  _("I'll guess at the port."))
                try:
                    self.port = CONTROL_SOCKET_PORT
                    self.send_command('no-op')
                    self.log(WARNING, _("Port found: %d") % self.port)
                    try:
                        f = open(filename, "w")
                        f.write(str(self.port))
                        f.close()
                    except:
                        traceback.print_exc()
                except:
                    # this is where this system falls down.
                    # There's another copy of BitTorrent running, or something locking the mutex,
                    # but I can't communicate with it.
                    self.log(WARNING, _("Could not find port."))
                
        
        # we're done reading the control file, release the mutex so other instances can lock it and read the file
        win32event.ReleaseMutex(self.mutex)

        return takeover        

    #blocking version without rawserver
    def send_command(self, command, *datas):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            s.connect(('127.0.0.1', self.port))
            s.send(tobinary(len(command)))
            s.send(command)
            for data in datas:
                s.send(tobinary(len(data)))
                s.send(data)
            s.close()
        except socket.error, e:
            try:
                s.close()
            except:
                pass
            raise BTFailure(_("Could not send command: ") + str(e))

   
    def stop(self):
        if self.master:
            r = win32event.WaitForSingleObject(self.mutex, win32event.INFINITE)
            filename = self._get_sic_path()
            try:
                os.remove(filename)
            except OSError, e:
                # print, but continue
                traceback.print_exc()
            self.master = 0
            win32event.ReleaseMutex(self.mutex)
            # close it so the named mutex goes away
            win32api.CloseHandle(self.mutex)
            self.mutex = None

if os.name == 'nt':
    class HandlerObject(pywin.mfc.object.Object):
        def __init__(self, handler, target):
            self.handler = handler
            pywin.mfc.object.Object.__init__(self, target)

    class Topic(HandlerObject):
        def __init__(self, handler, target):
            target.AddItem(dde.CreateStringItem(""))
            HandlerObject.__init__(self, handler, target)

        def Request(self, x):
            # null byte hack
            x = x.replace("\\**0", "\0")
            items = x.split("|")
            self.handler(items[0], *items[1:])
            return ("OK")

        # remote procedure call
        #def Exec(self, x):
        #    exec x

    class Server(HandlerObject):
        def __init__(self, log, *args):
            self.log = log
            HandlerObject.__init__(self, *args)

        def CreateSystemTopic(self):
            return Topic(self.handler, dde.CreateServerSystemTopic())

        def Status(self, s):
            #if self.log:
            #    self.log(INFO, _("IPC Status: %s") % s)
            pass

        def stop(self):
            self.Shutdown()
            self.Destroy()

class IPCWin32DDE(IPC):
    def create(self):
        self.server = None

        # try to connect first
        self.client = Server(None, None, dde.CreateServer())
        self.client.Create(app_name, dde.CBF_FAIL_SELFCONNECTIONS|dde.APPCMD_CLIENTONLY)
        self.conversation = dde.CreateConversation(self.client)
        try:
            self.conversation.ConnectTo(app_name, "controlsocket")
            raise BTFailure(_("DDE Conversation connected."))
        except dde.error, e:
            # no one is listening
            pass

        # clean up
        self.client.stop()
        del self.client
        del self.conversation

        # start server
        self.server = Server(self.log, self.handle_command, dde.CreateServer())
        self.server.Create(app_name, dde.CBF_FAIL_SELFCONNECTIONS|dde.APPCLASS_STANDARD)
        self.server.AddTopic(Topic(self.handle_command, dde.CreateTopic("controlsocket")))

    def send_command(self, command, *args):
        s = '|'.join([command, ] + list(args))
        # null byte hack
        if s.count("\0") > 0:
            self.log(WARNING, "IPC: String with null byte(s):" + s)
            s = s.replace("\0", "\\**0")
        result = self.conversation.Request(s)

    def stop(self):
        if self.server:
            server = self.server
            self.server = None
            server.stop()

if os.name == 'nt':
    #ipc_interface = IPCWin32Socket
    ipc_interface = IPCWin32DDE
else:
    ipc_interface = IPCUnixSocket