Skip to content

Instantly share code, notes, and snippets.

@rwat
Created September 15, 2010 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwat/581391 to your computer and use it in GitHub Desktop.
Save rwat/581391 to your computer and use it in GitHub Desktop.
(ns your-namespace
(:use [clojure.string :only [split]]))
; authored by ordnungswidrig
(defn is-rfc1918?
"Returns true if a string is a dotted quad in the IP ranges of 10.0.0.0/8,
192.168.0.0/16 or 172.16.0.0/22, else returns nil)"
[^String ip]
(when-let [[a b c d :as qs]
(map #(Short/valueOf %) (clojure.string/split ip #"\."))]
(and (= 4 (count qs)) (every? #(<= 0 % 255) qs)
(or (= 10 a)
(= [192 168] [a b])
(and (= 172 a) (<= 16 b 31))))))
(prn (is-rfc1918? "10.123.45.6"))
@rwat
Copy link
Author

rwat commented Oct 4, 2010

Thank you! I had forgotten about short / Short(s), too ; )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment