[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-API] [xen-api-libs.hg:PATCH 0 of 2] Uuid stuff.



On 29/07/10 16:46, magnus.therning@xxxxxxxxxxxxx wrote:
> This is not ready for merging, but I'd like some comments on the code
> and direction.
> 
> Besides the minor clean-up (is more needed?) it introduces a new
> function, make_uuid_random, which reads from /dev/random (and
> therefore may block).
> 
> The plan is to change make_uuid to use the PRNG in Random, and then
> modify xapi so that make_uuid_random is used where it's necessary
> (creating pool secret and sessions, what else?).
> 
> Is this a direction that's worthwhile to pursue?
> Would a make_uuid_urandom also be necessary?  (E.g. getting session
> UUIDs from /dev/random might be too heavy weight for something that's
> that short-lived.)

Here is a new version of the patch to xen-api-libs.hg:

diff -r 23575cf80b47 uuid/uuid.ml
--- a/uuid/uuid.ml      Mon Aug 02 16:33:30 2010 +0100
+++ b/uuid/uuid.ml      Tue Aug 03 10:18:03 2010 +0100
@@ -30,12 +30,34 @@

 let cookie_of_string s = s

-(* FIXME: using /dev/random is too slow but using /dev/urandom is too
-    deterministic. *)
-let dev_random = "/dev/urandom"
+let dev_random = "/dev/random"
+let dev_urandom = "/dev/urandom"

-let read_random n =
-  let ic = open_in_bin dev_random in
+let rnd_array n =
+       let fstbyte i = 0xff land i in
+       let sndbyte i = fstbyte (i lsr 8) in
+       let thdbyte i = sndbyte (i lsr 8) in
+       let rec rnd_list n acc = match n with
+               | 0 -> acc
+               | 1 ->
+                       let b = fstbyte (Random.bits ()) in
+                       b :: acc
+               | 2 ->
+                       let r = Random.bits () in
+                       let b1 = fstbyte r in
+                       let b2 = sndbyte r in
+                       b1 :: b2 :: acc
+               | n ->
+                       let r = Random.bits () in
+                       let b1 = fstbyte r in
+                       let b2 = sndbyte r in
+                       let b3 = thdbyte r in
+                       rnd_list (n - 3) (b1 :: b2 :: b3 :: acc)
+       in
+       Array.of_list (rnd_list n [])
+
+let read_array dev n =
+  let ic = open_in_bin dev in
   try
     let result = Array.init n (fun _ -> input_byte ic) in
     close_in ic;
@@ -50,10 +72,13 @@
     uuid.(6) uuid.(7) uuid.(8) uuid.(9) uuid.(10) uuid.(11)
     uuid.(12) uuid.(13) uuid.(14) uuid.(15)

-let make_uuid() = uuid_of_int_array (read_random 16)
+let make_uuid_prng () = uuid_of_int_array (rnd_array 16)
+let make_uuid_urnd () = uuid_of_int_array (read_array dev_urandom 16)
+let make_uuid_rnd () = uuid_of_int_array (read_array dev_random 16)
+let make_uuid = make_uuid_prng

 let make_cookie() =
-  let bytes = Array.to_list (read_random 64) in
+  let bytes = Array.to_list (read_array dev_urandom 64) in
   String.concat "" (List.map (Printf.sprintf "%1x") bytes)

 let int_array_of_uuid s =
diff -r 23575cf80b47 uuid/uuid.mli
--- a/uuid/uuid.mli     Mon Aug 02 16:33:30 2010 +0100
+++ b/uuid/uuid.mli     Tue Aug 03 10:18:03 2010 +0100
@@ -27,6 +27,9 @@

 (** Create a fresh UUID *)
 val make_uuid : unit -> 'a t
+val make_uuid_prng : unit -> 'a t
+val make_uuid_urnd : unit -> 'a t
+val make_uuid_rnd : unit -> 'a t

 (** Create a type-safe UUID. *)
 val of_string : string -> 'a t

-- 
Magnus Therning
magnus.therning@xxxxxxxxxxxxx          Jabber: magnusth@eng

There does not now, nor will there ever, exist a programming language
in which it is the least bit hard to write bad programs.
     -- Flon's Axiom

_______________________________________________
xen-api mailing list
xen-api@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/mailman/listinfo/xen-api


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.