diff --git a/VM1-6/Vagrantfile b/VM1-6/Vagrantfile index 3451f76..0d45707 100644 --- a/VM1-6/Vagrantfile +++ b/VM1-6/Vagrantfile @@ -25,7 +25,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.provider "virtualbox" do |vb| # Commenter pour s'exécuter sans GUI - vb.gui = true + # vb.gui = true vb.customize ["modifyvm", :id, "--memory", "768"] end diff --git a/VM3-6/Vagrantfile b/VM3-6/Vagrantfile index 806afd1..c62e61a 100644 --- a/VM3-6/Vagrantfile +++ b/VM3-6/Vagrantfile @@ -25,7 +25,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.provider "virtualbox" do |vb| # Commenter pour s'exécuter sans GUI - vb.gui = true + # vb.gui = true vb.customize ["modifyvm", :id, "--memory", "768"] end diff --git a/partage/Makefile b/partage/Makefile index 080b849..f2c2e93 100644 --- a/partage/Makefile +++ b/partage/Makefile @@ -1,40 +1,31 @@ -CC := gcc -Wall -Wextra -Wpedantic -Werror -Iinclude -std=c11 -g +CC := gcc -Wall -Wextra -Wpedantic -Werror -Iinclude -g + +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) -all: build test-iftun ext-in ext-out +all: ext-in ext-out test-iftun tunnel64d -build: - -mkdir build +$(OUT): $(OBJS) + $(CC) $(LDFLAGS) -o $@ $^ -ext-in: build/ext-in.o build/extremite.a - $(CC) $(CFLAGS) $^ -o $@ -ext-out: build/ext-out.o build/extremite.a - $(CC) $(CFLAGS) $^ -o $@ +-include $(DEPS) +$(BUILD_DIR)/%.o: src/%.c + $(CC) $(CFLAGS) -MP -MD $< -c -o $@ -build/ext-in.o: src/ext-in.c src/extremite.h - $(CC) $(CFLAGS) $< -c -o $@ - -build/ext-out.o: src/ext-out.c src/extremite.h - $(CC) $(CFLAGS) $< -c -o $@ - -build/extremite.a: build/extremite.o - ar rcs $@ $< - -build/extremite.o: src/extremite.c src/extremite.h - $(CC) $(CFLAGS) $< -c -o $@ - -test-iftun: build/test-iftun.o build/iftun.a - $(CC) $(CFLAGS) $^ -o $@ - -build/test-iftun.o: src/test-iftun.c src/iftun.h - $(CC) $(CFLAGS) $< -c -o $@ - -build/iftun.a: build/iftun.o - ar rcs $@ $< - -build/iftun.o: src/iftun.c src/iftun.h - $(CC) $(CFLAGS) $< -c -o $@ clean: -rm -f $(BUILD_DIR)/*.o diff --git a/partage/Makefile.old b/partage/Makefile.old new file mode 100644 index 0000000..9836fb9 --- /dev/null +++ b/partage/Makefile.old @@ -0,0 +1,46 @@ +CC := gcc -Wall -Wextra -Wpedantic -Werror -Iinclude -std=c11 -g -D_POSIX_C_SOURCE=200809L + + +all: build test-iftun ext-in ext-out tunnel64d + +build: + -mkdir build + +tunnel64d: build/tunnel64d.o build/iftun.a build/extremite.a + $(CC) $(CFLAGS) $^ -o $@ + +build/tunnel64d.o: src/tunnel64d.c src/iftun.h src/extremite.h + $(CC) $(CFLAGS) $< -c -o $@ + +ext-in: build/ext-in.o build/extremite.a build/iftun.a + $(CC) $(CFLAGS) $^ -o $@ + +ext-out: build/ext-out.o build/extremite.a build/iftun.a + $(CC) $(CFLAGS) $^ -o $@ + +build/ext-in.o: src/ext-in.c src/extremite.h src/iftun.h + $(CC) $(CFLAGS) $< -c -o $@ + +build/ext-out.o: src/ext-out.c src/extremite.h src/iftun.h + $(CC) $(CFLAGS) $< -c -o $@ + +build/extremite.a: build/extremite.o + ar rcs $@ $< + +build/extremite.o: src/extremite.c src/extremite.h + $(CC) $(CFLAGS) $< -c -o $@ + +test-iftun: build/test-iftun.o build/iftun.a + $(CC) $(CFLAGS) $^ -o $@ + +build/test-iftun.o: src/test-iftun.c src/iftun.h + $(CC) $(CFLAGS) $< -c -o $@ + +build/iftun.a: build/iftun.o + ar rcs $@ $< + +build/iftun.o: src/iftun.c src/iftun.h + $(CC) $(CFLAGS) $< -c -o $@ + +clean: + -rm -f build/* diff --git a/partage/build/ext-in.o b/partage/build/ext-in.o deleted file mode 100644 index 66ad5b5..0000000 Binary files a/partage/build/ext-in.o and /dev/null differ diff --git a/partage/build/ext-out.o b/partage/build/ext-out.o deleted file mode 100644 index e61bbd9..0000000 Binary files a/partage/build/ext-out.o and /dev/null differ diff --git a/partage/build/extremite.a b/partage/build/extremite.a deleted file mode 100644 index 15a13a2..0000000 Binary files a/partage/build/extremite.a and /dev/null differ diff --git a/partage/build/extremite.o b/partage/build/extremite.o deleted file mode 100644 index f0d5b50..0000000 Binary files a/partage/build/extremite.o and /dev/null differ diff --git a/partage/build/iftun.a b/partage/build/iftun.a deleted file mode 100644 index ab666db..0000000 Binary files a/partage/build/iftun.a and /dev/null differ diff --git a/partage/build/iftun.o b/partage/build/iftun.o deleted file mode 100644 index 79fbe41..0000000 Binary files a/partage/build/iftun.o and /dev/null differ diff --git a/partage/build/test-iftun.o b/partage/build/test-iftun.o deleted file mode 100644 index b3aa468..0000000 Binary files a/partage/build/test-iftun.o and /dev/null differ diff --git a/partage/capture.txt b/partage/capture.txt new file mode 100644 index 0000000..9f762ff --- /dev/null +++ b/partage/capture.txt @@ -0,0 +1,23 @@ +No. Time Source Destination Protocol Length Info + 1 0.000000000 172.16.2.1 172.16.2.10 ICMP 84 Echo (ping) request id=0x0529, seq=1/256, ttl=64 (no response found!) + +Frame 1: 84 bytes on wire (672 bits), 84 bytes captured (672 bits) on interface 0 +Raw packet data +Internet Protocol Version 4, Src: 172.16.2.1 (172.16.2.1), Dst: 172.16.2.10 (172.16.2.10) +Internet Control Message Protocol + +No. Time Source Destination Protocol Length Info + 2 1.009432000 172.16.2.1 172.16.2.10 ICMP 84 Echo (ping) request id=0x0529, seq=2/512, ttl=64 (no response found!) + +Frame 2: 84 bytes on wire (672 bits), 84 bytes captured (672 bits) on interface 0 +Raw packet data +Internet Protocol Version 4, Src: 172.16.2.1 (172.16.2.1), Dst: 172.16.2.10 (172.16.2.10) +Internet Control Message Protocol + +No. Time Source Destination Protocol Length Info + 3 2.016974000 172.16.2.1 172.16.2.10 ICMP 84 Echo (ping) request id=0x0529, seq=3/768, ttl=64 (no response found!) + +Frame 3: 84 bytes on wire (672 bits), 84 bytes captured (672 bits) on interface 0 +Raw packet data +Internet Protocol Version 4, Src: 172.16.2.1 (172.16.2.1), Dst: 172.16.2.10 (172.16.2.10) +Internet Control Message Protocol diff --git a/partage/config.vm1-6 b/partage/config.vm1-6 new file mode 100644 index 0000000..fb45a3f --- /dev/null +++ b/partage/config.vm1-6 @@ -0,0 +1,9 @@ +# interface tun +tun=tun0 +# adresse locale +inip=172.16.2.1/28 +inport=123 +options= +# adresse distante +outip=fc00:1234:2::36 +outport=123 diff --git a/partage/config.vm3-6 b/partage/config.vm3-6 new file mode 100644 index 0000000..cfae7ff --- /dev/null +++ b/partage/config.vm3-6 @@ -0,0 +1,9 @@ +# interface tun +tun=tun0 +# adresse locale +inip=172.16.2.10/28 +inport=123 +options= +# adresse distante +outip=fc00:1234:1::16 +outport=123 diff --git a/partage/configure-tun.sh b/partage/configure-tun.sh index 427851c..2339791 100755 --- a/partage/configure-tun.sh +++ b/partage/configure-tun.sh @@ -1,3 +1,3 @@ #!/bin/sh -ip l set tun0 up -ip a a 172.16.2.1/28 dev tun0 +ip l set "$1" up +ip a a "$2" dev tun0 diff --git a/partage/ext-in b/partage/ext-in index 4fea883..6dbdd48 100755 Binary files a/partage/ext-in and b/partage/ext-in differ diff --git a/partage/ext-out b/partage/ext-out index 71e33fb..e383295 100755 Binary files a/partage/ext-out and b/partage/ext-out differ diff --git a/partage/src/ext-in.c b/partage/src/ext-in.c index af6e578..d61bcf0 100644 --- a/partage/src/ext-in.c +++ b/partage/src/ext-in.c @@ -1,10 +1,13 @@ #include "extremite.h" +#include "iftun.h" #include #include +#include +#include -int main(int argc, char *argv[argc]){ +int main(int argc, char *argv[argc]) { int port = 123; if (argc == 3) { port = atoi(argv[2]); @@ -13,6 +16,17 @@ int main(int argc, char *argv[argc]){ fprintf(stderr, "Utilisation : %s adresse [port]\n", argv[0]); return 1; } - ext_in(argv[1], port, 0); + + if (getuid() != 0) { + fprintf(stderr, "%s doit être lancé en tant que superutilisateur.\n", argv[0]); + return 1; + } + int tun = tun_alloc("tun0"); + if (system("./configure-tun.sh tun0 172.16.2.1/28") != 0) { + fprintf(stderr, "Erreur lors de l’exécution du script de configuration de l’interface.\n"); + return 1; + } + ext_in(argv[1], port, tun); + return 0; } diff --git a/partage/src/ext-out.c b/partage/src/ext-out.c index 12a66bc..0bada03 100644 --- a/partage/src/ext-out.c +++ b/partage/src/ext-out.c @@ -1,7 +1,10 @@ #include "extremite.h" +#include "iftun.h" #include #include +#include +#include int main(int argc, char *argv[argc]) { @@ -13,6 +16,17 @@ int main(int argc, char *argv[argc]) { fprintf(stderr, "Utilisation : %s [port]\n", argv[0]); return 1; } - ext_out(port); + + if (getuid() != 0) { + fprintf(stderr, "%s doit être lancé en tant que superutilisateur.\n", argv[0]); + return 1; + } + int tun = tun_alloc("tun0"); + if (system("./configure-tun.sh tun0 172.16.2.10/28") != 0) { + fprintf(stderr, "Erreur lors de l’exécution du script de configuration de l’interface.\n"); + return 1; + } + ext_out(port, tun); + return 0; } diff --git a/partage/src/extremite.c b/partage/src/extremite.c index 295ea28..16337b9 100644 --- a/partage/src/extremite.c +++ b/partage/src/extremite.c @@ -1,5 +1,3 @@ -#define _POSIX_C_SOURCE 200112L - #include #include #include @@ -15,15 +13,21 @@ #include #include #include +#include -void ext_out(int port) { +#define BUFSIZE 1024 + + +void ext_out(int port, int out) { int server = socket(AF_INET6, SOCK_STREAM, 0); if (server == -1) { perror("socket"); exit(1); } + setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &(int) {1}, sizeof (int)); + struct sockaddr_in6 server_addr = {0}; server_addr.sin6_family = AF_INET6; server_addr.sin6_port = htons(port); @@ -49,10 +53,19 @@ void ext_out(int port) { 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[1024]; + char buf[BUFSIZE]; + ssize_t n; while (1) { - ssize_t n = read(client, buf, sizeof buf); - write(1, buf, n); + 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); @@ -73,17 +86,18 @@ void ext_in(const char addr[], int port, int in) { inet_pton(AF_INET6, addr, &sa.sin6_addr); puts("Connexion."); - if (connect(s, (struct sockaddr *) &sa, sizeof sa) == -1) { + while (connect(s, (struct sockaddr *) &sa, sizeof sa) == -1) { perror("connect"); - exit(1); + sleep(1); } char addr_pretty[INET6_ADDRSTRLEN] = ""; inet_ntop(AF_INET6, &(sa.sin6_addr), addr_pretty, sizeof addr_pretty); printf("Connecté à : %s\n", addr_pretty); - char buf[1024]; + char buf[BUFSIZE]; + ssize_t n; while (1) { - ssize_t n = read(in, buf, sizeof buf); + n = read(in, buf, sizeof buf); if (n == -1) { perror("read"); exit(1); @@ -97,3 +111,15 @@ void ext_in(const char addr[], int port, int in) { close(s); } + + +void ext_bidir(const char addr[], int port, int in, int out) { + pid_t pid = fork(); + if (pid == 0) { + ext_in(addr, port, in); + } + else { + ext_out(port, out); + waitpid(pid, NULL, 0); + } +} diff --git a/partage/src/extremite.h b/partage/src/extremite.h index 3ad66dc..ee70913 100644 --- a/partage/src/extremite.h +++ b/partage/src/extremite.h @@ -2,8 +2,9 @@ #define EXTREMITE_H -void ext_out(int port); +void ext_out(int port, int out); void ext_in(const char addr[], int port, int in); +void ext_bidir(const char addr[], int port, int in, int out); #endif diff --git a/partage/src/iftun.c b/partage/src/iftun.c index 25b3008..1d17640 100644 --- a/partage/src/iftun.c +++ b/partage/src/iftun.c @@ -17,27 +17,26 @@ #include -int tun_alloc(char *dev) { +int tun_alloc(const char *dev) { int fd = open("/dev/net/tun", O_RDWR); - if(fd < 0 ) { - perror("alloc tun"); + if(fd < 0) { + perror("open"); exit(1); } - struct ifreq ifr = {.ifr_flags = IFF_TUN}; - ifr.ifr_flags = IFF_TUN | IFF_NO_PI; + struct ifreq ifr = { + .ifr_flags = IFF_TUN | IFF_NO_PI + }; if (*dev) { strncpy(ifr.ifr_name, dev, IFNAMSIZ); } int err = ioctl(fd, TUNSETIFF, (void *) &ifr); - if(err < 0 ){ - close(fd); - return err; + if(err < 0){ + exit(1); } - strcpy(dev, ifr.ifr_name); return fd; } diff --git a/partage/src/iftun.h b/partage/src/iftun.h index a9139b0..3747b3b 100644 --- a/partage/src/iftun.h +++ b/partage/src/iftun.h @@ -2,7 +2,7 @@ #define IFTUN_H -int tun_alloc(char *dev); +int tun_alloc(const char *dev); int copy(int src, int dest); diff --git a/partage/src/tunnel64d.c b/partage/src/tunnel64d.c new file mode 100644 index 0000000..558b184 --- /dev/null +++ b/partage/src/tunnel64d.c @@ -0,0 +1,74 @@ +#include "iftun.h" +#include "extremite.h" + +#include +#include +#include +#include +#include +#include + + +char *dev; +char *inip; +char *outip; +int inport; +int outport; + + +int main(int argc, char *argv[argc]) { + if (argc != 2) { + fprintf(stderr, "Utilisation : %s fichier_conf\n", argv[0]); + } + if (getuid() != 0) { + fprintf(stderr, "%s doit être lancé en tant que superutilisateur.\n", argv[0]); + return 1; + } + + FILE *f = fopen(argv[1], "r"); + + char *line = NULL; + size_t n = 0; + ssize_t len; + while ((len = getline(&line, &n, f)) > 0) { + char *pair = strtok(line, "#"); + char *key = strtok(pair, "="); + char *val = strtok(NULL, "="); + if (val != NULL) { + line[len-1] = '\0'; + printf("%s=%s\n", key, val); + if (strcmp(key, "tun") == 0) { + dev = malloc(strlen(val)); + strcpy(dev, val); + } + else if (strcmp(key, "inip") == 0) { + inip = malloc(strlen(val) + 1); + strcpy(inip, val); + } + else if (strcmp(key, "outip") == 0) { + outip = malloc(strlen(val) + 1); + strcpy(outip, val); + } + else if (strcmp(key, "inport") == 0) { + inport = atoi(val); + } + else if (strcmp(key, "outport") == 0) { + outport = atoi(val); + } + } + } + if (line) free(line); + fclose(f); + + int tun = tun_alloc(dev); + + char full_cmd[1024] = ""; + sprintf(full_cmd, "./configure-tun.sh %s %s", dev, inip); + if (system(full_cmd) != 0) { + fprintf(stderr, "Erreur lors de l’exécution du script de configuration de l’interface.\n"); + return 1; + } + ext_bidir(outip, outport, tun, tun); + + return 0; +} diff --git a/partage/test-iftun b/partage/test-iftun index e9234a6..326c2a2 100755 Binary files a/partage/test-iftun and b/partage/test-iftun differ diff --git a/partage/tunnel64d b/partage/tunnel64d new file mode 100755 index 0000000..d94f557 Binary files /dev/null and b/partage/tunnel64d differ