Wrapper library for the BSD sockets API with a nicer C99 interface

Overview

A wrapper library for the BSD sockets API.

Why?

This library trades the series of getaddrinfo, socket, connect, bind, listen, etc. functions and their convoluted, casted arguments for just one function that takes two structs (configuration and output). By creatively using C99's "designated initializers", the configuration struct works rather like a configuration key/value hash; the output struct contains either the socket file descriptor or error information.

The sheer generality of the BSD sockets API also makes it rather unwieldy. While the sockets API can be used for a lot of esoteric things, there's no reason common use cases such as opening a TCP socket to a given host and port should take dozens of lines of code.

License

socket99 is released under the ISC license.

Requirements

This depends on C99 and a POSIX environment. You've got one of those lying around somewhere, right?

Basic Usage

Look at the fields in struct socket99_config listen in socket99.h, call socket99_open with a pointer to a configuration struct using the C99 designated initializer syntax. Only a few of the fields will be used, such as:

socket99_config cfg = {
    .host = "127.0.0.1",
    .port = 8080,
    .server = true,
    .nonblocking = true,
};

for a non-blocking TCP server that listens to 127.0.0.1. This function will return a bool for whether the socket was successfully created, and the result struct argument will be modified to contain a status code and either a file descriptor (on success) or error information on failure:

socket99_result res;   // result output in this struct
bool ok = socket99_open(&cfg, &res);

The configuration and result structs are no longer needed after the result struct's file descriptor has been saved / errors are handled, so both structs can be stack-allocated.

For more usage examples, look at test_socket99.c.

Running the tests

To run the tests:

$ make test

Note that the tests create a couple short-lived sockets on port 8080, and if that port is already in use, the tests will fail. To run the tests on a different port, set the PORT environment variable:

$ env PORT=12345 make test

Supported Use Cases

  • Client and server

  • TCP, UDP, and Unix domain sockets (either stream and datagram)

  • Blocking and nonblocking

  • IPV4, IPv6, and "don't care"

  • setsockopt(2) options

Future Development

Capturing other common use cases for sockets would be good.

Comments
  • Found memleak if make_tcp_udp returns unsuccessfully...

    Found memleak if make_tcp_udp returns unsuccessfully...

    Hello, I found a memory leak if make_tcp_udp returns unsuccessfully after getaddrinfo is called on line 197 of socket99.c. I've added freeaddrinfo calls before the relevant return statements within make_tcp_udp to fix this memory leak.

    opened by robert-warmka 3
  • Added -D_DARWIN_C_SOURCE=1 to compiler flags to ensure non-default exten...

    Added -D_DARWIN_C_SOURCE=1 to compiler flags to ensure non-default exten...

    I found this solution which eliminates the build warning when built on OSX Clang stating that snprintf is not defined.

    By default, when POSIX compliance is enabled, snprintf, and some other extensions are not available in the default stdio inclusion. Adding this flag re-enables these extensions, in addition to the POSIX requirements.

    I stumbled on this patch in this thread

    opened by barneywilliams 2
  • implicit snprintf declaration warning

    implicit snprintf declaration warning

    The -D_POSIX_C_SOURCE=1 define appears to exclude snprintf's prototype from stdio.h in some environments. Figure out if there's a different POSIX version that should be used, or another define that is also needed.

    opened by silentbicycle 2
  • Shouldn't you zero initialize the socket99_cfg struct before using it to avoid garbage data?

    Shouldn't you zero initialize the socket99_cfg struct before using it to avoid garbage data?

    So just from a quick look at this code, I'm wondering how exactly you avoid situations where the socket99_cfg struct is polluted with garbage data. In the socket99_open function, it's assumed that cfg->path will be NULL if the user doesn't want to make a UNIX domain socket, but if there's garbage data in the cfg->path member, would that not cause it to accidentally try creating a UNIX domain socket instead?

    opened by zeeman 1
  • Add function to construct error messages

    Add function to construct error messages

    Rather than making the user call strerror(3), etc., it should have a function that takes a socket99_result * and fprintf's e.g. "connect: Connection refused".

    enhancement 
    opened by silentbicycle 1
Owner
Scott Vokes
Scott Vokes
A simple tcp tunnel on c using sockets Right now it only supports linux systems

A simple tcp tunnel on c using sockets Right now it only supports linux systems build BY MAKE mkdir build make cd build ./tunnel.o <localport> <rem

notaweeb 8 Sep 20, 2021
Built a client-server application using TCP and UDP sockets, in which the clients can subscribe/unsubscribe to various topics.

Built a client-server application using TCP and UDP sockets, in which the clients can subscribe/unsubscribe to various topics.

null 1 Jun 22, 2022
Provide translation, currency conversion, and voting services. First using telnet you create a connection to a TCP socket, then the server connects to 3 UDP sockets hosted on other servers to do tasks.

to run micro servers g++ translator.cpp -o translator ./translator <port 1> g++ voting.cpp -o voting ./voting <port 2> g++ currency_converter.cpp -o c

Jacob Artuso 1 Oct 29, 2021
Creating a server-client application with C sockets.

C-ServerClient Creating a server-client application with C socket. How to use? Clone the project and cd in to the main directory. Open a terminal and

AmirH.Najafizadeh 9 Oct 2, 2022
CES - Coroutines, Epoll and Sockets

CES - Coroutines Epoll and Sockets This is a demonstration project for a complex coroutine use case. Non-production code Please note, that because of

RNDr. Simon Toth 32 Dec 24, 2022
A special version of Packet Batch that utilizes AF_XDP Linux sockets (this should be faster than the standard version, but not as fast as the DPDK).

Packet Batch (AF_XDP) Description This is a special version of Packet Batch that utilizes AF_XDP sockets instead of AF_PACKETv3 (which is what the sta

Packet Batch 17 Nov 9, 2022
The standard Packet Batch application that uses standard Linux sockets (AF_PACKETv3) for packet generation.

Packet Batch (Standard) Description This is the standard Packet Batch application that utilizes AF_PACKETv3 Linux sockets. Due to AF_PACKETv3 Linux so

Packet Batch 7 Oct 28, 2022
Simple local P2P chat on UDP sockets

Local P2P Chat This is a fully decentralized chat. To communicate, simply run it on computers in a single local network (using one port). All messages

Anton Khalitov 18 Dec 8, 2022
GnuTLS implements the TLS/SSL (Transport Layer Security aka Secure Sockets Layer) protocol

GnuTLS implements the TLS/SSL (Transport Layer Security aka Secure Sockets Layer) protocol

Jonathan Bastien-Filiatrault 3 Jun 3, 2021
An HTML5 parsing library in pure C99

Gumbo - A pure-C HTML5 parser. Gumbo is an implementation of the HTML5 parsing algorithm implemented as a pure C99 library with no outside dependencie

Google 5.1k Dec 25, 2022
The Telegram Bot API provides an HTTP API for creating Telegram Bots.

The Telegram Bot API provides an HTTP API for creating Telegram Bots.

Telegram Library 1.9k Jan 1, 2023
Header-only, event based, tiny and easy to use libuv wrapper in modern C++ - now available as also shared/static library!

Do you have a question that doesn't require you to open an issue? Join the gitter channel. If you use uvw and you want to say thanks or support the pr

Michele Caini 1.5k Jan 9, 2023
WinINet wrapper - tiny windows HTTPS library, no dependencies.

WNetWrap A tiny, dependency-free wrapper around WinINet for developers targeting Windows only, who need a lightweight native solution. Inspired by the

hack-tramp 26 Nov 4, 2022
C++ wrapper for librg network library

librg-cpp Description C++ wrapper for librg network library Warning: The wrapper is under heavy development and will have breaking changes still a fir

null 6 Sep 28, 2020
Mongoose Embedded Web Server Library - a multi-protocol embedded networking library with TCP/UDP, HTTP, WebSocket, MQTT built-in protocols, async DNS resolver, and non-blocking API.

Mongoose - Embedded Web Server / Embedded Networking Library Mongoose is a networking library for C/C++. It implements event-driven non-blocking APIs

Cesanta Software 9k Jan 1, 2023
Header-only C++14 library for getting network addresses associated with network interface without name lookups on Windows, macOS, Linux, and FreeBSD

NetIF Get addresses associated with network interfaces on a system without using name lookups. Header-only, requires C++14. Usage Add the header file

GMLC-TDC 9 Oct 17, 2022
Netif - Header-only C++14 library for getting network addresses associated with network interface without name lookups on Windows, macOS, Linux, and FreeBSD

NetIF Get addresses associated with network interfaces on a system without using name lookups. Header-only, requires C++14. Usage Add the header file

GMLC-TDC 9 Oct 17, 2022
libRocket - The HTML/CSS User Interface library

libRocket - The HTML/CSS User Interface Library http://librocket.com (!!! malicious domain !!!, last checked 23/feb/2020) libRocket is the C++ user in

null 1.1k Dec 31, 2022
An object oriented C++ wrapper for CURL (libcurl)

curlcpp An object-oriented C++ wrapper for cURL tool If you want to know a bit more about cURL and libcurl, you should go on the official website http

Giuseppe Persico 549 Jan 9, 2023