Skip to content

Commit f77142f

Browse files
committed
FIX: short-circuit for unsupported patterns
Promotes UnsupportedSocketType pattern to first element of pairlist to prevent patterns whose CPP macros are undefined from greedily displaying when calling show for -1 value Adds UnsupportedCmsgId pattern to Network.Socket.*.Cmsg and ensured proper short-circuiting Reimplements read/show used in Network.Socket.Posix.Cmsg in Network.Socket.Win32.Cmsg, omitting POSIX-exclusive CmsgIdFd pattern Removes POSIX-exclusive CmsgIdFd from Network.Socket export list Adds UnsupportedCmsgId pattern to Network.Socket export list
1 parent a41d302 commit f77142f

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed

Network/Socket.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ module Network.Socket
244244
,CmsgIdIPv6TClass
245245
,CmsgIdIPv4PktInfo
246246
,CmsgIdIPv6PktInfo
247-
,CmsgIdFd)
247+
,UnsupportedCmsgId)
248248
-- ** APIs for control message
249249
, lookupCmsg
250250
, filterCmsg

Network/Socket/Options.hsc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ getSockOpt s (SockOpt level opt) = do
388388

389389
socketOptionPairs :: [Pair SocketOption String]
390390
socketOptionPairs =
391-
[ (Debug, "Debug")
391+
[ (SockOpt -1 -1, "Unsupported")
392+
, (Debug, "Debug")
392393
, (ReuseAddr, "ReuseAddr")
393394
, (SoDomain, "SoDomain")
394395
, (Type, "Type")

Network/Socket/Posix/Cmsg.hsc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ data CmsgId = CmsgId {
3838
, cmsgType :: !CInt
3939
} deriving (Eq)
4040

41+
-- | Unsupported identifier
42+
pattern UnsupportedCmsgId :: CmsgId
43+
pattern UnsupportedCmsgId = CmsgId (-1) (-1)
44+
4145
-- | The identifier for 'IPv4TTL'.
4246
pattern CmsgIdIPv4TTL :: CmsgId
4347
#if defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
@@ -227,7 +231,8 @@ instance ControlMessage Fd where
227231

228232
cmsgIdPairs :: [Pair CmsgId String]
229233
cmsgIdPairs =
230-
[ (CmsgIdIPv4TTL, "CmsgIdIPv4TTL")
234+
[ (UnsupportedCmsgId, "Unsupported")
235+
, (CmsgIdIPv4TTL, "CmsgIdIPv4TTL")
231236
, (CmsgIdIPv6HopLimit, "CmsgIdIPv6HopLimit")
232237
, (CmsgIdIPv4TOS, "CmsgIdIPv4TOS")
233238
, (CmsgIdIPv6TClass, "CmsgIdIPv6TClass")
@@ -240,7 +245,7 @@ cmsgIdBijection :: Bijection CmsgId String
240245
cmsgIdBijection = Bijection{..}
241246
where
242247
defname = "CmsgId"
243-
defFwd = \(CmsgId l t) -> defname++show l++"_" ++show t
248+
defFwd = \(CmsgId l t) -> defname++show l++"_"++show t
244249
defBwd s =
245250
case splitAt (length defname) s of
246251
("CmsgId", nm) -> uncurry CmsgId $ _parse nm

Network/Socket/Types.hsc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,13 +1318,13 @@ instance Storable In6Addr where
13181318

13191319
socktypePairs :: [Pair SocketType String]
13201320
socktypePairs =
1321-
[ (Stream, "Stream")
1321+
[ (UnsupportedSocketType, "Unsupported")
1322+
, (Stream, "Stream")
13221323
, (Datagram, "Datagram")
13231324
, (Raw, "Raw")
13241325
, (RDM, "RDM")
13251326
, (SeqPacket, "SeqPacket")
13261327
, (NoSocketType, "NoSocketType")
1327-
, (UnsupportedSocketType, "Unsupported")
13281328
]
13291329

13301330
socktypeBijection :: Bijection SocketType String

Network/Socket/Win32/Cmsg.hsc

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import System.IO.Unsafe (unsafeDupablePerformIO)
1616

1717
import Network.Socket.Imports
1818
import Network.Socket.Types
19+
import Network.Socket.ReadShow
20+
21+
import qualified Text.Read as P
1922

2023
type DWORD = Word32
2124
type ULONG = Word32
@@ -32,7 +35,11 @@ data Cmsg = Cmsg {
3235
data CmsgId = CmsgId {
3336
cmsgLevel :: !CInt
3437
, cmsgType :: !CInt
35-
} deriving (Eq, Show)
38+
} deriving (Eq)
39+
40+
-- | Unsupported identifier
41+
pattern UnsupportedCmsgId :: CmsgId
42+
pattern UnsupportedCmsgId = CmsgId (-1) (-1)
3643

3744
-- | The identifier for 'IPv4TTL'.
3845
pattern CmsgIdIPv4TTL :: CmsgId
@@ -178,3 +185,31 @@ instance Storable IPv6PktInfo where
178185
In6Addr ha6 <- (#peek IN6_PKTINFO, ipi6_addr) p
179186
n :: ULONG <- (#peek IN6_PKTINFO, ipi6_ifindex) p
180187
return $ IPv6PktInfo (fromIntegral n) ha6
188+
189+
cmsgIdPairs :: [Pair CmsgId String]
190+
cmsgIdPairs =
191+
[ (UnsupportedCmsgId, "Unsupported")
192+
, (CmsgIdIPv4TTL, "CmsgIdIPv4TTL")
193+
, (CmsgIdIPv6HopLimit, "CmsgIdIPv6HopLimit")
194+
, (CmsgIdIPv4TOS, "CmsgIdIPv4TOS")
195+
, (CmsgIdIPv6TClass, "CmsgIdIPv6TClass")
196+
, (CmsgIdIPv4PktInfo, "CmsgIdIPv4PktInfo")
197+
, (CmsgIdIPv6PktInfo, "CmsgIdIPv6PktInfo")
198+
]
199+
200+
cmsgIdBijection :: Bijection CmsgId String
201+
cmsgIdBijection = Bijection{..}
202+
where
203+
defname = "CmsgId"
204+
defFwd = \(CmsgId l t) -> defname++show l++"_"++show t
205+
defBwd s =
206+
case splitAt (length defname) s of
207+
("CmsgId", nm) -> uncurry CmsgId $ _parse nm
208+
_ -> error "cmsgIdBijection: exception in WIP ReadShow code"
209+
pairs = cmsgIdPairs
210+
211+
instance Show CmsgId where
212+
show = forward cmsgIdBijection
213+
214+
instance Read CmsgId where
215+
readPrec = P.lexP >>= \(P.Ident x) -> return $ backward cmsgIdBijection x

0 commit comments

Comments
 (0)