summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2023-04-01 19:32:24 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2023-04-02 13:10:06 +0200
commit71786719c2480090c1d2df88bc390b088185d7cb (patch)
tree8bc1373f20eecb567db93b0514d34fdf905371ec /src/Network
parente01cae88b81fa6f9f35b32ff2e3ca57c34dd7f58 (diff)
Network namespace type
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/Ip.hs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/Network/Ip.hs b/src/Network/Ip.hs
index 76cc8f4..7488829 100644
--- a/src/Network/Ip.hs
+++ b/src/Network/Ip.hs
@@ -9,12 +9,19 @@ module Network.Ip (
allowsSubnets,
ipSubnet,
lanSubnet,
+
+ NetworkNamespace(..),
+ HasNetns(..),
+ textNetnsName,
+ callOn,
) where
import Data.Text (Text)
import Data.Text qualified as T
import Data.Word
+import System.Process
+
newtype IpPrefix = IpPrefix [Word8]
deriving (Eq, Ord)
@@ -43,3 +50,15 @@ ipSubnet num (IpPrefix prefix) = IpPrefix (prefix ++ [num])
lanSubnet :: IpPrefix -> IpPrefix
lanSubnet (IpPrefix prefix) = IpPrefix (take 3 $ prefix ++ repeat 0)
+
+newtype NetworkNamespace = NetworkNamespace Text
+ deriving (Eq, Ord)
+
+class HasNetns a where netnsName :: a -> NetworkNamespace
+
+textNetnsName :: NetworkNamespace -> Text
+textNetnsName (NetworkNamespace name) = name
+
+callOn :: HasNetns a => a -> Text -> IO ()
+callOn n cmd = callCommand $ T.unpack $ "ip netns exec \"" <> ns <> "\" " <> cmd
+ where NetworkNamespace ns = netnsName n