53 lines
762 B
C
53 lines
762 B
C
#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(const char *dev) {
|
|
int fd = open("/dev/net/tun", O_RDWR);
|
|
if(fd < 0) {
|
|
perror("open");
|
|
exit(1);
|
|
}
|
|
|
|
struct ifreq 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){
|
|
exit(1);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|