Currently, when I send a udp packet to an unreachable destination (e.g. an unbound local port), I get an event that I can't seem to distinguish from receiving a zero-length udp packet.
I am creating the socket like this:
CFSocketContext socketContext = { 0, (__bridge void *)self, CFRetain, CFRelease, NULL };
socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_DGRAM, IPPROTO_UDP, kCFSocketDataCallBack, (CFSocketCallBack)onReceivedData, &socketContext);
NSData* localEndPointData = [[IpEndPoint ipEndPointAtUnspecifiedAddressOnPort:specifiedLocalPort] sockaddrData];
CFSocketSetAddress(socket, (__bridge CFDataRef)localEndPointData);
CFSocketConnectToAddress(socket, (__bridge CFDataRef)[remoteEndPoint sockaddrData], -1);
and receiving events like so:
void onReceivedData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
if(type == kCFSocketDataCallBack && CFDataGetLength((CFDataRef)data) == 0) {
NSLog("Received empty packet");
}
}
If there is a socket listening at specifiedLocalPort
then things work properly. Sending data triggers no received events, receiving data triggers a received event. If there's no socket listening at specifiedLocalPort
, then sending data triggers a received event claiming an empty udp packet was received.
Am I doing something stupid, to cause this behaviour? How can I distinguish 'destination unreachable' from 'destination sent you an empty udp packet'?
errno
on failure? A lot of the C libraries work like that. – sapi