Skip to content

Instantly share code, notes, and snippets.

@Fugoes
Created March 14, 2019 12:44
Show Gist options
  • Save Fugoes/32c07ffc9bcf20185c51a15115ec1d17 to your computer and use it in GitHub Desktop.
Save Fugoes/32c07ffc9bcf20185c51a15115ec1d17 to your computer and use it in GitHub Desktop.
writev on tuntap fd
> 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
#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