Question

I am trying to set up a SilverLight policy server under MacOSX 10.7.2 (Lion). This requires that I create and bind a socket to port 943, since SilverLight requests the policy file on this port. Unfortunately, it seems I am unable to bind at that port, as the bind call fails with errno=49. I suppose I do not have access to this port. Perhaps I need root privileges? Or do I need to forward this port to another one that I can bind? I'm a bit new to network programming, so any help is greatly appreciated! I've also attached my source code. Perhaps I'm doing something wrong, although it works fine if I use the SilverLight restricted ports 4502-4532, which SilverLight communicates on once the policy file is successfully served.

- (void) start {
    CFSocketRef socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL);
    if (!socket) {
        [self errorWithName:@"Unable to create socket."];
        return;
    }

    int reuse = true;
    CFSocketNativeHandle fileDescriptor = CFSocketGetNative(socket);
    if (setsockopt(fileDescriptor, SOL_SOCKET, SO_REUSEADDR,
                   (void *)&reuse, sizeof(int)) != 0) {
        NSLog(@"Unable to set socket options.");
        return;
    }

    struct sockaddr_in address;
    memset(&address, 0, sizeof(address));
    address.sin_len = sizeof(address);
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    address.sin_port = htons(943);
    CFDataRef addressData = CFDataCreate(NULL, (const UInt8 *)&address, sizeof(address));
    [(id)addressData autorelease];

    CFSocketError error = CFSocketSetAddress(socket, addressData);
    if (error < 0) {
        NSLog(@"Error bind %d\n", errno);  //fails here for port 943.
        return;
    }

    NSFileHandle *listeningHandle = [[NSFileHandle alloc]
                   initWithFileDescriptor:fileDescriptor
                   closeOnDealloc:YES];

    [[NSNotificationCenter defaultCenter]
          addObserver:self
             selector:@selector(receiveIncomingConnectionNotification:)
                 name:NSFileHandleConnectionAcceptedNotification
               object:nil];

    [listeningHandle acceptConnectionInBackgroundAndNotify];
}
Was it helpful?

Solution

Only root has privileges for ports below 1024. Try running your code with sudo and see if that fixes your issue.

Edit:

Also, check out man strerror. It will take that relatively meaningless error code and give you a (slightly) more useful string.

#include <stdio.h>
#include <string.h>

int main (int argc, char const *argv[])
{
  printf("%s\n", strerror(49) );
  return 0;
}

Gives:

Can't assign requested address
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top