netc

A simple C library for opening network sockets using linux syscalls.
git clone git://depsterr.com/git/netc
Log | Files | Refs | README | LICENSE

commit 07cb89aa18a78decf68813c5e1ec566e52373ab1
parent fc3009a32d8bbcb7b050b779d0b4040e78abda56
Author: depsterr <depsterr@protonmail.com>
Date:   Fri,  3 Jul 2020 17:12:24 +0200

added info to readme and switched from switch to lookup table for error
decoding

Diffstat:
AREADME.md | 11+++++++++++
Mnetc.c | 12++++++------
2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,11 @@ +netc +==== + +A simple C library for opening network socket filedescriptors using linux syscalls. + +Functions +--------- + +To open a socket use `netc_open` which takes two arguments, both of type `const char*`, host and port. The function will then return a normal linux file descriptor. + +All functions which can fail may return an error value, to check if a function failed, simply check if it's return value is below zero. If a function failed you may use the `netc_decode_error` to get a string corresponding to the error code. diff --git a/netc.c b/netc.c @@ -15,13 +15,13 @@ typedef enum NetcError { OPEN_CONNECTION = -2, } NetcError; +static const char* NetcErrorStrings[] = { + [GET_ADDR_INFO * -1] = "Call to getaddrinfo failed.", + [OPEN_CONNECTION * -1] = "Unable to open a connection.", +}; + const char* netc_decode_error(NetcError err) { - switch (err) { - case GET_ADDR_INFO: - return "Call to getaddrinfo failed."; - case OPEN_CONNECTION: - return "Unable to open a connection."; - } + return NetcErrorStrings[err * -1]; } int netc_open(const char* host, const char* port) {