From ebfdeec1134703f659e547cd5f2f8e6e785106a5 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Thu, 27 May 2021 14:44:56 +0100 Subject: [PATCH] network: Implement and use new .scan callback 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 --- dns_sd.c | 34 ++++++++-------------------------- dns_sd.h | 4 ++++ iio-config.h.cmakein | 2 ++ iio-private.h | 2 -- network.c | 1 + scan.c | 8 -------- 6 files changed, 15 insertions(+), 36 deletions(-) diff --git a/dns_sd.c b/dns_sd.c index a50c70a79..e8b999866 100644 --- a/dns_sd.c +++ b/dns_sd.c @@ -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]; @@ -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); } /* @@ -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); @@ -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); diff --git a/dns_sd.h b/dns_sd.h index 567b79c9d..8f41ece9d 100644 --- a/dns_sd.h +++ b/dns_sd.h @@ -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 @@ -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 */ diff --git a/iio-config.h.cmakein b/iio-config.h.cmakein index a65102653..88b89c179 100644 --- a/iio-config.h.cmakein +++ b/iio-config.h.cmakein @@ -41,4 +41,6 @@ #cmakedefine NO_THREADS #cmakedefine HAS_LIBUSB_GETVERSION +#define IF_ENABLED(cfg, ptr) ((cfg) ? (ptr) : NULL) + #endif /* IIO_CONFIG_H */ diff --git a/iio-private.h b/iio-private.h index 94eff5e6b..939db2e75 100644 --- a/iio-private.h +++ b/iio-private.h @@ -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); diff --git a/network.c b/network.c index 82cc081a8..34db9e27b 100644 --- a/network.c +++ b/network.c @@ -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, diff --git a/scan.c b/scan.c index 30a9d2eda..50159b0ff 100644 --- a/scan.c +++ b/scan.c @@ -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; }