This commit is contained in:
papush! 2019-11-06 07:36:00 +01:00
parent 32d6d9f74c
commit c5108dcccf
26 changed files with 265 additions and 59 deletions

2
VM1-6/Vagrantfile vendored
View File

@ -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

2
VM3-6/Vagrantfile vendored
View File

@ -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

View File

@ -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

46
partage/Makefile.old Normal file
View File

@ -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/*

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

23
partage/capture.txt Normal file
View File

@ -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

9
partage/config.vm1-6 Normal file
View File

@ -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

9
partage/config.vm3-6 Normal file
View File

@ -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

View File

@ -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

Binary file not shown.

Binary file not shown.

View File

@ -1,10 +1,13 @@
#include "extremite.h"
#include "iftun.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
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 lexécution du script de configuration de linterface.\n");
return 1;
}
ext_in(argv[1], port, tun);
return 0;
}

View File

@ -1,7 +1,10 @@
#include "extremite.h"
#include "iftun.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
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 lexécution du script de configuration de linterface.\n");
return 1;
}
ext_out(port, tun);
return 0;
}

View File

@ -1,5 +1,3 @@
#define _POSIX_C_SOURCE 200112L
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/if.h>
@ -15,15 +13,21 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
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);
}
}

View File

@ -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

View File

@ -17,27 +17,26 @@
#include <linux/if_tun.h>
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;
}

View File

@ -2,7 +2,7 @@
#define IFTUN_H
int tun_alloc(char *dev);
int tun_alloc(const char *dev);
int copy(int src, int dest);

74
partage/src/tunnel64d.c Normal file
View File

@ -0,0 +1,74 @@
#include "iftun.h"
#include "extremite.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
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 lexécution du script de configuration de linterface.\n");
return 1;
}
ext_bidir(outip, outport, tun, tun);
return 0;
}

Binary file not shown.

BIN
partage/tunnel64d Executable file

Binary file not shown.