Skip to content

Commit

Permalink
dns-sd: bonjour: Code cleanup
Browse files Browse the repository at this point in the history
- Inline new_discovery_data() function in callers
- Declare variables at the beginning of functions
- Whitespace and alignment fixes
- Split really long lines to be <= 80 chars
- Use C-style comments

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed May 11, 2021
1 parent 1d42fa3 commit a2eb9b6
Showing 1 changed file with 105 additions and 87 deletions.
192 changes: 105 additions & 87 deletions dns_sd_bonjour.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,36 @@
#include <CFNetwork/CFNetwork.h>

/*
Implementation for DNS SD discovery for macOS using CFNetServices.
*/

static int new_discovery_data(struct dns_sd_discovery_data **data)
{
struct dns_sd_discovery_data *d;

d = zalloc(sizeof(struct dns_sd_discovery_data));
if (!d)
return -ENOMEM;

*data = d;
return 0;
}
* Implementation for DNS SD discovery for macOS using CFNetServices.
*/

static void __cfnet_browser_cb (
CFNetServiceBrowserRef browser,
CFOptionFlags flags,
CFTypeRef domainOrService,
CFStreamError* error,
void* info)
static void __cfnet_browser_cb(CFNetServiceBrowserRef browser,
CFOptionFlags flags,
CFTypeRef domainOrService,
CFStreamError *error,
void *info)
{
CFStreamError anError;
const CFNetServiceRef netService = (CFNetServiceRef)domainOrService;
struct dns_sd_discovery_data *dd = info;
char address_v4[DNS_SD_ADDRESS_STR_MAX+1] = "";
char address_v6[DNS_SD_ADDRESS_STR_MAX+1] = "";
char hostname[MAXHOSTNAMELEN];
char name[MAXHOSTNAMELEN];
bool have_v4 = false;
bool have_v6 = false;
struct sockaddr_in *sa;
CFStreamError anError;
CFStringRef targetHost;
CFStringRef svcName;
CFArrayRef addrArr;
CFDataRef data;
SInt32 port;

if ((flags & kCFNetServiceFlagIsDomain) != 0) {
IIO_ERROR("DNS SD: FATAL! Callback called for domain, not service.\n");
goto stop_browsing;
}

struct dns_sd_discovery_data *dd = (struct dns_sd_discovery_data *)info;

if (dd == NULL) {
IIO_ERROR("DNS SD: Missing info structure. Stop browsing.\n");
goto stop_browsing;
Expand All @@ -59,67 +59,65 @@ static void __cfnet_browser_cb (

iio_mutex_lock(dd->lock);

const CFNetServiceRef netService = (CFNetServiceRef)domainOrService;
if (netService == NULL) {
IIO_DEBUG("DNS SD: Net service is null.\n");
goto verify_flags;
}

if (!CFNetServiceResolveWithTimeout(netService, 10.0, &anError)) {
IIO_DEBUG("DNS SD: Resolve error: %ld.%d\n", anError.domain, anError.error);
IIO_DEBUG("DNS SD: Resolve error: %ld.%d\n",
anError.domain, anError.error);
goto exit;
}
CFStringRef targetHost = CFNetServiceGetTargetHost(netService);

targetHost = CFNetServiceGetTargetHost(netService);
if (targetHost == NULL) {
IIO_DEBUG("DNS SD: No valid target host for service.\n");
goto exit;
}

char hostname[MAXHOSTNAMELEN];
if (!CFStringGetCString(targetHost, hostname, sizeof(hostname), kCFStringEncodingASCII)) {
if (!CFStringGetCString(targetHost, hostname,
sizeof(hostname), kCFStringEncodingASCII)) {
IIO_ERROR("DNS SD: Could not translate hostname\n");
goto exit;
}

CFStringRef svcName = CFNetServiceGetName(netService);
char name[MAXHOSTNAMELEN];
if (!CFStringGetCString(svcName, name, sizeof(name), kCFStringEncodingASCII)) {
svcName = CFNetServiceGetName(netService);
if (!CFStringGetCString(svcName, name,
sizeof(name), kCFStringEncodingASCII)) {
IIO_ERROR("DNS SD: Could not translate service name\n");
goto exit;
}

SInt32 port = CFNetServiceGetPortNumber(netService);

CFArrayRef addrArr = CFNetServiceGetAddressing(netService);
port = CFNetServiceGetPortNumber(netService);
addrArr = CFNetServiceGetAddressing(netService);
if (addrArr == NULL) {
IIO_WARNING("DNS SD: No valid addresses for service %s.\n", name);
IIO_WARNING("DNS SD: No valid addresses for service %s.\n",
name);
goto exit;
}

bool have_v4 = FALSE;
bool have_v6 = FALSE;
char address_v4[DNS_SD_ADDRESS_STR_MAX+1] = "";
char address_v6[DNS_SD_ADDRESS_STR_MAX+1] = "";

for (CFIndex i = 0; i < CFArrayGetCount(addrArr); i++) {
struct sockaddr_in *sa = (struct sockaddr_in *)
CFDataGetBytePtr(CFArrayGetValueAtIndex(addrArr, i));
data = CFArrayGetValueAtIndex(addrArr, i);
sa = (struct sockaddr_in *) CFDataGetBytePtr(data);

switch(sa->sin_family) {
case AF_INET:
if (inet_ntop(sa->sin_family, &sa->sin_addr,
address_v4, sizeof(address_v4))) {
have_v4 = TRUE;
}
case AF_INET6:
if (inet_ntop(sa->sin_family, &sa->sin_addr,
address_v6, sizeof(address_v6))) {
have_v6 = TRUE;
}
case AF_INET:
if (inet_ntop(sa->sin_family, &sa->sin_addr,
address_v4, sizeof(address_v4))) {
have_v4 = true;
}
case AF_INET6:
if (inet_ntop(sa->sin_family, &sa->sin_addr,
address_v6, sizeof(address_v6))) {
have_v6 = true;
}
}
}

if (!have_v4 && !have_v6) {
IIO_WARNING("DNS SD: Can't resolve valid address for service %s.\n", name);
IIO_WARNING("DNS SD: Can't resolve valid address for "
"service %s.\n", name);
goto exit;
}

Expand All @@ -129,17 +127,18 @@ static void __cfnet_browser_cb (

dd->port = port;
dd->hostname = strdup(hostname);
if (have_v4) {

if (have_v4)
iio_strlcpy(dd->addr_str, address_v4, sizeof(dd->addr_str));
} else if(have_v6) {
else if(have_v6)
iio_strlcpy(dd->addr_str, address_v6, sizeof(dd->addr_str));
}

IIO_DEBUG("DNS SD: added %s (%s:%d)\n", hostname, dd->addr_str, port);

if (have_v4 || have_v6) {
// A list entry was filled, prepare new item on the list.
if (!new_discovery_data(&dd->next)) {
/* A list entry was filled, prepare new item on the list. */
dd->next = zalloc(sizeof(*dd->next));
if (dd->next) {
/* duplicate lock */
dd->next->lock = dd->lock;
} else {
Expand All @@ -161,26 +160,35 @@ static void __cfnet_browser_cb (
CFNetServiceBrowserStopSearch(browser, &anError);
}

int dnssd_find_hosts(struct dns_sd_discovery_data ** ddata)
int dnssd_find_hosts(struct dns_sd_discovery_data **ddata)
{
int ret = 0;
CFNetServiceClientContext clientContext = { 0 };
CFNetServiceBrowserRef serviceBrowser;
struct dns_sd_discovery_data *d;
CFRunLoopRunResult runRes;

This comment has been minimized.

Copy link
@barracuda156

barracuda156 Jul 13, 2024

Would be better to do this conditionally, since CFRunLoopRunResult is only present in macOS > 10.10.

UPD. Never mind, this was broken earlier, this commit only reshuffles the code.

CFRunLoopRef runLoop;
CFStringRef type;
CFStringRef domain;
CFStreamError error;
Boolean result;
int ret = 0;

IIO_DEBUG("DNS SD: Start service discovery.\n");

if (new_discovery_data(&d) < 0) {
d = zalloc(sizeof(*d));
if (!d)
return -ENOMEM;
}

d->lock = iio_mutex_create();
if (!d->lock) {
dnssd_free_all_discovery_data(d);
return -ENOMEM;
}

CFNetServiceClientContext clientContext = { 0, d, NULL, NULL, NULL };
CFNetServiceBrowserRef serviceBrowser = CFNetServiceBrowserCreate(
kCFAllocatorDefault, __cfnet_browser_cb, &clientContext);
clientContext.info = d;
serviceBrowser = CFNetServiceBrowserCreate(kCFAllocatorDefault,
__cfnet_browser_cb,
&clientContext);

if (serviceBrowser == NULL) {
IIO_ERROR("DNS SD: Failed to create service browser.\n");
Expand All @@ -189,34 +197,43 @@ int dnssd_find_hosts(struct dns_sd_discovery_data ** ddata)
goto exit;
}

CFRunLoopRef runLoop = CFRunLoopGetCurrent();
CFNetServiceBrowserScheduleWithRunLoop(serviceBrowser, runLoop, kCFRunLoopDefaultMode);
runLoop = CFRunLoopGetCurrent();
CFNetServiceBrowserScheduleWithRunLoop(serviceBrowser, runLoop,
kCFRunLoopDefaultMode);

CFStringRef type = CFSTR("_iio._tcp.");
CFStringRef domain = CFSTR("");
CFStreamError error;
Boolean result = CFNetServiceBrowserSearchForServices(serviceBrowser, domain, type, &error);
type = CFSTR("_iio._tcp.");
domain = CFSTR("");
result = CFNetServiceBrowserSearchForServices(serviceBrowser,
domain, type, &error);

if (result == false) {
IIO_ERROR("DNS SD: CFNetServiceBrowserSearchForServices failed (domain = %ld, error = %d)\n",
(long)error.domain, error.error);
IIO_ERROR("DNS SD: CFNetServiceBrowserSearchForServices "
"failed (domain = %ld, error = %d)\n",
(long)error.domain, error.error);

ret = -ENXIO;
} else {
CFRunLoopRunResult runRes = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 2, TRUE);
runRes = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 2, true);

if (runRes != kCFRunLoopRunHandledSource && runRes != kCFRunLoopRunTimedOut) {
if (runRes == kCFRunLoopRunFinished)
IIO_ERROR("DSN SD: CFRunLoopRunInMode completed kCFRunLoopRunFinished (%d)\n", runRes);
else if (runRes == kCFRunLoopRunStopped)
IIO_ERROR("DSN SD: CFRunLoopRunInMode completed kCFRunLoopRunStopped (%d)\n", runRes);
else
IIO_ERROR("DSN SD: CFRunLoopRunInMode completed for unknown reason (%d)\n", runRes);
if (runRes == kCFRunLoopRunFinished) {
IIO_ERROR("DNS SD: CFRunLoopRunInMode completed "
"kCFRunLoopRunFinished (%d)\n", runRes);
} else if (runRes == kCFRunLoopRunStopped) {
IIO_ERROR("DNS SD: CFRunLoopRunInMode completed "
"kCFRunLoopRunStopped (%d)\n", runRes);
} else {
IIO_ERROR("DNS SD: CFRunLoopRunInMode completed "
"for unknown reason (%d)\n", runRes);
}
} else {
if (runRes == kCFRunLoopRunHandledSource)
IIO_DEBUG("DSN SD: CFRunLoopRunInMode completed kCFRunLoopRunHandledSource (%d)\n", runRes);
else
IIO_DEBUG("DSN SD: CFRunLoopRunInMode completed kCFRunLoopRunTimedOut (%d)\n", runRes);
if (runRes == kCFRunLoopRunHandledSource) {
IIO_DEBUG("DNS SD: CFRunLoopRunInMode completed "
"kCFRunLoopRunHandledSource (%d)\n", runRes);
} else {
IIO_DEBUG("DNS SD: CFRunLoopRunInMode completed "
"kCFRunLoopRunTimedOut (%d)\n", runRes);
}
}

port_knock_discovery_data(&d);
Expand All @@ -228,7 +245,8 @@ int dnssd_find_hosts(struct dns_sd_discovery_data ** ddata)
CFRelease(serviceBrowser);
serviceBrowser = NULL;

IIO_DEBUG("DNS SD: Completed service discovery, return code : %d\n", ret);
IIO_DEBUG("DNS SD: Completed service discovery, "
"return code : %d\n", ret);

exit:
iio_mutex_destroy(d->lock);
Expand Down

0 comments on commit a2eb9b6

Please sign in to comment.