<commit_before>#include "common-static.h"
#include "ip-static.h"

#include "tun.h"

#include <stdio.h>
#include <fcntl.h>

#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/uio.h>

#ifdef __linux__
#include <linux/if.h>
#include <linux/if_tun.h>
#endif

#ifdef __APPLE__
#include <sys/sys_domain.h>
#include <sys/kern_control.h>
#include <net/if_utun.h>
#endif

#if defined(__APPLE__) || defined(__OpenBSD__)
#define GT_BSD_TUN
#endif

#ifdef __linux__
static int tun_create_by_id (_unused_ char *name, _unused_ size_t size, _unused_ unsigned id, _unused_ int mq)
{
    return -1;
}

static int tun_create_by_name (char *name, size_t size, char *dev_name, int mq)
{
    int fd = open("/dev/net/tun", O_RDWR);

    if (fd==-1)
        return -1;

    struct ifreq ifr = {
        .ifr_flags = IFF_TUN|IFF_NO_PI,
    };

    if (mq) {
#ifdef IFF_MULTI_QUEUE
        ifr.ifr_flags |= IFF_MULTI_QUEUE;
#else
        gt_na("IFF_MULTI_QUEUE");
#endif
    }

    str_cpy(ifr.ifr_name, dev_name, IFNAMSIZ-1);

    if (ioctl(fd, TUNSETIFF, &ifr)) {
        close(fd);
        return -1;
    }

    str_cpy(name, ifr.ifr_name, size-1);

    return fd;
}
#elif defined(__APPLE__)
static int tun_create_by_id (char *name, size_t size, unsigned id, _unused_ int mq)
{
    int fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);

    if (fd==-1)
        return -1;

    struct ctl_info ci;

    byte_set(&ci, 0, sizeof(ci));
    str_cpy(ci.ctl_name, UTUN_CONTROL_NAME, sizeof(ci.ctl_name)-1);

    if (ioctl(fd, CTLIOCGINFO, &ci)) {
        close(fd);
        return -1;
    }

    struct sockaddr_ctl sc = {
        .sc_id = ci.ctl_id,
        .sc_len = sizeof(sc),
        .sc_family = AF_SYSTEM,
        .ss_sysaddr = AF_SYS_CONTROL,
        .sc_unit = id+1,
    };

    if (connect(fd, (struct sockaddr *)&sc, sizeof(sc))) {
        close(fd);
        return -1;
    }

    snprintf(name, size, "/dev/utun%u", id);

    return fd;
}

static int tun_create_by_name (char *name, size_t size, char *dev_name, _unused_ int mq)
{
    unsigned id = 0;

    if (sscanf(dev_name, "/dev/utun%u", &id)!=1)
        return -1;

    return tun_create_by_id(name, size, id, mq);
}
#else
static int tun_create_by_id (char *name, size_t size, unsigned id, _unused_ int mq)
{
    snprintf(name, size, "/dev/tun%u", id);
    return open(name, O_RDWR);
}

static int tun_create_by_name (char *name, size_t size, char *dev_name, _unused_ int mq)
{
    str_cpy(name, dev_name, size-1);
    return open(name, O_RDWR);
}
#endif

int tun_create (char *dev_name, int mq)
{
    char name[64];
    int fd = -1;

    if (str_empty(dev_name)) {
        for (unsigned id=0; id<32 && fd==-1; id++)
            fd = tun_create_by_id(name, sizeof(name), id, mq);
    } else {
        fd = tun_create_by_name(name, sizeof(name), dev_name, mq);
    }

    if (fd!=-1)
        gt_print("tun name: %s\n", name);
<commit_msg>Print the tun name, not the path<commit_after>#include "common-static.h"
#include "ip-static.h"

#include "tun.h"

#include <stdio.h>
#include <fcntl.h>

#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/uio.h>

#ifdef __linux__
#include <linux/if.h>
#include <linux/if_tun.h>
#endif

#ifdef __APPLE__
#include <sys/sys_domain.h>
#include <sys/kern_control.h>
#include <net/if_utun.h>
#endif

#if defined(__APPLE__) || defined(__OpenBSD__)
#define GT_BSD_TUN
#endif

#ifdef __linux__
static int tun_create_by_name (char *name, size_t size, char *dev_name, int mq)
{
    int fd = open("/dev/net/tun", O_RDWR);

    if (fd==-1)
        return -1;

    struct ifreq ifr = {
        .ifr_flags = IFF_TUN|IFF_NO_PI,
    };

    if (mq) {
#ifdef IFF_MULTI_QUEUE
        ifr.ifr_flags |= IFF_MULTI_QUEUE;
#endif
    }

    str_cpy(ifr.ifr_name, dev_name, IFNAMSIZ-1);

    if (ioctl(fd, TUNSETIFF, &ifr)) {
        close(fd);
        return -1;
    }

    str_cpy(name, ifr.ifr_name, size-1);

    return fd;
}

static int tun_create_by_id (char *name, size_t size, unsigned id, int mq)
{
    char dev_name[64];

    snprintf(dev_name, sizeof(dev_name), "tun%u", id);

    return tun_create_by_name(name, size, dev_name, mq);
}
#elif defined(__APPLE__)
static int tun_create_by_id (char *name, size_t size, unsigned id, _unused_ int mq)
{
    int fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);

    if (fd==-1)
        return -1;

    struct ctl_info ci;

    byte_set(&ci, 0, sizeof(ci));
    str_cpy(ci.ctl_name, UTUN_CONTROL_NAME, sizeof(ci.ctl_name)-1);

    if (ioctl(fd, CTLIOCGINFO, &ci)) {
        close(fd);
        return -1;
    }

    struct sockaddr_ctl sc = {
        .sc_id = ci.ctl_id,
        .sc_len = sizeof(sc),
        .sc_family = AF_SYSTEM,
        .ss_sysaddr = AF_SYS_CONTROL,
        .sc_unit = id+1,
    };

    if (connect(fd, (struct sockaddr *)&sc, sizeof(sc))) {
        close(fd);
        return -1;
    }

    snprintf(name, size, "utun%u", id);

    return fd;
}

static int tun_create_by_name (char *name, size_t size, char *dev_name, int mq)
{
    unsigned id = 0;

    if (sscanf(dev_name, "utun%u", &id)!=1)
        return -1;

    return tun_create_by_id(name, size, id, mq);
}
#else
static int tun_create_by_name (char *name, size_t size, char *dev_name, _unused_ int mq)
{
    char path[64];

    snprintf(path, sizeof(path), "/dev/%s", dev_name);
    str_cpy(name, dev_name, size-1);

    return open(path, O_RDWR);
}

static int tun_create_by_id (char *name, size_t size, unsigned id, int mq)
{
    char dev_name[64];

    snprintf(dev_name, sizeof(dev_name), "tun%u", id);

    return tun_create_by_name(name, size, dev_name, mq);
}
#endif

int tun_create (char *dev_name, int mq)
{
    char name[64];
    int fd = -1;

#ifndef IFF_MULTI_QUEUE
    if (mq)
        gt_na("IFF_MULTI_QUEUE");
#endif

    if (str_empty(dev_name)) {
        for (unsigned id=0; id<32 && fd==-1; id++)
            fd = tun_create_by_id(name, sizeof(name), id, mq);
    } else {
        fd = tun_create_by_name(name, sizeof(name), dev_name, mq);
    }

    if (fd!=-1)
        gt_print("tun name: %s\n", name);

{'fraction_non_alphanumeric': 0.14569865738438587, 'fraction_numerical': 0.009116525774904691, 'mean_word_length': 2.9749670619235835, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1134345', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2472, 'n_words': 569}