Skip to content

Commit f7ee4d7

Browse files
committed
add prety print function for ipv6
1 parent d358f4e commit f7ee4d7

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/Packet.ml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let get_byte32 (n : Int32.t) (i : int) : int =
3030
to_int (logand 0xFFl (shift_right_logical n (8 * i)))
3131

3232
let get_byte (n:int64) (i:int) : int =
33-
if i < 0 || i > 5 then
33+
if i < 0 || i > 7 then
3434
raise (Invalid_argument "Int64.get_byte index out of range");
3535
Int64.to_int (Int64.logand 0xFFL (Int64.shift_right_logical n (8 * i)))
3636

@@ -58,6 +58,13 @@ let string_of_ip (ip : Int32.t) : string =
5858
Format.sprintf "%d.%d.%d.%d" (get_byte32 ip 3) (get_byte32 ip 2)
5959
(get_byte32 ip 1) (get_byte32 ip 0)
6060

61+
let string_of_ipv6 ((ip1,ip2) : int64*int64) : string =
62+
Format.sprintf "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
63+
(get_byte ip1 7) (get_byte ip1 6) (get_byte ip1 5) (get_byte ip1 4)
64+
(get_byte ip1 3) (get_byte ip1 2) (get_byte ip1 1) (get_byte ip1 0)
65+
(get_byte ip2 7) (get_byte ip2 6) (get_byte ip2 5) (get_byte ip2 4)
66+
(get_byte ip2 3) (get_byte ip2 2) (get_byte ip2 1) (get_byte ip2 0)
67+
6168
let string_of_mac (x:int64) : string =
6269
Format.sprintf "%02x:%02x:%02x:%02x:%02x:%02x"
6370
(get_byte x 5) (get_byte x 4) (get_byte x 3)
@@ -109,6 +116,8 @@ type nwProto = int8 with sexp
109116

110117
type nwTos = int8 with sexp
111118

119+
type ipv6Addr = int64*int64 with sexp
120+
112121
type tpPort = int16 with sexp
113122

114123
let mk_pseudo_header (src : nwAddr) (dst : nwAddr) (proto : int) (len : int) =
@@ -1376,3 +1385,27 @@ let mac_of_string (s : string) : dlAddr =
13761385
(logor (shift_left (parse_byte (List.nth bytes 3)) 16)
13771386
(logor (shift_left (parse_byte (List.nth bytes 4)) 8)
13781387
(parse_byte (List.nth bytes 5)))))))
1388+
1389+
let ipv6_of_string (s : string) : ipv6Addr =
1390+
let bytes = Str.split (Str.regexp ":") s in
1391+
let bytes_len = List.length bytes in
1392+
let rec fill_with_0 n =
1393+
if n = 0 then ["0"]
1394+
else "0"::(fill_with_0 (n-1)) in
1395+
let rec fill_bytes bytes =
1396+
match bytes with
1397+
| [] -> []
1398+
| ""::q -> List.append (fill_with_0 (8-bytes_len)) q
1399+
| t::q -> t::(fill_bytes q) in
1400+
let bytes = fill_bytes bytes in
1401+
let parse_byte str = Int64.of_string ("0x" ^ str) in
1402+
let open Int64 in
1403+
(logor (shift_left (parse_byte (List.nth bytes 0)) 48)
1404+
(logor (shift_left (parse_byte (List.nth bytes 1)) 32)
1405+
(logor (shift_left (parse_byte (List.nth bytes 2)) 16)
1406+
(parse_byte (List.nth bytes 3))))),
1407+
(logor (shift_left (parse_byte (List.nth bytes 4)) 48)
1408+
(logor (shift_left (parse_byte (List.nth bytes 5)) 32)
1409+
(logor (shift_left (parse_byte (List.nth bytes 6)) 16)
1410+
(parse_byte (List.nth bytes 7)))))
1411+

lib/Packet.mli

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type nwProto = int8 with sexp
5151
(** [nwTos] is the type of IPv4 types of service. *)
5252
type nwTos = int8 with sexp
5353

54+
(** [ipv6Addr] is the type of IPv6 addresses. *)
55+
type ipv6Addr = int64*int64 with sexp
56+
5457
(** [tpPort] is the type of transport protocol ports. *)
5558
type tpPort = int16 with sexp
5659

@@ -376,6 +379,12 @@ val string_of_nwProto : nwProto -> string
376379
(** [string_of_nwTos t] pretty-prints an IPv4 type of service. *)
377380
val string_of_nwTos : nwTos -> string
378381

382+
(** [string_of_ipv6 t] pretty-prints an IPv6 address. **)
383+
val string_of_ipv6 : ipv6Addr -> string
384+
385+
(** [string_of_ipv6 t] Converts a colon-separated IPv6 address to ipv6Addr. **)
386+
val ipv6_of_string : string -> ipv6Addr
387+
379388
(** [string_of_tpPort p] pretty-prints a transport protocol port number. *)
380389
val string_of_tpPort : tpPort -> string
381390

0 commit comments

Comments
 (0)