Last active
September 11, 2019 17:38
-
-
Save magisterquis/7282a480569df5c618b9994d5d55812a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* tapup.c | |
* Bring up a tap device and discard frames | |
* By J. Stuart McMurray | |
* Created 20190911 | |
* Last Modified 20190911 | |
*/ | |
#include <linux/if.h> | |
#include <linux/if_tun.h> | |
#include <sys/ioctl.h> | |
#include <err.h> | |
#include <fcntl.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
int | |
main(void) | |
{ | |
struct ifreq ifr; | |
char buf[2048]; | |
int fd; | |
int udp; | |
/* Device from which to request tap device */ | |
if (-1 == (fd = open("/dev/net/tun", O_RDWR))) | |
err(1, "open"); | |
/* Request our own tap device */ | |
memset(&ifr, 0, sizeof(ifr)); | |
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; | |
if (-1 == ioctl(fd, TUNSETIFF, (void *)&ifr)) | |
err(2, "ioctl"); | |
/* Bring device up. Why we need a different socket to pass to | |
* ioctl(2) is beyond me. */ | |
if (-1 == (udp = socket(AF_INET, SOCK_DGRAM, 0))) | |
err(3, "socket"); | |
ifr.ifr_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING | IFF_MULTICAST; | |
if (-1 == ioctl(udp, SIOCSIFFLAGS, &ifr)) /* Doesn't work with fd */ | |
err(4, "ioctl"); | |
if (-1 == close(udp)) /* Doesn't work with fd */ | |
err(5, "close"); | |
fprintf(stderr, "Device: %s\n", ifr.ifr_name); | |
/* Read and discard frames */ | |
for (;;) | |
if (0 >= read(fd, buf, sizeof(buf))) | |
err(6, "read"); | |
/* Unpossible */ | |
return 7; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment