Skip to content

Commit aa94d3c

Browse files
committed
remote: separate (Client|Server)Handshake(Input|Output) types
1 parent bdce1a3 commit aa94d3c

File tree

3 files changed

+79
-50
lines changed

3 files changed

+79
-50
lines changed

hnix-store-remote/src/System/Nix/Store/Remote/Client.hs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import System.Nix.Store.Remote.Logger (processOutput)
3030
import System.Nix.Store.Remote.MonadStore
3131
import System.Nix.Store.Remote.Socket (sockPutS, sockGetS)
3232
import System.Nix.Store.Remote.Serializer (bool, enum, int, mapErrorS, protoVersion, storeRequest, text, trustedFlag, workerMagic)
33-
import System.Nix.Store.Remote.Types.Handshake (Handshake(..))
33+
import System.Nix.Store.Remote.Types.Handshake (ClientHandshakeInput(..), ClientHandshakeOutput(..))
3434
import System.Nix.Store.Remote.Types.Logger (Logger)
3535
import System.Nix.Store.Remote.Types.ProtoVersion (ProtoVersion(..), ourProtoVersion)
3636
import System.Nix.Store.Remote.Types.StoreConfig (PreStoreConfig, StoreConfig, preStoreConfigToStoreConfig)
@@ -177,16 +177,23 @@ runStoreSocket
177177
-> Run m a
178178
runStoreSocket preStoreConfig code =
179179
runRemoteStoreT preStoreConfig $ do
180-
Handshake{..} <- greet
180+
ClientHandshakeOutput{..}
181+
<- greet
182+
ClientHandshakeInput
183+
{ clientHandshakeInputOurVersion = ourProtoVersion
184+
}
185+
181186
mapStoreConfig
182-
(preStoreConfigToStoreConfig handshakeProtoVersion)
187+
(preStoreConfigToStoreConfig
188+
clientHandshakeOutputLeastCommonVerison)
183189
code
184190

185191
where
186192
greet
187193
:: MonadIO m
188-
=> RemoteStoreT PreStoreConfig m Handshake
189-
greet = do
194+
=> ClientHandshakeInput
195+
-> RemoteStoreT PreStoreConfig m ClientHandshakeOutput
196+
greet ClientHandshakeInput{..} = do
190197

191198
sockPutS
192199
(mapErrorS
@@ -210,19 +217,19 @@ runStoreSocket preStoreConfig code =
210217
when (daemonVersion < ProtoVersion 1 10)
211218
$ throwError RemoteStoreError_ClientVersionTooOld
212219

213-
sockPutS protoVersion ourProtoVersion
220+
sockPutS protoVersion clientHandshakeInputOurVersion
214221

215-
let minimumCommonVersion = min daemonVersion ourProtoVersion
222+
let leastCommonVersion = min daemonVersion ourProtoVersion
216223

217-
when (minimumCommonVersion >= ProtoVersion 1 14)
224+
when (leastCommonVersion >= ProtoVersion 1 14)
218225
$ sockPutS int (0 :: Int) -- affinity, obsolete
219226

220-
when (minimumCommonVersion >= ProtoVersion 1 11) $ do
227+
when (leastCommonVersion >= ProtoVersion 1 11) $ do
221228
sockPutS
222229
(mapErrorS RemoteStoreError_SerializerPut bool)
223230
False -- reserveSpace, obsolete
224231

225-
daemonNixVersion <- if minimumCommonVersion >= ProtoVersion 1 33
232+
daemonNixVersion <- if leastCommonVersion >= ProtoVersion 1 33
226233
then do
227234
-- If we were buffering I/O, we would flush the output here.
228235
txtVer <-
@@ -233,19 +240,19 @@ runStoreSocket preStoreConfig code =
233240
pure $ Just txtVer
234241
else pure Nothing
235242

236-
remoteTrustsUs <- if minimumCommonVersion >= ProtoVersion 1 35
243+
remoteTrustsUs <- if leastCommonVersion >= ProtoVersion 1 35
237244
then do
238245
sockGetS
239246
$ mapErrorS RemoteStoreError_SerializerHandshake trustedFlag
240247
else pure Nothing
241248

242249
mapStoreConfig
243-
(preStoreConfigToStoreConfig minimumCommonVersion)
250+
(preStoreConfigToStoreConfig leastCommonVersion)
244251
processOutput
245252

246-
pure Handshake
247-
{ handshakeNixVersion = daemonNixVersion
248-
, handshakeTrust = remoteTrustsUs
249-
, handshakeProtoVersion = minimumCommonVersion
250-
, handshakeRemoteProtoVersion = daemonVersion
253+
pure ClientHandshakeOutput
254+
{ clientHandshakeOutputNixVersion = daemonNixVersion
255+
, clientHandshakeOutputTrust = remoteTrustsUs
256+
, clientHandshakeOutputLeastCommonVerison = leastCommonVersion
257+
, clientHandshakeOutputServerVersion = daemonVersion
251258
}

hnix-store-remote/src/System/Nix/Store/Remote/Server.hs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import System.Nix.Store.Remote.Types.ProtoVersion (HasProtoVersion(..), ProtoVer
2727
import System.Nix.Store.Remote.Types.Logger (BasicError(..), ErrorInfo, Logger(..))
2828

2929
import System.Nix.Store.Remote.MonadStore (WorkerError(..), WorkerException(..), RemoteStoreError(..), RemoteStoreT, runRemoteStoreT, mapStoreConfig)
30-
import System.Nix.Store.Remote.Types.Handshake (Handshake(..))
30+
import System.Nix.Store.Remote.Types.Handshake (ServerHandshakeInput(..), ServerHandshakeOutput(..))
3131
import System.Nix.Store.Remote.Types.ProtoVersion (ourProtoVersion)
3232
import System.Nix.Store.Remote.Types.WorkerMagic (WorkerMagic(..))
3333

@@ -79,28 +79,26 @@ processConnection
7979
-> PreStoreConfig
8080
-> m ()
8181
processConnection workerHelper preStoreConfig = do
82-
let handshake = Handshake
83-
{ handshakeNixVersion = Just "nixVersion (hnix-store-remote)"
84-
, handshakeTrust = Nothing
85-
-- TODO: doesn't make sense for server
86-
, handshakeProtoVersion = ourProtoVersion
87-
-- TODO: doesn't make sense for server
88-
, handshakeRemoteProtoVersion = ourProtoVersion
89-
}
90-
9182
~() <- void $ runRemoteStoreT preStoreConfig $ do
9283

93-
minimumCommonVersion <- greet handshake
84+
ServerHandshakeOutput{..}
85+
<- greet
86+
ServerHandshakeInput
87+
{ serverHandshakeInputNixVersion = "nixVersion (hnix-store-remote)"
88+
, serverHandshakeInputOurVersion= ourProtoVersion
89+
, serverHandshakeInputTrust = Nothing
90+
}
9491

9592
mapStoreConfig
96-
(preStoreConfigToStoreConfig minimumCommonVersion)
93+
(preStoreConfigToStoreConfig
94+
serverHandshakeOutputLeastCommonVersion)
9795
$ do
9896

9997
tunnelLogger <- liftIO $ newTunnelLogger
10098
-- Send startup error messages to the client.
10199
startWork tunnelLogger
102100

103-
-- TODO: do we need auth at all? probably?
101+
-- TODO: do we need auth at all? probably?
104102
-- If we can't accept clientVersion, then throw an error *here* (not above).
105103
--authHook(*store);
106104
stopWork tunnelLogger
@@ -124,9 +122,9 @@ processConnection workerHelper preStoreConfig = do
124122
-- Exchange the greeting.
125123
greet
126124
:: MonadIO m
127-
=> Handshake
128-
-> RemoteStoreT PreStoreConfig m ProtoVersion
129-
greet Handshake{..} = do
125+
=> ServerHandshakeInput
126+
-> RemoteStoreT PreStoreConfig m ServerHandshakeOutput
127+
greet ServerHandshakeInput{..} = do
130128
magic <-
131129
sockGetS
132130
$ mapErrorS
@@ -135,7 +133,9 @@ processConnection workerHelper preStoreConfig = do
135133

136134
liftIO $ print ("magic" :: Text, magic)
137135
when (magic /= WorkerMagic_One)
138-
$ throwError $ RemoteStoreError_WorkerException WorkerException_ProtocolMismatch
136+
$ throwError
137+
$ RemoteStoreError_WorkerException
138+
WorkerException_ProtocolMismatch
139139

140140
sockPutS
141141
(mapErrorS
@@ -144,13 +144,13 @@ processConnection workerHelper preStoreConfig = do
144144
)
145145
WorkerMagic_Two
146146

147-
sockPutS protoVersion ourProtoVersion
147+
sockPutS protoVersion serverHandshakeInputOurVersion
148148

149149
clientVersion <- sockGetS protoVersion
150150

151-
let minimumCommonVersion = min clientVersion ourProtoVersion
151+
let leastCommonVersion = min clientVersion ourProtoVersion
152152

153-
liftIO $ print ("Versions client, min" :: Text, clientVersion, minimumCommonVersion)
153+
liftIO $ print ("Versions client, min" :: Text, clientVersion, leastCommonVersion)
154154

155155
when (clientVersion < ProtoVersion 1 10)
156156
$ throwError
@@ -174,18 +174,20 @@ processConnection workerHelper preStoreConfig = do
174174
RemoteStoreError_SerializerPut
175175
text
176176
)
177-
-- TODO
178-
(maybe undefined id handshakeNixVersion)
177+
serverHandshakeInputNixVersion
179178

180179
when (clientVersion >= ProtoVersion 1 35) $ do
181180
sockPutS
182181
(mapErrorS
183182
RemoteStoreError_SerializerHandshake
184183
trustedFlag
185184
)
186-
handshakeTrust
185+
serverHandshakeInputTrust
187186

188-
pure minimumCommonVersion
187+
pure ServerHandshakeOutput
188+
{ serverHandshakeOutputLeastCommonVersion = leastCommonVersion
189+
, serverHandshakeOutputClientVersion = clientVersion
190+
}
189191

190192
simpleOp
191193
:: ( MonadIO m
Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
11
module System.Nix.Store.Remote.Types.Handshake
2-
( Handshake(..)
2+
( ClientHandshakeInput(..)
3+
, ClientHandshakeOutput(..)
4+
, ServerHandshakeInput(..)
5+
, ServerHandshakeOutput(..)
36
) where
47

58
import Data.Text (Text)
69
import GHC.Generics (Generic)
710
import System.Nix.Store.Remote.Types.ProtoVersion (ProtoVersion)
811
import System.Nix.Store.Remote.Types.TrustedFlag (TrustedFlag)
912

10-
-- | Data for initial protocol handshake
11-
data Handshake = Handshake
12-
{ handshakeNixVersion :: Maybe Text -- ^ Textual version, since 1.33
13-
, handshakeTrust :: Maybe TrustedFlag -- ^ Whether remote side trusts us
14-
, handshakeProtoVersion :: ProtoVersion -- ^ Minimum protocol supported by both sides
15-
, handshakeRemoteProtoVersion :: ProtoVersion -- ^ Protocol supported by remote side
16-
}
17-
deriving (Eq, Generic, Ord, Show)
13+
-- | Data sent by the client during initial protocol handshake
14+
data ClientHandshakeInput = ClientHandshakeInput
15+
{ clientHandshakeInputOurVersion :: ProtoVersion -- ^ Our protocol version (that we advertise to the server)
16+
} deriving (Eq, Generic, Ord, Show)
17+
18+
-- | Data received by the client via initial protocol handshake
19+
data ClientHandshakeOutput = ClientHandshakeOutput
20+
{ clientHandshakeOutputNixVersion :: Maybe Text -- ^ Textual version, since 1.33
21+
, clientHandshakeOutputTrust :: Maybe TrustedFlag -- ^ Whether remote side trusts us
22+
, clientHandshakeOutputLeastCommonVerison :: ProtoVersion -- ^ Minimum protocol version supported by both sides
23+
, clientHandshakeOutputServerVersion :: ProtoVersion -- ^ Protocol version supported by the server
24+
} deriving (Eq, Generic, Ord, Show)
25+
26+
-- | Data sent by the server during initial protocol handshake
27+
data ServerHandshakeInput = ServerHandshakeInput
28+
{ serverHandshakeInputNixVersion :: Text -- ^ Textual version, since 1.33
29+
, serverHandshakeInputOurVersion :: ProtoVersion -- ^ Our protocol version (that we advertise to the client)
30+
, serverHandshakeInputTrust :: Maybe TrustedFlag -- ^ Whether client should trusts us
31+
} deriving (Eq, Generic, Ord, Show)
32+
33+
-- | Data received by the server during initial protocol handshake
34+
data ServerHandshakeOutput = ServerHandshakeOutput
35+
{ serverHandshakeOutputLeastCommonVersion :: ProtoVersion -- ^ Minimum protocol version supported by both sides
36+
, serverHandshakeOutputClientVersion :: ProtoVersion -- ^ Protocol version supported by the client
37+
} deriving (Eq, Generic, Ord, Show)

0 commit comments

Comments
 (0)