-
-- (void)handleSocket:(int)sock
-{
- struct sockaddr identifier;
- int addrLen = sizeof(identifier);
-
- NSString *address;
- char *addressCString;
-
- sockfd = sock;
-
- if ( getpeername( sockfd, &identifier, &addrLen ) == -1 )
- {
- CMLog( @"ERROR: getpeername() failed" );
- }
-
- if ( identifier.sa_family == AF_INET )
- {
- struct sockaddr_in addr;
-
- addrLen = sizeof(addr);
-
- if ( getpeername( sockfd, (struct sockaddr *)(&addr), &addrLen ) == -1 )
- {
- CMLog( @"ERROR: getpeername() failed" );
- }
-
- if ( (addressCString = inet_ntoa( addr.sin_addr )) == NULL )
- {
- CMLog( @"ERROR: inet_ntoa() failed" );
- }
-
- address = [NSString stringWithCString:addressCString];
- }
- else
- {
- struct sockaddr_un addr;
-
- addrLen = sizeof(addr);
-
- if ( getpeername( sockfd, (struct sockaddr *)(&addr), &addrLen ) == -1 )
- {
- CMLog( @"ERROR: getpeername() failed" );
- }
-
- CMLog( @"client connection: %s", addr.sun_path );
-
- address = [NSString stringWithString:@"127.0.0.1"];
- }
-
- [rootProxy server:self connectedWithSocket:sockfd];
-
- [self setAddress:address];
- [self setAction:nil];
-}
-
-- (void)run
-{
- struct timeval tv;
- fd_set fdset, master;
- int numfds;
-
- int result;
-
- PacketHeader header;
- char *data = NULL;
-
- tv.tv_sec = 2;
- tv.tv_usec = 0;
-
- FD_ZERO( &fdset );
- FD_ZERO( &master );
- FD_SET( sockfd, &master );
-
- numfds = sockfd + 1;
-
- CMLog( @"SERVER start" );
-
- for (;;)
- {
- fdset = master;
-
- select( numfds, &fdset, NULL, NULL, &tv );
-
- if ( FD_ISSET( sockfd, &fdset ) )
- {
- if ( (result = ReadBuffer( sockfd, (char *)(&header), sizeof(header) )) != sizeof(header) )
- {
- break;
- }
-
- if ( !VerifyChecksum( header.checksum ) )
- {
- CMLog( @"checksum failed" );
- }
-
- if ( header.size != 0 )
- {
- if ( (data = (char *)malloc( header.size )) == NULL )
- {
- CMLog( @"failed to allocate buffer for reading a network packet" );
- break;
- }
-
- if ( (result = ReadBuffer( sockfd, data, header.size )) != header.size )
- {
- CMLog( @"failed to read the data of a network packet" );
- free( data );
- break;
- }
- }
-
- //CMLog( @"SERVER message %i/%i/%i", header.checksum, header.function, header.size );
-
- switch ( header.function )
- {
- case 1:
- [self sendProcessList];
- break;
-
- case 3:
- [self handleClearSearch];
- break;
-
- case 5:
- [self handleSearch:data size:header.size];
- break;
-
- case 8:
- [self handleChange:data size:header.size];
- break;
-
- case 10:
- [self handlePauseTarget];
- break;
-
- case 14:
- [self handleUndo];
- break;
-
- case 16:
- [self handleRedo];
- break;
-
- case 18:
- [self handleSetTargetPID:data size:header.size];
- break;
-
- }
-
- if ( header.size != 0 )
- {
- free( data );
- }
- }
- }
-
- close( sockfd );
-
- CMLog( @"SERVER close" );
-
- [rootProxy serverDisconnected:self];
-}
-
-
-- (void)setAddress:(NSString *)address
-{
- [rootProxy server:self changedAddress:address];
-}
-
-- (void)setAction:(NSString *)action
-{
- if ( action == nil )
- {
- [rootProxy server:self changedAction:@"Idle"];
- }
- else
- {
- [rootProxy server:self changedAction:action];
- }
-}
-
-- (void)firstSearchString8bit:(char const *)value size:(int)vsize
-{
- kern_return_t result;
-
- vm_address_t address = 0x0;
- vm_size_t size = 0;
- vm_region_basic_info_data_t info;
- mach_msg_type_number_t infoCnt = 8;
- mach_port_t object_name = 0;
-
- char *data;
- vm_size_t dataLength;
-
- TCaddress *results = NULL;
- int resultsAmount = 0;
-
- for (;;)
- {
- if ( (result = vm_region( processTask, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)(&info), &infoCnt, &object_name )) != KERN_SUCCESS )
- {
- if ( result != KERN_INVALID_ADDRESS )
- {
- CMLog( @"vm_region returned error: %i", result );
- }
- break;
- }
-
- if ( (info.protection & VM_PROT_READ) && (info.protection & VM_PROT_WRITE))
- {
- data = (char *)malloc( size );
- dataLength = size;
-
- if ( (result = vm_read_overwrite( processTask, address, size, (vm_address_t)data, &dataLength )) != KERN_SUCCESS && result != KERN_PROTECTION_FAILURE )
- {
- CMLog( @"vm_read_overwrite returned error: %i", result );
- free( data );
- break;
- }
-
- if ( result == KERN_SUCCESS )
- {
- //int i, top = dataLength - vsize;
-
- if ( (results = realloc( results, TCAddressSize*resultsAmount + dataLength )) == NULL )
- {
- CMLog( @"ERROR: could not expand buffer" );
- exit(0);
- }
-
- resultsAmount += bmsearch( (char *)value, vsize, (char *)data, dataLength, (void *)address, (void **)((char *)results+TCAddressSize*resultsAmount) );
- //resultsAmount += TBM( (char *)value, vsize, data, dataLength, (void **)((char *)results+TCAddressSize*resultsAmount) );
- //resultsAmount += SMITH( data, dataLength, (char *)value, vsize, (void **)((char *)results+TCAddressSize*resultsAmount) );
-
- /*for ( i = 0; i < top; i++ )
- {
- if ( strncmp( value, data+i, vsize ) == 0 )
- {
- results[resultsAmount++] = (TCaddress)address + i;
- }
- }*/
- }
-
- free( data );
- }
-
- address += size;
- }
-
- realloc( results, TCAddressSize*resultsAmount );
- [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_8_BIT data:results amount:resultsAmount]];
-
- CMLog( @"found %i of %i", resultsAmount, value );
-}
-
-- (void)firstSearchIntegerChar:(int8_t)value
-{
- kern_return_t result;
-
- vm_address_t address = 0x0;
- vm_size_t size = 0;
- vm_region_basic_info_data_t info;
- mach_msg_type_number_t infoCnt = 8;
- mach_port_t object_name = 0;
-
- int8_t *data;
- vm_size_t dataLength;
-
- TCaddress *results = NULL;
- int resultsAmount = 0;
-
- for (;;)
- {
- if ( (result = vm_region( processTask, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)(&info), &infoCnt, &object_name )) != KERN_SUCCESS )
- {
- if ( result != KERN_INVALID_ADDRESS )
- {
- CMLog( @"vm_region returned error: %i", result );
- }
- break;
- }
-
- if ( (info.protection & VM_PROT_READ) && (info.protection & VM_PROT_WRITE))
- {
- data = (int8_t *)malloc( size );
- dataLength = size;
-
- if ( (result = vm_read_overwrite( processTask, address, size, (vm_address_t)data, &dataLength )) != KERN_SUCCESS && result != KERN_PROTECTION_FAILURE )
- {
- CMLog( @"vm_read_overwrite returned error: %i", result );
- free( data );
- break;
- }
-
- if ( result == KERN_SUCCESS )
- {
- int i;
-
- if ( (results = (TCaddress *)realloc( results, TCAddressSize*resultsAmount + TCAddressSize*dataLength )) == NULL )
- {
- CMLog( @"ERROR: could not expand buffer" );
- exit(0);
- }
-
- for ( i = 0; i < dataLength; i++ )
- {
- if ( *(data+i) == value )
- {
- results[resultsAmount++] = (TCaddress)address + i;
- }
- }
- }
-
- free( data );
- }
-
- address += size;
- }
-
- realloc( results, TCAddressSize*resultsAmount );
- [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_8_BIT data:results amount:resultsAmount]];
-
- CMLog( @"found %i of %i", resultsAmount, value );
-}
-
-- (void)firstSearchIntegerShort:(int16_t)value
-{
- kern_return_t result;
-
- vm_address_t address = 0x0;
- vm_size_t size = 0;
- vm_region_basic_info_data_t info;
- mach_msg_type_number_t infoCnt = 8;
- mach_port_t object_name = 0;
-
- int16_t *data;
- vm_size_t dataLength;
-
- TCaddress *results = NULL;
- int resultsAmount = 0;
-
- for (;;)
- {
- if ( (result = vm_region( processTask, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)(&info), &infoCnt, &object_name )) != KERN_SUCCESS )
- {
- if ( result != KERN_INVALID_ADDRESS )
- {
- CMLog( @"vm_region returned error: %i", result );
- }
- break;
- }
-
- if ( (info.protection & VM_PROT_READ) && (info.protection & VM_PROT_WRITE))
- {
- data = (int16_t *)malloc( size );
- dataLength = size;
-
- if ( (result = vm_read_overwrite( processTask, address, size, (vm_address_t)data, &dataLength )) != KERN_SUCCESS && result != KERN_PROTECTION_FAILURE )
- {
- CMLog( @"vm_read_overwrite returned error: %i", result );
- free( data );
- break;
- }
-
- if ( result == KERN_SUCCESS )
- {
- int i, top = dataLength / sizeof(value);
-
- if ( (results = (TCaddress *)realloc( results, TCAddressSize*resultsAmount + 2*dataLength )) == NULL )
- {
- CMLog( @"ERROR: could not expand buffer" );
- exit(0);
- }
-
- for ( i = 0; i < top; i++ )
- {
- if ( *(data+i) == value )
- {
- results[resultsAmount++] = (TCaddress)address + i * sizeof(value);
- }
- }
- }
-
- free( data );
- }
-
- address += size;
- }
-
- realloc( results, TCAddressSize*resultsAmount );
- [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_16_BIT data:results amount:resultsAmount]];
-
- CMLog( @"found %i of %i", resultsAmount, value );
-}
-
-- (void)firstSearchIntegerLong:(int32_t)value
-{
- kern_return_t result;
-
- vm_address_t address = 0x0;
- vm_size_t size = 0;
- vm_region_basic_info_data_t info;
- mach_msg_type_number_t infoCnt = 8;
- mach_port_t object_name = 0;
-
- int32_t *data;
- vm_size_t dataLength;
-
- TCaddress *results = NULL;
- int resultsAmount = 0;
-
- /*unsigned zone_count = 10;
- vm_address_t *zones = (vm_address_t *)malloc( zone_count * sizeof(vm_address_t) );
- //memory_reader_t reader;
-
- if ( (result = malloc_get_all_zones( processTask, NULL, &zones, &zone_count )) != KERN_SUCCESS )
- {
- CMLog( @"malloc_get_all_zones error: %i", result );
- }
- else
- {
- //address = zones[0];
-
- int i;
-
- for ( i = 0; i < 10; i++ )
- {
- CMLog( @"malloc_get_all_zones[%i] = %X", i, (vm_address_t)zones[i] );
- }
- }*/
-
- for (;;)
- {
- if ( (result = vm_region( processTask, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)(&info), &infoCnt, &object_name )) != KERN_SUCCESS )
- {
- if ( result != KERN_INVALID_ADDRESS )
- {
- CMLog( @"vm_region returned error: %i", result );
- }
- break;
- }
-
- if ( (info.protection & VM_PROT_READ) && (info.protection & VM_PROT_WRITE) )
- {
- data = (int32_t *)malloc( size );
- dataLength = size;
-
- //CMLog( @"address: %.8X size: %i", address, size );
-
- if ( (result = vm_read_overwrite( processTask, address, size, (vm_address_t)data, &dataLength )) != KERN_SUCCESS && result != KERN_PROTECTION_FAILURE )
- {
- CMLog( @"vm_read_overwrite returned error: %i", result );
- free( data );
- break;
- }
-
- if ( result == KERN_SUCCESS )
- {
- int i, top = dataLength / sizeof(value);
-
- if ( (results = (TCaddress *)realloc( results, TCAddressSize*resultsAmount + dataLength )) == NULL )
- {
- CMLog( @"ERROR: could not expand buffer" );
- exit(0);
- }
-
- for ( i = 0; i < top; i++ )
- {
- if ( *(data+i) == value )
- {
- results[resultsAmount++] = (TCaddress)address + i * sizeof(value);
- }
- }
- }
-
- free( data );
- }
-
- address += size;
- }
-
- realloc( results, TCAddressSize*resultsAmount );
- [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_32_BIT data:results amount:resultsAmount]];
-
- CMLog( @"found %i of %i", resultsAmount, value );
-}
-
-- (void)firstSearchDecimalFloat:(float)value
-{
- kern_return_t result;
-
- vm_address_t address = 0x0;
- vm_size_t size = 0;
- vm_region_basic_info_data_t info;
- mach_msg_type_number_t infoCnt = 8;
- mach_port_t object_name = 0;
-
- float *data;
- vm_size_t dataLength;
-
- TCaddress *results = NULL;
- int resultsAmount = 0;
-
- for (;;)
- {
- if ( (result = vm_region( processTask, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)(&info), &infoCnt, &object_name )) != KERN_SUCCESS )
- {
- if ( result != KERN_INVALID_ADDRESS )
- {
- CMLog( @"vm_region returned error: %i", result );
- }
- break;
- }
-
- if ( (info.protection & VM_PROT_READ) && (info.protection & VM_PROT_WRITE))
- {
- data = (float *)malloc( size );
- dataLength = size;
-
- if ( (result = vm_read_overwrite( processTask, address, size, (vm_address_t)data, &dataLength )) != KERN_SUCCESS && result != KERN_PROTECTION_FAILURE )
- {
- CMLog( @"vm_read_overwrite returned error: %i", result );
- free( data );
- break;
- }
-
- if ( result == KERN_SUCCESS )
- {
- int i, top = dataLength / sizeof(value);
-
- if ( (results = realloc( results, TCAddressSize*resultsAmount + dataLength )) == NULL )
- {
- CMLog( @"ERROR: could not expand buffer" );
- exit(0);
- }
-
- for ( i = 0; i < top; i++ )
- {
- if ( CMCompareFloatsWithEpsilon( *(data+i), value, 0.1f ) == 0 )
- {
- results[resultsAmount++] = (TCaddress)address + i * sizeof(value);
- }
- }
- }
-
- free( data );
- }
-
- address += size;
- }
-
- realloc( results, TCAddressSize*resultsAmount );
- [searchResults addObject:[SearchResults resultsWithType:TYPE_DECIMAL size:SIZE_32_BIT data:results amount:resultsAmount]];
-
- CMLog( @"found %i of %i", resultsAmount, value );
-}
-
-- (void)firstSearchDecimalDouble:(double)value
-{
- kern_return_t result;
-
- vm_address_t address = 0x0;
- vm_size_t size = 0;
- vm_region_basic_info_data_t info;
- mach_msg_type_number_t infoCnt = 8;
- mach_port_t object_name = 0;
-
- double *data;
- vm_size_t dataLength;
-
- TCaddress *results = NULL;
- int resultsAmount = 0;
-
- CMLog( @"float search" );
-
- for (;;)
- {
- if ( (result = vm_region( processTask, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)(&info), &infoCnt, &object_name )) != KERN_SUCCESS )
- {
- if ( result != KERN_INVALID_ADDRESS )
- {
- CMLog( @"vm_region returned error: %i", result );
- }
- break;
- }
-
- if ( (info.protection & VM_PROT_READ) && (info.protection & VM_PROT_WRITE))
- {
- data = (double *)malloc( size );
- dataLength = size;
-
- if ( (result = vm_read_overwrite( processTask, address, size, (vm_address_t)data, &dataLength )) != KERN_SUCCESS && result != KERN_PROTECTION_FAILURE )
- {
- CMLog( @"vm_read_overwrite returned error: %i", result );
- free( data );
- break;
- }
-
- if ( result == KERN_SUCCESS )
- {
- int i, top = dataLength / sizeof(value);
-
- if ( (results = realloc( results, TCAddressSize*resultsAmount + dataLength )) == NULL )
- {
- CMLog( @"ERROR: could not expand buffer" );
- exit(0);
- }
-
- for ( i = 0; i < top; i++ )
- {
- if ( CMCompareDoublesWithEpsilon( *(data+i), value, 0.1 ) == 0 )
- {
- results[resultsAmount++] = (TCaddress)address + i * sizeof(value);
- }
- }
- }
-
- free( data );
- }
-
- address += size;
- }
-
- realloc( results, TCAddressSize*resultsAmount );
- [searchResults addObject:[SearchResults resultsWithType:TYPE_DECIMAL size:SIZE_64_BIT data:results amount:resultsAmount]];
-
- CMLog( @"found %i of %i", resultsAmount, value );
-}
-
-
-- (void)searchString8bit:(char const *)value size:(int)vsize
-{
- kern_return_t result;
-
- char *data;
- vm_size_t dataLength;
-
- TCaddress *results;
- int resultsAmount = 0;
-
- SearchResults *lastResults = [searchResults lastObject];
- TCaddress *lastResultsData = [lastResults data];
- int i, lastResultsAmount = [lastResults amount];
-
- if ( [lastResults type] != TYPE_INTEGER || [lastResults size] != SIZE_8_BIT )
- {
- [self sendError:@"This search is incompatible with the previous search." fatal:NO];
- return;
- }
-
- if ( (data = (char *)malloc( vsize )) == NULL )
- {
- CMLog( @"ERROR: could not create buffer" );
-
- [self sendError:@"The server cancelled the search because it ran out of memory." fatal:NO];
- return;
- }
-
- if ( (results = (TCaddress *)malloc( TCAddressSize*lastResultsAmount )) == NULL )
- {
- CMLog( @"ERROR: could not create buffer" );
-
- [self sendError:@"The server cancelled the search because it ran out of memory." fatal:NO];
- free( data );
- return;
- }
-
- for ( i = 0; i < lastResultsAmount; i++ )
- {
- TCaddress address = lastResultsData[i];
-
- //dataLength = sizeof(data);
-
- if ( (result = vm_read_overwrite( processTask, address, vsize, (vm_address_t)(data), &dataLength )) == KERN_SUCCESS )
- {
- if ( memcmp( data, value, dataLength ) == 0 )
- {
- results[resultsAmount++] = address;
- }
- }
- else
- {
- if ( result != KERN_PROTECTION_FAILURE )
- {
- CMLog( @"vm_read_overwrite returned error: %i", result );
- break;
- }
- }
- }
-
- realloc( results, TCAddressSize*resultsAmount );
- [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_8_BIT data:results amount:resultsAmount]];
-
- free( data );
-
- CMLog( @"found %i of %i", resultsAmount, value );
-}
-
-- (void)searchIntegerChar:(int8_t)value