diff --git a/partage/Makefile b/partage/Makefile index f2c2e93..d383ce4 100644 --- a/partage/Makefile +++ b/partage/Makefile @@ -1,32 +1,40 @@ -CC := gcc -Wall -Wextra -Wpedantic -Werror -Iinclude -g +CC := gcc -Wall -Wextra -Wpedantic -Werror -Wshadow=local -Iinclude -Isrc -g -std=c11 -LIBS := - -LDFLAGS := $(LDFLAGS) $(shell pkg-config --libs $(LIBS)) -CFLAGS := $(CFLAGS) $(shell pkg-config --cflags $(LIBS)) +# pkg-config +# LIBS := +# LDFLAGS := $(LDFLAGS) $(shell pkg-config --libs $(LIBS)) +# CFLAGS := $(CFLAGS) $(shell pkg-config --cflags $(LIBS)) CFLAGS := $(CFLAGS) -D_POSIX_C_SOURCE=200809L -BUILD_DIR ?= build - OUT := $(notdir $(shell pwd)) SRC := $(wildcard src/*.c) -OBJS := $(patsubst src/%.c,$(BUILD_DIR)/%.o,$(SRC)) -DEPS := $(wildcard $(BUILD_DIR)/*.d) +TEST := $(wildcard test/*.c) +OBJS := $(patsubst src/%.c,build/%.o,$(SRC)) +TEST_OBJS := $(patsubst test/%.c,build/test-%.o,$(TEST)) +DEPS := $(wildcard build/*.d) all: ext-in ext-out test-iftun tunnel64d -$(OUT): $(OBJS) +# $(OUT): $(OBJS) +$(OUT): $(CC) $(LDFLAGS) -o $@ $^ +test-ext-in: build/test-ext-in.o build/extremite.o build/iftun.o +test-ext-out: build/test-ext-out.o build/extremite.o build/iftun.o +test-iftun: build/test-iftun.o build/iftun.o + +tunnel64d: build/tunnel64d.o build/extremite.o build/iftun.o -include $(DEPS) -$(BUILD_DIR)/%.o: src/%.c +build/test-%.o: test/%.c + $(CC) $(CFLAGS) -MP -MD $< -c -o $@ +build/%.o: src/%.c $(CC) $(CFLAGS) -MP -MD $< -c -o $@ clean: - -rm -f $(BUILD_DIR)/*.o - -rm -f $(BUILD_DIR)/*.d + -rm -f build/*.o + -rm -f build/*.d diff --git a/partage/src/extremite.c b/partage/src/extremite.c index 16337b9..044096f 100644 --- a/partage/src/extremite.c +++ b/partage/src/extremite.c @@ -1,3 +1,5 @@ +#include "extremite.h" + #include #include #include @@ -19,7 +21,7 @@ #define BUFSIZE 1024 -void ext_out(int port, int out) { +int ext_out(int port) { int server = socket(AF_INET6, SOCK_STREAM, 0); if (server == -1) { perror("socket"); @@ -40,7 +42,7 @@ void ext_out(int port, int out) { inet_ntop(AF_INET6, &(server_addr.sin6_addr), server_addr_pretty, sizeof server_addr_pretty); printf("Écoute sur : %s\n", server_addr_pretty); - if (listen(server, SOMAXCONN) == -1) { + if (listen(server, 1) == -1) { perror("listen"); exit(1); } @@ -49,31 +51,35 @@ void ext_out(int port, int out) { socklen_t client_addr_len; puts("Attente d’un client."); int client = accept(server, (struct sockaddr *) &client_addr, &client_addr_len); + + close(server); + char client_addr_pretty[INET6_ADDRSTRLEN] = ""; inet_ntop(AF_INET6, &(client_addr.sin6_addr), client_addr_pretty, sizeof client_addr_pretty); printf("Client connecté : %s\n", client_addr_pretty); - char buf[BUFSIZE]; - ssize_t n; - while (1) { - n = read(client, buf, sizeof buf); - if (n == -1) { - perror("read"); - exit(1); - } - n = write(out, buf, n); - if (n == -1) { - perror("write"); - exit(1); - } - } + return client; - close(client); - close(server); + /* char buf[BUFSIZE]; */ + /* ssize_t n; */ + /* while (1) { */ + /* n = read(client, buf, sizeof buf); */ + /* if (n == -1) { */ + /* perror("read"); */ + /* exit(1); */ + /* } */ + /* n = write(out, buf, n); */ + /* if (n == -1) { */ + /* perror("write"); */ + /* exit(1); */ + /* } */ + /* } */ + + /* close(client); */ } -void ext_in(const char addr[], int port, int in) { +int ext_in(const char addr[], int port) { int s = socket(AF_INET6, SOCK_STREAM, 0); if (s == -1) { perror("socket"); @@ -94,32 +100,37 @@ void ext_in(const char addr[], int port, int in) { inet_ntop(AF_INET6, &(sa.sin6_addr), addr_pretty, sizeof addr_pretty); printf("Connecté à : %s\n", addr_pretty); - char buf[BUFSIZE]; - ssize_t n; - while (1) { - n = read(in, buf, sizeof buf); - if (n == -1) { - perror("read"); - exit(1); - } - n = write(s, buf, n); - if (n == -1) { - perror("write"); - exit(1); - } - } + return s; - close(s); + /* char buf[BUFSIZE]; */ + /* ssize_t n; */ + /* while (1) { */ + /* n = read(in, buf, sizeof buf); */ + /* if (n == -1) { */ + /* perror("read"); */ + /* exit(1); */ + /* } */ + /* n = write(s, buf, n); */ + /* if (n == -1) { */ + /* perror("write"); */ + /* exit(1); */ + /* } */ + /* } */ + + /* close(s); */ } void ext_bidir(const char addr[], int port, int in, int out) { + (void) in; + (void) out; + pid_t pid = fork(); if (pid == 0) { - ext_in(addr, port, in); + ext_in(addr, port); } else { - ext_out(port, out); + ext_out(port); waitpid(pid, NULL, 0); } } diff --git a/partage/src/extremite.h b/partage/src/extremite.h index ee70913..5753af1 100644 --- a/partage/src/extremite.h +++ b/partage/src/extremite.h @@ -2,8 +2,8 @@ #define EXTREMITE_H -void ext_out(int port, int out); -void ext_in(const char addr[], int port, int in); +int ext_out(int port); +int ext_in(const char addr[], int port); void ext_bidir(const char addr[], int port, int in, int out); diff --git a/partage/src/ext-in.c b/partage/test/ext-in.c similarity index 92% rename from partage/src/ext-in.c rename to partage/test/ext-in.c index d61bcf0..ded07e6 100644 --- a/partage/src/ext-in.c +++ b/partage/test/ext-in.c @@ -26,7 +26,9 @@ int main(int argc, char *argv[argc]) { fprintf(stderr, "Erreur lors de l’exécution du script de configuration de l’interface.\n"); return 1; } - ext_in(argv[1], port, tun); + + int ext = ext_in(argv[1], port); + copy(tun, ext); return 0; } diff --git a/partage/src/ext-out.c b/partage/test/ext-out.c similarity index 93% rename from partage/src/ext-out.c rename to partage/test/ext-out.c index 0bada03..6d9e138 100644 --- a/partage/src/ext-out.c +++ b/partage/test/ext-out.c @@ -26,7 +26,9 @@ int main(int argc, char *argv[argc]) { fprintf(stderr, "Erreur lors de l’exécution du script de configuration de l’interface.\n"); return 1; } - ext_out(port, tun); + + int ext = ext_out(port); + copy(ext, tun); return 0; } diff --git a/partage/src/test-iftun.c b/partage/test/iftun.c similarity index 100% rename from partage/src/test-iftun.c rename to partage/test/iftun.c