-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
executable file
·82 lines (64 loc) · 2.76 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# unsock: shim to automatically change AF_INET sockets to AF_UNIX, etc.
#
# Copyright 2022 Christian Kohlschuetter <[email protected]>
# SPDX-License-Identifier: Apache-2.0
#
LIBUNSOCK_API_VER_MAJOR = 1
LIBUNSOCK_API_VER_MINOR = 1
LIBUNSOCK_API_VER_PATCH = 0
CC ?= cc
EXTRA_CFLAGS ?=
PREFIX ?= /usr/local
INSTALL_LIB_DIR ?= lib
LIBUNSOCK_INSTALL_DIR = $(PREFIX)/$(INSTALL_LIB_DIR)/
LIBUNSOCK_SUFFIX = .so
DEBUG ?= 0
DEBUG_CFLAGS ?=
ifeq ($(DEBUG), 1)
DEBUG_CFLAGS += -DDEBUG
LIBUNSOCK_SUFFIX = -debug.so
endif
LIBUNSOCK = libunsock$(LIBUNSOCK_SUFFIX)
LIBUNSOCK_API_VER = $(LIBUNSOCK_API_VER_MAJOR).$(LIBUNSOCK_API_VER_MINOR).$(LIBUNSOCK_API_VER_PATCH)
CFLAGS = -std=c17 -Wall -Wextra -Os $(DEBUG_CFLAGS) $(EXTRA_CFLAGS)
all: $(LIBUNSOCK) clean-postbuild
test-ld:
$(CC) -o test-ld -shared /dev/null
clean-postbuild:
@rm -f test-ld
$(LIBUNSOCK): INTERP = `ldd test-ld | grep ".so" | grep -v " => " | tail -n 1 | cut -d'(' -f1|tr -d '\t '`
$(LIBUNSOCK): test-ld src/unsock.c src/main.c
$(CC) $(CFLAGS) -D_GNU_SOURCE -fvisibility=hidden \
-DINTERP=$(INTERP) \
-shared -fPIC -e unsock_main \
-o $(LIBUNSOCK).$(LIBUNSOCK_API_VER) \
src/unsock.c src/main.c \
-ldl
install: $(LIBUNSOCK)
@echo "Installing to $(LIBUNSOCK_INSTALL_DIR)"
@mkdir -p $(LIBUNSOCK_INSTALL_DIR)
@cp -v $(LIBUNSOCK).$(LIBUNSOCK_API_VER) $(LIBUNSOCK_INSTALL_DIR)/
cd $(LIBUNSOCK_INSTALL_DIR)/ ; ln -sf $(LIBUNSOCK).$(LIBUNSOCK_API_VER_MAJOR) $(LIBUNSOCK) ; ln -sf $(LIBUNSOCK).$(LIBUNSOCK_API_VER) $(LIBUNSOCK).$(LIBUNSOCK_API_VER_MAJOR)
uninstall:
@echo "Uninstalling from $(LIBUNSOCK_INSTALL_DIR)"
@rm -fv $(LIBUNSOCK_INSTALL_DIR)/$(LIBUNSOCK) $(LIBUNSOCK_INSTALL_DIR)/$(LIBUNSOCK).$(LIBUNSOCK_API_VER_MAJOR) $(LIBUNSOCK_INSTALL_DIR)/$(LIBUNSOCK).$(LIBUNSOCK_API_VER)
test: test_prepare test_nc test_bind0
@echo "Tests PASS."
test_prepare: $(LIBUNSOCK) clean-postbuild
test_nc: test_prepare
# Test unsock with "nc", pretending to listen on TCP port 7000 (which will turn into UNIX domain socket test/7000.sock)
@mkdir -p test
@rm -f "test/7000.sock" "test/7000.txt"
UNSOCK_DIR="$(PWD)/test" UNSOCK_BLOCK_INET6=1 UNSOCK_PORT=7000 UNSOCK_ADDR=127.0.0.1/0 LD_PRELOAD=./$(LIBUNSOCK).$(LIBUNSOCK_API_VER) nc -l -p 7000 | head -n 1 > test/7000.txt &
timeout 10s sh -c 'until [ -e "test/7000.sock" ]; do sleep 0.1; done'
echo "Hello world" | ( nc -U test/7000.sock || nc local:test/7000.sock )
cat test/7000.txt | grep -q "Hello world"
test_bind0: test_prepare
# Test binding on port 0 (random/anonymous port)
@rm -rf test/zero
@mkdir -p test/zero
( UNSOCK_DIR="$(PWD)/test/zero" UNSOCK_BLOCK_INET6=1 UNSOCK_ADDR=127.0.0.1/0 LD_PRELOAD=./$(LIBUNSOCK).$(LIBUNSOCK_API_VER) timeout 1 nc -l -p 0 ) || true
[ `ls test/zero/*sock | grep -c ".sock"` -eq 1 ]
.PHONY: test_prepare
clean:
rm -rf libunsock*.so* test