summaryrefslogtreecommitdiff
path: root/src/Network/DatagramServer
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-09 00:50:54 -0400
committerjoe <joe@jerkface.net>2017-07-09 00:50:54 -0400
commit035e1ffb0a3b7dbed7af5b8e0c180b44f687a672 (patch)
tree582a3c9c8c564d0781486676dbd359de894dc6fa /src/Network/DatagramServer
parent8fe93b8e1d1d968bdf0b8a35335b060d92a9d7d7 (diff)
Initialize Tox secret key.
Diffstat (limited to 'src/Network/DatagramServer')
-rw-r--r--src/Network/DatagramServer/Mainline.hs5
-rw-r--r--src/Network/DatagramServer/Tox.hs7
-rw-r--r--src/Network/DatagramServer/Types.hs26
3 files changed, 37 insertions, 1 deletions
diff --git a/src/Network/DatagramServer/Mainline.hs b/src/Network/DatagramServer/Mainline.hs
index 1f07b13f..fea64ee6 100644
--- a/src/Network/DatagramServer/Mainline.hs
+++ b/src/Network/DatagramServer/Mainline.hs
@@ -51,6 +51,7 @@ module Network.DatagramServer.Mainline
51 , KQueryArgs 51 , KQueryArgs
52 , QueryExtra(..) 52 , QueryExtra(..)
53 , ResponseExtra(..) 53 , ResponseExtra(..)
54 , PacketDestination(..)
54 55
55 , NodeId(..) 56 , NodeId(..)
56 , nodeIdSize 57 , nodeIdSize
@@ -347,6 +348,10 @@ instance WireFormat BValue KMessageOf where
347 encodeHeaders _ kmsg _ = L.toStrict $ BE.encode kmsg 348 encodeHeaders _ kmsg _ = L.toStrict $ BE.encode kmsg
348 encodePayload msg = fmap BE.toBEncode msg 349 encodePayload msg = fmap BE.toBEncode msg
349 350
351 initializeServerState _ mbid = do
352 i <- maybe genNodeId return mbid
353 return (i, ())
354
350-- | KRPC 'compact list' compatible encoding: contact information for 355-- | KRPC 'compact list' compatible encoding: contact information for
351-- nodes is encoded as a 26-byte string. Also known as "Compact node 356-- nodes is encoded as a 26-byte string. Also known as "Compact node
352-- info" the 20-byte Node ID in network byte order has the compact 357-- info" the 20-byte Node ID in network byte order has the compact
diff --git a/src/Network/DatagramServer/Tox.hs b/src/Network/DatagramServer/Tox.hs
index 8d2f9289..1763e74c 100644
--- a/src/Network/DatagramServer/Tox.hs
+++ b/src/Network/DatagramServer/Tox.hs
@@ -45,6 +45,7 @@ import Crypto.Error.Types
45import Data.Hashable 45import Data.Hashable
46import Text.PrettyPrint as PP hiding ((<>)) 46import Text.PrettyPrint as PP hiding ((<>))
47import Text.PrettyPrint.HughesPJClass hiding (($$), (<>)) 47import Text.PrettyPrint.HughesPJClass hiding (($$), (<>))
48import Data.ByteArray (convert)
48 49
49 50
50type Key32 = Word256 -- 32 byte key 51type Key32 = Word256 -- 32 byte key
@@ -391,4 +392,10 @@ instance WireFormat ByteString Message where
391 decodeHeaders ctx bs = runGet getMessage bs >>= decipher ctx 392 decodeHeaders ctx bs = runGet getMessage bs >>= decipher ctx
392 encodeHeaders ctx msg recipient = runPut $ putMessage $ encipher ctx (toxID recipient) msg 393 encodeHeaders ctx msg recipient = runPut $ putMessage $ encipher ctx (toxID recipient) msg
393 394
395 initializeServerState _ _ = do
396 k <- generateSecretKey
397 let Right nid = S.decode $ convert $ toPublic k
398 return (nid, ToxCipherContext k)
399
400
394instance Read (NodeId Message) where readsPrec d s = map (\(w,xs) -> (NodeId w, xs)) $ decodeHex s 401instance Read (NodeId Message) where readsPrec d s = map (\(w,xs) -> (NodeId w, xs)) $ decodeHex s
diff --git a/src/Network/DatagramServer/Types.hs b/src/Network/DatagramServer/Types.hs
index 14968764..6aa7aeaa 100644
--- a/src/Network/DatagramServer/Types.hs
+++ b/src/Network/DatagramServer/Types.hs
@@ -35,7 +35,7 @@ import Data.IP
35import Network.Socket 35import Network.Socket
36import Text.PrettyPrint as PP hiding ((<>)) 36import Text.PrettyPrint as PP hiding ((<>))
37import Text.PrettyPrint.HughesPJClass hiding (($$), (<>)) 37import Text.PrettyPrint.HughesPJClass hiding (($$), (<>))
38import Text.Read (readMaybe) 38import Text.Read (readMaybe, readEither)
39import Data.Serialize as S 39import Data.Serialize as S
40import qualified Data.ByteString.Char8 as Char8 40import qualified Data.ByteString.Char8 as Char8
41import qualified Data.ByteString as BS 41import qualified Data.ByteString as BS
@@ -43,6 +43,7 @@ import Data.ByteString.Base16 as Base16
43import System.Entropy 43import System.Entropy
44import Network.DatagramServer.Error 44import Network.DatagramServer.Error
45import Data.LargeWord 45import Data.LargeWord
46import Data.Char
46 47
47 48
48class (Eq a, Serialize a, Typeable a, Hashable a, Pretty a) 49class (Eq a, Serialize a, Typeable a, Hashable a, Pretty a)
@@ -274,6 +275,26 @@ deriving instance ( Show (NodeId dht)
274 , Show addr 275 , Show addr
275 , Show u ) => Show (NodeInfo dht addr u) 276 , Show u ) => Show (NodeInfo dht addr u)
276 277
278hexdigit c = ('0' <= c && c <= '9') || ( 'a' <= c && c <= 'f') || ( 'A' <= c && c <= 'F')
279
280instance ( FiniteBits (NodeId dht)
281 , Read (NodeId dht)
282 , Read (NodeAddr addr)
283 , Default u
284 ) => Read (NodeInfo dht addr u) where
285 readsPrec i = RP.readP_to_S $ do
286 RP.skipSpaces
287 let n = finiteBitSize (undefined :: NodeId dht) `div` 4
288 hexhash <- sequence $ replicate n (RP.satisfy hexdigit)
289 RP.char '@' RP.+++ RP.satisfy isSpace
290 addrstr <- RP.between (RP.char '(') (RP.char ')') (RP.munch (/=')'))
291 RP.+++ RP.munch (not . isSpace)
292 addr <- either fail return $ readEither addrstr
293 nid <- either fail return $ readEither hexhash
294 return $ NodeInfo nid addr def
295
296
297
277mapAddress :: (addr -> b) -> NodeInfo dht addr u -> NodeInfo dht b u 298mapAddress :: (addr -> b) -> NodeInfo dht addr u -> NodeInfo dht b u
278mapAddress f ni = ni { nodeAddr = fmap f (nodeAddr ni) } 299mapAddress f ni = ni { nodeAddr = fmap f (nodeAddr ni) }
279 300
@@ -348,6 +369,9 @@ class (Envelope envelope, Address (PacketDestination envelope)) => WireFormat ra
348 encodeHeaders :: CipherContext raw envelope -> envelope raw -> PacketDestination envelope -> ByteString 369 encodeHeaders :: CipherContext raw envelope -> envelope raw -> PacketDestination envelope -> ByteString
349 encodePayload :: SerializableTo raw a => envelope a -> envelope raw 370 encodePayload :: SerializableTo raw a => envelope a -> envelope raw
350 371
372 initializeServerState :: Proxy (envelope raw) -> Maybe (NodeId envelope) -> IO (NodeId envelope, CipherContext raw envelope)
373
374
351encodeHexDoc :: Serialize x => x -> Doc 375encodeHexDoc :: Serialize x => x -> Doc
352encodeHexDoc nid = PP.text $ Char8.unpack $ Base16.encode $ S.encode nid 376encodeHexDoc nid = PP.text $ Char8.unpack $ Base16.encode $ S.encode nid
353 377