-
Notifications
You must be signed in to change notification settings - Fork 13
/
ServerChild.m
462 lines (376 loc) · 14.4 KB
/
ServerChild.m
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* The Cheat - The legendary universal game trainer for Mac OS X.
* http://www.brokenzipper.com/trac/wiki/TheCheat
*
* Copyright (c) 2003-2011, Charles McGarvey et al.
*
* Distributable under the terms and conditions of the 2-clause BSD
* license; see the file COPYING for the legal text of the license.
*/
#import "ServerChild.h"
@interface ServerChild ( PrivateAPI )
- (void)_handlePacket;
- (void)_setClient:(NSString *)status;
@end
@implementation ServerChild
- (id)initWithSocket:(MySocket *)sock
{
return [self initWithSocket:sock delegate:nil];
}
- (id)initWithSocket:(MySocket *)sock delegate:(id)delegate
{
if ( self = [super init] ) {
_socket = [sock retain];
[_socket setDelegate:self];
_delegate = delegate;
// create the cheater object
_cheater = [[LocalCheater alloc] initWithDelegate:self];
[_cheater setShouldCopy:NO];
// start reading from the socket
[_socket readDataToLength:sizeof(TCPacketHeader) tag:0];
if ( [_delegate respondsToSelector:@selector(serverChildConnected:)] ) {
[_delegate serverChildConnected:self];
}
}
return self;
}
- (void)dealloc
{
//[_socket setDelegate:[self class]];
//[_socket disconnect];
[_socket setDelegate:nil];
[_socket release];
[_client release];
[_parameters release];
[_cheater release];
[super dealloc];
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#pragma mark MySocketDelegate
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
- (void)socket:(MySocket *)mySocket didReadData:(NSData *)theData tag:(int)tag
{
if ( tag == 0 && [theData length] == sizeof(TCPacketHeader) ) {
// got a packet header
memcpy( &_header, [theData bytes], sizeof(TCPacketHeader) );
if ( _header.size > 0 ) {
// request the rest of the packet
[mySocket readDataToLength:_header.size tag:1];
}
else {
[self _handlePacket];
// start reading the next packet
[mySocket readDataToLength:sizeof(TCPacketHeader) tag:0];
}
}
else if ( tag == 1 && [theData length] == _header.size ) {
// got packet parameter data
_parameters = [theData retain];
[self _handlePacket];
// start reading the next packet
[mySocket readDataToLength:sizeof(TCPacketHeader) tag:0];
}
else {
ChazLog( @"ServerChild - read unexpected data, disconnecting..." );
[_socket release];
_socket = nil;
if ( [_delegate respondsToSelector:@selector(serverChildDisconnected:)] ) {
[_delegate serverChildDisconnected:self];
}
}
}
- (void)socketDidDisconnect:(MySocket *)mySocket
{
[_socket release];
_socket = nil;
if ( [_delegate respondsToSelector:@selector(serverChildDisconnected:)] ) {
[_delegate serverChildDisconnected:self];
}
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#pragma mark Accessors
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
- (NSString *)host
{
return [_socket remoteHost];
}
- (NSString *)transfer
{
unsigned bytes = [_socket bytesRead] + [_socket bytesWritten];
double speed = [_socket readSpeed] + [_socket writeSpeed];
if ( speed > 0.0 ) {
return [NSString stringWithFormat:@"%.1fMB (%.1fkbps)", (float)bytes/1038576.0f, (float)speed/1024.0f];
}
return [NSString stringWithFormat:@"%.1fMB", (float)bytes/1038576.0f];
}
- (id)delegate
{
return _delegate;
}
- (void)setDelegate:(id)delegate
{
_delegate = delegate;
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#pragma mark PrivateAPI
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
- (void)_handlePacket
{
if ( strncmp( "connect", _header.name, sizeof(_header.name) ) == 0 ) {
ChazLog( @"ServerChild - connect received!!" );
[_cheater connect];
[self _setClient:[NSUnarchiver unarchiveObjectWithData:_parameters]];
}
else if ( strncmp( "authenticate", _header.name, sizeof(_header.name) ) == 0 ) {
ChazLog( @"ServerChild - process list requested" );
[_cheater authenticateWithPassword:[NSUnarchiver unarchiveObjectWithData:_parameters]];
}
else if ( strncmp( "getproclist", _header.name, sizeof(_header.name) ) == 0 ) {
ChazLog( @"ServerChild - process list requested" );
[_cheater getProcessList];
}
else if ( strncmp( "settarget", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater setTarget:[NSUnarchiver unarchiveObjectWithData:_parameters]];
}
else if ( strncmp( "pausetarget", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater pauseTarget];
}
else if ( strncmp( "resumetarget", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater resumeTarget];
}
else if ( strncmp( "limitresults", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater limitReturnedResults:[[NSUnarchiver unarchiveObjectWithData:_parameters] unsignedIntValue]];
}
else if ( strncmp( "search", _header.name, sizeof(_header.name) ) == 0 ) {
NSArray *params = [NSUnarchiver unarchiveObjectWithData:_parameters];
[_cheater searchForVariable:[params objectAtIndex:0] comparison:[[params objectAtIndex:1] unsignedCharValue]];
}
else if ( strncmp( "lastsearch", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater searchLastValuesComparison:[[NSUnarchiver unarchiveObjectWithData:_parameters] unsignedCharValue]];
}
else if ( strncmp( "cancelsearch", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater cancelSearch];
}
else if ( strncmp( "clearsearch", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater clearSearch];
}
else if ( strncmp( "dump", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater getMemoryDump];
}
else if ( strncmp( "canceldump", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater cancelMemoryDump];
}
else if ( strncmp( "changevars", _header.name, sizeof(_header.name) ) == 0 ) {
NSArray *params = [NSUnarchiver unarchiveObjectWithData:_parameters];
[_cheater makeVariableChanges:[params objectAtIndex:0] repeat:[[params objectAtIndex:1] boolValue]
interval:[[params objectAtIndex:2] doubleValue]];
}
else if ( strncmp( "stopchange", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater stopChangingVariables];
}
else if ( strncmp( "undo", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater undo];
}
else if ( strncmp( "redo", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater redo];
}
else if ( strncmp( "watchvars", _header.name, sizeof(_header.name) ) == 0 ) {
NSArray *params = [NSUnarchiver unarchiveObjectWithData:_parameters];
[_cheater watchVariablesAtIndex:[[params objectAtIndex:0] unsignedIntValue] count:[[params objectAtIndex:1] unsignedIntValue]
interval:[[params objectAtIndex:2] doubleValue]];
}
else if ( strncmp( "stopwatch", _header.name, sizeof(_header.name) ) == 0 ) {
[_cheater stopWatchingVariables];
}
[_parameters release];
_parameters = nil;
}
- (void)_setClient:(NSString *)client
{
[client retain];
[_client release];
_client = client;
if ( [_delegate respondsToSelector:@selector(serverChildChanged:)] ) {
[_delegate serverChildChanged:self];
}
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#pragma mark CheaterDelegate
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
- (void)cheaterDidConnect:(Cheater *)cheater
{
TCPacketHeader header = { TC_NIFTY, 0, "connected" };
ChazLog( @"SENT didconnect" );
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cheaterDidDisconnect:(Cheater *)cheater
{
// nothing needs to be done
}
- (void)cheaterRequiresAuthentication:(Cheater *)cheater
{
TCPacketHeader header = { TC_NIFTY, 0, "requireauth" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cheaterRejectedPassword:(Cheater *)cheater
{
TCPacketHeader header = { TC_NIFTY, 0, "rejectedauth" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cheaterAcceptedPassword:(Cheater *)cheater
{
TCPacketHeader header = { TC_NIFTY, 0, "authenticated" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cheater:(Cheater *)cheater didFindProcesses:(NSArray *)processes
{
NSData *params = [NSArchiver archivedDataWithRootObject:processes];
TCPacketHeader header = { TC_NIFTY, [params length], "proclist" };
ChazLog( @"SENT proclist" );
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)cheater:(Cheater *)cheater didAddProcess:(Process *)process
{
NSData *params = [NSArchiver archivedDataWithRootObject:process];
TCPacketHeader header = { TC_NIFTY, [params length], "addprocess" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)cheater:(Cheater *)cheater didRemoveProcess:(Process *)process
{
NSData *params = [NSArchiver archivedDataWithRootObject:process];
TCPacketHeader header = { TC_NIFTY, [params length], "removeprocess" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)cheater:(Cheater *)cheater didSetTarget:(Process *)target
{
NSData *params = [NSArchiver archivedDataWithRootObject:target];
TCPacketHeader header = { TC_NIFTY, [params length], "didsettarget" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)cheaterPausedTarget:(Cheater *)cheater
{
TCPacketHeader header = { TC_NIFTY, 0, "didpausetarget" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cheaterResumedTarget:(Cheater *)cheater
{
TCPacketHeader header = { TC_NIFTY, 0, "didresumetarget" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cheater:(Cheater *)cheater didFindVariables:(TCArray)variables actualAmount:(unsigned)count
{
struct {
unsigned actualAmount;
unsigned varCount;
unsigned varSize;
} varInfo = { count, TCArrayElementCount(variables), TCArrayElementSize(variables) };
unsigned bufferLen = varInfo.varCount * varInfo.varSize;
TCPacketHeader header = { TC_NIFTY, sizeof(varInfo) + bufferLen, "vars" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeBytes:&varInfo length:sizeof(varInfo) tag:0];
[_socket writeBytes:TCArrayBytes(variables) length:bufferLen tag:0];
TCReleaseArray( variables );
}
- (void)cheater:(Cheater *)cheater didFindValues:(TCArray)values
{
struct {
unsigned varCount;
unsigned varSize;
} varInfo = { TCArrayElementCount(values), TCArrayElementSize(values) };
unsigned bufferLen = varInfo.varCount * varInfo.varSize;
TCPacketHeader header = { TC_NIFTY, sizeof(varInfo) + bufferLen, "values" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeBytes:&varInfo length:sizeof(varInfo) tag:0];
[_socket writeBytes:TCArrayBytes(values) length:bufferLen tag:0];
TCReleaseArray( values );
}
- (void)cheaterDidCancelSearch:(Cheater *)cheater
{
TCPacketHeader header = { TC_NIFTY, 0, "didcancelsearch" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cheaterDidClearSearch:(Cheater *)cheater
{
TCPacketHeader header = { TC_NIFTY, 0, "didclearsearch" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cheater:(Cheater *)cheater didDumpMemory:(NSData *)memoryDump
{
TCPacketHeader header = { TC_NIFTY, [memoryDump length], "memdump" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:memoryDump tag:0];
}
- (void)cheaterDidCancelMemoryDump:(Cheater *)cheater
{
TCPacketHeader header = { TC_NIFTY, 0, "didcanceldump" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cheater:(Cheater *)cheater didChangeVariables:(unsigned)changeCount
{
NSData *params = [NSArchiver archivedDataWithRootObject:[NSNumber numberWithUnsignedInt:changeCount]];
TCPacketHeader header = { TC_NIFTY, [params length], "changedvars" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)cheaterDidStopChangingVariables:(Cheater *)cheater
{
TCPacketHeader header = { TC_NIFTY, 0, "didstopchanging" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cheater:(Cheater *)cheater didReportProgress:(int)progress
{
NSData *params = [NSArchiver archivedDataWithRootObject:[NSNumber numberWithInt:progress]];
TCPacketHeader header = { TC_NIFTY, [params length], "progress" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)cheater:(Cheater *)cheater didRevertToVariables:(TCArray)variables actualAmount:(unsigned)count
{
struct {
unsigned actualAmount;
unsigned varCount;
unsigned varSize;
} varInfo = { count, TCArrayElementCount(variables), TCArrayElementSize(variables) };
unsigned bufferLen = varInfo.varCount * varInfo.varSize;
TCPacketHeader header = { TC_NIFTY, sizeof(varInfo) + bufferLen, "revertedto" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeBytes:&varInfo length:sizeof(varInfo) tag:0];
[_socket writeBytes:TCArrayBytes(variables) length:bufferLen tag:0];
TCReleaseArray( variables );
}
- (void)cheaterDidUndo:(Cheater *)cheater
{
TCPacketHeader header = { TC_NIFTY, 0, "didundo" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cheaterDidRedo:(Cheater *)cheater
{
TCPacketHeader header = { TC_NIFTY, 0, "didredo" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cheater:(Cheater *)cheater variableAtIndex:(unsigned)index didChangeTo:(Variable *)variable
{
NSData *params = [NSArchiver archivedDataWithRootObject:[NSArray arrayWithObjects:[NSNumber numberWithUnsignedInt:index], variable, nil]];
TCPacketHeader header = { TC_NIFTY, [params length], "varchanged" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)cheater:(Cheater *)cheater didFailLastRequest:(NSString *)reason
{
NSData *params = [NSArchiver archivedDataWithRootObject:reason];
TCPacketHeader header = { TC_NIFTY, [params length], "failed" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)cheater:(Cheater *)cheater echo:(NSString *)message
{
NSData *params = [NSArchiver archivedDataWithRootObject:message];
TCPacketHeader header = { TC_NIFTY, [params length], "echo" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
@end