<commit_before>module(..., package.seeall)

local S = require("syscall")
local h = require("syscall.helpers")
local bit = require("bit")
local link = require("core.link")
local packet = require("core.packet")
local ffi = require("ffi")
local C = ffi.C

local c, t = S.c, S.types.t

RawSocket = {}

function RawSocket:new (ifname)
   assert(ifname)
   local tp = h.htons(c.ETH_P["ALL"])
   local sock, err = S.socket(c.AF.PACKET, bit.bor(c.SOCK.RAW, c.SOCK.NONBLOCK), tp)
   if not sock then return nil, err end
   local index, err = S.util.if_nametoindex(ifname, sock)
   if err then
      S.close(sock)
      return nil, err
   end
   local addr = t.sockaddr_ll{sll_family = c.AF.PACKET, sll_ifindex = index, sll_protocol = tp}
   local ok, err = S.bind(sock, addr)
   if not ok then
      S.close(sock)
      return nil, err
   end
   return setmetatable({sock = sock}, {__index = RawSocket})
end

function RawSocket:pull ()
   local l = self.output.tx
   if l == nil then return end
   while not link.full(l) and self:can_receive() do
      link.transmit(l, self:receive())
   end
end

function RawSocket:can_receive ()
   local ok, err = S.select({readfds = {self.sock}}, 0)
   return not (err or ok.count == 0)
end<commit_msg>Raise exception on error<commit_after>module(..., package.seeall)

local S = require("syscall")
local h = require("syscall.helpers")
local bit = require("bit")
local link = require("core.link")
local packet = require("core.packet")
local ffi = require("ffi")
local C = ffi.C

local c, t = S.c, S.types.t

RawSocket = {}

function RawSocket:new (ifname)
   assert(ifname)
   local tp = h.htons(c.ETH_P["ALL"])
   local sock, err = S.socket(c.AF.PACKET, bit.bor(c.SOCK.RAW, c.SOCK.NONBLOCK), tp)
   if not sock then error(err) end
   local index, err = S.util.if_nametoindex(ifname, sock)
   if err then
      S.close(sock)
      error(err)
   end
   local addr = t.sockaddr_ll{sll_family = c.AF.PACKET, sll_ifindex = index, sll_protocol = tp}
   local ok, err = S.bind(sock, addr)
   if not ok then
      S.close(sock)
      error(err)
   end
   return setmetatable({sock = sock}, {__index = RawSocket})
end

function RawSocket:pull ()
   local l = self.output.tx
   if l == nil then return end
   while not link.full(l) and self:can_receive() do
      link.transmit(l, self:receive())
   end
end

function RawSocket:can_receive ()
   local ok, err = S.select({readfds = {self.sock}}, 0)
   return not (err or ok.count == 0)
end
{'fraction_non_alphanumeric': 0.13791700569568754, 'fraction_numerical': 0.0016273393002441008, 'mean_word_length': 3.4068100358422937, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3590661', 'n_tokens_mistral': 905, 'n_tokens_neox': 888, 'n_words': 252}