Module: FFI::Packets::Util

Defined in:
lib/ffi/packets/util.rb

Constant Summary collapse

RX_IP4_ADDR =
/(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/
RX_MAC_ADDR =
/(?:(?:[a-f0-9]{1,2}[:-])?{5}[a-f0-9]{1,2})/i

Class Method Summary collapse

Class Method Details

.htonl(*args) ⇒ Object



36
37
38
# File 'lib/ffi/packets/util.rb', line 36

def Util.htonl(*args)
  FFI::DRY::NetEndian.htonl(*args)
end

.htons(*args) ⇒ Object



40
41
42
# File 'lib/ffi/packets/util.rb', line 40

def Util.htons(*args)
  FFI::DRY::NetEndian.htons(*args)
end

.ipv4_atol(str) ⇒ Object

takes a IPv4 number and returns it as a 32-bit number



9
10
11
12
13
14
15
16
17
# File 'lib/ffi/packets/util.rb', line 9

def Util.ipv4_atol(str)
  unless str =~ /^#{RX_IP4_ADDR}$/
    raise(::ArgumentError, "invalid IP address #{str.inspect}")
  else
    u32=0
    str.split('.',4).each {|o| u32 = ((u32 << 8) | o.to_i) }
    return u32
  end
end

.ipv4_ltoa(int) ⇒ Object

takes a 32-bit number and returns it as an IPv4 address string.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ffi/packets/util.rb', line 20

def Util.ipv4_ltoa(int)
  unless(int.is_a? Numeric and int <= 0xffffffff)
    raise(::ArgumentError, "not a long integer: #{int.inspect}")
  end
  ret = []
  4.times do 
    ret.unshift(int & 0xff)
    int >>= 8
  end
  ret.join('.')
end

.ntohl(*args) ⇒ Object



44
45
46
# File 'lib/ffi/packets/util.rb', line 44

def Util.ntohl(*args)
  FFI::DRY::NetEndian.ntohl(*args)
end

.ntohs(*args) ⇒ Object



48
49
50
# File 'lib/ffi/packets/util.rb', line 48

def Util.ntohs(*args)
  FFI::DRY::NetEndian.ntohs(*args)
end

.unhexify(str, d = /\s*/) ⇒ Object



32
33
34
# File 'lib/ffi/packets/util.rb', line 32

def Util.unhexify(str, d=/\s*/)
  str.to_s.strip.gsub(/([A-Fa-f0-9]{1,2})#{d}?/) { $1.hex.chr }
end