Create dummy tap interface
All checks were successful
Build PPPOE-Hero / Build-PPPOE-Hero (push) Successful in 22s
All checks were successful
Build PPPOE-Hero / Build-PPPOE-Hero (push) Successful in 22s
This commit is contained in:
parent
baeebe8a48
commit
a0327757ea
55
src/main.c
55
src/main.c
|
@ -1,3 +1,54 @@
|
||||||
int main(int argc, char* argv[]) {
|
#include <fcntl.h>
|
||||||
return(0);
|
#include <linux/if.h>
|
||||||
|
#include <linux/if_tun.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int tap_alloc(char *dev) {
|
||||||
|
struct ifreq ifr;
|
||||||
|
int fd, err;
|
||||||
|
|
||||||
|
if((fd = open("/dev/net/tun", O_RDWR)) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&ifr, 0, sizeof(ifr));
|
||||||
|
|
||||||
|
ifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE;
|
||||||
|
if(*dev) {
|
||||||
|
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
|
||||||
|
}
|
||||||
|
|
||||||
|
if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0) {
|
||||||
|
close(fd);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
int tap_fd;
|
||||||
|
char tap_name[IFNAMSIZ];
|
||||||
|
char buffer[4096];
|
||||||
|
strcpy(tap_name, "taptest");
|
||||||
|
tap_fd = tap_alloc(tap_name);
|
||||||
|
|
||||||
|
if(tap_fd < 0) {
|
||||||
|
perror("Error while allocating the tap interface");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
int nread = read(tap_fd,buffer,sizeof(buffer));
|
||||||
|
if (nread < 0) {
|
||||||
|
perror("Error while reading from tap");
|
||||||
|
close(tap_fd);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
printf("Read %d bytes from device %s\n", nread, tap_name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue