lol
This commit is contained in:
parent
f1fbdeb288
commit
2642e66fde
36
partage/Makefile
Normal file
36
partage/Makefile
Normal file
@ -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
|
3
partage/configure-tun.sh
Executable file
3
partage/configure-tun.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
ip l set tun0 up
|
||||
ip a a 172.16.2.1/28 dev tun0
|
26
partage/src/iftun.c
Normal file
26
partage/src/iftun.c
Normal file
@ -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;
|
||||
}
|
8
partage/src/iftun.h
Normal file
8
partage/src/iftun.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef IFTUN_H
|
||||
#define IFTUN_H
|
||||
|
||||
|
||||
int tun_alloc(char *dev);
|
||||
|
||||
|
||||
#endif
|
91
partage/src/tunalloc.c
Normal file
91
partage/src/tunalloc.c
Normal file
@ -0,0 +1,91 @@
|
||||
#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>
|
||||
|
||||
|
||||
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;
|
||||
}
|
Reference in New Issue
Block a user