passing integer as * void in objective-c/cocoa

242 views Asked by At

I'm trying to use the DNSServiceRegister(...) function as part of the DNS_SD package, and for the "host" it requires a hostname as a string. However, it's possible to register a host by IP address, but the IP address has to be formatted in binary representation (instead of 192.168.1.23 it'd be 0x1701a8c0).

I assume I can trick the function by passing in the value 1701a8c0 directly, but nothing i've tried seems to work. strncpy doesn't return the expected value, just pasting it doesn't work, creating an NSData then an NSString doesn't work. I could write a loop that builds the char array 1 entry at a time, but there must be another way?

    struct sockaddr_in sa;
 ...
        ipBinary=[[NSData alloc] initWithBytes:&(sa.sin_addr.s_addr) length:8];
    ipBinStr=[[NSString alloc] initWithData:ipBinary encoding:NSUTF8StringEncoding];

I haven't been able to get any variation of this to work either:

        strncpy(str,(char *)&(sa.sin_addr.s_addr),8);
1

There are 1 answers

5
Martin R On

To register a host by IP address, you probably have to pass the address in ASCII string representation, e.g. the string "192.168.1.23". You can use inet_ntoa() or better getnameinfo with the NI_NUMERICHOST flag to convert a struct sockaddr to a string.