Skip to content

Commit

Permalink
network: Implement and use new .scan callback
Browse files Browse the repository at this point in the history
Drop the old dnssd_context_scan() function and implement a proper
.scan() callback instead. The use of iio_scan_add_result() allows to
significantly reduce the code size.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Jun 15, 2021
1 parent 40f1961 commit ebfdeec
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 36 deletions.
34 changes: 8 additions & 26 deletions dns_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ static void dnssd_remove_node(const struct iio_context_params *params,
* DNS Service Discovery is turned on
*/

static int dnssd_fill_context_info(const struct iio_context_params *params,
struct iio_context_info *info,
char *hostname, char *addr_str, int port)
static int dnssd_add_scan_result(const struct iio_context_params *params,
struct iio_scan *scan_ctx,
char *hostname, char *addr_str, int port)
{
struct iio_context *ctx;
char uri[sizeof("ip:") + MAXHOSTNAMELEN + sizeof (":65535") + 1];
Expand Down Expand Up @@ -134,17 +134,7 @@ static int dnssd_fill_context_info(const struct iio_context_params *params,

iio_context_destroy(ctx);

info->uri = iio_strdup(uri);
if (!info->uri)
return -ENOMEM;

info->description = iio_strdup(description);
if (!info->description) {
free(info->uri);
return -ENOMEM;
}

return 0;
return iio_scan_add_result(scan_ctx, description, uri);
}

/*
Expand Down Expand Up @@ -250,11 +240,10 @@ void remove_dup_discovery_data(const struct iio_context_params *params,
*ddata = d;
}

int dnssd_context_scan(struct iio_scan_result *scan_result)
int dnssd_context_scan(const struct iio_context_params *params,
struct iio_scan *ctx)
{
const struct iio_context_params *params = get_default_params();
struct dns_sd_discovery_data *ddata, *ndata;
struct iio_context_info *info;
int ret = 0;

ret = dnssd_find_hosts(params, &ddata);
Expand All @@ -269,15 +258,8 @@ int dnssd_context_scan(struct iio_scan_result *scan_result)
goto fail;

for (ndata = ddata; ndata->next != NULL; ndata = ndata->next) {
info = iio_scan_result_add(scan_result);
if (!info) {
prm_err(params, "Out of memory when adding new scan result\n");
ret = -ENOMEM;
break;
}

ret = dnssd_fill_context_info(params, info,
ndata->hostname, ndata->addr_str,ndata->port);
ret = dnssd_add_scan_result(params, ctx, ndata->hostname,
ndata->addr_str,ndata->port);
if (ret < 0) {
prm_dbg(params, "Failed to add %s (%s) err: %d\n",
ndata->hostname, ndata->addr_str, ret);
Expand Down
4 changes: 4 additions & 0 deletions dns_sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct addrinfo;
struct AvahiSimplePoll;
struct AvahiAddress;
struct iio_context_params;
struct iio_scan;

/* Common structure which all dns_sd_[*] files fill out
* Anything that is dynamically allocated (malloc) needs to be managed
Expand Down Expand Up @@ -88,4 +89,7 @@ void port_knock_discovery_data(const struct iio_context_params *params,
int dnssd_resolve_host(const struct iio_context_params *params,
const char *hostname, char *ip_addr, const int addr_len);

int dnssd_context_scan(const struct iio_context_params *params,
struct iio_scan *ctx);

#endif /* __IIO_DNS_SD_H */
2 changes: 2 additions & 0 deletions iio-config.h.cmakein
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@
#cmakedefine NO_THREADS
#cmakedefine HAS_LIBUSB_GETVERSION

#define IF_ENABLED(cfg, ptr) ((cfg) ? (ptr) : NULL)

#endif /* IIO_CONFIG_H */
2 changes: 0 additions & 2 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ struct iio_context *
iio_create_dynamic_context(const struct iio_context_params *params,
const char *uri);

int dnssd_context_scan(struct iio_scan_result *scan_result);

ssize_t iio_device_get_sample_size_mask(const struct iio_device *dev,
const uint32_t *mask, size_t words);

Expand Down
1 change: 1 addition & 0 deletions network.c
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ static struct iio_context * network_clone(const struct iio_context *ctx)
}

static const struct iio_backend_ops network_ops = {
.scan = IF_ENABLED(HAVE_DNS_SD, dnssd_context_scan),
.create = network_create_context,
.clone = network_clone,
.open = network_open,
Expand Down
8 changes: 0 additions & 8 deletions scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ struct iio_scan * iio_scan(const struct iio_context_params *params,
}
}

if (HAVE_DNS_SD && has_backend(backends, "ip")) {
ret = dnssd_context_scan(&ctx->scan_result);
if (ret < 0) {
prm_perror(params, -ret,
"Unable to scan network context(s)");
}
}

return ctx;
}

Expand Down

0 comments on commit ebfdeec

Please sign in to comment.