From c2fbcde4449ffaf798f9ba4f742b942d33a1c1c8 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Fri, 10 Mar 2023 22:24:40 +0100 Subject: libdscp --- overlays/libdscp/default.nix | 11 +++++++++++ overlays/libdscp/src/Makefile | 16 ++++++++++++++++ overlays/libdscp/src/libdscp.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 overlays/libdscp/default.nix create mode 100644 overlays/libdscp/src/Makefile create mode 100644 overlays/libdscp/src/libdscp.c (limited to 'overlays/libdscp') diff --git a/overlays/libdscp/default.nix b/overlays/libdscp/default.nix new file mode 100644 index 00000000..47103fd3 --- /dev/null +++ b/overlays/libdscp/default.nix @@ -0,0 +1,11 @@ +{ final, prev, ... }: +{ + libdscp = prev.stdenv.mkDerivation { + pname = "libdscp"; + version = "0.0.0"; + + src = ./src; + + makeFlags = [ "PREFIX=$(out)" ]; + }; +} diff --git a/overlays/libdscp/src/Makefile b/overlays/libdscp/src/Makefile new file mode 100644 index 00000000..e76930d7 --- /dev/null +++ b/overlays/libdscp/src/Makefile @@ -0,0 +1,16 @@ +ifeq ($(PREFIX),) + PREFIX := /usr/local +endif + +.PHONY: all install + +all: libdscp + +libdscp: + $(CC) \ + -o $@.so $@.c \ + -D_GNU_SOURCE \ + -nostartfiles -shared +install: libdscp + install -d $(DESTDIR)$(PREFIX)/lib/ + install -m 644 libdscp.so $(DESTDIR)$(PREFIX)/lib/ diff --git a/overlays/libdscp/src/libdscp.c b/overlays/libdscp/src/libdscp.c new file mode 100644 index 00000000..1bc9a16d --- /dev/null +++ b/overlays/libdscp/src/libdscp.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include + +void _init(void); +int (*sys_connect)(int sockfd, const struct sockaddr *addr, socklen_t addrlen); +int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); + +int dscp_val = 0; + +void _init(void) { + const char* err; + + char* env_dscp; + + env_dscp = getenv("DSCP"); + + if (env_dscp) + dscp_val = atoi(env_dscp) << 2; + + sys_connect = dlsym(RTLD_NEXT, "connect"); + + err = dlerror(); + if (err != NULL) + (void)fprintf(stderr, "libdscp:dlsym (connect): %s\n", err); +} + +int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { + int oerrno = errno; + + setsockopt(sockfd, IPPROTO_IP, IP_TOS, &dscp_val, sizeof dscp_val); + setsockopt(sockfd, IPPROTO_IPV6, IPV6_TCLASS, &dscp_val, sizeof dscp_val); + + errno = oerrno; + return sys_connect(sockfd, addr, addrlen); +} -- cgit v1.2.3