lol sa coupe je passe ds un tunnel

This commit is contained in:
papush! 2019-10-25 23:53:43 +02:00
parent 7f836a7fa1
commit 32d6d9f74c
18 changed files with 167 additions and 82 deletions

View File

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

BIN
partage/build/ext-in.o Normal file

Binary file not shown.

BIN
partage/build/ext-out.o Normal file

Binary file not shown.

BIN
partage/build/extremite.a Normal file

Binary file not shown.

BIN
partage/build/extremite.o Normal file

Binary file not shown.

BIN
partage/build/iftun.a Normal file

Binary file not shown.

BIN
partage/build/iftun.o Normal file

Binary file not shown.

BIN
partage/build/test-iftun.o Normal file

Binary file not shown.

View File

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

BIN
partage/ext-in Executable file

Binary file not shown.

BIN
partage/ext-out Executable file

Binary file not shown.

18
partage/src/ext-in.c Normal file
View File

@ -0,0 +1,18 @@
#include "extremite.h"
#include <stdio.h>
#include <stdlib.h>
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;
}

18
partage/src/ext-out.c Normal file
View File

@ -0,0 +1,18 @@
#include "extremite.h"
#include <stdio.h>
#include <stdlib.h>
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;
}

99
partage/src/extremite.c Normal file
View File

@ -0,0 +1,99 @@
#define _POSIX_C_SOURCE 200112L
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
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 dun 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);
}

9
partage/src/extremite.h Normal file
View File

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

View File

@ -9,8 +9,10 @@ int main(int argc, char *argv[argc]) {
return 1;
}
int tun = tun_alloc(argv[1]);
fprintf(stderr, "Configurez linterface puis appuyez sur une touche.\n");
fprintf(stderr, "Configurez linterface puis appuyez sur <Entrée>... ");
fflush(stderr);
getchar();
fprintf(stderr, "Démarrage.");
copy(tun, 1);
return 0;
}

View File

@ -1,79 +0,0 @@
#define _POSIX_C_SOURCE 200112L
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/if.h>
#include <linux/if_tun.h>
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;
}

BIN
partage/test-iftun Executable file

Binary file not shown.