Skip to content

Commit

Permalink
Mirror fixes from CUPS 2.x.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed May 21, 2024
1 parent f5ab915 commit 4b98470
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 27 deletions.
2 changes: 2 additions & 0 deletions cups/dest.c
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,8 @@ cups_dest_query_cb(
(void)if_index;
(void)rrtype;

DEBUG_printf("5cups_dest_query_cb(query=%p, context=%p, flags=0x%x, if_index=%u, fullname=\"%s\", rrtype=%u, rdata=%p, rdlen=%u)", (void *)query, context, (unsigned)flags, (unsigned)if_index, fullname, rrtype, rdata, rdlen);

// Only process "add" data...
if (!(flags & CUPS_DNSSD_FLAGS_ADD) || (flags & CUPS_DNSSD_FLAGS_ERROR))
return;
Expand Down
105 changes: 79 additions & 26 deletions cups/dnssd.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct _cups_dnssd_s // DNS-SD context

#else // HAVE_AVAHI
cups_mutex_t mutex; // Avahi poll mutex
bool in_callback; // Doing a callback?
AvahiClient *client; // Avahi client connection
AvahiSimplePoll *poll; // Avahi poll class
cups_thread_t monitor; // Monitoring thread
Expand Down Expand Up @@ -795,6 +796,12 @@ cupsDNSSDBrowseNew(
#elif _WIN32

#else // HAVE_AVAHI
if (!dnssd->in_callback)
{
DEBUG_puts("2cupsDNSSDBrowseNew: Locking mutex.");
cupsMutexLock(&dnssd->mutex);
}

browse->num_browsers = 1;
browse->browsers[0] = avahi_service_browser_new(dnssd->client, avahi_if_index(if_index), AVAHI_PROTO_UNSPEC, types, /*domain*/NULL, /*flags*/0, (AvahiServiceBrowserCallback)avahi_browse_cb, browse);

Expand All @@ -803,6 +810,13 @@ cupsDNSSDBrowseNew(
report_error(dnssd, "Unable to create DNS-SD browse request: %s", avahi_strerror(avahi_client_errno(dnssd->client)));
free(browse);
browse = NULL;

if (!dnssd->in_callback)
{
DEBUG_puts("2cupsDNSSDBrowseNew: Unlocking mutex.");
cupsMutexUnlock(&dnssd->mutex);
}

goto done;
}

Expand All @@ -818,7 +832,13 @@ cupsDNSSDBrowseNew(
}
}

avahi_simple_poll_wakeup(dnssd->poll);
if (!dnssd->in_callback)
{
DEBUG_puts("2cupsDNSSDBrowseNew: Unlocking mutex.");
cupsMutexUnlock(&dnssd->mutex);

avahi_simple_poll_wakeup(dnssd->poll);
}
#endif // HAVE_MDNSRESPONDER

DEBUG_printf("2cupsDNSSDBrowseNew: Adding browse=%p", (void *)browse);
Expand Down Expand Up @@ -908,6 +928,8 @@ cupsDNSSDQueryNew(
cups_dnssd_query_t *query; // Query request


DEBUG_printf("cupsDNSSDQueryNew(dnssd=%p, if_index=%u, fullname=\"%s\", rrtype=%u, query_cb=%p, cb_data=%p)", (void *)dnssd, if_index, fullname, rrtype, query_cb, cb_data);

// Range check input...
if (!dnssd || !fullname || !query_cb)
return (NULL);
Expand All @@ -926,6 +948,7 @@ cupsDNSSDQueryNew(
if (!dnssd->queries)
{
// Create an array of resolvers...
DEBUG_puts("2cupsDNSSDQueryNew: Creating queries array.");
if ((dnssd->queries = cupsArrayNew(NULL, NULL, NULL, 0, NULL, (cups_afree_cb_t)delete_query)) == NULL)
{
// Unable to create...
Expand All @@ -950,8 +973,21 @@ cupsDNSSDQueryNew(
#elif _WIN32

#else // HAVE_AVAHI
if (!dnssd->in_callback)
{
DEBUG_puts("4avahi_poll_cb: Locking mutex.");
cupsMutexLock(&dnssd->mutex);
}

query->browser = avahi_record_browser_new(dnssd->client, avahi_if_index(if_index), AVAHI_PROTO_UNSPEC, fullname, AVAHI_DNS_CLASS_IN, rrtype, 0, (AvahiRecordBrowserCallback)avahi_query_cb, query);
avahi_simple_poll_wakeup(dnssd->poll);

if (!dnssd->in_callback)
{
DEBUG_puts("4avahi_poll_cb: Unlocking mutex.");
cupsMutexUnlock(&dnssd->mutex);

avahi_simple_poll_wakeup(dnssd->poll);
}

if (!query->browser)
{
Expand Down Expand Up @@ -1070,23 +1106,6 @@ cupsDNSSDResolveNew(
resolve->cb = resolve_cb;
resolve->cb_data = cb_data;

DEBUG_puts("2cupsDNSSDResolveNew: Write locking rwlock.");
cupsRWLockWrite(&dnssd->rwlock);

if (!dnssd->resolves)
{
// Create an array of resolvers...
DEBUG_puts("2cupsDNSSDResolveNew: Creating resolver array.");
if ((dnssd->resolves = cupsArrayNew(NULL, NULL, NULL, 0, NULL, (cups_afree_cb_t)delete_resolve)) == NULL)
{
// Unable to create...
DEBUG_printf("2cupsDNSSDResolveNew: Unable to allocate memory: %s", strerror(errno));
free(resolve);
resolve = NULL;
goto done;
}
}

#ifdef HAVE_MDNSRESPONDER
DNSServiceErrorType error; // Error, if any

Expand All @@ -1102,8 +1121,21 @@ cupsDNSSDResolveNew(
#elif _WIN32

#else // HAVE_AVAHI
if (!dnssd->in_callback)
{
DEBUG_puts("2cupsDNSSDResolveNew: Locking mutex.");
cupsMutexLock(&dnssd->mutex);
}

resolve->resolver = avahi_service_resolver_new(dnssd->client, avahi_if_index(if_index), AVAHI_PROTO_UNSPEC, name, type, domain, AVAHI_PROTO_UNSPEC, /*flags*/0, (AvahiServiceResolverCallback)avahi_resolve_cb, resolve);
avahi_simple_poll_wakeup(dnssd->poll);

if (!dnssd->in_callback)
{
DEBUG_puts("2cupsDNSSDResolveNew: Unlocking mutex.");
cupsMutexUnlock(&dnssd->mutex);

avahi_simple_poll_wakeup(dnssd->poll);
}

if (!resolve->resolver)
{
Expand All @@ -1114,14 +1146,31 @@ cupsDNSSDResolveNew(
}
#endif // HAVE_MDNSRESPONDER

DEBUG_puts("2cupsDNSSDResolveNew: Write locking rwlock.");
cupsRWLockWrite(&dnssd->rwlock);

if (!dnssd->resolves)
{
// Create an array of resolvers...
DEBUG_puts("2cupsDNSSDResolveNew: Creating resolver array.");
if ((dnssd->resolves = cupsArrayNew(NULL, NULL, NULL, 0, NULL, (cups_afree_cb_t)delete_resolve)) == NULL)
{
// Unable to create...
DEBUG_printf("2cupsDNSSDResolveNew: Unable to allocate memory: %s", strerror(errno));
free(resolve);
resolve = NULL;
goto done;
}
}

DEBUG_printf("2cupsDNSSDResolveNew: Adding resolver %p.", (void *)resolve);
cupsArrayAdd(dnssd->resolves, resolve);

done:

DEBUG_puts("2cupsDNSSDResolveNew: Unlocking rwlock.");
cupsRWUnlock(&dnssd->rwlock);

done:

return (resolve);
}

Expand Down Expand Up @@ -2107,7 +2156,9 @@ avahi_browse_cb(
return;
}

browse->dnssd->in_callback = true;
(browse->cb)(browse, browse->cb_data, cups_flags, (uint32_t)if_index, name, type, domain);
browse->dnssd->in_callback = false;
}


Expand Down Expand Up @@ -2272,14 +2323,14 @@ avahi_poll_cb(struct pollfd *ufds, // I - File descriptors for poll

DEBUG_printf("3avahi_poll_cb(ufds=%p, nfds=%u, timeout=%d, dnssd=%p)", (void *)ufds, nfds, timeout, (void *)dnssd);

DEBUG_puts("4avahi_poll_cb: Locking mutex.");
DEBUG_puts("4avahi_poll_cb: Unlocking mutex.");
cupsMutexUnlock(&dnssd->mutex);

DEBUG_puts("4avahi_poll_cb: Polling sockets...");
ret = poll(ufds, nfds, timeout);
DEBUG_printf("4avahi_poll_cb: poll() returned %d...", ret);

DEBUG_puts("4avahi_poll_cb: Unlocking mutex.");
DEBUG_puts("4avahi_poll_cb: Locking mutex.");
cupsMutexLock(&dnssd->mutex);

return (ret);
Expand Down Expand Up @@ -2310,7 +2361,9 @@ avahi_query_cb(

DEBUG_printf("3avahi_query_cb(..., event=%s, fullname=\"%s\", ..., query=%p)", avahi_events[event], fullname, query);

(query->cb)(query, query->cb_data, event == AVAHI_BROWSER_NEW ? CUPS_DNSSD_FLAGS_NONE : CUPS_DNSSD_FLAGS_ERROR, (uint32_t)if_index, fullname, rrtype, rdata, rdlen);
query->dnssd->in_callback = true;
(query->cb)(query, query->cb_data, event == AVAHI_BROWSER_FAILURE ? CUPS_DNSSD_FLAGS_ERROR : event == AVAHI_BROWSER_NEW ? CUPS_DNSSD_FLAGS_ADD : CUPS_DNSSD_FLAGS_NONE, (uint32_t)if_index, fullname, rrtype, rdata, rdlen);
query->dnssd->in_callback = false;
}


Expand Down Expand Up @@ -2370,7 +2423,7 @@ avahi_resolve_cb(
DEBUG_printf("4avahi_resolve_cb: fullname=\"%s\"", fullname);

// Do the resolve callback and free the TXT record stuff...
(resolve->cb)(resolve, resolve->cb_data, event == AVAHI_RESOLVER_FOUND ? CUPS_DNSSD_FLAGS_NONE : CUPS_DNSSD_FLAGS_ERROR, (uint32_t)if_index, fullname, host, port, num_txt, txt);
(resolve->cb)(resolve, resolve->cb_data, event == AVAHI_RESOLVER_FAILURE ? CUPS_DNSSD_FLAGS_ERROR : CUPS_DNSSD_FLAGS_NONE, (uint32_t)if_index, fullname, host, port, num_txt, txt);

cupsFreeOptions(num_txt, txt);
}
Expand Down

0 comments on commit 4b98470

Please sign in to comment.