<commit_before>
#include "packet_queue.hpp"
#include "if_addrs.hpp"

#include <fcntl.h>
#include <string>
#include <thread>

#define ELPP_THREAD_SAFE
#include "easylogging++.h"

#ifdef __APPLE_CC__
int tun_alloc(std::string &dev) {
  int fd = 0;
  for(int i = 0; i < 255; ++i) {
    char buf[128];
    sprintf(buf, "/dev/tun%d", i);
    printf("try:%s\n", buf);
    if( (fd = open(buf, O_RDWR)) > 0 ) {
      char *slash = strrchr(buf, '/');
      if (slash) {
        dev = slash + 1;
      } else {
        dev = buf;
      }
      return fd;
    }
  }
  return -1;
}
#else
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_tun.h>
int tun_alloc(std::string &dev) {
  int fd = open("/dev/net/tun", O_RDWR);
  if(fd < 0) {
   return -1;
  }
  for (int i  = 0; i < 255; ++i) {
    struct ifreq ifr;
    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_flags = IFF_TUN;
    char buf[128];
    sprintf(buf, "/dev/tun%d", i);
    printf("try:%s\n", buf);
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
    int err = ioctl(fd, TUNSETIFF, &ifr);
    if (err >= 0) {
      dev =  ifr.ifr_name
      return fd;
    }
  }
  close(fd);
  return -1;
#endif


class TunDevice {
private:
  const IfAddrs &ifAddrs;
  PacketQueue fromTun;
  PacketQueue toTun;
  bool running;
  int tunFd;
  std::string tunDevName;
  std::unique_ptr<std::thread> fromTunThread;
  std::unique_ptr<std::thread> toTunThread;


  bool startOnTun() {
    const int fd = tun_alloc(tunDevName);
    if (fd < 0) {
      LOG(ERROR) << "tun_alloc failed with:" << errno;<commit_msg>linux fix<commit_after>
#include "packet_queue.hpp"
#include "if_addrs.hpp"

#include <fcntl.h>
#include <string>
#include <thread>

#define ELPP_THREAD_SAFE
#include "easylogging++.h"

#ifdef __APPLE_CC__
int tun_alloc(std::string &dev) {
  int fd = 0;
  for(int i = 0; i < 255; ++i) {
    char buf[128];
    sprintf(buf, "/dev/tun%d", i);
    printf("try:%s\n", buf);
    if( (fd = open(buf, O_RDWR)) > 0 ) {
      char *slash = strrchr(buf, '/');
      if (slash) {
        dev = slash + 1;
      } else {
        dev = buf;
      }
      return fd;
    }
  }
  return -1;
}
#else
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_tun.h>
int tun_alloc(std::string &dev) {
  int fd = open("/dev/net/tun", O_RDWR);
  if(fd < 0) {
   return -1;
  }
  for (int i  = 0; i < 255; ++i) {
    struct ifreq ifr;
    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_flags = IFF_TUN;
    char buf[IFNAMSIZ];
    snprintf(buf, sizeof(buf), "tun%d", i);
    LOG(INFO) << "try:" <<  buf;
    strncpy(ifr.ifr_name, buf, IFNAMSIZ);
    int err = ioctl(fd, TUNSETIFF, &ifr);
    if (err >= 0) {
      dev =  ifr.ifr_name;
      return fd;
    }
  }
  close(fd);
  return -1;
}
#endif


class TunDevice {
private:
  const IfAddrs &ifAddrs;
  PacketQueue fromTun;
  PacketQueue toTun;
  bool running;
  int tunFd;
  std::string tunDevName;
  std::unique_ptr<std::thread> fromTunThread;
  std::unique_ptr<std::thread> toTunThread;


  bool startOnTun() {
    const int fd = tun_alloc(tunDevName);
    if (fd < 0) {
      LOG(ERROR) << "tun_alloc failed with:" << errno;
{'fraction_non_alphanumeric': 0.16984178237003553, 'fraction_numerical': 0.014530190506942202, 'mean_word_length': 2.838909541511772, 'pattern_counts': {'":': 0, '<': 39, '<?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': '5850750', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1308, 'n_words': 300}