CFStream crashes after setting SOCKS proxy config
Asked Answered
M

1

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);
Mutual answered 22/8, 2012 at 9:50 Comment(5)
Nobody has encountered this before?Mutual
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?Garceau
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.Mutual
did you happen to figure this out?Garceau
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.Mutual
O
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.

Orchard answered 22/11, 2012 at 13:4 Comment(1)
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 SourceMutual

© 2022 - 2024 — McMap. All rights reserved.