chibipub

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit cf6ffb9d5a17ba2539f497c9395cfebb01d564f3
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 18 Nov 2020 09:14:06 -0800

initial commit

Diffstat:
A.envrc | 1+
A.gitignore | 2++
AMakefile | 5+++++
Adefault.nix | 7+++++++
Awolfsocks.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 97 insertions(+), 0 deletions(-)

diff --git a/.envrc b/.envrc @@ -0,0 +1 @@ +use nix diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +.build-result +/wolfsocks diff --git a/Makefile b/Makefile @@ -0,0 +1,5 @@ + +CFLAGS = -Wall -Werror -std=c89 + +wolfsocks: wolfsocks.c + $(CC) $(CFLAGS) $< $(LDFLAGS) -o $@ diff --git a/default.nix b/default.nix @@ -0,0 +1,7 @@ +{ pkgs ? import <nixpkgs> {} }: +with pkgs; +stdenv.mkDerivation { + name = "project"; + nativeBuildInputs = [ ]; + buildInputs = [ ]; +} diff --git a/wolfsocks.c b/wolfsocks.c @@ -0,0 +1,82 @@ + +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <netdb.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> + +#define BUF_SIZE 1024 + +void error(char *msg) +{ + perror(msg); + exit(1); +} + +int main(int argc, char *argv[]) +{ + static const char buffer[BUF_SIZE]; + int client_len; + + FILE *stream; + int parent, client; + struct sockaddr_in server_addr, client_addr; + struct hostent *client_host; + char *client_addr_str; + + if ((parent = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + error("socket"); + } + + memset(&server_addr, 0, sizeof(server_addr)); + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + server_addr.sin_port = htons(5188); + + if (bind(parent, (struct sockaddr *)&server_addr, + sizeof(server_addr)) < 0) { + error("bind"); + } + + if (listen(parent, 5) < 0) { + error("listen"); + } + + client_len = sizeof(client_addr); + while (1) { + client = accept(parent, (struct sockaddr *) &client_addr, + &client_len); + if (client < 0) { + error("accept"); + } + + client_host = gethostbyaddr( + (const char *)&client_addr.sin_addr.s_addr, + sizeof(client_addr.sin_addr.s_addr), AF_INET); + + if (!client_host) { + error("gethostbyaddr"); + } + + client_addr_str = inet_ntoa(client_addr.sin_addr); + + if (!client_addr_str) { + error("inet_htoa"); + } + + if ((stream = open(client, "r+")) == NULL) { + error("fdopen"); + } + + fgets(buffer, BUF_SIZE, stream); + printf("%s\n", buffer); + fclose(stream); + exit(2); + } + + return 0; +}