Module: DhcpsApi::RubyStructAttrHelpers
- Included in:
- DHCPS_Struct, Server
- Defined in:
- lib/dhcpsapi/common.rb
Instance Method Summary collapse
- #dhcp_client_uid_to_mac(dhcp_client_uid) ⇒ Object
- #ip_to_uint32(ip_address) ⇒ Object
- #mac_address_to_array_of_uint8(mac_address) ⇒ Object
- #to_string(a_ptr) ⇒ Object
- #to_wchar_string(a_string) ⇒ Object
- #uint32_to_ip(encoded_ip) ⇒ Object
- #wchar_string_length(a_ptr) ⇒ Object
Instance Method Details
#dhcp_client_uid_to_mac(dhcp_client_uid) ⇒ Object
31 32 33 |
# File 'lib/dhcpsapi/common.rb', line 31 def dhcp_client_uid_to_mac(dhcp_client_uid) dhcp_client_uid[:data].read_array_of_type(:uint8, :read_uint8, dhcp_client_uid[:data_length]).map {|w| "%02X" % w}.join(":") end |
#ip_to_uint32(ip_address) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/dhcpsapi/common.rb', line 19 def ip_to_uint32(ip_address) ip_address.strip.split(".").inject(0) do |all_octets, current_octet| all_octets = all_octets << 8 all_octets |= current_octet.to_i all_octets end end |
#mac_address_to_array_of_uint8(mac_address) ⇒ Object
35 36 37 |
# File 'lib/dhcpsapi/common.rb', line 35 def mac_address_to_array_of_uint8(mac_address) mac_address.split(":").map {|part| part.to_i(16)} end |
#to_string(a_ptr) ⇒ Object
6 7 8 9 |
# File 'lib/dhcpsapi/common.rb', line 6 def to_string(a_ptr) return "" if a_ptr.null? a_ptr.get_bytes(0, wchar_string_length(a_ptr)*2).force_encoding('utf-16LE').encode('utf-8') end |
#to_wchar_string(a_string) ⇒ Object
39 40 41 |
# File 'lib/dhcpsapi/common.rb', line 39 def to_wchar_string(a_string) (a_string + "\x00").encode('utf-16LE') end |
#uint32_to_ip(encoded_ip) ⇒ Object
27 28 29 |
# File 'lib/dhcpsapi/common.rb', line 27 def uint32_to_ip(encoded_ip) (0..3).inject([]) {|all_octets, current_octet| all_octets << ((encoded_ip >> 8*current_octet) & 0xFF)}.reverse.join(".") end |
#wchar_string_length(a_ptr) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/dhcpsapi/common.rb', line 11 def wchar_string_length(a_ptr) str = FFI::Pointer.new(:uint16, a_ptr) (0..511).each do |i| return i if str[i].read_uint16 == 0 end 511 end |