summaryrefslogtreecommitdiff
path: root/ip.pl
diff options
context:
space:
mode:
Diffstat (limited to 'ip.pl')
-rw-r--r--ip.pl118
1 files changed, 0 insertions, 118 deletions
diff --git a/ip.pl b/ip.pl
deleted file mode 100644
index d5a6e33..0000000
--- a/ip.pl
+++ /dev/null
@@ -1,118 +0,0 @@
-% vim: syntax=prolog
-
-:- use_module(library(clpfd)).
-:- use_module(library(dcg/basics)).
-
-make_ip4(A, B, C, D, ip4(A, B, C, D, Addr)) :-
- Addr #= (A * 2^24 + B * 2^16 + C * 2^8 + D).
-
-ip4(A, B, C, D, Addr) :-
- A in 0..255,
- B in 0..255,
- C in 0..255,
- D in 0..255,
- Addr #= (A * 2^24 + B * 2^16 + C * 2^8 + D).
-
-ip4_range(ip4(_, _, _, _), Range) :-
- Range in 0..32.
-
-ip_format(ip4(A, B, C, D, _), Str) :-
- format(string(Str), "~w.~w.~w.~w", [A, B, C, D]).
-
-ip_format(ip4_range(ip4(A, B, C, D, _), Range), Str) :-
- format(string(Str), "~w.~w.~w.~w/~w", [A, B, C, D, Range]).
-
-ip4_num(D) --> integer(D), { D >= 0, D =< 255 }.
-ip4_syntax(A, B, C, D) --> ip4_num(A), ".", ip4_num(B), ".", ip4_num(C), ".", ip4_num(D).
-ip4_range_syntax(A, B, C, D, R) -->
- ip4_syntax(A, B, C, D), "/", number(R),
- { R >= 0, R =< 32 }.
-
-ip4_parse(Str, Obj) :-
- string_codes(Str, Codes),
- phrase(ip4_syntax(A, B, C, D), Codes),
- Obj = ip4(A, B, C, D).
-
-ip4_range_parse(Str, Obj) :-
- string_codes(Str, Codes),
- phrase(ip4_range_syntax(A, B, C, D, Range), Codes),
- make_ip4(A, B, C, D, Ip),
- Obj = ip4_range(Ip, Range).
-
-ip6_syntax(A, B, C, D, E, F, G, H) -->
- ip6_num(A), ":",
- ip6_num(B), ":",
- ip6_num(C), ":",
- ip6_num(D), ":",
- ip6_num(E), ":",
- ip6_num(F), ":",
- ip6_num(G), ":",
- ip6_num(H).
-ip6_num(D) --> xinteger(D), { D >= 0, D =< 65535 }.
-
-ip6_parse(Str, Obj) :-
- string_codes(Str, Codes),
- phrase(ip6_syntax(A, B, C, D, E, F, G, H), Codes),
- Obj = ip6(A, B, C, D, E, F, G, H).
-
-ip_parse(Str, Obj) :-
- string_codes(Str, Codes),
- (
- phrase(ip4_syntax(A, B, C, D), Codes)
- -> Obj = ip4(A, B, C, D)
- ; (
- phrase(ip4_range_syntax(A, B, C, D, R), Codes)
- -> make_ip4(A, B, C, D, Ip),
- Obj = ip4_range(Ip, R)
- ; phrase(ip6_syntax(A, B, C, D, E, F, G, H), Codes),
- Obj = ip6(A, B, C, D, E, F, G, H)
- )
- ).
-
-:- begin_tests(lists).
-:- use_module(library(lists)).
-
-test(ip4) :-
- ip4(127, 0, 0, 1, A),
- assertion(A =:= (127 * 2**24 + 1)).
-
-test(ip_format) :-
- ip_format(ip4(127, 0, 0, 1, _), Str),
- assertion(Str == "127.0.0.1").
-
-test(ip_format) :-
- Ip = ip4_range(ip4(192, 168, 0, 0, _), 24),
- ip_format(Ip, Str),
- assertion(Str == "192.168.0.0/24").
-
-test(ip4_parse) :-
- ip4_parse("1.2.3.4", Ip),
- assertion(Ip == ip4(1, 2, 3, 4)).
-
-test(ip4_range_parse) :-
- ip4_range_parse("1.2.3.4/24", Ip),
- A #= 2^24 + 2 * 2^16 + 3 * 2^8 + 4,
- assertion(Ip == ip4_range(ip4(1, 2, 3, 4, A), 24)).
-
-test(ip6_parse) :-
- ip6_parse("1:2:3:4:5:6:a:b", Ip),
- assertion(Ip == ip6(1, 2, 3, 4, 5, 6, 10, 11)).
-
-test(ip6_parse) :-
- ip6_parse("0:2:3:4:5:6:a:b", Ip),
- assertion(Ip == ip6(0, 2, 3, 4, 5, 6, 10, 11)).
-
-test(ip_parse) :-
- ip_parse("127.0.0.1", Ip),
- assertion(Ip == ip4(127, 0, 0, 1)).
-
-test(ip_parse) :-
- ip_parse("192.168.10.4/24", R),
- make_ip4(192, 168, 10, 4, Ip),
- assertion(R == ip4_range(Ip, 24)).
-
-test(ip_parse) :-
- ip_parse("0:2:3:4:5:6:a:b", Ip),
- assertion(Ip == ip6(0, 2, 3, 4, 5, 6, 10, 11)).
-
-:- end_tests(lists).