Skip to content

Commit 8787bf0

Browse files
committed
fixing the Show instance of IPv4-mapped IPv6 address on little endian machines
1 parent de0a8a3 commit 8787bf0

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Network/Socket/Info.hsc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ unpackBits ((k,v):xs) r
448448
-----------------------------------------------------------------------------
449449
-- SockAddr
450450

451+
-- |
452+
--
453+
-- >>> SockAddrInet6 80 0 (0,0,0xffff,0x01020304) 0
454+
-- [::ffff:1.2.3.4]:80
451455
instance Show SockAddr where
452456
showsPrec _ (SockAddrUnix str) = showString str
453457
showsPrec _ (SockAddrInet port ha)
@@ -467,6 +471,11 @@ showHostAddress ip =
467471
let (u3, u2, u1, u0) = hostAddressToTuple ip in
468472
foldr1 (.) . intersperse (showChar '.') $ map showInt [u3, u2, u1, u0]
469473

474+
showHostAddress' :: HostAddress -> ShowS
475+
showHostAddress' ip =
476+
let (u3, u2, u1, u0) = hostAddressToTuple' ip in
477+
foldr1 (.) . intersperse (showChar '.') $ map showInt [u3, u2, u1, u0]
478+
470479
-- Taken from showIPv6 in Data.IP.Addr.
471480

472481
-- | Show an IPv6 address in the most appropriate notation, based on recommended
@@ -478,10 +487,10 @@ showHostAddress6 :: HostAddress6 -> ShowS
478487
showHostAddress6 ha6@(a1, a2, a3, a4)
479488
-- IPv4-Mapped IPv6 Address
480489
| a1 == 0 && a2 == 0 && a3 == 0xffff =
481-
showString "::ffff:" . showHostAddress a4
490+
showString "::ffff:" . showHostAddress' a4
482491
-- IPv4-Compatible IPv6 Address (exclude IPRange ::/112)
483492
| a1 == 0 && a2 == 0 && a3 == 0 && a4 >= 0x10000 =
484-
showString "::" . showHostAddress a4
493+
showString "::" . showHostAddress' a4
485494
-- length of longest run > 1, replace it with "::"
486495
| end - begin > 1 =
487496
showFields prefix . showString "::" . showFields suffix

Network/Socket/Types.hsc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module Network.Socket.Types (
5757
, isSupportedSockAddr
5858
, HostAddress
5959
, hostAddressToTuple
60+
, hostAddressToTuple'
6061
, tupleToHostAddress
6162
, HostAddress6
6263
, hostAddress6ToTuple
@@ -1231,6 +1232,11 @@ hostAddressToTuple ha' =
12311232
byte i = fromIntegral (ha `shiftR` i) :: Word8
12321233
in (byte 24, byte 16, byte 8, byte 0)
12331234

1235+
hostAddressToTuple' :: HostAddress -> (Word8, Word8, Word8, Word8)
1236+
hostAddressToTuple' ha =
1237+
let byte i = fromIntegral (ha `shiftR` i) :: Word8
1238+
in (byte 24, byte 16, byte 8, byte 0)
1239+
12341240
-- | Converts IPv4 quadruple to 'HostAddress'.
12351241
tupleToHostAddress :: (Word8, Word8, Word8, Word8) -> HostAddress
12361242
tupleToHostAddress (b3, b2, b1, b0) =

0 commit comments

Comments
 (0)