summaryrefslogtreecommitdiff
path: root/DHTTransport.hs
diff options
context:
space:
mode:
Diffstat (limited to 'DHTTransport.hs')
-rw-r--r--DHTTransport.hs47
1 files changed, 40 insertions, 7 deletions
diff --git a/DHTTransport.hs b/DHTTransport.hs
index 778390cf..3d008174 100644
--- a/DHTTransport.hs
+++ b/DHTTransport.hs
@@ -14,6 +14,7 @@ module DHTTransport
14 , Pong(..) 14 , Pong(..)
15 , GetNodes(..) 15 , GetNodes(..)
16 , SendNodes(..) 16 , SendNodes(..)
17 , DHTPublicKey
17 , CookieRequest 18 , CookieRequest
18 , Cookie 19 , Cookie
19 , DHTRequest 20 , DHTRequest
@@ -149,15 +150,16 @@ data DHTRequest
149-- `1` `uint8_t` (0x01) 150-- `1` `uint8_t` (0x01)
150-- `8` `uint64_t` random number (the same that was received in request) 151-- `8` `uint64_t` random number (the same that was received in request)
151 | NATPong Nonce8 152 | NATPong Nonce8
152 | DHTPK DHTPublicKey 153 | DHTPK LongTermKeyWrap
153 154
154instance Sized DHTRequest where 155instance Sized DHTRequest where
155 size = VarSize $ \case 156 size = VarSize $ \case
156 NATPing _ -> 10 157 NATPing _ -> 10
157 NATPong _ -> 10 158 NATPong _ -> 10
158 DHTPK dhtpk -> 41 + case size of 159 DHTPK wrap -> 1{-typ-} + 32{-key-} + 24{-nonce-}
159 ConstSize nodes -> nodes 160 + case size of
160 VarSize sznodes -> sznodes (dhtpkNodes dhtpk) 161 ConstSize n -> n
162 VarSize f -> f (wrapData wrap)
161 163
162instance Serialize DHTRequest where 164instance Serialize DHTRequest where
163 get = do 165 get = do
@@ -172,6 +174,9 @@ instance Serialize DHTRequest where
172 put (NATPong n) = put (0xfe01 :: Word16) >> put n 174 put (NATPong n) = put (0xfe01 :: Word16) >> put n
173 put (DHTPK pk) = put (0x9c :: Word8) >> put pk 175 put (DHTPK pk) = put (0x9c :: Word8) >> put pk
174 176
177-- DHT public key packet:
178-- (As Onion data packet?)
179--
175-- | Length | Contents | 180-- | Length | Contents |
176-- |:------------|:------------------------------------| 181-- |:------------|:------------------------------------|
177-- | `1` | `uint8_t` (0x9c) | 182-- | `1` | `uint8_t` (0x9c) |
@@ -179,12 +184,40 @@ instance Serialize DHTRequest where
179-- | `32` | Our DHT public key | 184-- | `32` | Our DHT public key |
180-- | `[39, 204]` | Maximum of 4 nodes in packed format | 185-- | `[39, 204]` | Maximum of 4 nodes in packed format |
181data DHTPublicKey = DHTPublicKey 186data DHTPublicKey = DHTPublicKey
182 { dhtpkNonce :: Nonce8 187 { dhtpkNonce :: Nonce8 -- no_replay
183 , dhtpk :: PublicKey 188 , dhtpk :: PublicKey -- dht public key
184 , dhtpkNodes :: SendNodes 189 , dhtpkNodes :: SendNodes -- other reachable nodes
190 }
191
192-- When sent as a DHT request packet (this is the data sent in the DHT request
193-- packet):
194--
195-- Length Contents
196-- :--------- :-------------------------------
197-- `1` `uint8_t` (0x9c)
198-- `32` Long term public key of sender
199-- `24` Nonce
200-- variable Encrypted payload
201data LongTermKeyWrap = LongTermKeyWrap
202 { wrapLongTermKey :: PublicKey
203 , wrapNonce :: Nonce24
204 , wrapData :: Encrypted DHTPublicKey
185 } 205 }
186 206
207instance Serialize LongTermKeyWrap where
208 get = LongTermKeyWrap <$> getPublicKey <*> get <*> get
209 put (LongTermKeyWrap key nonce dta) = putPublicKey key >> put nonce >> put dta
210
211
212instance Sized DHTPublicKey where
213 -- NOTE: 41 bytes includes the 1-byte tag 0x9c in the size.
214 -- WARNING: Serialize instance does not include this byte FIXME
215 size = VarSize $ \(DHTPublicKey _ _ nodes) -> 41 + case size of
216 ConstSize nodes -> nodes
217 VarSize sznodes -> sznodes nodes
218
187instance Serialize DHTPublicKey where 219instance Serialize DHTPublicKey where
220 -- TODO: This should agree with Sized instance.
188 get = DHTPublicKey <$> get <*> getPublicKey <*> get 221 get = DHTPublicKey <$> get <*> getPublicKey <*> get
189 put (DHTPublicKey nonce key nodes) = do 222 put (DHTPublicKey nonce key nodes) = do
190 put nonce 223 put nonce