I expect there is a shockingly obvious answer to this, but I've been stuck a couple of days on Problem Obvious and could use a tip / cake-with-file to escape from development jail.
I have used DNSServiceRef / Bonjour to advertise my service, and have used the same to get a list of what is advertised (using the hit on lo0 for the moment since still testing). So, now I have a machine name "mymachine.local." and the right port number to connect to. Yay!
What I can not figure out is how to get that information into a (IPV6) sockaddr so I can use it with connect. The point of confusion for me is that DNSServiceGetAddrInfo() does not take a port argument, and I see no other place to squeeze this information into the sockaddr returned by the DNSServiceGetAddrInfoReply.
If I just use a IPV6 socket with that sockaddr, I get back EADDRNOTAVAIL. Haven't tried IPv4. No error is returned from DNSServiceGetAddrInfo. I'm reading around that this may be because the port is 0, and indeed I can't find any spot in this pathway to squeeze that information in.
I'll attach an obligatory bit of code so that the reader may feel more grounded:
// My DNSServiceGetAddrInfoReply
void ServiceList::Node::AddressInfoCallback( DNSServiceRef __nonnull _sdRef,
DNSServiceFlags _flags,
uint32_t _interfaceIndex,
DNSServiceErrorType _errorCode,
const char * __nullable _hostname,
const struct sockaddr * __nullable _address,
uint32_t UNUSED _ttl, void * __nonnull context)
{
printf( "AddressInfo: \"%s\"\n", _hostname);
AddrInfo * info = (AddrInfo*) context;
if( kDNSServiceErr_NoError != _errorCode || NULL == _hostname || NULL == _address)
{
LOG_ERROR("Failed to get address info on \"%s\"\n", (const char*) info->hostTarget);
delete info;
return;
}
int err = connect(info->socket, _address, _address->sa_len); // returns EADDRNOTAVAIL on IPv6 socket.
What am I really trying to do? I'd like to connect to the address and port that I from my DNSServiceResolveReply.