Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsupported address family #56

Closed
marek22k opened this issue May 27, 2023 · 2 comments · Fixed by #61
Closed

unsupported address family #56

marek22k opened this issue May 27, 2023 · 2 comments · Fixed by #61

Comments

@marek22k
Copy link

Hello,

I do not understand the following behavior:

3.2.2 :001 > require "ipaddr"
 => true 
3.2.2 :002 > x = IPAddr.new("172.20.222.160")
 => #<IPAddr: IPv4:172.20.222.160/255.255.255.255> 
3.2.2 :003 > IPAddr.ntop(x.hton)
 => "172.20.222.160" 
3.2.2 :004 > x.hton
 => "\xAC\x14\xDE\xA0" 
3.2.2 :005 > IPAddr.ntop("\xAC\x14\xDE\xA0")
/home/marek/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/ipaddr.rb:120:in `ntop': unsupported address family (IPAddr::AddressFamilyError)

      raise AddressFamilyError, "unsupported address family"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	from (irb):5:in `<main>'
	from /home/marek/.rvm/gems/ruby-3.2.2/gems/irb-1.6.4/exe/irb:9:in `<top (required)>'
	from /home/marek/.rvm/gems/ruby-3.2.2/bin/irb:25:in `load'
	from /home/marek/.rvm/gems/ruby-3.2.2/bin/irb:25:in `<main>'
	from /home/marek/.rvm/gems/ruby-3.2.2/bin/ruby_executable_hooks:22:in `eval'
	from /home/marek/.rvm/gems/ruby-3.2.2/bin/ruby_executable_hooks:22:in `<main>'
3.2.2 :006 > y = x.hton
 => "\xAC\x14\xDE\xA0" 
3.2.2 :007 > IPAddr.ntop(y)
 => "172.20.222.160" 
3.2.2 :008 > y.class
 => String 
3.2.2 :001 > require "ipaddr"
 => true 
3.2.2 :002 > File.write("test", IPAddr.new("172.20.222.160").hton)
 => 4 
3.2.2 :003 > IPAddr.ntop(File.read("test"))
/home/marek/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/ipaddr.rb:120:in `ntop': unsupported address family (IPAddr::AddressFamilyError)

      raise AddressFamilyError, "unsupported address family"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	from (irb):3:in `<main>'
	from /home/marek/.rvm/gems/ruby-3.2.2/gems/irb-1.6.4/exe/irb:9:in `<top (required)>'
	from /home/marek/.rvm/gems/ruby-3.2.2/bin/irb:25:in `load'
	from /home/marek/.rvm/gems/ruby-3.2.2/bin/irb:25:in `<main>'
	from /home/marek/.rvm/gems/ruby-3.2.2/bin/ruby_executable_hooks:22:in `eval'
	from /home/marek/.rvm/gems/ruby-3.2.2/bin/ruby_executable_hooks:22:in `<main>'

If I try to pass the binary string as a string, an error is returned, but if I do not pass the string as a variable, it works.
This error also occurred when I tried to read an IP address from an MRT file. I then had the string displayed and tried it manually (see above). But I also got the same error.

hanazuki added a commit to hanazuki/ipaddr that referenced this issue Nov 27, 2023
`IPAddr.ntop` takes a binary representation of IP address, whose
length should be 4 or 16 *bytes* (not characters/codepoints).

This patch fixes the bug that `IPAddr.ntop` doesn't handle Strings in
a multibyte encoding correctly.

Fixes: ruby#56
hanazuki added a commit to hanazuki/ipaddr that referenced this issue Nov 27, 2023
`IPAddr.ntop` takes the binary representation of an IP address, whose
length should be 4 or 16 *bytes* (not characters/codepoints).

This patch fixes the bug that `IPAddr.ntop` doesn't handle Strings in
a multibyte encoding correctly.

Fixes: ruby#56
@knu
Copy link
Member

knu commented Nov 27, 2023

Actually, it is not appropriate to pass a non-binary string. A string literal written in source code is encoded in the default encoding, which is usually UTF-8.

require "ipaddr"

x = IPAddr.new("172.20.222.160")
p x.hton #=> "\xAC\x14\xDE\xA0"
p x.hton.encoding #=> #<Encoding:ASCII-8BIT>
# It properly round-trips.
p IPAdd.ntoh(x.hton) #=> "172.20.222.160"

p "\xAC\x14\xDE\xA0".encoding #=> #<Encoding::UTF-8>
# ntop does not take a non-binary string
p IPAddr.ntop("\xAC\x14\xDE\xA0") rescue $! #=> #<IPAddr::AddressFamilyError: unsupported address family>

# The correct way
p IPAddr.ntop("\xAC\x14\xDE\xA0".b) #=> "172.20.222.160"

@marek22k
Copy link
Author

Thanks for the explanation!

hanazuki added a commit to hanazuki/ipaddr that referenced this issue Nov 28, 2023
`IPAddr.ntop` takes the binary representation of an IP address, whose
length should be 4 or 16 *bytes* (not characters/codepoints).

The current implementation accepts strings in any encoding, but for
some values in non-BINARY encoding, it fails proper length check and
raises an `AddressFamilyError`. Since passing strings in a multibyte
encoding has never worked correctly for years, this patch makes it an
explicit error with an `InvalidAddressError`.

Fixes: ruby#56
hanazuki added a commit to hanazuki/ipaddr that referenced this issue Nov 28, 2023
`IPAddr.ntop` takes the binary representation of an IP address, whose
length should be 4 or 16 *bytes* (not characters/codepoints).

The current implementation accepts strings in any encoding, but for
some values in non-BINARY encoding, it fails proper length check and
raises an `AddressFamilyError`. Since passing strings in a multibyte
encoding has never worked correctly for years, this patch makes it an
explicit error with an `InvalidAddressError`.

Fixes: ruby#56
hanazuki added a commit to hanazuki/ipaddr that referenced this issue Nov 28, 2023
`IPAddr.ntop` takes the binary representation of an IP address, whose
length should be 4 or 16 *bytes* (not characters/codepoints).

The current implementation accepts strings in any encoding, but for
some values in non-BINARY encoding, it fails proper length check and
raises an `AddressFamilyError`. Since passing strings in a multibyte
encoding has never worked correctly for years, this patch makes it an
explicit error with an `InvalidAddressError`.

Fixes: ruby#56
hanazuki added a commit to hanazuki/ipaddr that referenced this issue Nov 28, 2023
`IPAddr.ntop` takes the binary representation of an IP address, whose
length should be 4 or 16 *bytes* (not characters/codepoints).

The current implementation accepts strings in any encoding, but for
some values in non-BINARY encoding, it fails proper length check and
raises an `AddressFamilyError`. Since passing strings in a multibyte
encoding has never worked correctly for years, this patch makes it an
explicit error with an `InvalidAddressError`.

Fixes: ruby#56
hsbt pushed a commit to hsbt/ruby that referenced this issue Dec 25, 2023
`IPAddr.ntop` takes the binary representation of an IP address, whose
length should be 4 or 16 *bytes* (not characters/codepoints).

The current implementation accepts strings in any encoding, but for
some values in non-BINARY encoding, it fails proper length check and
raises an `AddressFamilyError`. Since passing strings in a multibyte
encoding has never worked correctly for years, this patch makes it an
explicit error with an `InvalidAddressError`.

Fixes: ruby/ipaddr#56

ruby/ipaddr@a33fd14d4a
hsbt pushed a commit to hsbt/ruby that referenced this issue Dec 25, 2023
`IPAddr.ntop` takes the binary representation of an IP address, whose
length should be 4 or 16 *bytes* (not characters/codepoints).

The current implementation accepts strings in any encoding, but for
some values in non-BINARY encoding, it fails proper length check and
raises an `AddressFamilyError`. Since passing strings in a multibyte
encoding has never worked correctly for years, this patch makes it an
explicit error with an `InvalidAddressError`.

Fixes: ruby/ipaddr#56

ruby/ipaddr@a33fd14d4a
hsbt pushed a commit to hsbt/ruby that referenced this issue Dec 25, 2023
`IPAddr.ntop` takes the binary representation of an IP address, whose
length should be 4 or 16 *bytes* (not characters/codepoints).

The current implementation accepts strings in any encoding, but for
some values in non-BINARY encoding, it fails proper length check and
raises an `AddressFamilyError`. Since passing strings in a multibyte
encoding has never worked correctly for years, this patch makes it an
explicit error with an `InvalidAddressError`.

Fixes: ruby/ipaddr#56

ruby/ipaddr@a33fd14d4a
hsbt pushed a commit to hsbt/ruby that referenced this issue Dec 25, 2023
`IPAddr.ntop` takes the binary representation of an IP address, whose
length should be 4 or 16 *bytes* (not characters/codepoints).

The current implementation accepts strings in any encoding, but for
some values in non-BINARY encoding, it fails proper length check and
raises an `AddressFamilyError`. Since passing strings in a multibyte
encoding has never worked correctly for years, this patch makes it an
explicit error with an `InvalidAddressError`.

Fixes: ruby/ipaddr#56

ruby/ipaddr@a33fd14d4a
hsbt pushed a commit to ruby/ruby that referenced this issue Dec 25, 2023
`IPAddr.ntop` takes the binary representation of an IP address, whose
length should be 4 or 16 *bytes* (not characters/codepoints).

The current implementation accepts strings in any encoding, but for
some values in non-BINARY encoding, it fails proper length check and
raises an `AddressFamilyError`. Since passing strings in a multibyte
encoding has never worked correctly for years, this patch makes it an
explicit error with an `InvalidAddressError`.

Fixes: ruby/ipaddr#56

ruby/ipaddr@a33fd14d4a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants