<commit_before>#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include "net/sniffer.h"
#include "fs/file.h"

net::sniffer::sniffer()
{
  _M_fd = -1;

  _M_buf = MAP_FAILED;

  _M_frames = NULL;

  _M_idx = 0;

  _M_npackets = 0;

  _M_max_pcap_filesize = 0;

  _M_running = false;
}

net::sniffer::~sniffer()
{
  if (_M_buf != MAP_FAILED) {
    munmap(_M_buf, _M_ring_size);
  }

  if (_M_fd != -1) {
    close(_M_fd);
  }

  if (_M_frames) {
    free(_M_frames);
  }
}

bool net::sniffer::create(const char* interface, const char* pathname, size_t ring_size, size_t max_pcap_filesize)
{
  // Sanity checks.
  if ((ring_size < kMinRingSize) || (ring_size > kMaxRingSize)) {
    return false;
  }

  size_t len;
  if ((len = strlen(interface)) >= IFNAMSIZ) {
    return false;
  }

  size_t pathnamelen;
  if ((pathnamelen = strlen(pathname)) >= sizeof(_M_pathname)) {
    return false;
  }

  if (max_pcap_filesize > 0) {
    // Check that the file can be opened for reading/writing.
    fs::file f;
    if (!f.open(pathname, O_CREAT | O_RDWR)) {
      fprintf(stderr, "Couldn't open capture file %s for writing.\n", pathname);
      return false;
    }

    unlink(pathname);
  }

  // Create socket.
  if ((_M_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
    perror("socket");
    return false;
  }

  // Get header length (only for TPACKET_V2).
#ifdef HAVE_TPACKET_V2
  if (!get_header_length()) {
    return false;
  }
#endif // HAVE_TPACKET_V2

  // Set packet version (only for TPACKET_V3 or TPACKET_V2).
#if defined(HAVE_TPACKET_V3) || defined(HAVE_TPACKET_V2)
  #ifdef HAVE_TPACKET_V3
    if (!set_packet_version(TPACKET_V3)) {
  #else
    if (!set_packet_version(TPACKET_V2)) {
  #endif
      return false;
    }
#endif // defined(HAVE_TPACKET_V3) || defined(HAVE_TPACKET_V2)

  // Get interface index.
  struct ifreq ifr;
  memcpy(ifr.ifr_name, interface, len + 1);

  if (ioctl(_M_fd, SIOCGIFINDEX, &ifr) < 0) {
    perror("ioctl");
    return false;
  }

  // Setup packet ring.
  if (!setup_packet_ring(ring_size)) {
    return false;
  }

  if (max_pcap_filesize == 0) {
    // Open capture file (unlimited file size).
    if (!_M_pcap_file.open(pathname)) {
      fprintf(stderr, "Couldn't open capture file %s for writing.\n", pathname);
      return false;
    }
  } else {
    if (!_M_pkts.allocate(max_pcap_filesize)) {<commit_msg>Put the interface in promiscuous mode.<commit_after>#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include "net/sniffer.h"
#include "fs/file.h"

net::sniffer::sniffer()
{
  _M_fd = -1;

  _M_buf = MAP_FAILED;

  _M_frames = NULL;

  _M_idx = 0;

  _M_npackets = 0;

  _M_max_pcap_filesize = 0;

  _M_running = false;
}

net::sniffer::~sniffer()
{
  if (_M_buf != MAP_FAILED) {
    munmap(_M_buf, _M_ring_size);
  }

  if (_M_fd != -1) {
    close(_M_fd);
  }

  if (_M_frames) {
    free(_M_frames);
  }
}

bool net::sniffer::create(const char* interface, const char* pathname, size_t ring_size, size_t max_pcap_filesize)
{
  // Sanity checks.
  if ((ring_size < kMinRingSize) || (ring_size > kMaxRingSize)) {
    return false;
  }

  size_t len;
  if ((len = strlen(interface)) >= IFNAMSIZ) {
    return false;
  }

  size_t pathnamelen;
  if ((pathnamelen = strlen(pathname)) >= sizeof(_M_pathname)) {
    return false;
  }

  if (max_pcap_filesize > 0) {
    // Check that the file can be opened for reading/writing.
    fs::file f;
    if (!f.open(pathname, O_CREAT | O_RDWR)) {
      fprintf(stderr, "Couldn't open capture file %s for writing.\n", pathname);
      return false;
    }

    unlink(pathname);
  }

  // Create socket.
  if ((_M_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
    perror("socket");
    return false;
  }

  // Get header length (only for TPACKET_V2).
#ifdef HAVE_TPACKET_V2
  if (!get_header_length()) {
    return false;
  }
#endif // HAVE_TPACKET_V2

  // Set packet version (only for TPACKET_V3 or TPACKET_V2).
#if defined(HAVE_TPACKET_V3) || defined(HAVE_TPACKET_V2)
  #ifdef HAVE_TPACKET_V3
    if (!set_packet_version(TPACKET_V3)) {
  #else
    if (!set_packet_version(TPACKET_V2)) {
  #endif
      return false;
    }
#endif // defined(HAVE_TPACKET_V3) || defined(HAVE_TPACKET_V2)

  // Get interface index.
  struct ifreq ifr;
  memcpy(ifr.ifr_name, interface, len + 1);

  if (ioctl(_M_fd, SIOCGIFINDEX, &ifr) < 0) {
    perror("ioctl");
    return false;
  }

  // Put the interface in promiscuous mode.
  struct packet_mreq mr;
  memset(&mr, 0, sizeof(struct packet_mreq));
  mr.mr_ifindex = ifr.ifr_ifindex;
  mr.mr_type = PACKET_MR_PROMISC;
  if (setsockopt(_M_fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof(struct packet_mreq)) < 0) {
    perror("setsockopt");
    return false;
  }

  // Setup packet ring.
  if (!setup_packet_ring(ring_size)) {
    return false;
  }

  if (max_pcap_filesize == 0) {
    // Open capture file (unlimited file size).
    if (!_M_pcap_file.open(pathname)) {
      fprintf(stderr, "Couldn't open capture file %s for writing.\n", pathname);
      return false;
    }
  } else {
    if (!_M_pkts.allocate(max_pcap_filesize)) {
{'fraction_non_alphanumeric': 0.14324668242137792, 'fraction_numerical': 0.008362115978912925, 'mean_word_length': 3.4016, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 35, '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': '5346801', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2215, 'n_words': 475}