Re: Returning Distant IP Address from CFReadStream
Re: Returning Distant IP Address from CFReadStream
- Subject: Re: Returning Distant IP Address from CFReadStream
- From: Quinn <email@hidden>
- Date: Mon, 9 Mar 2009 15:14:02 +0000
At 10:14 -0400 9/3/09, Vincent Lecours wrote:
I have some troubles getting back the IP address back from the
CFReadStream in my program.
Some points:
o kCFStreamPropertySocketNativeHandle won't return anything useful
until the stream has been opened. You'll get back NULL in that case.
o You don't need the CFSocketRef stuff. Once you have
CFSocketNativeHandle, you can pass that directly to
<x-man-page://2/getpeername>.
o In your CFDataGetBytes call you should be passing in the
sizeof(native) rather than CFDataGetLength(nativeProp). That way, if
nativeProp is the wrong size (very unlikely), you won't have a buffer
overrun.
o You don't need the CFDataGetBytes call at all. You can just get a
pointer to the bytes and indirect it.
Here's some code I wrote to do this just today. It assumes that
self.output is a NSOutputStream, which is toll-free bridged to
CFWriteStream.
- (int)fdOutput
{
int result;
CFDataRef fdData;
result = -1;
if (self.output != nil) {
fdData = CFWriteStreamCopyProperty(
(CFWriteStreamRef) self.output,
kCFStreamPropertySocketNativeHandle
);
if (fdData != nil) {
assert(CFDataGetLength(fdData) == sizeof(result));
result = *(int *) CFDataGetBytePtr(fdData);
CFRelease(fdData);
}
}
return result;
}
S+E
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden