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 fc3009a32d8bbcb7b050b779d0b4040e78abda56
Author: depsterr <depsterr@protonmail.com>
Date:   Fri,  3 Jul 2020 16:58:20 +0200

Added initial files

Diffstat:
A.gitignore | 2++
ALICENSE | 21+++++++++++++++++++++
AMakefile | 10++++++++++
Anetc.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +*.so +*.o diff --git a/LICENSE b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) depsterr <depsterr at protonmail dot com> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile @@ -0,0 +1,10 @@ +PREFIX?=/usr + +default: + $(CC) netc.c -shared -o netc.so +object: + $(CC) netc.c -c -o netc.o +install: default + install -Dm755 netc.so $(DESTDIR)$(PREFIX)/lib64/netc.so +clean: + rm -f netc.o netc.so diff --git a/netc.c b/netc.c @@ -0,0 +1,53 @@ +#include <sys/socket.h> +#include <sys/stat.h> +#include <sys/types.h> + +#include <fcntl.h> +#include <netdb.h> +#include <unistd.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +typedef enum NetcError { + GET_ADDR_INFO = -1, + OPEN_CONNECTION = -2, +} NetcError; + +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."; + } +} + +int netc_open(const char* host, const char* port) { + struct addrinfo hints, *res = NULL; + int fd = -1; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; /* IPv4 or IPv6 */ + hints.ai_flags = AI_NUMERICSERV; /* No name lookup */ + hints.ai_socktype = SOCK_STREAM; + if(getaddrinfo(host, port, &hints, &res)) + return GET_ADDR_INFO; + + for(;res;res = res->ai_next) { + if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) + continue; + if (connect(fd, res->ai_addr, res->ai_addrlen) == -1) { + close(fd); + continue; + } + } + + if (fd == -1) + return OPEN_CONNECTION; + + freeaddrinfo(res); + + return fd; +}