2019-10-25 23:53:43 +02:00
|
|
|
|
#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>
|
2019-11-06 07:36:00 +01:00
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define BUFSIZE 1024
|
2019-10-25 23:53:43 +02:00
|
|
|
|
|
|
|
|
|
|
2019-11-06 07:36:00 +01:00
|
|
|
|
void ext_out(int port, int out) {
|
2019-10-25 23:53:43 +02:00
|
|
|
|
int server = socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
|
if (server == -1) {
|
|
|
|
|
perror("socket");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 07:36:00 +01:00
|
|
|
|
setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &(int) {1}, sizeof (int));
|
|
|
|
|
|
2019-10-25 23:53:43 +02:00
|
|
|
|
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);
|
|
|
|
|
|
2019-11-06 07:36:00 +01:00
|
|
|
|
char buf[BUFSIZE];
|
|
|
|
|
ssize_t n;
|
2019-10-25 23:53:43 +02:00
|
|
|
|
while (1) {
|
2019-11-06 07:36:00 +01:00
|
|
|
|
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);
|
|
|
|
|
}
|
2019-10-25 23:53:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.");
|
2019-11-06 07:36:00 +01:00
|
|
|
|
while (connect(s, (struct sockaddr *) &sa, sizeof sa) == -1) {
|
2019-10-25 23:53:43 +02:00
|
|
|
|
perror("connect");
|
2019-11-06 07:36:00 +01:00
|
|
|
|
sleep(1);
|
2019-10-25 23:53:43 +02:00
|
|
|
|
}
|
|
|
|
|
char addr_pretty[INET6_ADDRSTRLEN] = "";
|
|
|
|
|
inet_ntop(AF_INET6, &(sa.sin6_addr), addr_pretty, sizeof addr_pretty);
|
|
|
|
|
printf("Connecté à : %s\n", addr_pretty);
|
|
|
|
|
|
2019-11-06 07:36:00 +01:00
|
|
|
|
char buf[BUFSIZE];
|
|
|
|
|
ssize_t n;
|
2019-10-25 23:53:43 +02:00
|
|
|
|
while (1) {
|
2019-11-06 07:36:00 +01:00
|
|
|
|
n = read(in, buf, sizeof buf);
|
2019-10-25 23:53:43 +02:00
|
|
|
|
if (n == -1) {
|
|
|
|
|
perror("read");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
n = write(s, buf, n);
|
|
|
|
|
if (n == -1) {
|
|
|
|
|
perror("write");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(s);
|
|
|
|
|
}
|
2019-11-06 07:36:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|