From 4b7f8e625d6cab8ae25074fc3339a5403ec5fb36 Mon Sep 17 00:00:00 2001 From: joe Date: Sat, 14 Oct 2017 16:34:24 -0400 Subject: Partitioned friend-request transport from the onion transport. --- src/Network/Tox/DHT/Transport.hs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'src/Network/Tox/DHT') diff --git a/src/Network/Tox/DHT/Transport.hs b/src/Network/Tox/DHT/Transport.hs index 187e23f2..16af0e3f 100644 --- a/src/Network/Tox/DHT/Transport.hs +++ b/src/Network/Tox/DHT/Transport.hs @@ -16,7 +16,8 @@ module Network.Tox.DHT.Transport , Pong(..) , GetNodes(..) , SendNodes(..) - , DHTPublicKey + , DHTPublicKey(..) + , FriendRequest(..) , CookieRequest , Cookie , DHTRequest @@ -35,10 +36,12 @@ import Network.QueryResponse import Control.Arrow import Control.Monad import Data.Bool -import qualified Data.ByteString as B - ;import Data.ByteString (ByteString) +import qualified Data.ByteString as B + ;import Data.ByteString (ByteString) +import Data.Functor.Contravariant +import Data.Monoid +import Data.Serialize as S import Data.Tuple -import Data.Serialize as S import Data.Word import Network.Socket @@ -203,6 +206,13 @@ data DHTPublicKey = DHTPublicKey , dhtpkNodes :: SendNodes -- other reachable nodes } +-- int8_t (0x20 sent over onion, 0x12 for sent over net_crypto) +-- [uint32_t nospam][Message (UTF8) 1 to ONION_CLIENT_MAX_DATA_SIZE bytes] +data FriendRequest = FriendRequest + { friendNoSpam :: Word32 + , friendRequestText :: ByteString -- UTF8 + } + -- When sent as a DHT request packet (this is the data sent in the DHT request -- packet): -- @@ -231,6 +241,13 @@ instance Sized DHTPublicKey where ConstSize nodes -> nodes VarSize sznodes -> sznodes nodes +instance Sized Word32 where size = ConstSize 4 + +-- FIXME: Inconsitently, this type does not include the 0x20 or 0x12 tag byte +-- where the DHTPublicKey type does include its tag. +instance Sized FriendRequest where + size = contramap friendNoSpam size <> contramap friendRequestText (VarSize B.length) + instance Serialize DHTPublicKey where -- TODO: This should agree with Sized instance. get = DHTPublicKey <$> get <*> getPublicKey <*> get @@ -239,6 +256,10 @@ instance Serialize DHTPublicKey where putPublicKey key put nodes +instance Serialize FriendRequest where + get = FriendRequest <$> get <*> (remaining >>= getBytes) + put (FriendRequest nospam txt) = put nospam >> putByteString txt + newtype GetNodes = GetNodes NodeId deriving (Eq,Ord,Show,Read,S.Serialize) -- cgit v1.2.3