Created
March 14, 2019 12:44
-
-
Save Fugoes/32c07ffc9bcf20185c51a15115ec1d17 to your computer and use it in GitHub Desktop.
writev on tuntap fd
This file contains 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
> sudo ip tuntap add dev test mode tun user $(whoami) | |
> sudo ip link set test up | |
> gcc one_pkt.c -o one_pkt | |
> ./one_pkt |
This file contains 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
#include <stdio.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/ioctl.h> | |
#include <sys/uio.h> | |
#include <net/if.h> | |
#include <linux/if_tun.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <stdint.h> | |
int tun_alloc(const char *dev) { | |
struct ifreq ifr; | |
int fd, err; | |
if ((fd = open("/dev/net/tun", O_RDWR)) < 0) { | |
perror("open()"); | |
return fd; | |
} | |
memset(&ifr, 0, sizeof(ifr)); | |
ifr.ifr_flags = IFF_TUN | IFF_NO_PI; | |
strncpy(ifr.ifr_name, dev, IFNAMSIZ); | |
if ((err = ioctl(fd, TUNSETIFF, &ifr)) < 0) { | |
perror("ioctl()"); | |
close(fd); | |
return err; | |
} | |
return fd; | |
} | |
int main() { | |
int tun_fd = tun_alloc("test"); | |
uint8_t buf0[500] = | |
{0x45, 0x00, 0x00, 0x54, 0x8d, 0x4a, 0x40, 0x00, 0x40, 0x01, 0x64, 0x05, 0xc0, 0xa8, 0x64, 0x03, 0xc0, 0xa8, 0x64, | |
0x05}; | |
uint8_t buf1[500]; | |
struct iovec iv[2]; | |
iv[0].iov_base = buf0; | |
iv[0].iov_len = 500; | |
iv[1].iov_base = buf1; | |
iv[1].iov_len = 500; | |
writev(tun_fd, iv, 2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment