pppoe-hero/src/main.c

55 lines
1.0 KiB
C

#include <fcntl.h>
#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);
}
}