7

What's wrong with the code below? I use AsyncSocket to connect to a SOCKS proxy and set the proxy settings on onSocketWillConnect delegate method. If I omit the calls to CFReadStreamSetProperty and CFWriteStreamSetProperty the socket connection will follow through smoothly. Else, I get a [Not A Type retain] on a deallocated instance with no traceable stack trace (It might be CFNetwork related?). Does anyone have any idea what gives?

CFDictionaryRef proxyDict = CFNetworkCopySystemProxySettings();
CFMutableDictionaryRef socksConfig = CFDictionaryCreateMutableCopy(NULL, 0, proxyDict);
CFDictionarySetValue(socksConfig, kCFStreamPropertySOCKSProxyHost, CFSTR("192.168.1.148"));
CFDictionarySetValue(socksConfig, kCFStreamPropertySOCKSProxyPort, (__bridge CFNumberRef)[NSNumber numberWithInt:3129]);
CFDictionarySetValue(socksConfig, kCFStreamPropertySOCKSVersion, kCFStreamSocketSOCKSVersion4);

// set SOCKS for read streams
CFReadStreamRef readStream = [sock getCFReadStream];
if (!CFReadStreamSetProperty(readStream, kCFStreamPropertySOCKSProxy, socksConfig)) {
  CFStreamError error = CFReadStreamGetError(readStream);
  NSLog(@"[SEVERE] Web Socket Read Stream Error: %ld[%ld]", error.domain, error.error);
}

// set SOCKS for write stream
CFWriteStreamRef writeStream = [sock getCFWriteStream];
if (!CFWriteStreamSetProperty(writeStream, kCFStreamPropertySOCKSProxy, socksConfig)) {
  CFStreamError error = CFWriteStreamGetError(writeStream);
  NSLog(@"[SEVERE] Web Socket Write Stream Error: %ld[%ld]", error.domain, error.error);
}

// Release
CFRelease(socksConfig);
CFRelease(proxyDict);
LaN
  • 228
  • 2
  • 9
  • Nobody has encountered this before? – LaN Aug 23 '12 at 06:52
  • When exactly does this code crash? I have similar code in use here (using GCDAsyncSocket) which works fine. Does the code crash immediately when you call `CFReadStreamSetProperty`? – yfrancis Oct 31 '12 at 02:47
  • It crashes after the SOCKS handshake. I think it has something to do with the SOCKSv4 handshake since it doesn't crash when connecting on a SOCKSv5 port using the same code. – LaN Oct 31 '12 at 06:06
  • did you happen to figure this out? – yfrancis Nov 04 '12 at 07:18
  • Unfortunately, no. I didn't have the opportunity to investigate this further except for that SOCKSv4 seems to trigger the issue and not SOCKSv5. Ideas are welcome, though. – LaN Nov 04 '12 at 15:39

1 Answers1

1

From the documentation of CFReadStream:

Properties that can be set configure the behavior of the stream and may be modifiable only at particular times, such as before the stream has been opened. (In fact, you should assume that you can set properties only before opening the stream, unless otherwise noted.)

onSocketWillConnect may be too late to set those properties.

Sirko
  • 72,589
  • 19
  • 149
  • 183
Axel K.
  • 105
  • 1
  • 2
  • Yes, socket stream properties are to be set before opening (as much as possible). But this is not the case, as per the method name, `onSocketWillConnect` is called _before_ the socket is opened. [AsyncSocket Source](https://github.com/lanavelino/CocoaAsyncSocket/blob/master/RunLoop/AsyncSocket.m) – LaN Nov 22 '12 at 16:26