Skip to content

Commit

Permalink
making more api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Weber committed Apr 21, 2012
1 parent ca571c5 commit 42f047d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
46 changes: 34 additions & 12 deletions docs/uberdoc.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3071,7 +3071,11 @@
and {:ignore-trunction true :print-query true} will return ["-i","-p"] and {:ignore-trunction true :print-query true} will return ["-i","-p"]
</code></pre> </code></pre>
</td><td class="codes"><pre class="brush: clojure">(defn- convert-dig-options [options-map] </td><td class="codes"><pre class="brush: clojure">(defn- convert-dig-options [options-map]
(filter seq [(when (:tcp options-map) &quot;-t&quot;) (when (:ignore-trunction options-map) &quot;-i&quot;) (when (:print-query options-map) &quot;-p&quot;)]))</pre></td></tr><tr><td class="docs"><p>given a map and a sequence of keys, it verifies that either all the keys from the sequence are present in the map or none of them are.</p> (filter seq [(when (:tcp options-map) &quot;-t&quot;) (when (:ignore-trunction options-map) &quot;-i&quot;) (when (:print-query options-map) &quot;-p&quot;)]))</pre></td></tr><tr><td class="docs"><p>Utility method that prefixes a dig server with '@' or returns nil if the server was not specified.</p>
</td><td class="codes"><pre class="brush: clojure">(defn- dig-server [ds]
(when ds (str &quot;@&quot; ds)))</pre></td></tr><tr><td class="docs"><p>Returns a seq of the-name prefixed with "-x" if this is a reverse query, otherwise it just returns the-name</p>
</td><td class="codes"><pre class="brush: clojure">(defn- dig-name [the-name is-reverse]
(if is-reverse [&quot;-x&quot; the-name] [the-name]))</pre></td></tr><tr><td class="docs"><p>given a map and a sequence of keys, it verifies that either all the keys from the sequence are present in the map or none of them are.</p>
</td><td class="codes"><pre class="brush: clojure">(defn- all-or-none [m s] </td><td class="codes"><pre class="brush: clojure">(defn- all-or-none [m s]
(or (every? #(contains? m %) s) (or (every? #(contains? m %) s)
(not-any? #(contains? m %) s)))</pre></td></tr><tr><td class="docs"><p>Returns (int 4) for IPv4, (int 6) for IPv6 and error otherwise.</p> (not-any? #(contains? m %) s)))</pre></td></tr><tr><td class="docs"><p>Returns (int 4) for IPv4, (int 6) for IPv6 and error otherwise.</p>
Expand Down Expand Up @@ -3194,22 +3198,27 @@
<p>or with multiple values:</p> <p>or with multiple values:</p>


<pre><code> <pre><code>
(dns-lookup "www.google.com" "www.bing.com") (dns-lookup-to-stdout "www.google.com" "www.bing.com")
</code></pre> </code></pre>


<p>or if you have a seq of things to look up:</p> <p>or if you have a seq of things to look up:</p>


<pre><code> <pre><code>
(apply dns-lookup ["www.google.com" "www.bing.com"]) (apply dns-lookup-to-stdout ["www.google.com" "www.bing.com"])
</code></pre> </code></pre>
</td><td class="codes"><pre class="brush: clojure">(defn dns-lookup-to-stdout [&amp; to-lookups] (lookup/main (into-array String to-lookups)))</pre></td></tr><tr><td class="docs"><p>Lookup hostname(s) by resource record type. This prints the result to stdout, it does not return a seq of the data.</p> </td><td class="codes"><pre class="brush: clojure">(defn dns-lookup-to-stdout [&amp; to-lookups] (lookup/main (into-array String to-lookups)))</pre></td></tr><tr><td class="docs"><p>Lookup hostname(s) by resource record type. This prints the result to stdout, it does not return a seq of the data.</p>


<p>example:</p> <p>example:</p>


<pre><code> <pre><code>
(dns-loookup-by-type Type/PTR "www.google.com" "www.bing.com") (dns-loookup-by-type-to-stdout Type/PTR "www.google.com" "www.bing.com")
</code></pre>
</td><td class="codes"><pre class="brush: clojure">(defn dns-lookup-by-type-to-stdout [rr-type &amp; to-lookups] (lookup/main (into-array String (into [&quot;-t&quot; (Type/string rr-type)] to-lookups))))</pre></td></tr><tr><td class="docs"><p>Returns a map with two keys (aliases and answers). Each maps to a sequence.
Example</p>

<pre><code>
(dns-lookup "www.google.com" Type/A)
</code></pre> </code></pre>
</td><td class="codes"><pre class="brush: clojure">(defn dns-lookup-by-type-to-stdout [rr-type &amp; to-lookups] (lookup/main (into-array String (into [&quot;-t&quot; (Type/string rr-type)] to-lookups))))</pre></td></tr><tr><td class="docs"><p>Returns a map with two keys (aliases and answers). Each maps to a sequence.</p>
</td><td class="codes"><pre class="brush: clojure">(defn dns-lookup </td><td class="codes"><pre class="brush: clojure">(defn dns-lookup
([{:keys [to-lookup rr-type] :or {rr-type Type/A}}] ([{:keys [to-lookup rr-type] :or {rr-type Type/A}}]
(let [lkup (Lookup. (to-name to-lookup) (int rr-type))] (let [lkup (Lookup. (to-name to-lookup) (int rr-type))]
Expand All @@ -3218,15 +3227,25 @@
:answers (if (= (.getResult lkup) Lookup/SUCCESSFUL) :answers (if (= (.getResult lkup) Lookup/SUCCESSFUL)
(seq (.getAnswers lkup)) (seq (.getAnswers lkup))
[])})) [])}))
([to-lookup rr-type] (dns-lookup {:to-lookup to-lookup :rr-type rr-type})))</pre></td></tr><tr><td class="docs"><p>Returns the hostname when passed an ip-address (a reverse DNS lookup).</p> ([to-lookup rr-type] (dns-lookup {:to-lookup to-lookup :rr-type rr-type})))</pre></td></tr><tr><td class="docs"><p>Returns the hostname when passed an ip-address (a reverse DNS lookup).
Example</p>

<pre><code>
(reverse-dns-lookup "74.125.227.81")
</code></pre>
</td><td class="codes"><pre class="brush: clojure">(defn reverse-dns-lookup [^String ip-address] </td><td class="codes"><pre class="brush: clojure">(defn reverse-dns-lookup [^String ip-address]
(.getHostByAddr (.createNameService (DNSJavaNameServiceDescriptor.)) (ip-address-to-byte-array ip-address)))</pre></td></tr><tr><td class="docs"><p>Executes a DNS query by passing in a resource record to use as the query. You can optionally provide a resolver.</p> (.getHostByAddr (.createNameService (DNSJavaNameServiceDescriptor.)) (ip-address-to-byte-array ip-address)))</pre></td></tr><tr><td class="docs"><p>Executes a DNS query by passing in a resource record to use as the query. You can optionally provide a resolver.</p>
</td><td class="codes"><pre class="brush: clojure">(defn dns-query </td><td class="codes"><pre class="brush: clojure">(defn dns-query
([rr] ([rr]
(.send (SimpleResolver.) (Message/newQuery rr))) (.send (SimpleResolver.) (Message/newQuery rr)))
([rslvr rr] ([rslvr rr]
(.send rslvr (Message/newQuery rr))))</pre></td></tr><tr><td class="docs"><p>dig is a DNS utility that provides a great deal more detail than a simple lookup. It contains all the DNS information in the UDP packets. (.send rslvr (Message/newQuery rr))))</pre></td></tr><tr><td class="docs"><p>dig is a DNS utility that provides a great deal more detail than a simple lookup. It contains all the DNS information in the UDP packets.</p>
dig's options look something like:</p>
<p><em>While dig is fantastic, use this function with extreme caution. If you pass in invalid options, it will for the JVM process to exit.</em></p>

<p>I <em>HIGHLY</em> recommend that you use the dns-query function instead of this one!</p>

<p>dig's options look something like:</p>


<pre><code> <pre><code>
dig [@server] name [&lt;type&gt;] [&lt;class&gt;] [&lt;options&gt;] dig [@server] name [&lt;type&gt;] [&lt;class&gt;] [&lt;options&gt;]
Expand All @@ -3235,7 +3254,7 @@
<p>The type defaults to A and dclass defaults to IN <p>The type defaults to A and dclass defaults to IN
A simple example:</p> A simple example:</p>


<p>(dns-dig {:name "www.google.com"})</p> <p>(dns-dig-to-stdout {:name "www.google.com"})</p>


<p>Again, this prints the result to standard out. <p>Again, this prints the result to standard out.
use -x &lt;name&gt; for "name" to get a reverse lookup use -x &lt;name&gt; for "name" to get a reverse lookup
Expand All @@ -3260,10 +3279,13 @@
-e &lt;edns&gt; -- not supported here -e &lt;edns&gt; -- not supported here
-d &lt;edns&gt; -- not supported here -d &lt;edns&gt; -- not supported here
</code></pre> </code></pre>

<p>todo this seems to be ignoring the RR type at the moment... or it does some "smart" conversion of the type...</p>
</td><td class="codes"><pre class="brush: clojure">(defn dns-dig-to-stdout </td><td class="codes"><pre class="brush: clojure">(defn dns-dig-to-stdout
[{the-server :server the-name :name the-type :type options-map :options the-class :dclass :as the-args :or {:dclass DClass/IN}}] ([{the-server :server the-name :name the-type :type options-map :options dclass :dclass is-reverse :is-reverse :as the-args :or {dclass (:dclass rr-defaults) is-reverse false}}]
{:pre [(all-or-none the-args [:server :type :dclass])]} ; if :server is present, :class and :type must be as well (for all permutations...) {:pre [(all-or-none the-args [:type :dclass])]} ; if :dclass is present then :type must be as well and vice versa
(dig/main (into-array String (filter seq (into [the-server the-name the-type the-class] (convert-dig-options options-map))))))</pre></td></tr><tr><td class="docs"><h2>Ways to get a Zone</h2> (dig/main (into-array String (filter seq (into (flatten [(dig-server the-server) (dig-name the-name is-reverse) (str the-type) (str dclass)]) (convert-dig-options options-map))))))
([the-name is-reverse] (dig/main (into-array String (flatten [(dig-name the-name is-reverse) (str Type/A) (str DClass/IN)])))))</pre></td></tr><tr><td class="docs"><h2>Ways to get a Zone</h2>
</td><td class="codes"></td></tr><tr><td class="docs"><p>Generally prefer a Zone over a Master.</p> </td><td class="codes"></td></tr><tr><td class="docs"><p>Generally prefer a Zone over a Master.</p>
</td><td class="codes"></td></tr><tr><td class="docs"><p>Read the zone from a file. It can be a java.io.File object or a String file path.</p> </td><td class="codes"></td></tr><tr><td class="docs"><p>Read the zone from a file. It can be a java.io.File object or a String file path.</p>


Expand Down
38 changes: 31 additions & 7 deletions src/clj_dns/core.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@
(defn- convert-dig-options [options-map] (defn- convert-dig-options [options-map]
(filter seq [(when (:tcp options-map) "-t") (when (:ignore-trunction options-map) "-i") (when (:print-query options-map) "-p")])) (filter seq [(when (:tcp options-map) "-t") (when (:ignore-trunction options-map) "-i") (when (:print-query options-map) "-p")]))


;; Utility method that prefixes a dig server with '@' or returns nil if the server was not specified.
(defn- dig-server [ds]
(when ds (str "@" ds)))

;; Returns a seq of the-name prefixed with "-x" if this is a reverse query, otherwise it just returns the-name
(defn- dig-name [the-name is-reverse]
(if is-reverse ["-x" the-name] [the-name]))

;; given a map and a sequence of keys, it verifies that either all the keys from the sequence are present in the map or none of them are. ;; given a map and a sequence of keys, it verifies that either all the keys from the sequence are present in the map or none of them are.
(defn- all-or-none [m s] (defn- all-or-none [m s]
(or (every? #(contains? m %) s) (or (every? #(contains? m %) s)
Expand Down Expand Up @@ -211,23 +219,27 @@
;; </code></pre> ;; </code></pre>
;; or with multiple values: ;; or with multiple values:
;; <pre><code> ;; <pre><code>
;; (dns-lookup "www.google.com" "www.bing.com") ;; (dns-lookup-to-stdout "www.google.com" "www.bing.com")
;; </code></pre> ;; </code></pre>
;; or if you have a seq of things to look up: ;; or if you have a seq of things to look up:
;; <pre><code> ;; <pre><code>
;; (apply dns-lookup ["www.google.com" "www.bing.com"]) ;; (apply dns-lookup-to-stdout ["www.google.com" "www.bing.com"])
;; </code></pre> ;; </code></pre>
(defn dns-lookup-to-stdout [& to-lookups] (lookup/main (into-array String to-lookups))) (defn dns-lookup-to-stdout [& to-lookups] (lookup/main (into-array String to-lookups)))


;; Lookup hostname(s) by resource record type. This prints the result to stdout, it does not return a seq of the data. ;; Lookup hostname(s) by resource record type. This prints the result to stdout, it does not return a seq of the data.
;; ;;
;; example: ;; example:
;; <pre><code> ;; <pre><code>
;; (dns-loookup-by-type Type/PTR "www.google.com" "www.bing.com") ;; (dns-loookup-by-type-to-stdout Type/PTR "www.google.com" "www.bing.com")
;; </code></pre> ;; </code></pre>
(defn dns-lookup-by-type-to-stdout [rr-type & to-lookups] (lookup/main (into-array String (into ["-t" (Type/string rr-type)] to-lookups)))) (defn dns-lookup-by-type-to-stdout [rr-type & to-lookups] (lookup/main (into-array String (into ["-t" (Type/string rr-type)] to-lookups))))


;; Returns a map with two keys (aliases and answers). Each maps to a sequence. ;; Returns a map with two keys (aliases and answers). Each maps to a sequence.
;; Example
;; <pre><code>
;; (dns-lookup "www.google.com" Type/A)
;; </code></pre>
(defn dns-lookup (defn dns-lookup
([{:keys [to-lookup rr-type] :or {rr-type Type/A}}] ([{:keys [to-lookup rr-type] :or {rr-type Type/A}}]
(let [lkup (Lookup. (to-name to-lookup) (int rr-type))] (let [lkup (Lookup. (to-name to-lookup) (int rr-type))]
Expand All @@ -239,6 +251,10 @@
([to-lookup rr-type] (dns-lookup {:to-lookup to-lookup :rr-type rr-type}))) ([to-lookup rr-type] (dns-lookup {:to-lookup to-lookup :rr-type rr-type})))


;; Returns the hostname when passed an ip-address (a reverse DNS lookup). ;; Returns the hostname when passed an ip-address (a reverse DNS lookup).
;; Example
;; <pre><code>
;; (reverse-dns-lookup "74.125.227.81")
;; </code></pre>
(defn reverse-dns-lookup [^String ip-address] (defn reverse-dns-lookup [^String ip-address]
(.getHostByAddr (.createNameService (DNSJavaNameServiceDescriptor.)) (ip-address-to-byte-array ip-address))) (.getHostByAddr (.createNameService (DNSJavaNameServiceDescriptor.)) (ip-address-to-byte-array ip-address)))


Expand All @@ -250,6 +266,11 @@
(.send rslvr (Message/newQuery rr)))) (.send rslvr (Message/newQuery rr))))


;; dig is a DNS utility that provides a great deal more detail than a simple lookup. It contains all the DNS information in the UDP packets. ;; dig is a DNS utility that provides a great deal more detail than a simple lookup. It contains all the DNS information in the UDP packets.
;;
;; *While dig is fantastic, use this function with extreme caution. If you pass in invalid options, it will for the JVM process to exit.*
;;
;; I *HIGHLY* recommend that you use the dns-query function instead of this one!
;;
;; dig's options look something like: ;; dig's options look something like:
;; ;;
;; <pre><code> ;; <pre><code>
Expand All @@ -259,7 +280,7 @@
;; The type defaults to A and dclass defaults to IN ;; The type defaults to A and dclass defaults to IN
;; A simple example: ;; A simple example:
;; ;;
;; (dns-dig {:name "www.google.com"}) ;; (dns-dig-to-stdout {:name "www.google.com"})
;; ;;
;; Again, this prints the result to standard out. ;; Again, this prints the result to standard out.
;; use -x &lt;name&gt; for "name" to get a reverse lookup ;; use -x &lt;name&gt; for "name" to get a reverse lookup
Expand All @@ -282,10 +303,13 @@
;; -e &lt;edns&gt; -- not supported here ;; -e &lt;edns&gt; -- not supported here
;; -d &lt;edns&gt; -- not supported here ;; -d &lt;edns&gt; -- not supported here
;; </code></pre> ;; </code></pre>
;;
;; todo this seems to be ignoring the RR type at the moment... or it does some "smart" conversion of the type...
(defn dns-dig-to-stdout (defn dns-dig-to-stdout
[{the-server :server the-name :name the-type :type options-map :options the-class :dclass :as the-args :or {:dclass DClass/IN}}] ([{the-server :server the-name :name the-type :type options-map :options dclass :dclass is-reverse :is-reverse :as the-args :or {dclass (:dclass rr-defaults) is-reverse false}}]
{:pre [(all-or-none the-args [:server :type :dclass])]} ; if :server is present, :class and :type must be as well (for all permutations...) {:pre [(all-or-none the-args [:type :dclass])]} ; if :dclass is present then :type must be as well and vice versa
(dig/main (into-array String (filter seq (into [the-server the-name the-type the-class] (convert-dig-options options-map)))))) (dig/main (into-array String (filter seq (into (flatten [(dig-server the-server) (dig-name the-name is-reverse) (str the-type) (str dclass)]) (convert-dig-options options-map))))))
([the-name is-reverse] (dig/main (into-array String (flatten [(dig-name the-name is-reverse) (str Type/A) (str DClass/IN)])))))


;; ## Ways to get a Zone ;; ## Ways to get a Zone


Expand Down

0 comments on commit 42f047d

Please sign in to comment.