Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sock_resolve_required #356

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add sock_resolve_required()
  • Loading branch information
gperciva committed May 13, 2022
commit 4036498c6e8eceeba923c26b7ea8f81ca1078a4b
32 changes: 32 additions & 0 deletions libcperciva/util/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,38 @@ sock_resolve(const char * addr)
return (NULL);
}

/**
* sock_resolve_required(addr):
* Return a NULL-terminated array of pointers to sock_addr structures.
* If the array would be empty, print an error and return NULL instead.
*/
struct sock_addr **
sock_resolve_required(const char * addr)
{
struct sock_addr ** sas;

/* Resolve target address. */
if ((sas = sock_resolve(addr)) == NULL) {
warnp("Error resolving socket address: %s", addr);
goto err0;
}

/* Check that the array is not empty. */
if (sas[0] == NULL) {
warn0("No addresses found for %s", addr);
goto err1;
}

/* Success! */
return (sas);

err1:
sock_addr_freelist(sas);
err0:
/* Failure! */
return (NULL);
}

/**
* sock_listener(sa):
* Create a socket, attempt to set SO_REUSEADDR, bind it to the socket address
Expand Down
7 changes: 7 additions & 0 deletions libcperciva/util/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ struct sock_addr;
*/
struct sock_addr ** sock_resolve(const char *);

/**
* sock_resolve_required(addr):
* Return a NULL-terminated array of pointers to sock_addr structures.
* If the array would be empty, print an error and return NULL instead.
*/
struct sock_addr ** sock_resolve_required(const char *);

/**
* sock_listener(sa):
* Create a socket, attempt to set SO_REUSEADDR, bind it to the socket address
Expand Down