diff --git a/partage/Makefile b/partage/Makefile index 89b0f79..080b849 100644 --- a/partage/Makefile +++ b/partage/Makefile @@ -1,11 +1,29 @@ CC := gcc -Wall -Wextra -Wpedantic -Werror -Iinclude -std=c11 -g -all: build test-iftun +all: build test-iftun ext-in ext-out build: -mkdir build +ext-in: build/ext-in.o build/extremite.a + $(CC) $(CFLAGS) $^ -o $@ + +ext-out: build/ext-out.o build/extremite.a + $(CC) $(CFLAGS) $^ -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 $@ diff --git a/partage/build/ext-in.o b/partage/build/ext-in.o new file mode 100644 index 0000000..66ad5b5 Binary files /dev/null and b/partage/build/ext-in.o differ diff --git a/partage/build/ext-out.o b/partage/build/ext-out.o new file mode 100644 index 0000000..e61bbd9 Binary files /dev/null and b/partage/build/ext-out.o differ diff --git a/partage/build/extremite.a b/partage/build/extremite.a new file mode 100644 index 0000000..15a13a2 Binary files /dev/null and b/partage/build/extremite.a differ diff --git a/partage/build/extremite.o b/partage/build/extremite.o new file mode 100644 index 0000000..f0d5b50 Binary files /dev/null and b/partage/build/extremite.o differ diff --git a/partage/build/iftun.a b/partage/build/iftun.a new file mode 100644 index 0000000..ab666db Binary files /dev/null and b/partage/build/iftun.a differ diff --git a/partage/build/iftun.o b/partage/build/iftun.o new file mode 100644 index 0000000..79fbe41 Binary files /dev/null and b/partage/build/iftun.o differ diff --git a/partage/build/test-iftun.o b/partage/build/test-iftun.o new file mode 100644 index 0000000..b3aa468 Binary files /dev/null and b/partage/build/test-iftun.o differ diff --git a/partage/config.sh b/partage/config.sh index 381da5b..6b4c924 100755 --- a/partage/config.sh +++ b/partage/config.sh @@ -48,7 +48,7 @@ case $(hostname) in ip address add fc00:1234:2::26/64 dev eth2 ;; vm3-6) - ip address add fc00:1234:2::26/64 dev eth1 + ip address add fc00:1234:2::36/64 dev eth1 ip address add 172.16.2.186/28 dev eth2 ip route add $lan1_6 via fc00:1234:2::26 ip route add $lan1 via 172.16.2.183 diff --git a/partage/ext-in b/partage/ext-in new file mode 100755 index 0000000..4fea883 Binary files /dev/null and b/partage/ext-in differ diff --git a/partage/ext-out b/partage/ext-out new file mode 100755 index 0000000..71e33fb Binary files /dev/null and b/partage/ext-out differ diff --git a/partage/src/ext-in.c b/partage/src/ext-in.c new file mode 100644 index 0000000..af6e578 --- /dev/null +++ b/partage/src/ext-in.c @@ -0,0 +1,18 @@ +#include "extremite.h" + +#include +#include + + +int main(int argc, char *argv[argc]){ + int port = 123; + if (argc == 3) { + port = atoi(argv[2]); + } + else if (argc != 2) { + fprintf(stderr, "Utilisation : %s adresse [port]\n", argv[0]); + return 1; + } + ext_in(argv[1], port, 0); + return 0; +} diff --git a/partage/src/ext-out.c b/partage/src/ext-out.c new file mode 100644 index 0000000..12a66bc --- /dev/null +++ b/partage/src/ext-out.c @@ -0,0 +1,18 @@ +#include "extremite.h" + +#include +#include + + +int main(int argc, char *argv[argc]) { + int port = 123; + if (argc == 2) { + port = atoi(argv[1]); + } + else if (argc != 1) { + fprintf(stderr, "Utilisation : %s [port]\n", argv[0]); + return 1; + } + ext_out(port); + return 0; +} diff --git a/partage/src/extremite.c b/partage/src/extremite.c new file mode 100644 index 0000000..295ea28 --- /dev/null +++ b/partage/src/extremite.c @@ -0,0 +1,99 @@ +#define _POSIX_C_SOURCE 200112L + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +void ext_out(int port) { + int server = socket(AF_INET6, SOCK_STREAM, 0); + if (server == -1) { + perror("socket"); + exit(1); + } + + struct sockaddr_in6 server_addr = {0}; + server_addr.sin6_family = AF_INET6; + server_addr.sin6_port = htons(port); + server_addr.sin6_addr = in6addr_any; + if (bind(server, (struct sockaddr *) &server_addr, sizeof server_addr) == -1) { + perror("bind"); + exit(1); + } + char server_addr_pretty[INET6_ADDRSTRLEN] = ""; + 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) { + perror("listen"); + exit(1); + } + + struct sockaddr_in6 client_addr; + socklen_t client_addr_len; + puts("Attente d’un client."); + int client = accept(server, (struct sockaddr *) &client_addr, &client_addr_len); + 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[1024]; + while (1) { + ssize_t n = read(client, buf, sizeof buf); + write(1, buf, n); + } + + close(client); + close(server); +} + + +void ext_in(const char addr[], int port, int in) { + int s = socket(AF_INET6, SOCK_STREAM, 0); + if (s == -1) { + perror("socket"); + exit(1); + } + + struct sockaddr_in6 sa = {0}; + sa.sin6_family = AF_INET6; + sa.sin6_port = htons(port); + inet_pton(AF_INET6, addr, &sa.sin6_addr); + + puts("Connexion."); + if (connect(s, (struct sockaddr *) &sa, sizeof sa) == -1) { + perror("connect"); + exit(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]; + while (1) { + ssize_t 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); +} diff --git a/partage/src/extremite.h b/partage/src/extremite.h new file mode 100644 index 0000000..3ad66dc --- /dev/null +++ b/partage/src/extremite.h @@ -0,0 +1,9 @@ +#ifndef EXTREMITE_H +#define EXTREMITE_H + + +void ext_out(int port); +void ext_in(const char addr[], int port, int in); + + +#endif diff --git a/partage/src/test-iftun.c b/partage/src/test-iftun.c index 3f3ec14..321c500 100644 --- a/partage/src/test-iftun.c +++ b/partage/src/test-iftun.c @@ -9,8 +9,10 @@ int main(int argc, char *argv[argc]) { return 1; } int tun = tun_alloc(argv[1]); - fprintf(stderr, "Configurez l’interface puis appuyez sur une touche.\n"); + fprintf(stderr, "Configurez l’interface puis appuyez sur ... "); + fflush(stderr); getchar(); + fprintf(stderr, "Démarrage."); copy(tun, 1); return 0; } diff --git a/partage/src/tunalloc.c b/partage/src/tunalloc.c deleted file mode 100644 index ba20e2b..0000000 --- a/partage/src/tunalloc.c +++ /dev/null @@ -1,79 +0,0 @@ -#define _POSIX_C_SOURCE 200112L - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include - - -void ext_out() { - struct addrinfo hints = { - .ai_family = AF_UNSPEC, - .ai_socktype = SOCK_DGRAM, - .ai_flags = AI_PASSIVE, - .ai_protocol = 0, - .ai_canonname = NULL, - .ai_addr = NULL, - .ai_next = NULL, - }; - - struct addrinfo *results; - int s = getaddrinfo(NULL, "123", &hints, &results); - if (s != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); - exit(1); - } - - struct addrinfo *rp; - int sfd; - for (rp = results; rp != NULL; rp = rp->ai_next) { - sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if (sfd == -1) - continue; - if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0) - break; - close(sfd); - } - - if (rp == NULL) { - fprintf(stderr, "Could not bind\n"); - exit(EXIT_FAILURE); - } - freeaddrinfo(results); - - - struct sockaddr_storage peer_addr; - socklen_t peer_addr_len; - char buf[1024]; - while (1) { - peer_addr_len = sizeof (struct sockaddr_storage); - ssize_t n = recvfrom(sfd, buf, sizeof buf, 0, - (struct sockaddr *) &peer_addr, &peer_addr_len); - if (n == -1) { - perror("recvfrom: "); - exit(1); - } - write(1, buf, n); - } -} - - -int main(int argc, char *argv[argc]){ - int tun = tun_alloc(argv[1]); - /* printf("Configure %s puis appuie sur un touche…\n", argv[1]); */ - /* getchar(); */ - /* copy(tun, 1); */ - /* ext_out(); */ - return 0; -} diff --git a/partage/test-iftun b/partage/test-iftun new file mode 100755 index 0000000..e9234a6 Binary files /dev/null and b/partage/test-iftun differ