Skip to content

Commit

Permalink
Convert ipv6 to an uint128 and compare
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhoerberg committed Dec 13, 2022
1 parent a5331b7 commit 3663384
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/socket/address.cr
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ class Socket
addr.s_addr & 0x000000ff_u32 == 0x0000007f_u32
in LibC::In6Addr
addr8 = ipv6_addr8(addr)
addr8 == StaticArray[0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 1_u8] || # "::1"
addr8.to_slice[0, 13] == StaticArray[0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 255_u8, 255_u8, 127_u8].to_slice # "::ffff:127.0.0.1/104"
num = addr8.unsafe_as(UInt128)
num == (1_u128 << 120) || # "::1"
num & 0xffffffffffffffffffffffffff_u128 == 0x7fffff00000000000000000000_u128 # "::ffff:127.0.0.1/104"
end
end

Expand Down

0 comments on commit 3663384

Please sign in to comment.