Skip to content

Commit

Permalink
Add socket99_snprintf, for constructing nicer error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
silentbicycle committed Dec 15, 2014
1 parent a9422fa commit 6a9827f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
51 changes: 51 additions & 0 deletions socket99.c
Expand Up @@ -36,6 +36,7 @@ static bool set_defaults_and_check_cfg(socket99_config *cfg);
static bool make_tcp_udp(socket99_config *cfg, socket99_result *out);
static bool make_unixdomain(socket99_config *cfg, socket99_result *out);
static bool set_nonblocking(socket99_result *out);
static const char *status_key(enum socket99_status s);

/* Attempt to open a socket, according to the configuration stored in
* CFG. Returns whether the the socket opened; further details will be
Expand All @@ -62,6 +63,28 @@ bool socket99_open(socket99_config *cfg, socket99_result *res) {
return true;
}

/* Construct an error message in BUF, based on the status codes
* in *RES. This has the same return value and general behavior
* as snprintf -- if the return value is >= buf_size, the string
* has been truncated. Returns -1 if either BUF or RES are NULL. */
int socket99_snprintf(char *buf, size_t buf_size, socket99_result *res) {
if (buf == NULL || res == NULL) { return 0; }
return snprintf(buf, buf_size, "%s: %s",
status_key(res->status),
(res->status == SOCKET99_ERROR_GETADDRINFO
? gai_strerror(res->getaddrinfo_error)
: strerror(res->saved_errno)));
}

/* Print an error message based on the status contained in *RES. */
void socket99_fprintf(FILE *f, socket99_result *res) {
if (f == NULL || res == NULL) { return; }
fprintf(f, "%s: %s\n", status_key(res->status),
(res->status == SOCKET99_ERROR_GETADDRINFO
? gai_strerror(res->getaddrinfo_error)
: strerror(res->saved_errno)));
}

/* Set "hints" in an addrinfo struct, to be passed to getaddrinfo. */
void socket99_set_hints(socket99_config *cfg, struct addrinfo *hints) {
if (cfg == NULL || hints == NULL) { return; }
Expand Down Expand Up @@ -212,6 +235,8 @@ static bool make_tcp_udp(socket99_config *cfg, socket99_result *out) {
if (out->status == SOCKET99_OK) {
return fail_with_errno(out, SOCKET99_ERROR_UNKNOWN);
} else {
out->saved_errno = errno;
errno = 0;
return false;
}
}
Expand All @@ -233,3 +258,29 @@ static bool set_nonblocking(socket99_result *out) {
}
return true;
}

static const char *status_key(enum socket99_status s) {
switch (s) {
case SOCKET99_OK:
return "ok";
case SOCKET99_ERROR_GETADDRINFO:
return "getaddrinfo";
case SOCKET99_ERROR_SOCKET:
return "socket";
case SOCKET99_ERROR_BIND:
return "bind";
case SOCKET99_ERROR_LISTEN:
return "listen";
case SOCKET99_ERROR_CONNECT:
return "connect";
case SOCKET99_ERROR_FCNTL:
return "fcntl";
case SOCKET99_ERROR_SNPRINTF:
return "snprintf";
case SOCKET99_ERROR_CONFIGURATION:
return "configuration";
case SOCKET99_ERROR_UNKNOWN:
default:
return "unknown";
}
}
11 changes: 10 additions & 1 deletion socket99.h
Expand Up @@ -2,7 +2,7 @@
#define SOCKET99_H

#define SOCKET99_VERSION_MAJOR 0
#define SOCKET99_VERSION_MINOR 1
#define SOCKET99_VERSION_MINOR 1 // dev, pre 0.2.0
#define SOCKET99_VERSION_PATCH 0

#include <stdint.h>
Expand Down Expand Up @@ -69,6 +69,15 @@ typedef struct {
* stored in RES. */
bool socket99_open(socket99_config *cfg, socket99_result *res);

/* Construct an error message in BUF, based on the status codes
* in *RES. This has the same return value and general behavior
* as snprintf -- if the return value is >= buf_size, the string
* has been truncated. */
int socket99_snprintf(char *buf, size_t buf_size, socket99_result *res);

/* Print an error message based on the status contained in *RES. */
void socket99_fprintf(FILE *f, socket99_result *res);

/* Set "hints" in an addrinfo struct, to be passed to getaddrinfo. */
void socket99_set_hints(socket99_config *cfg, struct addrinfo *hints);

Expand Down
2 changes: 1 addition & 1 deletion test_socket99.c
Expand Up @@ -94,7 +94,7 @@ int main(int argc, char **argv) {
printf("pass %s\n", tc->name);
return 0;
} else {
printf("FAIL %s\n", tc->name);
socket99_fprintf(stderr, res);
return 1;
}
} else {
Expand Down

0 comments on commit 6a9827f

Please sign in to comment.