Skip to content

Commit d9b87fc

Browse files
Initial commit
This commit adds a simple version of tinycat with the bare minimum functionality. Tinycat is a lightweight (<100K) netcat alternative which is statically linked (against musl). Tinycat exclusively deals with TCP, and can only handle IP and UNIX domain sockets. The netcat options -k, -l and -U are implemented and they have the same meaning (refer to the help message for details).
0 parents  commit d9b87fc

31 files changed

+1659
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Build directory
2+
/build
3+
4+
# Build dependencies
5+
/deps

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "lib/argparse"]
2+
path = lib/argparse
3+
url = git@github.com:cofyc/argparse.git

CMakeLists.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
3+
set(CMAKE_C_COMPILER "${CMAKE_CURRENT_LIST_DIR}/deps/bin/musl-gcc")
4+
5+
project(tinycat C)
6+
7+
add_executable(tinycat)
8+
9+
add_subdirectory(lib)
10+
add_subdirectory(src)
11+
12+
target_link_libraries(
13+
tinycat
14+
PRIVATE
15+
argparse
16+
)
17+
18+
target_link_options(
19+
tinycat
20+
PRIVATE
21+
-static
22+
-static-libgcc
23+
)
24+
25+
target_include_directories(
26+
tinycat
27+
PRIVATE
28+
include
29+
)
30+
31+
target_compile_options(
32+
tinycat
33+
PRIVATE
34+
-Wall
35+
-Wextra
36+
-std=c17
37+
$<$<CONFIG:Release,RelWithDebInfo>:-Os>
38+
)
39+
40+
target_compile_definitions(
41+
tinycat
42+
PRIVATE
43+
_POSIX_C_SOURCE=200809L
44+
)

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

build.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#! /usr/bin/env bash
2+
3+
set -e
4+
5+
CURRENT_DIR="$(dirname $0)"
6+
BUILD_DIR="${CURRENT_DIR}/build"
7+
8+
mkdir -p ${BUILD_DIR}
9+
pushd ${BUILD_DIR} > /dev/null
10+
cmake -G "Ninja Multi-Config" ..
11+
ninja -f build-Debug.ninja
12+
ninja -f build-Release.ninja
13+
ninja -f build-RelWithDebInfo.ninja
14+
popd > /dev/null
15+

clean.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#! /usr/bin/env bash
2+
3+
set -e
4+
5+
BUILD_DIR="$(dirname $0)/build"
6+
rm -rf ${BUILD_DIR:?}

include/cleanup.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
void setupCleanup();
4+
5+
void registerCreatedUnixSocket(char const * const path);
6+
void registerOpenedSocket(int const socket);
7+
void closeSocket(int const socket);
8+
9+
void handleInterrupt();

include/cli.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include <stdbool.h>
4+
5+
typedef enum
6+
{
7+
TcpSocket,
8+
UnixSocket,
9+
} SocketType;
10+
11+
typedef struct
12+
{
13+
bool loop;
14+
bool listen;
15+
char const * unixSocket;
16+
char const * address;
17+
char const * port;
18+
} Args;
19+
20+
Args parse(int argc, char const * * argv);
21+
22+
int toString(char * buf, int size, Args const * args);
23+
24+
SocketType getSocketType(Args const * args);

include/client.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
#include "cli.h"
4+
5+
void client(Args const * args);
6+

include/config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#define MAX_CONNECTIONS (1)

0 commit comments

Comments
 (0)