summaryrefslogtreecommitdiff
path: root/src/Propellor/Types/Dns.hs
diff options
context:
space:
mode:
authorFélix Sipma2015-09-15 21:23:27 +0200
committerJoey Hess2015-09-15 21:17:05 -0400
commitf3f7bb19bb6f30f48ae6d7e272bc59b7fa8efd10 (patch)
treea09592824d8862cff4562e038842f8c1f7db19c0 /src/Propellor/Types/Dns.hs
parent00e824fd0460d5275fc6c6730dd701623f3492c3 (diff)
add PTR record type to Propellor.Types.DNS.Record
- add canonicalIP and reverseIP to Propellor.Types.Dns - remove corresponding canonical and revIP from Propellor.Property.Unbound - Propellor.Property.Dns: convert rValue, rField and genRecord to return Maybe String Signed-off-by: Félix Sipma <felix.sipma@no-log.org>
Diffstat (limited to 'src/Propellor/Types/Dns.hs')
-rw-r--r--src/Propellor/Types/Dns.hs26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/Propellor/Types/Dns.hs b/src/Propellor/Types/Dns.hs
index d78c78fd..1c83854e 100644
--- a/src/Propellor/Types/Dns.hs
+++ b/src/Propellor/Types/Dns.hs
@@ -10,6 +10,8 @@ import Data.Word
import Data.Monoid
import qualified Data.Map as M
import qualified Data.Set as S
+import Data.List
+import Data.String.Utils (split, replace)
type Domain = String
@@ -91,8 +93,32 @@ data Record
| SRV Word16 Word16 Word16 BindDomain
| SSHFP Int Int String
| INCLUDE FilePath
+ | PTR ReverseIP
deriving (Read, Show, Eq, Ord, Typeable)
+type ReverseIP = String
+
+reverseIP :: IPAddr -> ReverseIP
+reverseIP (IPv4 addr) = intercalate "." (reverse $ split "." addr) ++ ".in-addr.arpa"
+reverseIP addr@(IPv6 _) = reverse (intersperse '.' $ replace ":" "" $ fromIPAddr $ canonicalIP addr) ++ ".ip6.arpa"
+
+canonicalIP :: IPAddr -> IPAddr
+canonicalIP (IPv4 addr) = IPv4 addr
+canonicalIP (IPv6 addr) = IPv6 $ intercalate ":" $ map canonicalGroup $ split ":" $ replaceImplicitGroups addr
+ where
+ canonicalGroup g = case length g of
+ 0 -> "0000"
+ 1 -> "000" ++ g
+ 2 -> "00" ++ g
+ 3 -> "0" ++ g
+ _ -> g
+ emptyGroups n = iterate (++ ":") "" !! n
+ numberOfImplicitGroups a = 8 - length (split ":" $ replace "::" "" a)
+ replaceImplicitGroups a = concat $ aux $ split "::" a
+ where
+ aux [] = []
+ aux (x : xs) = x : emptyGroups (numberOfImplicitGroups a) : xs
+
getIPAddr :: Record -> Maybe IPAddr
getIPAddr (Address addr) = Just addr
getIPAddr _ = Nothing