This commit is contained in:
papush! 2019-11-13 11:10:05 +01:00
parent c5108dcccf
commit 33cdb682d6
6 changed files with 76 additions and 53 deletions

View File

@ -1,32 +1,40 @@
CC := gcc -Wall -Wextra -Wpedantic -Werror -Iinclude -g
CC := gcc -Wall -Wextra -Wpedantic -Werror -Wshadow=local -Iinclude -Isrc -g -std=c11
LIBS :=
LDFLAGS := $(LDFLAGS) $(shell pkg-config --libs $(LIBS))
CFLAGS := $(CFLAGS) $(shell pkg-config --cflags $(LIBS))
# pkg-config
# 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)
TEST := $(wildcard test/*.c)
OBJS := $(patsubst src/%.c,build/%.o,$(SRC))
TEST_OBJS := $(patsubst test/%.c,build/test-%.o,$(TEST))
DEPS := $(wildcard build/*.d)
all: ext-in ext-out test-iftun tunnel64d
$(OUT): $(OBJS)
# $(OUT): $(OBJS)
$(OUT):
$(CC) $(LDFLAGS) -o $@ $^
test-ext-in: build/test-ext-in.o build/extremite.o build/iftun.o
test-ext-out: build/test-ext-out.o build/extremite.o build/iftun.o
test-iftun: build/test-iftun.o build/iftun.o
tunnel64d: build/tunnel64d.o build/extremite.o build/iftun.o
-include $(DEPS)
$(BUILD_DIR)/%.o: src/%.c
build/test-%.o: test/%.c
$(CC) $(CFLAGS) -MP -MD $< -c -o $@
build/%.o: src/%.c
$(CC) $(CFLAGS) -MP -MD $< -c -o $@
clean:
-rm -f $(BUILD_DIR)/*.o
-rm -f $(BUILD_DIR)/*.d
-rm -f build/*.o
-rm -f build/*.d

View File

@ -1,3 +1,5 @@
#include "extremite.h"
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/if.h>
@ -19,7 +21,7 @@
#define BUFSIZE 1024
void ext_out(int port, int out) {
int ext_out(int port) {
int server = socket(AF_INET6, SOCK_STREAM, 0);
if (server == -1) {
perror("socket");
@ -40,7 +42,7 @@ void ext_out(int port, int out) {
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) {
if (listen(server, 1) == -1) {
perror("listen");
exit(1);
}
@ -49,31 +51,35 @@ void ext_out(int port, int out) {
socklen_t client_addr_len;
puts("Attente dun client.");
int client = accept(server, (struct sockaddr *) &client_addr, &client_addr_len);
close(server);
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[BUFSIZE];
ssize_t n;
while (1) {
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);
}
}
return client;
close(client);
close(server);
/* char buf[BUFSIZE]; */
/* ssize_t n; */
/* while (1) { */
/* 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); */
}
void ext_in(const char addr[], int port, int in) {
int ext_in(const char addr[], int port) {
int s = socket(AF_INET6, SOCK_STREAM, 0);
if (s == -1) {
perror("socket");
@ -94,32 +100,37 @@ void ext_in(const char addr[], int port, int in) {
inet_ntop(AF_INET6, &(sa.sin6_addr), addr_pretty, sizeof addr_pretty);
printf("Connecté à : %s\n", addr_pretty);
char buf[BUFSIZE];
ssize_t n;
while (1) {
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);
}
}
return s;
close(s);
/* char buf[BUFSIZE]; */
/* ssize_t n; */
/* while (1) { */
/* 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); */
}
void ext_bidir(const char addr[], int port, int in, int out) {
(void) in;
(void) out;
pid_t pid = fork();
if (pid == 0) {
ext_in(addr, port, in);
ext_in(addr, port);
}
else {
ext_out(port, out);
ext_out(port);
waitpid(pid, NULL, 0);
}
}

View File

@ -2,8 +2,8 @@
#define EXTREMITE_H
void ext_out(int port, int out);
void ext_in(const char addr[], int port, int in);
int ext_out(int port);
int ext_in(const char addr[], int port);
void ext_bidir(const char addr[], int port, int in, int out);

View File

@ -26,7 +26,9 @@ int main(int argc, char *argv[argc]) {
fprintf(stderr, "Erreur lors de lexécution du script de configuration de linterface.\n");
return 1;
}
ext_in(argv[1], port, tun);
int ext = ext_in(argv[1], port);
copy(tun, ext);
return 0;
}

View File

@ -26,7 +26,9 @@ int main(int argc, char *argv[argc]) {
fprintf(stderr, "Erreur lors de lexécution du script de configuration de linterface.\n");
return 1;
}
ext_out(port, tun);
int ext = ext_out(port);
copy(ext, tun);
return 0;
}