Skip to content

Commit

Permalink
network: Add timeout argument to create_socket()
Browse files Browse the repository at this point in the history
Instead of hardcoding a DEFAULT_TIMEOUT_MS timeout value in
create_socket(), get the timeout as an argument to the function.

This requires all callers that don't have access to the timeout
value to be modified to receive this value as a function argument.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed May 20, 2021
1 parent 6d85454 commit 4735905
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
11 changes: 10 additions & 1 deletion dns_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "dns_sd.h"
#include "iio-lock.h"
#include "iio-private.h"
#include "network.h"

#ifdef _WIN32
#include <winsock2.h>
Expand All @@ -32,6 +33,8 @@
#define close(s) closesocket(s)
#endif

#define DEFAULT_TIMEOUT_MS 5000

static void dnssd_free_discovery_data(struct dns_sd_discovery_data *d)
{
free(d->hostname);
Expand Down Expand Up @@ -173,8 +176,14 @@ void port_knock_discovery_data(const struct iio_context_params *params,
struct dns_sd_discovery_data **ddata)
{
struct dns_sd_discovery_data *d, *ndata;
unsigned int timeout_ms;
int i, ret;

if (params->timeout_ms)
timeout_ms = params->timeout_ms;
else
timeout_ms = DEFAULT_TIMEOUT_MS;

d = *ddata;
iio_mutex_lock(d->lock);
for (i = 0, ndata = d; ndata->next != NULL; ) {
Expand All @@ -197,7 +206,7 @@ void port_knock_discovery_data(const struct iio_context_params *params,
gai_strerror(ret));
} else {
for (rp = res; rp != NULL; rp = rp->ai_next) {
fd = create_socket(rp);
fd = create_socket(rp, timeout_ms);
if (fd < 0) {
IIO_DEBUG("Unable to open %s%s socket ('%s:%d' %s)\n",
rp->ai_family == AF_INET ? "ipv4" : "",
Expand Down
4 changes: 0 additions & 4 deletions dns_sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ struct dns_sd_cb_data {
const struct iio_context_params *params;
};

/* This functions is implemented in network.c, but used in dns_sd.c
*/
int create_socket(const struct addrinfo *addrinfo);

/* These functions are common, and implemented in dns_sd_[*].c based on the
* implementations: avahi (linux), bonjour (mac), or ServiceDiscovery (Win10)
*/
Expand Down
15 changes: 7 additions & 8 deletions network.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,8 @@ static int do_connect(int fd, const struct addrinfo *addrinfo,
return 0;
}

int create_socket(const struct addrinfo *addrinfo)
int create_socket(const struct addrinfo *addrinfo, unsigned int timeout)
{
const unsigned int timeout = DEFAULT_TIMEOUT_MS;
int ret, fd, yes = 1;

fd = do_create_socket(addrinfo);
Expand Down Expand Up @@ -303,7 +302,7 @@ static int network_open(const struct iio_device *dev,
if (ppdata->io_ctx.fd >= 0)
goto out_mutex_unlock;

ret = create_socket(pdata->addrinfo);
ret = create_socket(pdata->addrinfo, DEFAULT_TIMEOUT_MS);
if (ret < 0) {
IIO_ERROR("Create socket: %d\n", ret);
goto out_mutex_unlock;
Expand Down Expand Up @@ -1007,8 +1006,8 @@ struct iio_context * network_create_context(const struct iio_context_params *par
struct iio_context *ctx;
struct iiod_client *iiod_client;
struct iio_context_pdata *pdata;
unsigned int i, timeout_ms = DEFAULT_TIMEOUT_MS;
size_t uri_len;
unsigned int i;
int fd, ret;
char *description, *uri;
#ifdef _WIN32
Expand Down Expand Up @@ -1084,7 +1083,7 @@ struct iio_context * network_create_context(const struct iio_context_params *par
return NULL;
}

fd = create_socket(res);
fd = create_socket(res, timeout_ms);
if (fd < 0) {
errno = -fd;
goto err_free_addrinfo;
Expand All @@ -1107,7 +1106,7 @@ struct iio_context * network_create_context(const struct iio_context_params *par
pdata->iiod_client = iiod_client;
pdata->io_ctx.fd = fd;
pdata->addrinfo = res;
pdata->io_ctx.timeout_ms = DEFAULT_TIMEOUT_MS;
pdata->io_ctx.timeout_ms = timeout_ms;

pdata->msg_trunc_supported = msg_trunc_supported(&pdata->io_ctx);
if (pdata->msg_trunc_supported)
Expand Down Expand Up @@ -1161,7 +1160,7 @@ struct iio_context * network_create_context(const struct iio_context_params *par
}

dev->pdata->io_ctx.fd = -1;
dev->pdata->io_ctx.timeout_ms = DEFAULT_TIMEOUT_MS;
dev->pdata->io_ctx.timeout_ms = timeout_ms;
#ifdef WITH_NETWORK_GET_BUFFER
dev->pdata->memfd = -1;
#endif
Expand Down Expand Up @@ -1193,7 +1192,7 @@ struct iio_context * network_create_context(const struct iio_context_params *par

free(uri);
iiod_client_set_timeout(pdata->iiod_client, &pdata->io_ctx,
calculate_remote_timeout(DEFAULT_TIMEOUT_MS));
calculate_remote_timeout(timeout_ms));
return ctx;

err_free_uri:
Expand Down
1 change: 1 addition & 0 deletions network.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void cleanup_cancel(struct iiod_client_pdata *io_ctx);
void do_cancel(struct iiod_client_pdata *io_ctx);
int wait_cancellable(struct iiod_client_pdata *io_ctx, bool read);

int create_socket(const struct addrinfo *addrinfo, unsigned int timeout);
int do_create_socket(const struct addrinfo *addrinfo);
int do_select(int fd, unsigned int timeout);

Expand Down

0 comments on commit 4735905

Please sign in to comment.