From 71786719c2480090c1d2df88bc390b088185d7cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 1 Apr 2023 19:32:24 +0200 Subject: Network namespace type --- src/Network/Ip.hs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/Network/Ip.hs') 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 -- cgit v1.2.3