From 2642e66fde569beca8823ad823db27daef860f5c Mon Sep 17 00:00:00 2001 From: COLIN Cyril Date: Fri, 25 Oct 2019 11:37:27 +0200 Subject: [PATCH] lol --- partage/Makefile | 36 ++++++++++++++++ partage/configure-tun.sh | 3 ++ partage/src/iftun.c | 26 ++++++++++++ partage/src/iftun.h | 8 ++++ partage/src/tunalloc.c | 91 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 164 insertions(+) create mode 100644 partage/Makefile create mode 100755 partage/configure-tun.sh create mode 100644 partage/src/iftun.c create mode 100644 partage/src/iftun.h create mode 100644 partage/src/tunalloc.c diff --git a/partage/Makefile b/partage/Makefile new file mode 100644 index 0000000..c019893 --- /dev/null +++ b/partage/Makefile @@ -0,0 +1,36 @@ +CC := gcc -Wall -Wextra -Wpedantic -Werror -Iinclude -g + + +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 $@ + + +clean: + -rm -f $(BUILD_DIR)/*.o + -rm -f $(BUILD_DIR)/*.d diff --git a/partage/configure-tun.sh b/partage/configure-tun.sh new file mode 100755 index 0000000..427851c --- /dev/null +++ b/partage/configure-tun.sh @@ -0,0 +1,3 @@ +#!/bin/sh +ip l set tun0 up +ip a a 172.16.2.1/28 dev tun0 diff --git a/partage/src/iftun.c b/partage/src/iftun.c new file mode 100644 index 0000000..63105ba --- /dev/null +++ b/partage/src/iftun.c @@ -0,0 +1,26 @@ +#include "iftun.h" + + +int tun_alloc(char *dev) { + int fd = open("/dev/net/tun", O_RDWR); + if(fd < 0 ) { + perror("alloc tun"); + exit(1); + } + + struct ifreq ifr = {.ifr_flags = IFF_TUN}; + ifr.ifr_flags = IFF_TUN | IFF_NO_PI; + + if (*dev) { + strncpy(ifr.ifr_name, dev, IFNAMSIZ); + } + + int err = ioctl(fd, TUNSETIFF, (void *) &ifr); + if(err < 0 ){ + close(fd); + return err; + } + + strcpy(dev, ifr.ifr_name); + return fd; +} diff --git a/partage/src/iftun.h b/partage/src/iftun.h new file mode 100644 index 0000000..7d3732b --- /dev/null +++ b/partage/src/iftun.h @@ -0,0 +1,8 @@ +#ifndef IFTUN_H +#define IFTUN_H + + +int tun_alloc(char *dev); + + +#endif diff --git a/partage/src/tunalloc.c b/partage/src/tunalloc.c new file mode 100644 index 0000000..b3a38e9 --- /dev/null +++ b/partage/src/tunalloc.c @@ -0,0 +1,91 @@ +#define _POSIX_C_SOURCE 200112L + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + + +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, + .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(); */ + system("./configure-tun.sh"); + /* copy(tun, 1); */ + /* ext_out(); */ + return 0; +}