Class: Ethernet::RawSocketFactory::BpfSocketWrapper
- Inherits:
-
Object
- Object
- Ethernet::RawSocketFactory::BpfSocketWrapper
- Defined in:
- lib/ethernet/raw_socket_factory_darwin.rb
Overview
Wraps a BPF file descriptor into a socket-like interface.
Instance Method Summary collapse
-
#close ⇒ Object
Implements Socket#close.
-
#initialize(bpf) ⇒ BpfSocketWrapper
constructor
Creates a wrapper for a BPF file descriptor.
-
#recv(buffer_size) ⇒ Object
Implements Socket#recv.
-
#send(buffer, flags) ⇒ Object
Implements Socket#send.
Constructor Details
#initialize(bpf) ⇒ BpfSocketWrapper
Creates a wrapper for a BPF file descriptor.
130 131 132 133 134 |
# File 'lib/ethernet/raw_socket_factory_darwin.rb', line 130 def initialize(bpf) @bpf = bpf @read_size = read_buffer_length @queue = [] end |
Instance Method Details
#close ⇒ Object
Implements Socket#close.
160 161 162 |
# File 'lib/ethernet/raw_socket_factory_darwin.rb', line 160 def close @bpf.close end |
#recv(buffer_size) ⇒ Object
Implements Socket#recv.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ethernet/raw_socket_factory_darwin.rb', line 137 def recv(buffer_size) while @queue.empty? read_buffer = @bpf.sysread @read_size bytes_read = read_buffer.length offset = 0 while offset < bytes_read # struct bpf_hdr in /usr/include/net/bpf.h , captured_size, original_size, header_size = *read_buffer.unpack('QLLS') @queue.push read_buffer[header_size, captured_size] # BPF_WORDALIGN in /usr/include/net/bpf.h offset += (header_size + captured_size + 3) & 0xFFF4 end end @queue.shift end |
#send(buffer, flags) ⇒ Object
Implements Socket#send.
155 156 157 |
# File 'lib/ethernet/raw_socket_factory_darwin.rb', line 155 def send(buffer, flags) @bpf.write buffer end |