@@ -30,7 +30,7 @@ let get_byte32 (n : Int32.t) (i : int) : int =
30
30
to_int (logand 0xFFl (shift_right_logical n (8 * i)))
31
31
32
32
let get_byte (n :int64 ) (i :int ) : int =
33
- if i < 0 || i > 5 then
33
+ if i < 0 || i > 7 then
34
34
raise (Invalid_argument " Int64.get_byte index out of range" );
35
35
Int64. to_int (Int64. logand 0xFFL (Int64. shift_right_logical n (8 * i)))
36
36
@@ -58,6 +58,13 @@ let string_of_ip (ip : Int32.t) : string =
58
58
Format. sprintf " %d.%d.%d.%d" (get_byte32 ip 3 ) (get_byte32 ip 2 )
59
59
(get_byte32 ip 1 ) (get_byte32 ip 0 )
60
60
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
+
61
68
let string_of_mac (x :int64 ) : string =
62
69
Format. sprintf " %02x:%02x:%02x:%02x:%02x:%02x"
63
70
(get_byte x 5 ) (get_byte x 4 ) (get_byte x 3 )
@@ -109,6 +116,8 @@ type nwProto = int8 with sexp
109
116
110
117
type nwTos = int8 with sexp
111
118
119
+ type ipv6Addr = int64 * int64 with sexp
120
+
112
121
type tpPort = int16 with sexp
113
122
114
123
let mk_pseudo_header (src : nwAddr ) (dst : nwAddr ) (proto : int ) (len : int ) =
@@ -1376,3 +1385,27 @@ let mac_of_string (s : string) : dlAddr =
1376
1385
(logor (shift_left (parse_byte (List. nth bytes 3 )) 16 )
1377
1386
(logor (shift_left (parse_byte (List. nth bytes 4 )) 8 )
1378
1387
(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
+
0 commit comments