Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cardano-diffusion/lib/Cardano/Network/Diffusion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
module Cardano.Network.Diffusion
( module Cardano.Network.Diffusion.Types
, run
-- * Utils
, Diffusion.readIPAndPort
) where

import Control.DeepSeq (NFData)
Expand Down
37 changes: 4 additions & 33 deletions cardano-diffusion/ping/Cardano/Network/Ping.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ import System.IO qualified as IO
import System.Random (initStdGen)
import Text.Read (readMaybe)

import Cardano.Network.Diffusion (readIPAndPort)
import Cardano.Network.Diffusion.Configuration (defaultChainSyncIdleTimeout)
import Cardano.Network.NodeToClient qualified as NodeToClient
import Cardano.Network.NodeToClient.Version
Expand Down Expand Up @@ -147,7 +148,7 @@ data PingMode =
-- ^ query handshake parameters
deriving (Eq, Show)

type Port = Word
type Port = Socket.PortNumber

-- | There are three stages for resolving addresses.
--
Expand Down Expand Up @@ -335,43 +336,13 @@ argParser =
addrParser :: Parser (Address (Unresolved SRVOrFilePathUnresolved))
addrParser =
argument
( uncurry IP <$> readIPv4AndPort
<|> uncurry IP <$> readIPv6AndPort
( uncurry IP <$> readIPAndPort
<|> readDomainNameOrFilePath
)
( help "List of IP/DNS/SRV address and ports or UNIX socket paths, e.g. 127.0.0.1:3001 [::1]:3001 example.org:3001."
<> metavar "ADDRS"
)
where
-- note: `Read` instances for `IP`, `IPv4`, `IPv6` expect no trailing
-- characters after the address, thus we need to find the split position
-- first.

-- parse IPv4 address and port in a form `127.0.0.1:3001`
readIPv4AndPort :: ReadM (IP, Port)
readIPv4AndPort =
eitherReader $ \s -> do
case splitWith ':' s of
Nothing -> Left s
Just (addrStr, portStr) ->
maybe (Left s) Right $
(,) <$> readMaybe addrStr
<*> readMaybe portStr

-- parse IPv6 address and port in a form `[::1]:3001` or a UNIX file path
readIPv6AndPort :: ReadM (IP, Port)
readIPv6AndPort =
eitherReader $ \s ->
case s of
('[':s') ->
case splitWith ']' s' of
Just (addrStr, ':' : portStr) ->
maybe (Left s) Right $
(,) <$> readMaybe addrStr
<*> readMaybe portStr
_ -> Left s
_ -> Left s

readDomainNameOrFilePath :: ReadM (Address (Unresolved SRVOrFilePathUnresolved))
readDomainNameOrFilePath = eitherReader $ Right . mkAddress

Expand Down Expand Up @@ -418,7 +389,7 @@ instance Exception AddressResolutionError where
-- | Log messages to stderr.
--
data PingWarning = AddressResolutionError AddressResolutionError
| DNSResolution DNS.Domain [IP] Word
| DNSResolution DNS.Domain [IP] Port
| Error SomeException
| ConnectError SockAddr SomeException

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
A new scriv changelog fragment.

Uncomment the section that is right (remove the HTML comment wrapper).
For top level release notes, leave all the headers commented out.
-->

### Breaking

- `Ouroboros.Network.Diffusion.Configuration` now has a single `dcAddresses ::
[Either ntnFd ntnAddr]` field, instead of the two `dcIPv[46]Address`. This
allows us to support multiple interfaces. Use
`Ouroboros.Network.Diffusion.readIPAddressAndPort` to parse `IP:Port` pari
from a command line.

<!--
### Non-Breaking

- A bullet item for the Non-Breaking category.

-->
<!--
### Patch

- A bullet item for the Patch category.

-->
10 changes: 5 additions & 5 deletions ouroboros-network/demo/connection-manager.hs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ withBidirectionalConnectionManager
-> CM.ConnStateIdSupply m
-> DiffTime -- protocol idle timeout
-> DiffTime -- wait time timeout
-> Maybe peerAddr
-> [peerAddr]
-> Random.StdGen
-> ClientAndServerData
-- ^ series of request possible to do with the bidirectional connection
Expand All @@ -214,7 +214,7 @@ withBidirectionalConnectionManager snocket makeBearer socket
connStateIdSupply
protocolIdleTimeout
timeWaitTimeout
localAddress
localAddresses
stdGen
ClientAndServerData {
hotInitiatorRequests,
Expand Down Expand Up @@ -261,8 +261,8 @@ withBidirectionalConnectionManager snocket makeBearer socket
-- ConnectionManagerTrace
tracer = ("cm",) `contramap` debugTracer,
trTracer = ("cm-state",) `contramap` debugTracer,
ipv4Address = localAddress,
ipv6Address = Nothing,
ipv4Address = localAddresses,
ipv6Address = [],
addressType = \_ -> Just IPv4Address,
snocket = snocket,
makeBearer = makeBearer,
Expand Down Expand Up @@ -488,7 +488,7 @@ bidirectionalExperiment
withBidirectionalConnectionManager
snocket makeBearer socket0 connStateIdSupply
protocolIdleTimeout timeWaitTimeout
(Just localAddr) stdGen clientAndServerData $
[localAddr] stdGen clientAndServerData $
\connectionManager _serverAddr _inbGovAsync -> forever' $ do
-- runInitiatorProtocols returns a list of results per each protocol
-- in each bucket (warm \/ hot \/ established); but we run only one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ data Arguments handlerTrace socket peerAddr handle handleError versionNumber ver
-- bidirectional @TCP@ connections, it must be the same as the server
-- listening @IPv4@ address.
--
ipv4Address :: Maybe peerAddr,
ipv4Address :: [peerAddr],

-- | @IPv6@ address of the connection manager. If given, outbound
-- connections to an @IPv6@ address will bound to it. To use
-- bidirectional @TCP@ connections, it must be the same as the server
-- listening @IPv6@ address.
--
ipv6Address :: Maybe peerAddr,
ipv6Address :: [peerAddr],

addressType :: peerAddr -> Maybe AddressType,

Expand Down Expand Up @@ -1470,10 +1470,10 @@ with args@Arguments {
)
$ \socket -> do
traceWith tracer (TrConnectionNotFound provenance peerAddr)
let addr = case addressType peerAddr of
Nothing -> Nothing
Just IPv4Address -> ipv4Address
Just IPv6Address -> ipv6Address
addr <- case addressType peerAddr of
Nothing -> pure Nothing
Just IPv4Address -> randomElement stdGenVar ipv4Address
Just IPv6Address -> randomElement stdGenVar ipv6Address
configureSocket socket addr
-- only bind to the ip address if:
-- the diffusion is given `ipv4/6` addresses;
Expand Down Expand Up @@ -2444,3 +2444,13 @@ data Trace peerAddr handlerTrace
| TrUnexpectedlyFalseAssertion (AssertionLocation peerAddr)
-- ^ This case is unexpected at call site.
deriving Show


randomElement :: MonadSTM m
=> StrictTVar m StdGen -> [a] -> m (Maybe a)
randomElement _ [] = pure Nothing
randomElement _ [a] = pure $ Just a
randomElement stdGenVar as = do
stdGen <- atomically $ stateTVar stdGenVar Random.splitGen
let (indx, _) = Random.uniformR (0, length as - 1) stdGen
return $ Just $ as List.!! indx
Original file line number Diff line number Diff line change
Expand Up @@ -725,10 +725,10 @@ prop_valid_transitions (Fixed rnd) (SkewedBool bindToLocalAddress) scheduleMap =
in counterexample ("\nTransition Trace\n" ++ (intercalate "\n" . map show $ cmTrace))
(verifyTrace cmTrace)
where
myAddress :: Maybe Addr
myAddress :: [Addr]
myAddress = if bindToLocalAddress
then Just (TestAddress 0)
else Nothing
then [TestAddress 0]
else []

verifyTrace :: [TestAbstractTransitionTrace] -> Property
verifyTrace = conjoin
Expand Down Expand Up @@ -771,7 +771,7 @@ prop_valid_transitions (Fixed rnd) (SkewedBool bindToLocalAddress) scheduleMap =
tracer,
trTracer,
ipv4Address = myAddress,
ipv6Address = Nothing,
ipv6Address = [],
addressType = \_ -> Just IPv4Address,
snocket = snocket,
makeBearer = makeFDBearer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ multinodeExperiment inboundTrTracer trTracer inboundTracer debugTracer cmTracer
( withInitiatorOnlyConnectionManager
name simTimeouts nullTracer cmTracer stdGen
snocket makeBearer connStateIdSupply
(Just localAddr) (mkNextRequests connVar)
[localAddr] (mkNextRequests connVar)
timeLimitsHandshake acceptedConnLimit
( \ connectionManager ->
connectionLoop SingInitiatorMode localAddr cc connectionManager Map.empty connVar
Expand Down Expand Up @@ -805,7 +805,7 @@ multinodeExperiment inboundTrTracer trTracer inboundTracer debugTracer cmTracer
inboundTracer muxTracers debugTracer
stdGen
snocket makeBearer connStateIdSupply
(\_ -> pure ()) fd (Just localAddr) serverAcc
(\_ -> pure ()) fd [localAddr] serverAcc
(mkNextRequests connVar)
timeLimitsHandshake
acceptedConnLimit
Expand All @@ -823,7 +823,7 @@ multinodeExperiment inboundTrTracer trTracer inboundTracer debugTracer cmTracer
Unidirectional ->
Job ( withInitiatorOnlyConnectionManager
name simTimeouts trTracer cmTracer stdGen snocket makeBearer
connStateIdSupply (Just localAddr)
connStateIdSupply [localAddr]
(mkNextRequests connVar)
timeLimitsHandshake
acceptedConnLimit
Expand Down Expand Up @@ -2267,7 +2267,7 @@ prop_server_accept_error (Fixed rnd) (AbsIOError ioerr) =
makeFDBearer
connStateIdSupply
(\_ -> pure ())
socket0 (Just addr)
socket0 [addr]
[accumulatorInit pdata]
nextRequests
noTimeLimitsHandshake
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ withInitiatorOnlyConnectionManager
-- ^ series of request possible to do with the bidirectional connection
-- manager towards some peer.
-> CM.ConnStateIdSupply m
-> Maybe peerAddr
-> [peerAddr]
-> TemperatureBundle (ConnectionId peerAddr -> STM m [req])
-- ^ Functions to get the next requests for a given connection
-> ProtocolTimeLimits (Handshake UnversionedProtocol Term)
Expand All @@ -280,7 +280,7 @@ withInitiatorOnlyConnectionManager
-> m a)
-> m a
withInitiatorOnlyConnectionManager name timeouts trTracer tracer stdGen snocket makeBearer connStateIdSupply
localAddr nextRequests handshakeTimeLimits acceptedConnLimit k = do
localAddrs nextRequests handshakeTimeLimits acceptedConnLimit k = do
mainThreadId <- myThreadId
let muxTracers :: Mx.TracersWithBearer (ConnectionId peerAddr) m
muxTracers = Mx.Tracers {
Expand Down Expand Up @@ -317,8 +317,8 @@ withInitiatorOnlyConnectionManager name timeouts trTracer tracer stdGen snocket
trTracer = (WithName name . fmap CM.abstractState)
`contramap` trTracer,
-- This is actually the low level bearer tracer
ipv4Address = localAddr,
ipv6Address = Nothing,
ipv4Address = localAddrs,
ipv6Address = [],
addressType = \_ -> Just IPv4Address,
snocket,
makeBearer,
Expand Down Expand Up @@ -459,7 +459,7 @@ withBidirectionalConnectionManager
-> (socket -> m ()) -- ^ configure socket
-> socket
-- ^ listening socket
-> Maybe peerAddr
-> [peerAddr]
-> acc
-- ^ Initial state for the server
-> TemperatureBundle (ConnectionId peerAddr -> STM m [req])
Expand All @@ -482,7 +482,7 @@ withBidirectionalConnectionManager name timeouts
stdGen
snocket makeBearer connStateIdSupply
confSock socket
localAddress
localAddresses
accumulatorInit nextRequests
handshakeTimeLimits
acceptedConnLimit k = do
Expand Down Expand Up @@ -516,8 +516,8 @@ withBidirectionalConnectionManager name timeouts
trTracer = (WithName name . fmap CM.abstractState)
`contramap` trTracer,
-- low level bearer tracer
ipv4Address = localAddress,
ipv6Address = Nothing,
ipv4Address = localAddresses,
ipv6Address = [],
addressType = \_ -> Just IPv4Address,
snocket,
makeBearer,
Expand Down Expand Up @@ -765,15 +765,15 @@ unidirectionalExperiment stdGen timeouts snocket makeBearer confSock socket clie
nextReqs <- oneshotNextRequests clientAndServerData
connStateIdSupply <- atomically $ CM.newConnStateIdSupply (Proxy @m)
withInitiatorOnlyConnectionManager
"client" timeouts nullTracer nullTracer stdGen' snocket makeBearer connStateIdSupply Nothing nextReqs
"client" timeouts nullTracer nullTracer stdGen' snocket makeBearer connStateIdSupply [] nextReqs
timeLimitsHandshake maxAcceptedConnectionsLimit
$ \connectionManager ->
withBidirectionalConnectionManager "server" timeouts
nullTracer nullTracer nullTracer
nullTracer Mx.nullTracers nullTracer
stdGen''
snocket makeBearer connStateIdSupply
confSock socket Nothing
confSock socket []
[accumulatorInit clientAndServerData]
noNextRequests
timeLimitsHandshake
Expand Down Expand Up @@ -859,7 +859,7 @@ bidirectionalExperiment
nullTracer nullTracer nullTracer nullTracer Mx.nullTracers
nullTracer stdGen' snocket makeBearer
connStateIdSupply confSock
socket0 (Just localAddr0)
socket0 [localAddr0]
[accumulatorInit clientAndServerData0]
nextRequests0
noTimeLimitsHandshake
Expand All @@ -869,7 +869,7 @@ bidirectionalExperiment
nullTracer nullTracer nullTracer nullTracer Mx.nullTracers
nullTracer stdGen'' snocket makeBearer
connStateIdSupply confSock
socket1 (Just localAddr1)
socket1 [localAddr1]
[accumulatorInit clientAndServerData1]
nextRequests1
noTimeLimitsHandshake
Expand Down
Loading
Loading