ajout d’iftun et test-iftun

This commit is contained in:
papush! 2019-10-25 18:07:02 +02:00
parent 2642e66fde
commit 70bb38e63a
5 changed files with 59 additions and 40 deletions

View File

@ -1,35 +1,22 @@
CC := gcc -Wall -Wextra -Wpedantic -Werror -Iinclude -g
CC := gcc -Wall -Wextra -Wpedantic -Werror -Iinclude -std=c11 -g
all: build test-iftun
build:
-mkdir build
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: build/iftun.a
# LIBS :=
# LDFLAGS := $(LDFLAGS) $(shell pkg-config --libs $(LIBS))
# CFLAGS := $(CFLAGS) $(shell pkg-config --cflags $(LIBS))
BUILD_DIR ?= build
OUT := $(notdir $(shell pwd))
SRC := $(wildcard src/*.c)
OBJS := $(patsubst src/%.c,$(BUILD_DIR)/%.o,$(SRC))
DEPS := $(wildcard $(BUILD_DIR)/*.d)
$(BUILD_DIR)/$(OUT): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $^
-include $(DEPS)
$(BUILD_DIR)/%.o: src/%.c
$(CC) $(CFLAGS) -MP -MD $< -c -o $@
ar rcs $@ $<
build/iftun.o: src/iftun.c src/iftun.h
$(CC) $(CFLAGS) $< -c -o $@
clean:
-rm -f $(BUILD_DIR)/*.o

View File

@ -1,5 +1,21 @@
#include "iftun.h"
#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>
int tun_alloc(char *dev) {
int fd = open("/dev/net/tun", O_RDWR);
@ -24,3 +40,14 @@ int tun_alloc(char *dev) {
strcpy(dev, ifr.ifr_name);
return fd;
}
int copy(int src, int dest) {
char buf[1024];
int n = 0;
while (1) {
n = read(src, buf, 1024);
write(dest, buf, n);
}
}

View File

@ -3,6 +3,7 @@
int tun_alloc(char *dev);
int copy(int src, int dest);
#endif

16
partage/src/test-iftun.c Normal file
View File

@ -0,0 +1,16 @@
#include "iftun.h"
#include <stdio.h>
int main(int argc, char *argv[argc]) {
if (argc != 2) {
fprintf(stderr, "Utilisation : %s interface\n", argv[0]);
return 1;
}
int tun = tun_alloc(argv[1]);
printf("Configurez linterface puis appuyez sur une touche.\n");
getchar();
copy(tun, 1);
return 0;
}

View File

@ -17,17 +17,6 @@
#include <linux/if_tun.h>
int copy(int src, int dest) {
char buf[1024];
int n = 0;
while (1) {
n = read(src, buf, 1024);
write(dest, buf, n);
}
}
void ext_out() {
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
@ -84,7 +73,6 @@ 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(); */
system("./configure-tun.sh");
/* copy(tun, 1); */
/* ext_out(); */
return 0;