hfingerd

hhvn.uk:79
Log | Files | Refs | LICENSE

commit 0b38c16e99e6f7c62fe275703ed16c3140ed6482
Author: hhvn <dev@hhvn.uk>
Date:   Sat,  5 Jun 2021 18:59:55 +0100

makefile arg.h main.c: init basic server

Diffstat:
Aarg.h | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amain.c | 177+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amakefile | 12++++++++++++
3 files changed, 259 insertions(+), 0 deletions(-)

diff --git a/arg.h b/arg.h @@ -0,0 +1,70 @@ +/* + * Copy me if you can. + * by 20h + * + * 2012-2016 Christoph Lohmann <20h at r-36 dot net> + * + * 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. + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][0] == '-'\ + && argv[0][1];\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + int i_;\ + for (i_ = 1, brk_ = 0, argv_ = argv;\ + argv[0][i_] && !brk_;\ + i_++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][i_];\ + switch (argc_) + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define EARGF(x) ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][i_+1] != '\0')?\ + (&argv[0][i_+1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][i_+1] != '\0')?\ + (&argv[0][i_+1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/main.c b/main.c @@ -0,0 +1,177 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <libgen.h> +#include <stdarg.h> +#include <netdb.h> +#include <errno.h> +#include <pwd.h> +#include <sys/types.h> +#include <sys/socket.h> +#include "arg.h" + +#define EXIT_USAGE 2 +#define HOST_DFLT "localhost" +#define PORT_DFLT "79" +#define CQUEUE 255 + +char *argv0; + +void +usage(void) { + printf("usage: %s [-h host] [-p port]\n", basename(argv0)); + exit(EXIT_USAGE); +} + +int +verbose(const char *format, ...) { + va_list ap; + + va_start(ap, format); + vfprintf(stderr, format, ap); + va_end(ap); + + return 1; +} + +int +error(const char *format, ...) { + va_list ap; + + va_start(ap, format); + fprintf(stderr, "Error: "); + vfprintf(stderr, format, ap); + va_end(ap); + + return 1; +} + +int +die(const int exitc, const char *format, ...) { + va_list ap; + + va_start(ap, format); + fprintf(stderr, "Fatal: "); + vfprintf(stderr, format, ap); + va_end(ap); + + exit(exitc); +} + +int +getsock(struct addrinfo *hints, char *host, char *port) { + struct addrinfo *ai; + int sret, sockopt; + int fd; + + if ((sret = getaddrinfo(host, port, hints, &ai)) != 0 || ai == NULL) + die(1, "getaddrinfo() for %s:%s, %s\n", host, port, gai_strerror(sret)); + + fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + sockopt = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (int*)&sockopt, sizeof(int)) == -1) + die(1, "setsockopt(): %s\n", strerror(errno)); + if (bind(fd, ai->ai_addr, ai->ai_addrlen) == -1) + die(1, "bind(): %s\n", strerror(errno)); + if (listen(fd, CQUEUE) == -1) + die(1, "listen(): %s\n", strerror(errno)); + verbose("Listening on port %s with fd %d\n", port, fd); + + freeaddrinfo(ai); + + return fd; +} + +int +read_line(int fd, char *dest, size_t len) { + size_t i = 0; + char c = 0; + + do { + if (read(fd, &c, sizeof(char)) != sizeof(char)) + return 0; + if (c != '\r') + dest[i++] = c; + } while (c != '\n' && i < len); + + dest[i-1] = '\0'; + return 1; +} + +void +get_userlist(int fd) { + /* TODO */ + return; +} + +void +get_plan(int fd, char *user) { + /* TODO */ + return; +} + +void +handler(int fd) { + char user[1024]; + + read_line(fd, user, sizeof(user)); + + if (user[0] == '\0') + get_userlist(fd); + else + get_plan(fd, user); +} + +int +main(int argc, char *argv[]) { + struct sockaddr *addr; + struct addrinfo hints; + char *host = NULL, + *port = NULL; + int sock, handle; + int serrno; + pid_t pid; + + ARGBEGIN { + case 'h': + host = EARGF(usage()); + break; + case 'p': + port = EARGF(usage()); + break; + } ARGEND; + + if (host == NULL) + host = HOST_DFLT; + if (port == NULL) + port = PORT_DFLT; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_flags = AI_PASSIVE; + hints.ai_socktype = SOCK_STREAM; + sock = getsock(&hints, host, port); + + for (;;) { + if ((handle = accept(sock, addr, (socklen_t *)sizeof(struct sockaddr))) == -1) + die(1, "accept(): %s\n", strerror(errno)); + verbose("Accepted client with handle %d\n", handle); + + switch (pid = fork()) { + case -1: + error("fork(): %s\n", strerror(serrno)); + shutdown(handle, SHUT_RDWR); + close(handle); + break; + case 0: + handler(handle); + shutdown(handle, SHUT_RDWR); + close(handle); + break; + default: + verbose("Forking handle %d to PID %d\n", handle, pid); + } + } + return 0; +} diff --git a/makefile b/makefile @@ -0,0 +1,12 @@ +CC ?= cc +OBJ = main.o +BIN = hfingerd + +$(BIN): $(OBJ) + $(CC) -g $(CFLAGS) $(LDFLAGS) -o $@ $< + +.c.o: + $(CC) -g $(CFLAGS) $(LDFLAGS) -c $< + +clean: + rm $(BIN) $(OBJ)