Skip to content

Commit 3fec7fe

Browse files
committed
resolving orphan instance
1 parent 4975027 commit 3fec7fe

2 files changed

Lines changed: 59 additions & 63 deletions

File tree

Network/Socket/Info.hsc

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE RecordWildCards #-}
3-
{-# OPTIONS_GHC -fno-warn-orphans #-}
43

54
#include "HsNet.h"
65
##include "HsNetDef.h"
@@ -447,68 +446,6 @@ unpackBits ((k,v):xs) r
447446
| r .&. v /= 0 = k : unpackBits xs (r .&. complement v)
448447
| otherwise = unpackBits xs r
449448

450-
-----------------------------------------------------------------------------
451-
-- SockAddr
452-
453-
-- |
454-
--
455-
-- >>> SockAddrInet6 80 0 (0,0,0xffff,0x01020304) 0
456-
-- [::ffff:1.2.3.4]:80
457-
instance Show SockAddr where
458-
showsPrec _ (SockAddrUnix str) = showString str
459-
showsPrec _ (SockAddrInet port ha)
460-
= showHostAddress ha
461-
. showString ":"
462-
. shows port
463-
showsPrec _ (SockAddrInet6 port _ ha6 _)
464-
= showChar '['
465-
. showHostAddress6 ha6
466-
. showString "]:"
467-
. shows port
468-
469-
470-
-- Taken from on the implementation of showIPv4 in Data.IP.Addr
471-
showHostAddress :: HostAddress -> ShowS
472-
showHostAddress ip =
473-
let (u3, u2, u1, u0) = hostAddressToTuple ip in
474-
foldr1 (.) . intersperse (showChar '.') $ map showInt [u3, u2, u1, u0]
475-
476-
showHostAddress' :: HostAddress -> ShowS
477-
showHostAddress' ip =
478-
let (u3, u2, u1, u0) = hostAddressToTuple' ip in
479-
foldr1 (.) . intersperse (showChar '.') $ map showInt [u3, u2, u1, u0]
480-
481-
-- Taken from showIPv6 in Data.IP.Addr.
482-
483-
-- | Show an IPv6 address in the most appropriate notation, based on recommended
484-
-- representation proposed by <http://tools.ietf.org/html/rfc5952 RFC 5952>.
485-
--
486-
-- /The implementation is completely compatible with the current implementation
487-
-- of the `inet_ntop` function in glibc./
488-
showHostAddress6 :: HostAddress6 -> ShowS
489-
showHostAddress6 ha6@(a1, a2, a3, a4)
490-
-- IPv4-Mapped IPv6 Address
491-
| a1 == 0 && a2 == 0 && a3 == 0xffff =
492-
showString "::ffff:" . showHostAddress' a4
493-
-- IPv4-Compatible IPv6 Address (exclude IPRange ::/112)
494-
| a1 == 0 && a2 == 0 && a3 == 0 && a4 >= 0x10000 =
495-
showString "::" . showHostAddress' a4
496-
-- length of longest run > 1, replace it with "::"
497-
| end - begin > 1 =
498-
showFields prefix . showString "::" . showFields suffix
499-
| otherwise =
500-
showFields fields
501-
where
502-
fields =
503-
let (u7, u6, u5, u4, u3, u2, u1, u0) = hostAddress6ToTuple ha6 in
504-
[u7, u6, u5, u4, u3, u2, u1, u0]
505-
showFields = foldr (.) id . intersperse (showChar ':') . map showHex
506-
prefix = take begin fields -- fields before "::"
507-
suffix = drop end fields -- fields after "::"
508-
begin = end + diff -- the longest run of zeros
509-
(diff, end) = minimum $
510-
scanl (\c i -> if i == 0 then c - 1 else 0) 0 fields `zip` [0..]
511-
512449
-----------------------------------------------------------------------------
513450

514451
-- | A utility function to open a socket with `AddrInfo`.

Network/Socket/Types.hsc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,65 @@ instance NFData SockAddr where
10841084
rnf (SockAddrInet6 _ _ _ _) = ()
10851085
rnf (SockAddrUnix str) = rnf str
10861086

1087+
-- |
1088+
--
1089+
-- >>> SockAddrInet6 80 0 (0,0,0xffff,0x01020304) 0
1090+
-- [::ffff:1.2.3.4]:80
1091+
instance Show SockAddr where
1092+
showsPrec _ (SockAddrUnix str) = showString str
1093+
showsPrec _ (SockAddrInet port ha)
1094+
= showHostAddress ha
1095+
. showString ":"
1096+
. shows port
1097+
showsPrec _ (SockAddrInet6 port _ ha6 _)
1098+
= showChar '['
1099+
. showHostAddress6 ha6
1100+
. showString "]:"
1101+
. shows port
1102+
1103+
1104+
-- Taken from on the implementation of showIPv4 in Data.IP.Addr
1105+
showHostAddress :: HostAddress -> ShowS
1106+
showHostAddress ip =
1107+
let (u3, u2, u1, u0) = hostAddressToTuple ip in
1108+
foldr1 (.) . intersperse (showChar '.') $ map showInt [u3, u2, u1, u0]
1109+
1110+
showHostAddress' :: HostAddress -> ShowS
1111+
showHostAddress' ip =
1112+
let (u3, u2, u1, u0) = hostAddressToTuple' ip in
1113+
foldr1 (.) . intersperse (showChar '.') $ map showInt [u3, u2, u1, u0]
1114+
1115+
-- Taken from showIPv6 in Data.IP.Addr.
1116+
1117+
-- | Show an IPv6 address in the most appropriate notation, based on recommended
1118+
-- representation proposed by <http://tools.ietf.org/html/rfc5952 RFC 5952>.
1119+
--
1120+
-- /The implementation is completely compatible with the current implementation
1121+
-- of the `inet_ntop` function in glibc./
1122+
showHostAddress6 :: HostAddress6 -> ShowS
1123+
showHostAddress6 ha6@(a1, a2, a3, a4)
1124+
-- IPv4-Mapped IPv6 Address
1125+
| a1 == 0 && a2 == 0 && a3 == 0xffff =
1126+
showString "::ffff:" . showHostAddress' a4
1127+
-- IPv4-Compatible IPv6 Address (exclude IPRange ::/112)
1128+
| a1 == 0 && a2 == 0 && a3 == 0 && a4 >= 0x10000 =
1129+
showString "::" . showHostAddress' a4
1130+
-- length of longest run > 1, replace it with "::"
1131+
| end - begin > 1 =
1132+
showFields prefix . showString "::" . showFields suffix
1133+
| otherwise =
1134+
showFields fields
1135+
where
1136+
fields =
1137+
let (u7, u6, u5, u4, u3, u2, u1, u0) = hostAddress6ToTuple ha6 in
1138+
[u7, u6, u5, u4, u3, u2, u1, u0]
1139+
showFields = foldr (.) id . intersperse (showChar ':') . map showHex
1140+
prefix = take begin fields -- fields before "::"
1141+
suffix = drop end fields -- fields after "::"
1142+
begin = end + diff -- the longest run of zeros
1143+
(diff, end) = minimum $
1144+
scanl (\c i -> if i == 0 then c - 1 else 0) 0 fields `zip` [0..]
1145+
10871146
-- | Is the socket address type supported on this system?
10881147
isSupportedSockAddr :: SockAddr -> Bool
10891148
isSupportedSockAddr addr = case addr of

0 commit comments

Comments
 (0)