~bratsche/transmission/notify-actions

« back to all changes in this revision

Viewing changes to macosx/IPCController.m

  • Committer: Bazaar Package Importer
  • Date: 2008-10-31 13:11:43 UTC
  • Revision ID: jamesw@ubuntu.com-20081031131143-1xburb867kue4y07
Tags: upstream-ubuntu-1.32
ImportĀ upstreamĀ versionĀ 1.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/******************************************************************************
2
 
 * $Id: IPCController.m 5715 2008-04-28 18:09:17Z charles $
3
 
 *
4
 
 * Copyright (c) 2007-2008 Transmission authors and contributors
5
 
 *
6
 
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 
 * copy of this software and associated documentation files (the "Software"),
8
 
 * to deal in the Software without restriction, including without limitation
9
 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 
 * and/or sell copies of the Software, and to permit persons to whom the
11
 
 * Software is furnished to do so, subject to the following conditions:
12
 
 *
13
 
 * The above copyright notice and this permission notice shall be included in
14
 
 * all copies or substantial portions of the Software.
15
 
 *
16
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 
 * DEALINGS IN THE SOFTWARE.
23
 
 *****************************************************************************/
24
 
 
25
 
#include <sys/types.h>
26
 
#include <sys/socket.h>
27
 
#include <sys/un.h>
28
 
#include <assert.h>
29
 
#include <errno.h>
30
 
#include <string.h>
31
 
#include <unistd.h>
32
 
 
33
 
#include "bencode.h"
34
 
#include "ipcparse.h"
35
 
#include "transmission.h"
36
 
#include "utils.h"
37
 
 
38
 
#import "IPCController.h"
39
 
#import "Torrent.h"
40
 
#import "PrefsController.h"
41
 
 
42
 
static void
43
 
getaddr( struct sockaddr_un * );
44
 
static NSArray *
45
 
bencarray( benc_val_t * val, int type );
46
 
static void
47
 
msg_lookup   ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
48
 
static void
49
 
msg_info     ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
50
 
void
51
 
msg_infoall  ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
52
 
void
53
 
msg_action   ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
54
 
void
55
 
msg_actionall( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
56
 
void
57
 
msg_addold   ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
58
 
void
59
 
msg_addnew   ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
60
 
void
61
 
msg_getbool  ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
62
 
void
63
 
msg_getint   ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
64
 
void
65
 
msg_getstr   ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
66
 
void
67
 
msg_setbool  ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
68
 
void
69
 
msg_setint   ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
70
 
void
71
 
msg_setstr   ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
72
 
void
73
 
msg_empty    ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
74
 
void
75
 
msg_sup      ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
76
 
static void
77
 
msg_default  ( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg );
78
 
 
79
 
@interface IPCClient : NSObject
80
 
{
81
 
    NSFileHandle    * _handle;
82
 
    struct ipc_info * _ipc;
83
 
    IPCController   * _controller;
84
 
    NSMutableData   * _buf;
85
 
}
86
 
 
87
 
- (id)              initClient: (IPCController *) controller
88
 
                         funcs: (struct ipc_funcs *) funcs
89
 
                        handle: (NSFileHandle *) handle;
90
 
- (IPCController *) controller;
91
 
- (struct ipc_info *)      ipc;
92
 
- (void)               gotdata: (NSNotification *) notification;
93
 
- (BOOL)              sendresp: (uint8_t *) buf
94
 
                          size: (size_t) size;
95
 
- (BOOL)         sendrespEmpty: (enum ipc_msg) msgid
96
 
                           tag: (int64_t) tag;
97
 
- (BOOL)           sendrespInt: (enum ipc_msg) msgid
98
 
                           tag: (int64_t) tag
99
 
                           val: (int64_t) val;
100
 
- (BOOL)           sendrespStr: (enum ipc_msg) msgid
101
 
                           tag: (int64_t) tag
102
 
                           val: (NSString *) val;
103
 
- (void)          sendrespInfo: (enum ipc_msg) respid
104
 
                           tag: (int64_t) tag
105
 
                      torrents: (NSArray *) tors
106
 
                         types: (int) types;
107
 
 
108
 
@end
109
 
 
110
 
@interface IPCController (Private)
111
 
 
112
 
- (void)  newclient: (NSNotification *) notification;
113
 
- (void) killclient: (IPCClient *) client;
114
 
 
115
 
NSUserDefaults          * fDefaults;
116
 
PrefsController         * fPrefsController;
117
 
 
118
 
 
119
 
@end
120
 
 
121
 
@implementation IPCController
122
 
 
123
 
- (id) init
124
 
{
125
 
    struct sockaddr_un sun;
126
 
 
127
 
    self = [super init];
128
 
    if( nil == self )
129
 
        return nil;
130
 
 
131
 
    getaddr( &sun );
132
 
    unlink( sun.sun_path );
133
 
    _sock    = [[NSSocketPort alloc]
134
 
                   initWithProtocolFamily: PF_UNIX
135
 
                               socketType: SOCK_STREAM
136
 
                                 protocol: 0
137
 
                                  address: [NSData dataWithBytes: &sun
138
 
                                                          length: sizeof(sun)]];
139
 
    _listen  = [[NSFileHandle alloc]
140
 
                   initWithFileDescriptor: [_sock socket]
141
 
                           closeOnDealloc: YES];
142
 
    _funcs   = ipc_initmsgs();
143
 
    _clients = [[NSMutableArray alloc] init];
144
 
 
145
 
    /* XXX is this error checking bogus? */
146
 
    if( nil  == _sock    ||
147
 
        nil  == _listen  ||
148
 
        NULL == _funcs   ||
149
 
        nil  == _clients )
150
 
    {
151
 
        [self release];
152
 
        return nil;
153
 
    }
154
 
 
155
 
    ipc_addmsg( _funcs, IPC_MSG_ADDMANYFILES, msg_addold    );
156
 
    ipc_addmsg( _funcs, IPC_MSG_ADDONEFILE,   msg_addnew    );
157
 
    ipc_addmsg( _funcs, IPC_MSG_AUTOMAP,      msg_setbool   );
158
 
    ipc_addmsg( _funcs, IPC_MSG_AUTOSTART,    msg_setbool   );
159
 
    ipc_addmsg( _funcs, IPC_MSG_CRYPTO,       msg_setstr    );
160
 
    ipc_addmsg( _funcs, IPC_MSG_DIR,          msg_setstr    );
161
 
    ipc_addmsg( _funcs, IPC_MSG_DOWNLIMIT,    msg_setint    );
162
 
    ipc_addmsg( _funcs, IPC_MSG_GETAUTOMAP,   msg_getbool   );
163
 
    ipc_addmsg( _funcs, IPC_MSG_GETAUTOSTART, msg_getbool   );
164
 
    ipc_addmsg( _funcs, IPC_MSG_GETCRYPTO,    msg_getstr    );
165
 
    ipc_addmsg( _funcs, IPC_MSG_GETDIR,       msg_getstr    );
166
 
    ipc_addmsg( _funcs, IPC_MSG_GETDOWNLIMIT, msg_getint    );
167
 
    ipc_addmsg( _funcs, IPC_MSG_GETINFO,      msg_info      );
168
 
    ipc_addmsg( _funcs, IPC_MSG_GETINFOALL,   msg_infoall   );
169
 
    ipc_addmsg( _funcs, IPC_MSG_GETPEX,       msg_getbool   );
170
 
    ipc_addmsg( _funcs, IPC_MSG_GETPORT,      msg_getint    );
171
 
    ipc_addmsg( _funcs, IPC_MSG_GETSTAT,      msg_info      );
172
 
    ipc_addmsg( _funcs, IPC_MSG_GETSTATALL,   msg_infoall   );
173
 
    ipc_addmsg( _funcs, IPC_MSG_GETUPLIMIT,   msg_getint    );
174
 
    ipc_addmsg( _funcs, IPC_MSG_LOOKUP,       msg_lookup    );
175
 
    ipc_addmsg( _funcs, IPC_MSG_NOOP,         msg_empty     );
176
 
    ipc_addmsg( _funcs, IPC_MSG_PEX,          msg_setbool   );
177
 
    ipc_addmsg( _funcs, IPC_MSG_PORT,         msg_setint    );
178
 
    ipc_addmsg( _funcs, IPC_MSG_QUIT,         msg_empty     );
179
 
    ipc_addmsg( _funcs, IPC_MSG_REMOVE,       msg_action    );
180
 
    ipc_addmsg( _funcs, IPC_MSG_REMOVEALL,    msg_actionall );
181
 
    ipc_addmsg( _funcs, IPC_MSG_START,        msg_action    );
182
 
    ipc_addmsg( _funcs, IPC_MSG_STARTALL,     msg_actionall );
183
 
    ipc_addmsg( _funcs, IPC_MSG_STOP,         msg_action    );
184
 
    ipc_addmsg( _funcs, IPC_MSG_STOPALL,      msg_actionall );
185
 
    ipc_addmsg( _funcs, IPC_MSG_SUP,          msg_sup       );
186
 
    ipc_addmsg( _funcs, IPC_MSG_UPLIMIT,      msg_setint    );
187
 
    ipc_addmsg( _funcs, IPC_MSG_VERIFY,       msg_action    );
188
 
    ipc_setdefmsg( _funcs, msg_default );
189
 
 
190
 
    [[NSNotificationCenter defaultCenter]
191
 
        addObserver: self
192
 
           selector: @selector(newclient:)
193
 
               name: NSFileHandleConnectionAcceptedNotification
194
 
             object: _listen];
195
 
    [_listen acceptConnectionInBackgroundAndNotify];
196
 
 
197
 
    return self;
198
 
}
199
 
 
200
 
- (void) dealloc
201
 
{
202
 
    struct sockaddr_un sun;
203
 
 
204
 
    [[NSNotificationCenter defaultCenter] removeObserver: self];
205
 
    [_listen  release];
206
 
    [_sock    release];
207
 
    [_clients release];
208
 
    ipc_freemsgs( _funcs );
209
 
    getaddr( &sun );
210
 
    unlink( sun.sun_path );
211
 
    [super dealloc];
212
 
}
213
 
 
214
 
- (id) delegate
215
 
{
216
 
    return _delegate;
217
 
}
218
 
 
219
 
- (void) setDelegate: (id) newdelegate
220
 
{
221
 
    _delegate = newdelegate;
222
 
}
223
 
 
224
 
- (void) setPrefsController: (id) thePrefsController
225
 
{
226
 
    fPrefsController = thePrefsController;
227
 
}
228
 
 
229
 
@end
230
 
 
231
 
@implementation IPCController (Private)
232
 
 
233
 
- (void) newclient: (NSNotification *) notification
234
 
{
235
 
    NSDictionary * info;
236
 
    NSFileHandle * handle;
237
 
    NSNumber     * error;
238
 
    IPCClient    * client;
239
 
    
240
 
 
241
 
    info   = [notification userInfo];
242
 
    handle = [info objectForKey: NSFileHandleNotificationFileHandleItem];
243
 
    error  = [info objectForKey: @"NSFileHandleError"];
244
 
 
245
 
    if( nil != error )
246
 
    {
247
 
        NSLog( @"Failed to accept IPC socket connection: %@", error );
248
 
        return;
249
 
    }
250
 
 
251
 
    [_listen acceptConnectionInBackgroundAndNotify];
252
 
 
253
 
    if( nil == handle )
254
 
        return;
255
 
 
256
 
    client = [[IPCClient alloc]
257
 
                 initClient: self
258
 
                      funcs: _funcs
259
 
                     handle: handle];
260
 
    if( nil == client )
261
 
        return;
262
 
 
263
 
    [_clients addObject:client];
264
 
    [client release];
265
 
}
266
 
 
267
 
- (void) killclient: (IPCClient *) client
268
 
{
269
 
    [_clients removeObject: client];
270
 
}
271
 
 
272
 
@end
273
 
 
274
 
@implementation IPCClient
275
 
 
276
 
- (id) initClient: (IPCController *) controller
277
 
            funcs: (struct ipc_funcs *) funcs
278
 
           handle: (NSFileHandle *) handle
279
 
{
280
 
    uint8_t * buf;
281
 
    size_t    size;
282
 
 
283
 
    self = [super init];
284
 
    if( nil == self )
285
 
        return nil;
286
 
 
287
 
    _handle     = [handle retain];
288
 
    _ipc        = ipc_newcon( funcs );
289
 
    _controller = controller;
290
 
    _buf        = [[NSMutableData alloc] init];
291
 
 
292
 
    buf = ipc_mkvers( &size, "Transmission Mac OS X " LONG_VERSION_STRING );
293
 
    if( NULL == _ipc || nil == _buf || NULL == buf ||
294
 
        ![self sendresp: buf size: size] )
295
 
    {
296
 
        [self release];
297
 
        return nil;
298
 
    }
299
 
 
300
 
    [[NSNotificationCenter defaultCenter]
301
 
        addObserver: self
302
 
           selector: @selector(gotdata:)
303
 
               name: NSFileHandleReadCompletionNotification
304
 
             object: _handle];
305
 
    [_handle readInBackgroundAndNotify];
306
 
 
307
 
    return self;
308
 
}
309
 
 
310
 
- (void) dealloc
311
 
{
312
 
    [[NSNotificationCenter defaultCenter] removeObserver: self];
313
 
    [_handle release];
314
 
    [_buf    release];
315
 
    ipc_freecon( _ipc );
316
 
    [super   dealloc];
317
 
}
318
 
 
319
 
- (IPCController *) controller
320
 
{
321
 
    return _controller;
322
 
}
323
 
 
324
 
- (struct ipc_info *) ipc
325
 
{
326
 
    return _ipc;
327
 
}
328
 
 
329
 
- (void) gotdata: (NSNotification *) notification
330
 
{
331
 
    NSDictionary * info;
332
 
    NSData       * data;
333
 
    NSNumber     * error;
334
 
    ssize_t        res;
335
 
 
336
 
    info  = [notification userInfo];
337
 
    data  = [info objectForKey: NSFileHandleNotificationDataItem];
338
 
    error = [info objectForKey: @"NSFileHandleError"];
339
 
 
340
 
    if( nil != error )
341
 
    {
342
 
        NSLog( @"Failed to read from IPC socket connection: %@", error );
343
 
        [_controller killclient: self];
344
 
        return;
345
 
    }
346
 
 
347
 
    if( nil == data || 0 == [data length] )
348
 
    {
349
 
        [_controller killclient: self];
350
 
        return;
351
 
    }
352
 
 
353
 
    [_handle readInBackgroundAndNotify];
354
 
    [_buf appendData: data];
355
 
 
356
 
    if( IPC_MIN_MSG_LEN > [_buf length] )
357
 
        return;
358
 
 
359
 
    res = ipc_handleMessages( _ipc, [_buf mutableBytes], [_buf length], self );
360
 
 
361
 
    if( 0 > res )
362
 
    {
363
 
        switch( errno )
364
 
        {
365
 
            case EPERM:
366
 
                NSLog( @"IPC client has unsupported protocol version" );
367
 
                break;
368
 
            case EINVAL:
369
 
                NSLog( @"IPC protocol parse error" );
370
 
                break;
371
 
            default:
372
 
                NSLog( @"IPC parsing failed" );
373
 
                break;
374
 
        }
375
 
        [_controller killclient: self];
376
 
        return;
377
 
    }
378
 
    else if( 0 < res )
379
 
    {
380
 
        assert( res <= [_buf length]);
381
 
        if( res < [_buf length])
382
 
        {
383
 
            memmove( [_buf mutableBytes], [_buf bytes] + res,
384
 
                     [_buf length] - res );
385
 
        }
386
 
        [_buf setLength: ([_buf length] - res)];
387
 
    }
388
 
}
389
 
 
390
 
- (BOOL) sendresp: (uint8_t *) buf
391
 
             size: (size_t) size
392
 
{
393
 
    @try
394
 
    {
395
 
        [_handle writeData: [NSData dataWithBytesNoCopy: buf
396
 
                                                 length: size
397
 
                                           freeWhenDone: YES]];
398
 
        return YES;
399
 
    }
400
 
    @catch( NSException * ex )
401
 
    {
402
 
        NSLog( @"Failed to write to IPC socket connection" );
403
 
        return NO;
404
 
    }
405
 
}
406
 
 
407
 
- (BOOL) sendrespEmpty: (enum ipc_msg) msgid
408
 
                   tag: (int64_t) tag
409
 
{
410
 
    uint8_t * buf;
411
 
    size_t    size;
412
 
 
413
 
    buf = ipc_mkempty( _ipc, &size, msgid, tag );
414
 
    if( NULL == buf )
415
 
        return NO;
416
 
 
417
 
    return [self sendresp: buf
418
 
                     size: size];
419
 
}
420
 
 
421
 
- (BOOL) sendrespInt: (enum ipc_msg) msgid
422
 
                 tag: (int64_t) tag
423
 
                 val: (int64_t) val
424
 
{
425
 
    uint8_t * buf;
426
 
    size_t    size;
427
 
 
428
 
    buf = ipc_mkint( _ipc, &size, msgid, tag, val );
429
 
    if( NULL == buf )
430
 
        return NO;
431
 
 
432
 
    return [self sendresp: buf
433
 
                     size: size];
434
 
}
435
 
 
436
 
- (BOOL) sendrespStr: (enum ipc_msg) msgid
437
 
                 tag: (int64_t) tag
438
 
                 val: (NSString *) val
439
 
{
440
 
    uint8_t       * buf;
441
 
    size_t          size;
442
 
    NSData        * data;
443
 
    NSMutableData * sucky;
444
 
 
445
 
    if( [val canBeConvertedToEncoding: NSUTF8StringEncoding] )
446
 
        buf = ipc_mkstr( _ipc, &size, msgid, tag,
447
 
                         [val cStringUsingEncoding: NSUTF8StringEncoding] );
448
 
    else
449
 
    {
450
 
        data = [val dataUsingEncoding: NSUTF8StringEncoding
451
 
                 allowLossyConversion: YES];
452
 
        /* XXX this sucks, I should add a length argument to ipc_mkstr() */
453
 
        sucky = [NSMutableData dataWithData: data];
454
 
        [sucky appendBytes: "" length: 1];
455
 
        buf = ipc_mkstr( _ipc, &size, msgid, tag, [sucky bytes] );
456
 
    }
457
 
    if( NULL == buf )
458
 
        return NO;
459
 
 
460
 
    return [self sendresp: buf
461
 
                     size: size];
462
 
}
463
 
 
464
 
- (void) sendrespInfo: (enum ipc_msg) respid
465
 
                  tag: (int64_t) tag
466
 
             torrents: (NSArray *) tors
467
 
                types: (int) types
468
 
{
469
 
    benc_val_t     packet, * pkinf;
470
 
    NSEnumerator * enumerator;
471
 
    Torrent      * tor;
472
 
    uint8_t      * buf;
473
 
    size_t         size;
474
 
    int            res;
475
 
 
476
 
    pkinf = ipc_initval( _ipc, respid, tag, &packet, TYPE_LIST );
477
 
    if( NULL == pkinf )
478
 
        goto fail;
479
 
    if( tr_bencListReserve( pkinf, [tors count] ) )
480
 
    {
481
 
        tr_bencFree( &packet );
482
 
        goto fail;
483
 
    }
484
 
 
485
 
    enumerator = [tors objectEnumerator];
486
 
    while( nil != ( tor = [enumerator nextObject] ) )
487
 
    {
488
 
        if( IPC_MSG_INFO == respid )
489
 
            res = ipc_addinfo( pkinf, [tor torrentID], [tor torrentStruct], types );
490
 
        else
491
 
            res = ipc_addstat( pkinf, [tor torrentID], [tor torrentStruct], types );
492
 
        if( 0 > res )
493
 
        {
494
 
            tr_bencFree( &packet );
495
 
            goto fail;
496
 
        }
497
 
    }
498
 
 
499
 
    buf = ipc_serialize( &packet, &size );
500
 
    tr_bencFree( &packet );
501
 
    if( NULL == buf )
502
 
        goto fail;
503
 
    [self sendresp: buf size: size ];
504
 
    return;
505
 
 
506
 
  fail:
507
 
    NSLog( @"Failed to create IPC reply packet" );
508
 
    [_controller killclient: self];
509
 
}
510
 
 
511
 
 
512
 
@end
513
 
 
514
 
void getaddr( struct sockaddr_un * sun )
515
 
{
516
 
    bzero( sun, sizeof *sun );
517
 
    sun->sun_family = AF_LOCAL;
518
 
    tr_buildPath( sun->sun_path, sizeof( sun->sun_path ), tr_getDefaultConfigDir(), "socket", NULL );
519
 
}
520
 
 
521
 
NSArray * bencarray( benc_val_t * val, int type )
522
 
{
523
 
    int              ii;
524
 
    NSMutableArray * ret;
525
 
    benc_val_t     * item;
526
 
 
527
 
    assert( TYPE_STR == type || TYPE_INT == type );
528
 
 
529
 
    if( NULL == val || TYPE_LIST != val->type )
530
 
        return nil;
531
 
 
532
 
    ret = [NSMutableArray arrayWithCapacity: val->val.l.count];
533
 
    for( ii = 0; ii < val->val.l.count; ii++ )
534
 
    {
535
 
        item = &val->val.l.vals[ii];
536
 
        if( type != item->type )
537
 
            return nil;
538
 
        if( TYPE_STR == type )
539
 
        {
540
 
            [ret addObject: [NSString stringWithCString: item->val.s.s
541
 
                                               encoding: NSUTF8StringEncoding]];
542
 
        }
543
 
        else
544
 
        {
545
 
            [ret addObject: [NSNumber numberWithLongLong: (long long) item->val.i]];
546
 
        }
547
 
    }
548
 
 
549
 
    return ret;
550
 
}
551
 
 
552
 
void msg_lookup( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
553
 
{
554
 
    IPCClient      * client = arg;
555
 
    NSArray        * hashes, * tors;
556
 
 
557
 
    hashes = bencarray( val, TYPE_STR );
558
 
    if( NULL == hashes )
559
 
    {
560
 
        [client sendrespEmpty: IPC_MSG_BAD tag: tag];
561
 
        return;
562
 
    }
563
 
 
564
 
    tors  = [[[client controller] delegate] ipcGetTorrentsByHash: hashes];
565
 
    [client sendrespInfo: IPC_MSG_INFO
566
 
                     tag: tag
567
 
                torrents: tors
568
 
                   types: IPC_INF_HASH];
569
 
}
570
 
 
571
 
void msg_info( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
572
 
{
573
 
    IPCClient  * client = arg;
574
 
    enum ipc_msg respid;
575
 
    benc_val_t * typesval;
576
 
    int          types;
577
 
    NSArray    * ids, * tors;
578
 
 
579
 
    if( NULL == val || TYPE_DICT != val->type )
580
 
        goto bad;
581
 
 
582
 
    typesval = tr_bencDictFind( val, "type" );
583
 
    if( NULL == typesval || TYPE_LIST != typesval->type ||
584
 
        nil == ( ids = bencarray( tr_bencDictFind( val, "id" ), TYPE_INT ) ) )
585
 
        goto bad;
586
 
 
587
 
    respid = ( IPC_MSG_GETINFO == msgid ? IPC_MSG_INFO : IPC_MSG_STAT );
588
 
    tors   = [[[client controller] delegate] ipcGetTorrentsByID: ids];
589
 
    types  = ipc_infotypes( respid, typesval );
590
 
    [client sendrespInfo: respid tag: tag torrents: tors types: types];
591
 
    return;
592
 
 
593
 
  bad:
594
 
    NSLog( @"Got bad IPC packet" );
595
 
    [client sendrespEmpty: IPC_MSG_BAD tag: tag];
596
 
}
597
 
 
598
 
void msg_infoall( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
599
 
{
600
 
    IPCClient  * client = arg;
601
 
    enum ipc_msg respid;
602
 
    int          types;
603
 
    NSArray    * tors;
604
 
 
605
 
    if( NULL == val || TYPE_LIST != val->type )
606
 
    {
607
 
        NSLog( @"Got bad IPC packet" );
608
 
        [client sendrespEmpty: IPC_MSG_BAD tag: tag];
609
 
        return;
610
 
    }
611
 
 
612
 
    respid = ( IPC_MSG_GETINFOALL == msgid ? IPC_MSG_INFO : IPC_MSG_STAT );
613
 
    tors   = [[[client controller] delegate] ipcGetTorrentsByID: nil];
614
 
    types  = ipc_infotypes( respid, val );
615
 
    [client sendrespInfo: respid tag: tag torrents: tors types: types];
616
 
}
617
 
 
618
 
void msg_action( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
619
 
{
620
 
    IPCClient * client = arg;
621
 
    BOOL        res;
622
 
    NSArray   * ids, * tors;
623
 
    id          delegate;
624
 
 
625
 
    ids = bencarray( val, TYPE_INT );
626
 
    if( nil == ids )
627
 
    {
628
 
        NSLog( @"Got bad IPC packet" );
629
 
        [client sendrespEmpty: IPC_MSG_BAD tag: tag];
630
 
        return;
631
 
    }
632
 
 
633
 
    delegate = [[client controller] delegate];
634
 
    tors     = [delegate ipcGetTorrentsByID: ids];
635
 
    switch( msgid )
636
 
    {
637
 
        case IPC_MSG_REMOVE:
638
 
            res = [delegate ipcRemoveTorrents: tors];
639
 
            break;
640
 
        case IPC_MSG_START:
641
 
            res = [delegate ipcStartTorrents: tors];
642
 
            break;
643
 
        case IPC_MSG_STOP:
644
 
            res = [delegate ipcStopTorrents: tors];
645
 
            break;
646
 
        case IPC_MSG_VERIFY:
647
 
            res = [delegate ipcVerifyTorrents: tors];
648
 
            break;
649
 
        default:
650
 
            assert( 0 );
651
 
            return;
652
 
    }
653
 
 
654
 
    if( res )
655
 
        [client sendrespEmpty: IPC_MSG_OK tag: tag];
656
 
    else
657
 
        [client sendrespEmpty: IPC_MSG_FAIL tag: tag];
658
 
}
659
 
 
660
 
void msg_actionall( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
661
 
{
662
 
    IPCClient * client = arg;
663
 
    BOOL        res;
664
 
    NSArray   * tors;
665
 
    id          delegate;
666
 
 
667
 
    delegate = [[client controller] delegate];
668
 
    tors     = [delegate ipcGetTorrentsByID: nil];
669
 
    switch( msgid )
670
 
    {
671
 
        case IPC_MSG_REMOVEALL:
672
 
            res = [delegate ipcRemoveTorrents: tors];
673
 
            break;
674
 
        case IPC_MSG_STARTALL:
675
 
            res = [delegate ipcStartTorrents: tors];
676
 
            break;
677
 
        case IPC_MSG_STOPALL:
678
 
            res = [delegate ipcStopTorrents: tors];
679
 
            break;
680
 
        default:
681
 
            assert( 0 );
682
 
            return;
683
 
    }
684
 
 
685
 
    if( res )
686
 
        [client sendrespEmpty: IPC_MSG_OK tag: tag];
687
 
    else
688
 
        [client sendrespEmpty: IPC_MSG_FAIL tag: tag];
689
 
}
690
 
 
691
 
void msg_addold( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
692
 
{
693
 
    IPCClient * client = arg;
694
 
    NSArray   * paths;
695
 
 
696
 
    paths = bencarray( val, TYPE_STR );
697
 
    if( nil == paths )
698
 
    {
699
 
        NSLog( @"Got bad IPC packet" );
700
 
        [client sendrespEmpty: IPC_MSG_BAD tag: tag];
701
 
        return;
702
 
    }
703
 
 
704
 
    /* XXX should send back info message with torrent IDs */
705
 
    if( [[[client controller] delegate] ipcAddTorrents: paths] )
706
 
        [client sendrespEmpty: IPC_MSG_OK tag: tag];
707
 
    else
708
 
        [client sendrespEmpty: IPC_MSG_FAIL tag: tag];
709
 
}
710
 
 
711
 
void msg_addnew( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
712
 
{
713
 
    IPCClient  * client = arg;
714
 
    benc_val_t * fileval, * dataval, * dirval, * autoval;
715
 
    NSString   * file, * dir;
716
 
    NSData     * data;
717
 
    BOOL         autobool, res;
718
 
 
719
 
    if( NULL == val || TYPE_DICT != val->type )
720
 
        goto bad;
721
 
 
722
 
    fileval = tr_bencDictFind( val, "file" );
723
 
    dataval = tr_bencDictFind( val, "data" );
724
 
    dirval  = tr_bencDictFind( val, "directory" );
725
 
    autoval = tr_bencDictFind( val, "autostart" );
726
 
    if( ( ( NULL == fileval || TYPE_STR != fileval->type )   &&
727
 
          ( NULL == dataval || TYPE_STR != dataval->type ) ) ||
728
 
        ( ( NULL != fileval && TYPE_STR == fileval->type )   &&
729
 
          ( NULL != dataval && TYPE_STR == dataval->type ) ) ||
730
 
          ( NULL != dirval  && TYPE_STR != dirval->type  )   ||
731
 
          ( NULL != autoval && TYPE_INT != autoval->type ) )
732
 
        goto bad;
733
 
 
734
 
    dir = ( NULL == dirval ? nil :
735
 
            [NSString stringWithCString: dirval->val.s.s
736
 
                               encoding: NSUTF8StringEncoding] );
737
 
    autobool = ( NULL == autoval || 0 == autoval->val.i ? NO : YES );
738
 
 
739
 
    if( NULL != fileval )
740
 
    {
741
 
        file = [NSString stringWithCString: fileval->val.s.s
742
 
                                  encoding: NSUTF8StringEncoding];
743
 
        if( NULL == autoval )
744
 
            res = [[[client controller] delegate]
745
 
                      ipcAddTorrentFile: file
746
 
                              directory: dir];
747
 
        else
748
 
            res = [[[client controller] delegate]
749
 
                      ipcAddTorrentFileAutostart: file
750
 
                                       directory: dir
751
 
                                       autostart: autobool];
752
 
    }
753
 
    else
754
 
    {
755
 
        data = [NSData dataWithBytes: dataval->val.s.s
756
 
                              length: dataval->val.s.i];
757
 
        if( NULL == autoval )
758
 
            res = [[[client controller] delegate]
759
 
                      ipcAddTorrentData: data
760
 
                              directory: dir];
761
 
        else
762
 
            res = [[[client controller] delegate]
763
 
                      ipcAddTorrentDataAutostart: data
764
 
                                       directory: dir
765
 
                                       autostart: autobool];
766
 
    }
767
 
 
768
 
    /* XXX should send back info message with torrent ID */
769
 
    if( res )
770
 
        [client sendrespEmpty: IPC_MSG_OK tag: tag];
771
 
    else
772
 
        [client sendrespEmpty: IPC_MSG_FAIL tag: tag];
773
 
    return;
774
 
 
775
 
  bad:
776
 
    NSLog( @"Got bad IPC packet" );
777
 
    [client sendrespEmpty: IPC_MSG_BAD tag: tag];
778
 
}
779
 
 
780
 
void msg_getbool( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
781
 
{
782
 
    IPCClient * client = arg;
783
 
    
784
 
    fDefaults = [NSUserDefaults standardUserDefaults];
785
 
 
786
 
    switch( msgid )
787
 
    {
788
 
        case IPC_MSG_GETAUTOMAP:
789
 
            [client sendrespInt:IPC_MSG_AUTOMAP tag:tag val:[fDefaults boolForKey:@"NatTraversal"]];
790
 
            break;
791
 
        case IPC_MSG_GETAUTOSTART:
792
 
            [client sendrespInt:IPC_MSG_AUTOSTART tag:tag val:[fDefaults boolForKey:@"AutoStartDownload"]];
793
 
            break;
794
 
        case IPC_MSG_GETPEX:
795
 
            [client sendrespInt:IPC_MSG_PEX tag:tag val:[fDefaults boolForKey:@"PEXGlobal"]];
796
 
            break;
797
 
        default:
798
 
            assert( 0 );
799
 
            break;
800
 
    }
801
 
}
802
 
 
803
 
void msg_getint( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
804
 
{
805
 
    IPCClient * client = arg;
806
 
    
807
 
    fDefaults = [NSUserDefaults standardUserDefaults];
808
 
    
809
 
    int theValue;
810
 
    
811
 
    switch( msgid )
812
 
    {
813
 
        case IPC_MSG_GETDOWNLIMIT:
814
 
            if ( [fDefaults boolForKey:@"CheckDownload"] )
815
 
                theValue = [fDefaults integerForKey:@"DownloadLimit"];
816
 
            else
817
 
                theValue = -1;
818
 
            [client sendrespInt:IPC_MSG_DOWNLIMIT tag:tag val:theValue];
819
 
            break;
820
 
        case IPC_MSG_GETPORT:
821
 
            [client sendrespInt:IPC_MSG_PORT tag:tag val:[fDefaults integerForKey:@"BindPort"]];
822
 
            break;
823
 
        case IPC_MSG_GETUPLIMIT:
824
 
            if ( [fDefaults boolForKey:@"CheckUpload"] )
825
 
                theValue = [fDefaults integerForKey:@"UploadLimit"];
826
 
            else
827
 
                theValue = -1;
828
 
            [client sendrespInt:IPC_MSG_UPLIMIT tag:tag val:theValue];
829
 
            break;
830
 
        default:
831
 
            assert( 0 );
832
 
            break;
833
 
    }
834
 
}
835
 
 
836
 
void msg_getstr( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
837
 
{
838
 
    IPCClient * client = arg;
839
 
    NSString    * cryptoValue;
840
 
 
841
 
    fDefaults = [NSUserDefaults standardUserDefaults];
842
 
    
843
 
    switch( msgid )
844
 
    {
845
 
            
846
 
        case IPC_MSG_GETDIR:
847
 
            [client sendrespStr:IPC_MSG_DIR tag:tag val:[fDefaults stringForKey:@"DownloadFolder"]];
848
 
            break;
849
 
            
850
 
        case IPC_MSG_GETCRYPTO:
851
 
            if ([fDefaults boolForKey: @"EncryptionPrefer"])
852
 
                if ([fDefaults boolForKey: @"EncryptionRequire"])
853
 
                    cryptoValue = @"required";
854
 
                else
855
 
                    cryptoValue = @"preferred";
856
 
            else
857
 
                cryptoValue = @"plaintext";
858
 
            
859
 
            [client sendrespStr:IPC_MSG_CRYPTO tag:tag val:cryptoValue];
860
 
            break;
861
 
            
862
 
        default:
863
 
            assert( 0 );
864
 
            break;
865
 
    }
866
 
}
867
 
 
868
 
void msg_setbool( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
869
 
{
870
 
    IPCClient * client = arg;
871
 
 
872
 
    if( NULL == val || TYPE_INT != val->type )
873
 
    {
874
 
        NSLog( @"Got bad IPC packet" );
875
 
        [client sendrespEmpty: IPC_MSG_BAD tag: tag];
876
 
        return;
877
 
    }
878
 
    
879
 
    fDefaults = [NSUserDefaults standardUserDefaults];
880
 
    
881
 
    switch( msgid )
882
 
    {
883
 
        case IPC_MSG_AUTOMAP:
884
 
            [fDefaults setBool:(bool)val->val.i forKey:@"NatTraversal"];
885
 
            [fPrefsController setNat:nil];
886
 
            [client sendrespEmpty:IPC_MSG_OK tag:tag];
887
 
            break;
888
 
        case IPC_MSG_AUTOSTART:
889
 
            [fDefaults setBool:(bool)val->val.i forKey:@"AutoStartDownload"];
890
 
            [client sendrespEmpty:IPC_MSG_OK tag:tag];
891
 
            break;
892
 
        case IPC_MSG_PEX:
893
 
            [fDefaults setBool:(bool)val->val.i forKey:@"PEXGlobal"];
894
 
            [fPrefsController setPEX:nil];
895
 
            [client sendrespEmpty: IPC_MSG_OK tag: tag];
896
 
            break;
897
 
        default:
898
 
            assert( 0 );
899
 
            break;
900
 
    }
901
 
}
902
 
 
903
 
void msg_setint( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
904
 
{
905
 
    IPCClient * client = arg;
906
 
 
907
 
    if( NULL == val || TYPE_INT != val->type )
908
 
    {
909
 
        NSLog( @"Got bad IPC packet" );
910
 
        [client sendrespEmpty: IPC_MSG_BAD tag: tag];
911
 
        return;
912
 
    }
913
 
    
914
 
    fDefaults = [NSUserDefaults standardUserDefaults];
915
 
    switch( msgid )
916
 
    {
917
 
        case IPC_MSG_DOWNLIMIT:
918
 
            if ( val->val.i < 0 )
919
 
                [fDefaults setBool:NO forKey:@"CheckDownload"];
920
 
            else
921
 
            {
922
 
                [fDefaults setBool:YES forKey:@"CheckDownload"];
923
 
                [fDefaults setInteger:val->val.i forKey:@"DownloadLimit"];
924
 
            }
925
 
            [fPrefsController updateLimitFields];
926
 
            [fPrefsController applySpeedSettings: nil];
927
 
            break;
928
 
        case IPC_MSG_PORT:
929
 
            [fPrefsController setPort:[NSNumber numberWithInt:val->val.i]];
930
 
            [fPrefsController updatePortField];
931
 
            break;
932
 
        case IPC_MSG_UPLIMIT:
933
 
            if ( val->val.i < 0 )
934
 
                [fDefaults setBool:NO forKey:@"CheckUpload"];
935
 
            else
936
 
            {
937
 
                [fDefaults setBool:YES forKey:@"CheckUpload"];
938
 
                [fDefaults setInteger:val->val.i forKey:@"UploadLimit"];
939
 
            }
940
 
            [fPrefsController updateLimitFields];
941
 
            [fPrefsController applySpeedSettings: nil];
942
 
            break;
943
 
        default:
944
 
            assert( 0 );
945
 
            break;
946
 
    }
947
 
    [client sendrespEmpty:IPC_MSG_OK tag:tag];
948
 
}
949
 
 
950
 
void msg_setstr( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
951
 
{
952
 
    IPCClient * client = arg;
953
 
 
954
 
    if( NULL == val || TYPE_STR != val->type )
955
 
    {
956
 
        NSLog( @"Got bad IPC packet" );
957
 
        [client sendrespEmpty: IPC_MSG_BAD tag: tag];
958
 
        return;
959
 
    }
960
 
             
961
 
    fDefaults = [NSUserDefaults standardUserDefaults];
962
 
             
963
 
    switch( msgid )
964
 
    {
965
 
        case IPC_MSG_DIR:
966
 
             [fDefaults setObject:[NSString stringWithCString: val->val.s.s] forKey:@"DownloadFolder"];
967
 
             [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
968
 
             [client sendrespEmpty: IPC_MSG_OK tag: tag];
969
 
             break;
970
 
            
971
 
        case IPC_MSG_CRYPTO:
972
 
            if(!strcasecmp(val->val.s.s, "required"))
973
 
            {
974
 
                [fDefaults setBool:YES  forKey: @"EncryptionPrefer"];
975
 
                [fDefaults setBool:YES  forKey: @"EncryptionRequire"];
976
 
            }
977
 
            
978
 
            else if(!strcasecmp(val->val.s.s, "preferred"))
979
 
            {
980
 
                [fDefaults setBool:YES  forKey: @"EncryptionPrefer"];
981
 
                [fDefaults setBool:NO  forKey: @"EncryptionRequire"];
982
 
            }
983
 
            
984
 
            else if(!strcasecmp(val->val.s.s, "plaintext"))
985
 
            {
986
 
                [fDefaults setBool:NO  forKey: @"EncryptionPrefer"];
987
 
                [fDefaults setBool:NO  forKey: @"EncryptionRequire"];
988
 
            }
989
 
            [fPrefsController setEncryptionMode:nil];
990
 
            [client sendrespEmpty: IPC_MSG_OK tag: tag];
991
 
            break;
992
 
           
993
 
        default:
994
 
             assert( 0 );
995
 
             break;
996
 
    }
997
 
}
998
 
 
999
 
void msg_empty( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
1000
 
{
1001
 
    IPCClient * client = arg;
1002
 
 
1003
 
    switch( msgid )
1004
 
    {
1005
 
        case IPC_MSG_NOOP:
1006
 
            [client sendrespEmpty: IPC_MSG_OK tag: tag];
1007
 
            break;
1008
 
        case IPC_MSG_QUIT:
1009
 
            [[[client controller] delegate] ipcQuit];
1010
 
            [client sendrespEmpty: IPC_MSG_OK tag: tag];
1011
 
            break;
1012
 
        default:
1013
 
            assert( 0 );
1014
 
            break;
1015
 
    }
1016
 
}
1017
 
 
1018
 
void msg_sup( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
1019
 
{
1020
 
    IPCClient       * client = arg;
1021
 
    benc_val_t        packet, * pkval, * name;
1022
 
    struct ipc_info * ipc;
1023
 
    int               ii;
1024
 
    enum ipc_msg      found;
1025
 
    uint8_t         * buf;
1026
 
    size_t            size;
1027
 
 
1028
 
    if( NULL == val || TYPE_LIST != val->type )
1029
 
        goto bad;
1030
 
 
1031
 
    ipc   = [client ipc];
1032
 
    pkval = ipc_initval( ipc, IPC_MSG_SUP, tag, &packet, TYPE_LIST );
1033
 
    if( NULL == pkval )
1034
 
        goto fail;
1035
 
    if( tr_bencListReserve( pkval, val->val.l.count ) )
1036
 
    {
1037
 
        tr_bencFree( &packet );
1038
 
        goto fail;
1039
 
    }
1040
 
 
1041
 
    for( ii = 0; val->val.l.count > ii; ii++ )
1042
 
    {
1043
 
        name = &val->val.l.vals[ii];
1044
 
        if( NULL == name || TYPE_STR != name->type )
1045
 
            goto bad;
1046
 
        found = ipc_msgid( ipc, name->val.s.s );
1047
 
        if( IPC__MSG_COUNT == found || !ipc_ishandled( ipc, found ) )
1048
 
        {
1049
 
            continue;
1050
 
        }
1051
 
        tr_bencInitStr( tr_bencListAdd( pkval ),
1052
 
                        name->val.s.s, name->val.s.i, 1 );
1053
 
    }
1054
 
 
1055
 
    buf = ipc_serialize( &packet, &size );
1056
 
    tr_bencFree( &packet );
1057
 
    if( NULL == buf )
1058
 
        goto fail;
1059
 
    [client sendresp: buf size: size ];
1060
 
    return;
1061
 
 
1062
 
  bad:
1063
 
    NSLog( @"Got bad IPC packet" );
1064
 
    [client sendrespEmpty: IPC_MSG_BAD tag: tag];
1065
 
    return;
1066
 
 
1067
 
  fail:
1068
 
    NSLog( @"Failed to create IPC reply packet" );
1069
 
    [[client controller] killclient: client];
1070
 
}
1071
 
 
1072
 
void msg_default( enum ipc_msg msgid, benc_val_t * val, int64_t tag, void * arg )
1073
 
{
1074
 
    IPCClient * client = arg;
1075
 
 
1076
 
    [client sendrespEmpty: IPC_MSG_NOTSUP tag: tag];
1077
 
}