views:

256

answers:

0

Hi,

I have a strange problem with some multiple input/output streams and NSURLConnection:

First I open each one NSInputStream and NSOutputStream. They are communicating with a socket connection. After some communication I open a second socket connection. After that I need to connect to a web server by using NSURLConnection. After the connection connectionDidFinishLoading some of my NSString variables are unaccessable or printing some strange system stuff. It seems to be a wonderful buffer overflow.

The socket connections:

- (void)initSocketConnection {
 CFReadStreamRef readStream;
 CFWriteStreamRef writeStream;
 CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"messenger.hotmail.com", 1863, &readStream, &writeStream);
 inputStream = (NSInputStream *)readStream;
 outputStream = (NSOutputStream *)writeStream;
 [inputStream setDelegate:self];
 [outputStream setDelegate:self];
 [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
 [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
 [inputStream open];
 [outputStream open];

 state = 1;

 NSString * str = @"VER 1 MSNP8 CVR0\n";
 NSLog(@"State: %d Command: %@", state, str);
 const uint8_t * rawstring = (const uint8_t *)[str UTF8String];
 [outputStream write:rawstring maxLength:strlen((const char *)[str UTF8String])];
 //challange = @"HHHH";
}
- (void)initSocketConnection2:(NSString *)ip port:(NSString *)port {
 //NSLog(@"IP: %@, Port: %@", ip, port);
 CFReadStreamRef readStream2;
 CFWriteStreamRef writeStream2;
 CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)ip, [port intValue], &readStream2, &writeStream2);
 inputStream2 = (NSInputStream *)readStream2;
 outputStream2 = (NSOutputStream *)writeStream2;
 [inputStream2 setDelegate:self];
 [outputStream2 setDelegate:self];
 [inputStream2 scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
 [outputStream2 scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
 [inputStream2 open];
 [outputStream2 open];

 NSString * str = @"VER 4 MSNP8 CVR0\n";
 NSLog(@"State: %d Command: %@", state, str);
 const uint8_t * rawstring = (const uint8_t *)[str UTF8String];
 [outputStream2 write:rawstring maxLength:strlen((const char *)[str UTF8String])];
}

- (void)getTicked:(NSData *)chllg {
 /*NSString *res = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"https://nexus.passport.com/rdr/pprdr.asp"] encoding:NSUTF8StringEncoding error:nil];
 NSLog(@"%@", res);*/
 challange = chllg;
 Passport *pspt = [[Passport alloc] init];
 [pspt setParent:self];
 [pspt initPassport];
}


- (void)finished {
 NSLog(@"Challange: %@", challange);
}

- (IBAction)sendCommand:(id)sender {
 //NSLog(@"Sending: %@", textField.text);
 /*NSString * str = [NSString stringWithFormat: @"%@\n", textField.text];
 const uint8_t * rawstring = (const uint8_t *)[str UTF8String];
 [outputStream write:rawstring maxLength:strlen((const char *)[str UTF8String])];*/

}

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
 NSMutableData *_data = [[[NSMutableData alloc] init] retain];
 [_data setLength:0];

 NSString *str;
 const uint8_t *rawstring;

 NSString *recv;
 NSArray *lines;

 NSInteger i;

 NSString *cmd;
 NSString *ns;
 switch(eventCode) {
  case NSStreamEventHasBytesAvailable:
  {
   uint8_t buf[102400];
   unsigned int len = 0;
   len = [(NSInputStream *)stream read:buf maxLength:102400];
   if(len) {
    [_data appendBytes:buf length:len];
    switch (state) {
     case 1:;
      NSLog(@"State: %d Response: %@", state, [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]);
      state = 2;
      str = @"CVR 2 0x0409 win 4.10 i386 MSNMSGR 6.2.0208 MSMSGS [email protected]\n";
      NSLog(@"State: %d Command: %@", state, str);
      rawstring = (const uint8_t *)[str UTF8String];
      [outputStream write:rawstring maxLength:strlen((const char *)[str UTF8String])];
      break;
     case 2:;
      NSLog(@"State: %d Response: %@", state, [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]);
      state = 3;
      str = @"USR 3 TWN I [email protected]\n";
      NSLog(@"State: %d Command: %@", state, str);
      rawstring = (const uint8_t *)[str UTF8String];
      [outputStream write:rawstring maxLength:strlen((const char *)[str UTF8String])];
      break;
     case 3:;
      NSLog(@"State: %d Response: %@", state, [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]);
      state = 4;
      recv = [[[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
      //NSLog(@"%@", recv);
      lines = [recv componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
      for (i = 0; i < [lines count]; i++) {
       if([[NSString stringWithString:[lines objectAtIndex:i]] length] >= 3) {
        cmd = [[NSString stringWithString:[lines objectAtIndex:i]] substringToIndex:3];
        //NSLog(@"%@", cmd);
        if ([cmd isEqualToString:@"XFR"]) {
         ns = [[[NSString stringWithString:[lines objectAtIndex:i]] componentsSeparatedByString:@" "] objectAtIndex:3];
         NSString *ip = [[ns componentsSeparatedByString:@":"] objectAtIndex:0];
         NSString *port = [[ns componentsSeparatedByString:@":"] objectAtIndex:1];
         [self initSocketConnection2:ip port:port];
         break;
         //[self initSocketConnection2:ip port:port];
        }
       }
      }
      break;
     case 4:;
      NSLog(@"State: %d Response: %@", state, [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]);
      state = 5;
      str = @"CVR 5 0x0409 win 4.10 i386 MSNMSGR 6.2.0208 MSMSGS [email protected]\n";
      NSLog(@"State: %d Command: %@", state, str);
      rawstring = (const uint8_t *)[str UTF8String];
      [outputStream2 write:rawstring maxLength:strlen((const char *)[str UTF8String])];
      break;
     case 5:;
      NSLog(@"State: %d Response: %@", state, [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]);
      state = 6;
      str = @"USR 6 TWN I [email protected]\n";
      NSLog(@"State: %d Command: %@", state, str);
      rawstring = (const uint8_t *)[str UTF8String];
      [outputStream2 write:rawstring maxLength:strlen((const char *)[str UTF8String])];
      break;
     case 6:;
      NSLog(@"State: %d Response: %@", state, [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]);
      state = 7;
      recv = [[[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

      lines = [recv componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
      for (i = 0; i < [lines count]; i++) {
       if([[NSString stringWithString:[lines objectAtIndex:i]] length] >= 3) {
        cmd = [[NSString stringWithString:[lines objectAtIndex:i]] substringToIndex:3];
        //NSLog(@"%@", cmd);
        if ([cmd isEqualToString:@"USR"]) {
         ns = [[[NSString stringWithString:[lines objectAtIndex:i]] componentsSeparatedByString:@" "] objectAtIndex:4];
         //challange = [NSString stringWithString:ns];
         [self getTicked:[ns dataUsingEncoding:NSUTF8StringEncoding]];
         break;
         //[self initSocketConnection2:ip port:port];
        }
       }
      }
      break;
     case 7:;
      NSLog(@"State: %d Response: %@", state, [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]);
      state = 8;
      recv = [[[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

      lines = [recv componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
      for (i = 0; i < [lines count]; i++) {
       if([[NSString stringWithString:[lines objectAtIndex:i]] length] >= 3) {
        cmd = [[NSString stringWithString:[lines objectAtIndex:i]] substringToIndex:3];
        //NSLog(@"%@", cmd);
        /*if ([cmd isEqualToString:@"USR"]) {
         ns = [[[NSString stringWithString:[lines objectAtIndex:i]] componentsSeparatedByString:@" "] objectAtIndex:4];
         //challange = [NSString stringWithString:ns];
         [self getTicked:ns];
         break;
         //[self initSocketConnection2:ip port:port];
        }*/
       }
      }
      break;
     default:
      break;
    }
   }
      break;
  }
    }
 [_data release];
 _data = nil;
}

The NSURLConnection:

- (void)initPassport {
 NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://nexus.passport.com/rdr/pprdr.asp"]

             cachePolicy:NSURLRequestUseProtocolCachePolicy

            timeoutInterval:60.0];

 // create the connection with the request

 // and start loading the data

 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

 if (theConnection) {
  //receivedData = [[NSMutableData alloc] init];
 }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
 /*NSArray *urls = [[[[(NSHTTPURLResponse*)response allHeaderFields] objectForKey:@"Passporturls"] componentsSeparatedByString:@","] retain];
    NSMutableDictionary *tmpdict = [[[NSMutableDictionary alloc] init] retain];
    for (NSInteger i = 0; i < [urls count]; i++) {
    [tmpdict setObject:[NSString stringWithString:[[[urls objectAtIndex:i] componentsSeparatedByString:@"="] objectAtIndex:1]] forKey:[NSString stringWithString:[[[urls objectAtIndex:i] componentsSeparatedByString:@"="] objectAtIndex:0]]];
    }
    NSLog(@"%@", challange);
    passportUrls = [NSDictionary dictionaryWithDictionary:(NSDictionary*)tmpdict];
    [tmpdict release];
    [urls release];
    [connection cancel];
    state = 8;
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://%@", [passportUrls objectForKey:@"DALogin"]]]];
    [theRequest setHTTPMethod:@"POST"];
    NSLog(@"%@", challange);
    [theRequest setHTTPBody:[[NSString stringWithFormat:@"Authorization=Passport1.4 OrgVerb=GET,OrgURL=%@,sign-in=%@,pwd=dapain,%@\n", [[NSString stringWithString:@"http://messenger.msn.com"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [[NSString stringWithString:@"[email protected]"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], challange] dataUsingEncoding:NSUTF8StringEncoding]];
    //NSLog(@"%@", [[NSString alloc] initWithData:[theRequest HTTPBody] encoding:NSUTF8StringEncoding]);
    NSURLConnection *c = [NSURLConnection connectionWithRequest:theRequest delegate:self];
    if (c) {

    }*/
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
 NSLog(@"Finished");
 [self finished];
}

- (void)finished {
 [parent finished];
}
- (id)parent {
 return parent;
}
- (void)setParent:(id)sender {
 parent = sender;
}