aboutsummaryrefslogtreecommitdiffstats
path: root/src/Network/WireGuard/Internal/Data
diff options
context:
space:
mode:
authorBaylac-Jacqué Félix <felix@alternativebit.fr>2017-08-17 16:11:33 +0200
committerBaylac-Jacqué Félix <felix@alternativebit.fr>2017-09-16 17:10:35 +0200
commite36a4cf345fede7f8b9f6a57ac842bf48fd9c068 (patch)
tree052c4f45baf7edd5e7e574da246b3740bdf85358 /src/Network/WireGuard/Internal/Data
parentImplement and test RPC show Peer feature. (diff)
downloadwireguard-hs-e36a4cf345fede7f8b9f6a57ac842bf48fd9c068.tar.xz
wireguard-hs-e36a4cf345fede7f8b9f6a57ac842bf48fd9c068.zip
Extracted RPC types to proper module.
Diffstat (limited to 'src/Network/WireGuard/Internal/Data')
-rw-r--r--src/Network/WireGuard/Internal/Data/RpcTypes.hs63
-rw-r--r--src/Network/WireGuard/Internal/Data/Types.hs78
2 files changed, 141 insertions, 0 deletions
diff --git a/src/Network/WireGuard/Internal/Data/RpcTypes.hs b/src/Network/WireGuard/Internal/Data/RpcTypes.hs
new file mode 100644
index 0000000..a3c148b
--- /dev/null
+++ b/src/Network/WireGuard/Internal/Data/RpcTypes.hs
@@ -0,0 +1,63 @@
+module Network.WireGuard.Internal.Data.RpcTypes(
+ OpType(..),
+ RpcRequest(..),
+ RpcSetPayload(..),
+ RpcDevicePayload(..),
+ RpcPeerPayload(..)
+) where
+
+import Data.Word (Word64)
+import Data.IP (IPRange(..))
+import Crypto.Noise.DH (dhSecToBytes)
+import Network.Socket.Internal (SockAddr)
+
+import Network.WireGuard.Internal.Data.Types (PublicKey, KeyPair,
+ Time)
+-- | Kind of client operation.
+--
+-- See <https://www.wireguard.com/xplatform/#configuration-protocol> for more informations.
+data OpType = Get | Set
+
+-- | Request wrapper. The payload is set only for Set operations.
+--
+-- See <https://www.wireguard.com/xplatform/#configuration-protocol> for more informations.
+data RpcRequest = RpcRequest {
+ opType :: OpType,
+ payload :: Maybe RpcSetPayload
+}
+
+-- | Payload sent together with a set RPC operation.
+data RpcSetPayload = RpcSetPayload {
+ devicePayload :: RpcDevicePayload,
+ peersPayload :: [RpcPeerPayload]
+}
+
+-- | Device related payload sent together with a set RPC operation.
+data RpcDevicePayload = RpcDevicePayload {
+ pk :: Maybe KeyPair,
+ listenPort :: Int,
+ fwMark :: Maybe Word,
+ replacePeers :: Bool
+}
+
+instance Show RpcDevicePayload where
+ show (RpcDevicePayload kp lp fwM rpp) = show (showKeyPair <$> kp) ++ show lp ++ show fwM ++ show rpp
+ where
+ showKeyPair (pk, _) = show $ dhSecToBytes pk
+
+instance Eq RpcDevicePayload where
+ (==) (RpcDevicePayload pk1 prt1 fw1 rp1) (RpcDevicePayload pk2 prt2 fw2 rp2) =
+ ((dhSecToBytes . fst) <$> pk1) == ((dhSecToBytes . fst) <$> pk2) && (prt1 == prt2) &&
+ (rp1 == rp2) && (fw1 == fw2)
+
+-- | Peer related payload sent together with a set RPC operation.
+data RpcPeerPayload = RpcPeerPayload {
+ pubK :: PublicKey,
+ remove :: Bool,
+ endpoint :: SockAddr,
+ persistantKeepaliveInterval :: Int,
+ allowedIp :: [IPRange],
+ rxBytes :: Word64,
+ txBytes :: Word64,
+ lastHandshake :: Time
+}
diff --git a/src/Network/WireGuard/Internal/Data/Types.hs b/src/Network/WireGuard/Internal/Data/Types.hs
new file mode 100644
index 0000000..53c3cea
--- /dev/null
+++ b/src/Network/WireGuard/Internal/Data/Types.hs
@@ -0,0 +1,78 @@
+module Network.WireGuard.Internal.Data.Types
+ ( Index
+ , Counter
+ , PeerId
+ , PublicKey
+ , PrivateKey
+ , KeyPair
+ , PresharedKey
+ , Time
+ , UdpPacket
+ , TunPacket
+ , EncryptedPayload
+ , AuthTag
+ , TAI64n
+ , SessionKey(..)
+ , WireGuardError(..)
+ , getPeerId
+ , farFuture
+ ) where
+
+import Control.Exception (Exception, SomeException)
+import qualified Crypto.Noise.DH as DH
+import Crypto.Noise.DH.Curve25519 (Curve25519)
+import Data.ByteArray (ScrubbedBytes)
+import qualified Data.ByteArray as BA
+import qualified Data.ByteString as BS
+import Foreign.C.Types (CTime (..))
+import Network.Socket (SockAddr)
+import System.Posix.Types (EpochTime)
+
+import Data.Word
+
+type Index = Word32
+type Counter = Word64
+type PeerId = BS.ByteString
+
+type PublicKey = DH.PublicKey Curve25519
+type PrivateKey = DH.SecretKey Curve25519
+type KeyPair = DH.KeyPair Curve25519
+type PresharedKey = ScrubbedBytes
+
+type Time = EpochTime
+
+type UdpPacket = (BS.ByteString, SockAddr)
+type TunPacket = ScrubbedBytes
+
+type EncryptedPayload = BS.ByteString
+type AuthTag = BS.ByteString
+type TAI64n = BS.ByteString
+
+data SessionKey = SessionKey
+ { sendKey :: !ScrubbedBytes
+ , recvKey :: !ScrubbedBytes
+ }
+
+data WireGuardError
+ = DecryptFailureError
+ | DestinationNotReachableError
+ | DeviceNotReadyError
+ | EndPointUnknownError
+ | HandshakeInitiationReplayError
+ | InvalidIPPacketError
+ | InvalidWGPacketError String
+ | NoiseError SomeException
+ | NonceReuseError
+ | OutdatedPacketError
+ | RemotePeerNotFoundError
+ | SourceAddrBlockedError
+ | UnknownIndexError
+ deriving (Show)
+
+instance Exception WireGuardError
+
+getPeerId :: PublicKey -> PeerId
+getPeerId = BA.convert . DH.dhPubToBytes
+
+farFuture :: Time
+farFuture = CTime maxBound