Skip to content

Commit d7302a6

Browse files
committed
Merge PR #452.
2 parents c513332 + cb7b9c5 commit d7302a6

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

Network/Socket/Posix/Cmsg.hsc

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ data CmsgId = CmsgId {
3636

3737
-- | The identifier for 'IPv4TTL'.
3838
pattern CmsgIdIPv4TTL :: CmsgId
39-
#if defined(darwin_HOST_OS)
39+
#if defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
4040
pattern CmsgIdIPv4TTL = CmsgId (#const IPPROTO_IP) (#const IP_RECVTTL)
4141
#else
4242
pattern CmsgIdIPv4TTL = CmsgId (#const IPPROTO_IP) (#const IP_TTL)
@@ -48,7 +48,7 @@ pattern CmsgIdIPv6HopLimit = CmsgId (#const IPPROTO_IPV6) (#const IPV6_HOPLIMIT)
4848

4949
-- | The identifier for 'IPv4TOS'.
5050
pattern CmsgIdIPv4TOS :: CmsgId
51-
#if defined(darwin_HOST_OS)
51+
#if defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
5252
pattern CmsgIdIPv4TOS = CmsgId (#const IPPROTO_IP) (#const IP_RECVTOS)
5353
#else
5454
pattern CmsgIdIPv4TOS = CmsgId (#const IPPROTO_IP) (#const IP_TOS)
@@ -60,11 +60,19 @@ pattern CmsgIdIPv6TClass = CmsgId (#const IPPROTO_IPV6) (#const IPV6_TCLASS)
6060

6161
-- | The identifier for 'IPv4PktInfo'.
6262
pattern CmsgIdIPv4PktInfo :: CmsgId
63+
#if defined(IP_PKTINFO)
6364
pattern CmsgIdIPv4PktInfo = CmsgId (#const IPPROTO_IP) (#const IP_PKTINFO)
65+
#else
66+
pattern CmsgIdIPv4PktInfo = CmsgId (-1) (-1)
67+
#endif
6468

6569
-- | The identifier for 'IPv6PktInfo'.
6670
pattern CmsgIdIPv6PktInfo :: CmsgId
71+
#if defined(IPV6_PKTINFO)
6772
pattern CmsgIdIPv6PktInfo = CmsgId (#const IPPROTO_IPV6) (#const IPV6_PKTINFO)
73+
#else
74+
pattern CmsgIdIPv6PktInfo = CmsgId (-1) (-1)
75+
#endif
6876

6977
-- | The identifier for 'Fd'.
7078
pattern CmsgIdFd :: CmsgId
@@ -115,7 +123,7 @@ decodeCmsg (Cmsg cmsid (PS fptr off len))
115123
----------------------------------------------------------------
116124

117125
-- | Time to live of IPv4.
118-
#if defined(darwin_HOST_OS)
126+
#if defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
119127
newtype IPv4TTL = IPv4TTL CChar deriving (Eq, Show, Storable)
120128
#else
121129
newtype IPv4TTL = IPv4TTL CInt deriving (Eq, Show, Storable)
@@ -160,6 +168,7 @@ instance ControlMessage IPv4PktInfo where
160168
controlMessageId = CmsgIdIPv4PktInfo
161169

162170
instance Storable IPv4PktInfo where
171+
#if defined (IP_PKTINFO)
163172
sizeOf _ = (#size struct in_pktinfo)
164173
alignment _ = alignment (undefined :: CInt)
165174
poke p (IPv4PktInfo n sa ha) = do
@@ -171,6 +180,12 @@ instance Storable IPv4PktInfo where
171180
sa <- (#peek struct in_pktinfo, ipi_spec_dst) p
172181
ha <- (#peek struct in_pktinfo, ipi_addr) p
173182
return $ IPv4PktInfo n sa ha
183+
#else
184+
sizeOf _ = 0
185+
alignment _ = 1
186+
poke _ _ = error "Unsupported control message type"
187+
peek _ = error "Unsupported control message type"
188+
#endif
174189

175190
----------------------------------------------------------------
176191

@@ -184,6 +199,7 @@ instance ControlMessage IPv6PktInfo where
184199
controlMessageId = CmsgIdIPv6PktInfo
185200

186201
instance Storable IPv6PktInfo where
202+
#if defined (IPV6_PKTINFO)
187203
sizeOf _ = (#size struct in6_pktinfo)
188204
alignment _ = alignment (undefined :: CInt)
189205
poke p (IPv6PktInfo n ha6) = do
@@ -193,6 +209,12 @@ instance Storable IPv6PktInfo where
193209
In6Addr ha6 <- (#peek struct in6_pktinfo, ipi6_addr) p
194210
n :: CInt <- (#peek struct in6_pktinfo, ipi6_ifindex) p
195211
return $ IPv6PktInfo (fromIntegral n) ha6
212+
#else
213+
sizeOf _ = 0
214+
alignment _ = 1
215+
poke _ _ = error "Unsupported control message type"
216+
peek _ = error "Unsupported control message type"
217+
#endif
196218

197219
----------------------------------------------------------------
198220

tests/Network/SocketSpec.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Network.SocketSpec (main, spec) where
66
import Control.Concurrent (threadDelay, forkIO)
77
import Control.Concurrent.MVar (readMVar)
88
import Control.Monad
9+
import Data.Maybe (fromJust)
910
import Network.Socket
1011
import Network.Socket.ByteString
1112
import Network.Test.Common
@@ -93,19 +94,18 @@ spec = do
9394

9495
#if defined(mingw32_HOST_OS)
9596
let lpdevname = "loopback_0"
96-
#elif defined(darwin_HOST_OS)
97+
#elif defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
9798
let lpdevname = "lo0"
9899
#else
99100
let lpdevname = "lo"
100101
#endif
101102

102-
describe "ifNameToIndex" $ do
103-
it "converts a name to an index" $
104-
ifNameToIndex lpdevname `shouldReturn` Just 1
105-
106-
describe "ifIndexToName" $ do
107-
it "converts an index to a name" $
108-
ifIndexToName 1 `shouldReturn` Just lpdevname
103+
describe "ifNameToIndex and ifIndexToName" $ do
104+
it "convert a name to an index and back" $
105+
do
106+
n <- ifNameToIndex lpdevname
107+
n `shouldNotBe` Nothing
108+
ifIndexToName (fromJust n) `shouldReturn` Just lpdevname
109109

110110
describe "socket" $ do
111111
let gc = do

0 commit comments

Comments
 (0)