Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,672
6,212
I received this email from a customer:

Im in the process of testing your software to make sure it meets our requirements. I have set two instances up and I can see our network is blocking the traffic from your tool.

Can I ask what protocol or ports your software uses??

I have tested it on my home network and it works brilliantly but I need to understand what is required to get the tool working in a corporate network.

It's been over a year since I last touched the networking part of Battery Status, and I believe I copied most of it out of a sample project. I've been trying to track down a spot where a port is chosen... it appears to be done in the function that I've reproduced (in a condensed fashion - I took out a lot of error checking code) below:

Code:
socklen_t           namelen;
int fdForListening = socket(AF_INET6, SOCK_STREAM, 0);
struct sockaddr_in6 serverAddress6;
memset(&serverAddress6, 0, sizeof(serverAddress6));
serverAddress6.sin6_family = AF_INET6;
serverAddress6.sin6_len    = sizeof(serverAddress6);
bind(fdForListening, (const struct sockaddr *) &serverAddress6, sizeof(serverAddress6));
namelen = sizeof(serverAddress6);
getsockname(fdForListening, (struct sockaddr *) &serverAddress6, &namelen);
chosenPort = ntohs(serverAddress6.sin6_port);

// checks an error code, repeats using IPv4 instead of IPv6 if the error is equal to EAFNOSUPPORT... omitting that because it's nearly identical code...

// Listen for connections on our socket, then create a CFSocket to route any connections to a run loop based callback.

listen(fdForListening, 5);

// Register our service with Bonjour.

CFSocketContext     context = {0, self, NULL, NULL, NULL};
CFRunLoopSourceRef  rls;
self->_listeningSocket = CFSocketCreateWithNative(NULL, fdForListening, kCFSocketAcceptCallBack, ListeningSocketCallback, &context);
fdForListening = -1;        // so that the clean up code doesn't close it
rls = CFSocketCreateRunLoopSource(NULL, self->_listeningSocket, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
CFRelease(rls);
[[NSNetService alloc] initWithDomain:@"" type:kServiceTypeBatteryStatus name:self.serviceName port:chosenPort]

Checking the documentation on all these functions, it seems that the port is automagically chosen (by Bonjour?) Which brings me to my question: what does my end user have to do to enable this code to work on their Mac?

I searched through System Preferences but nothing jumped out at me as relevant.
 
Last edited:
The code doesn't appear to be specifying a specific port. Since a port isn't specified before calling bind(), the operating system is assigning you a free one. The getsockname() call is pulling out the port number that the operating system assigned.

I don't know what your app is doing with the network, so it's hard to me to tell what the issue is. Is your app accepting connections from other computers to query information?

If so, the problem is that you're going to get blocked if a firewall is enabled. The comments imply maybe you're communicating with Bonjour. Honestly, I don't know if doing that will somehow open a firewall port on your Mac, but I doubt it. The comment about "corporate environment" also implies using a firewall which is pretty standard practice.

That would be why the user is asking about specific ports and protocols. If the port is hard-coded into your app then a firewall hole could be opened to allow external connections to your app. Or if your app used HTML or some other well-known protocol, the firewall could be programmed to allow it.

But if you're using a custom protocol and you aren't hard-coding a port to use then that's not going to be compatible if a firewall is enabled on the Mac or on some other part of the network where the Mac is connected.

I would read the Bonjour document about if or how it handles opening holes in the firewall on the Mac to allow connections. I haven't studied Bonjour enough to know about those details.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.