All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ian Campbell <ian.campbell-Sxgqhf6Nn4DQT0dZR+AlfA@public.gmane.org>
To: xen-devel-GuqFBffKawuULHF6PoxzQEEOCMrvLtNR@public.gmane.org,
	xen-api-GuqFBffKawuULHF6PoxzQEEOCMrvLtNR@public.gmane.org
Cc: Ian Campbell <ian.campbell-Sxgqhf6Nn4DQT0dZR+AlfA@public.gmane.org>
Subject: [PATCH 1 of 5] ocaml: resynchronise uuid library with xen-api-libs.hg
Date: Tue, 07 Dec 2010 14:32:52 +0000	[thread overview]
Message-ID: <21d9d889ec30f6ac5526.1291732372@zakaz.uk.xensource.com> (raw)
In-Reply-To: <patchbomb.1291732371-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>

# HG changeset patch
# User root-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org
# Date 1291307719 18000
# Node ID 21d9d889ec30f6ac55267c745a1cae56c768f63b
# Parent  8ecbddf6cad3a2fbb63b273658b9f67429754d3d
ocaml: resynchronise uuid library with xen-api-libs.hg

Signed-off-by: Ian Campbell <ian.campbell-Sxgqhf6Nn4DQT0dZR+AlfA@public.gmane.org>

diff -r 8ecbddf6cad3 -r 21d9d889ec30 tools/ocaml/libs/uuid/uuid.ml
--- a/tools/ocaml/libs/uuid/uuid.ml	Thu Dec 02 10:09:24 2010 -0500
+++ b/tools/ocaml/libs/uuid/uuid.ml	Thu Dec 02 11:35:19 2010 -0500
@@ -1,6 +1,5 @@
 (*
- * Copyright (C) 2006-2007 XenSource Ltd.
- * Copyright (C) 2008      Citrix Ltd.
+ * Copyright (C) 2006-2010 Citrix Systems Inc.
  * Author Vincent Hanquez <vincent.hanquez-mvvWK6WmYclDPfheJLI6IQ@public.gmane.org>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -14,9 +13,7 @@
  * GNU Lesser General Public License for more details.
  *)
 
-(** Type-safe UUIDs. *)
-
-(** Internally, a UUID is simply a string. *)
+(* Internally, a UUID is simply a string. *)
 type 'a t = string
 
 type cookie = string
@@ -24,6 +21,8 @@ type cookie = string
 let of_string s = s
 let to_string s = s
 
+let null = ""
+
 (* deprecated: we don't need to duplicate the uuid prefix/suffix *)
 let uuid_of_string = of_string
 let string_of_uuid = to_string
@@ -32,12 +31,34 @@ let string_of_cookie s = s
 
 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;
@@ -52,30 +73,14 @@ let uuid_of_int_array uuid =
     uuid.(6) uuid.(7) uuid.(8) uuid.(9) uuid.(10) uuid.(11)
     uuid.(12) uuid.(13) uuid.(14) uuid.(15)
 
-(** Return a new random UUID *)
-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_urnd
 
-(** Return a new random, big UUID (hopefully big and random enough to be
-    unguessable) *)
 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 hexencode x = 
-    let nibble x =
-      char_of_int (if x < 10 
-		   then int_of_char '0' + x
-		   else int_of_char 'a' + (x - 10)) in
-    let result = String.make (String.length x * 2) ' ' in
-    for i = 0 to String.length x - 1 do
-      let byte = int_of_char x.[i] in
-      result.[i * 2 + 0] <- nibble((byte lsr 4) land 15);
-      result.[i * 2 + 1] <- nibble((byte lsr 0) land 15);
-    done;
-    result in
-  let n = 64 in
-  hexencode (String.concat "" (List.map (fun x -> String.make 1 (char_of_int x)) (Array.to_list (read_n_random_bytes n))))
-*)
 
 let int_array_of_uuid s =
   try
@@ -86,3 +91,10 @@ let int_array_of_uuid s =
              a10; a11; a12; a13; a14; a15; ]);
     Array.of_list !l
   with _ -> invalid_arg "Uuid.int_array_of_uuid"
+
+let is_uuid str =
+	try
+		Scanf.sscanf str
+			"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
+			(fun _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ -> true)
+	with _ -> false
diff -r 8ecbddf6cad3 -r 21d9d889ec30 tools/ocaml/libs/uuid/uuid.mli
--- a/tools/ocaml/libs/uuid/uuid.mli	Thu Dec 02 10:09:24 2010 -0500
+++ b/tools/ocaml/libs/uuid/uuid.mli	Thu Dec 02 11:35:19 2010 -0500
@@ -1,6 +1,5 @@
 (*
- * Copyright (C) 2006-2007 XenSource Ltd.
- * Copyright (C) 2008      Citrix Ltd.
+ * Copyright (C) 2006-2010 Citrix Systems Inc.
  * Author Vincent Hanquez <vincent.hanquez-mvvWK6WmYclDPfheJLI6IQ@public.gmane.org>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -13,41 +12,56 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Lesser General Public License for more details.
  *)
-
 (** Type-safe UUIDs.
     Probably need to refactor this; UUIDs are used in two places:
-    1. to uniquely name things across the cluster
-    2. as secure session IDs
+    + to uniquely name things across the cluster
+    + as secure session IDs
+
     There is the additional constraint that current Xen tools use 
     a particular format of UUID (the 16 byte variety generated by fresh ())
+
+	Also, cookies aren't UUIDs and should be put somewhere else.
 *)
 
-(** A 128-bit UUID referencing a value of type 'a. *)
+(** A 128-bit UUID.  Using phantom types ('a) to achieve the requires type-safety. *)
 type 'a t
 
-(** A 512-bit UUID. *)
+(** 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 UUID from a string. *)
+val of_string : string -> 'a t
+
+(** Marshal a UUID to a string. *)
+val to_string : 'a t -> string
+
+(** A null UUID, as if such a thing actually existed.  It turns out to be
+ * useful though. *)
+val null : 'a t
+
+(** Deprecated alias for {! Uuid.of_string} *)
+val uuid_of_string : string -> 'a t
+
+(** Deprecated alias for {! Uuid.to_string} *)
+val string_of_uuid : 'a t -> string
+
+(** Convert an array to a UUID. *)
+val uuid_of_int_array : int array -> 'a t
+
+(** Convert a UUID to an array. *)
+val int_array_of_uuid : 'a t -> int array
+
+(** Check whether a string is a UUID. *)
+val is_uuid : string -> bool
+
+(** A 512-bit cookie. *)
 type cookie
 
-(** Create a fresh (unique!) UUID *)
-val make_uuid : unit -> 'a t
-
-(** Create a fresh secure (bigger and hopefully unguessable) UUID *)
 val make_cookie : unit -> cookie
 
-(** Create a type-safe UUID. *)
-val of_string : string -> 'a t
-
-(** Marshal a UUID to a (type-unsafe) string. *)
-val to_string : 'a t -> string
-
-(* deprecated alias for previous one *)
-val uuid_of_string : string -> 'a t
-val string_of_uuid : 'a t -> string
-
 val cookie_of_string : string -> cookie
 
 val string_of_cookie : cookie -> string
-
-val uuid_of_int_array : int array -> 'a t
-
-val int_array_of_uuid : 'a t -> int array

  parent reply	other threads:[~2010-12-07 14:32 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-18 10:49 XCP: Allow XCP to use ocaml library bindings in Xen unstable (which will become Xen 4.1) Ian Campbell
2010-11-18 10:50 ` [PATCH 0 of 3] XCP: Allow XCP to use ocaml library bindings in Xen unstable (which will become Xen 4.1) -- xen-dist-ocaml.hg Ian Campbell
     [not found]   ` <patchbomb.1290077407-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>
2010-11-18 10:50     ` [PATCH 1 of 3] Query rpm to find build directories Ian Campbell
2010-11-18 10:50     ` [PATCH 2 of 3] Define $(RPM) for use in Makefile Ian Campbell
2010-11-18 10:50   ` [PATCH 3 of 3] Install additional packages Ian Campbell
     [not found] ` <1290077348.31507.5282.camel-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>
2010-11-18 10:50   ` [PATCH 0 of 4] XCP: Allow XCP to use ocaml library bindings in Xen unstable (which will become Xen 4.1) -- xen-api-libs.hg Ian Campbell
     [not found]     ` <patchbomb.1290077414-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>
2010-11-18 10:50       ` [PATCH 1 of 4] Query rpm to find build directories Ian Campbell
2010-11-18 10:50       ` [PATCH 2 of 4] xc: split xc non-upstream bindings into xcext module Ian Campbell
2010-11-18 14:36         ` [Xen-API] " Vincent Hanquez
2010-11-18 15:35           ` Ian Campbell
2010-11-19 11:43             ` Vincent Hanquez
     [not found]               ` <4CE662D9.6030808-mvvWK6WmYclDPfheJLI6IQ@public.gmane.org>
2010-11-19 13:32                 ` Ian Campbell
     [not found]             ` <1290094550.31507.5391.camel-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>
2010-11-19 17:43               ` [Xen-devel] " Ian Jackson
     [not found]                 ` <19686.46942.593712.651242-msK/Ju9w1zmnROeE8kUsYhEHtJm+Wo+I@public.gmane.org>
2010-11-22 13:05                   ` Ian Campbell
     [not found]                 ` <m2n.s.1PKW6s-0019ae@chiark.greenend.org.uk>
     [not found]                   ` <m2n.s.1PKW6s-0019ae-QGMSyCZBOSwv4zxTlrOuLwNdhmdF6hFW@public.gmane.org>
2010-11-23 18:02                     ` Ian Jackson
2010-11-19 13:42           ` Re: [Xen-API] " Gianni Tedesco
2010-11-18 10:50     ` [PATCH 3 of 4] add configure-time option to use system installed Xen library bindings Ian Campbell
2010-11-18 10:50     ` [PATCH 4 of 4] REBASE-4.1: default to using system installed bindings if Xen is available Ian Campbell
2010-11-18 10:50   ` [PATCH 0 of 6] XCP: Allow XCP to use ocaml library bindings in Xen unstable (which will become Xen 4.1) -- xen-api.hg Ian Campbell
2010-11-18 10:50     ` [PATCH 1 of 6] Query rpm to find build directories Ian Campbell
2010-11-18 10:50     ` [PATCH 2 of 6] xc: use Xcext library rather than Xc where appropriate Ian Campbell
     [not found]     ` <patchbomb.1290077422-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>
2010-11-18 10:50       ` [PATCH 3 of 6] REBASE-4.1: vmops: apply CPUID to all domains Ian Campbell
2010-11-18 10:50       ` [PATCH 5 of 6] HACK/PoC: adjust for minor differences in upstream Xc bindings Ian Campbell
2010-11-18 10:50     ` [PATCH 4 of 6] REBASE-4.1: xenops: update PV console path in xenstore Ian Campbell
2010-11-18 10:50     ` [PATCH 6 of 6] HACK: xenguest updates for Xen 4.0/4.1 Ian Campbell
2010-11-18 10:50   ` [PATCH 0 of 7] XCP: Allow XCP to use ocaml library bindings in Xen unstable (which will become Xen 4.1) -- xen-unstable.hg Ian Campbell
     [not found]     ` <patchbomb.1290077432-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>
2010-11-18 10:50       ` [PATCH 1 of 7] libxc: rename safe_strerror to _xc_safestrerror and pass XC handle Ian Campbell
2010-11-18 10:50       ` [PATCH 4 of 7] ocaml: resynchronise uuid library with xen-api-libs.hg Ian Campbell
2010-11-18 11:32         ` Gianni Tedesco
2010-11-18 12:27           ` Ian Campbell
     [not found]             ` <1290083222.31507.5290.camel-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>
2010-11-18 14:44               ` [Xen-devel] " Vincent Hanquez
2010-11-18 10:50       ` [PATCH 6 of 7] ocaml: xc bindings: use libxenctrl and libxenguest Ian Campbell
2010-11-18 10:50       ` [PATCH 7 of 7] HACK: Do not install some stuff which conflicts with out-off-tree XCP versions of the same Ian Campbell
2010-11-18 10:50     ` [PATCH 2 of 7] libxc: allow caller to specify no re-entrancy protection when opening the interface Ian Campbell
2010-11-18 10:50     ` [PATCH 3 of 7] ocaml: install built modules Ian Campbell
2010-11-18 10:50     ` [PATCH 5 of 7] ocaml: add dependency to module metadata Ian Campbell
2010-12-07 14:30   ` [Xen-devel] XCP: Allow XCP to use ocaml library bindings in Xen unstable (which will become Xen 4.1) Ian Campbell
2010-12-07 14:32     ` [PATCH 0 of 5] XCP: Allow XCP to use ocaml library bindings in Xen unstable (which will become Xen 4.1) -- xen-unstable.hg Ian Campbell
     [not found]       ` <patchbomb.1291732371-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>
2010-12-07 14:32         ` Ian Campbell [this message]
2010-12-07 14:32         ` [PATCH 2 of 5] ocaml: add dependency to module metadata Ian Campbell
2010-12-07 14:32         ` [PATCH 4 of 5] ocaml: evtchn+xc bindings: use libxenctrl and libxenguest Ian Campbell
2010-12-07 14:32       ` [PATCH 3 of 5] ocaml: rename Evtchn.bind_virq as Evtchn.bind_dom_exc_virq Ian Campbell
2010-12-07 14:32       ` [PATCH 5 of 5] HACK: Do not install some stuff which conflicts with out-off-tree XCP versions of the same Ian Campbell
2010-12-13 17:23       ` [PATCH 0 of 5] XCP: Allow XCP to use ocaml library bindings in Xen unstable (which will become Xen 4.1) -- xen-unstable.hg Stefano Stabellini
2010-12-15 11:03         ` Vincent Hanquez
2010-12-16  9:56           ` Ian Campbell
     [not found]       ` <m2n.s.1PPykG-000YDj@chiark.greenend.org.uk>
2010-12-14 19:22         ` [PATCH 1 of 5] ocaml: resynchronise uuid library with xen-api-libs.hg Ian Jackson
2011-01-06 17:38         ` Ian Jackson
     [not found]     ` <1291732233.13966.3198.camel-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>
2010-12-07 14:32       ` [PATCH 0 of 3] XCP: Allow XCP to use ocaml library bindings in Xen unstable (which will become Xen 4.1) -- xen-dist-ocaml.hg Ian Campbell
     [not found]         ` <patchbomb.1291732364-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>
2010-12-07 14:32           ` [PATCH 1 of 3] Query rpm to find build directories Ian Campbell
2010-12-07 14:32           ` [PATCH 3 of 3] Install additional packages Ian Campbell
2010-12-07 14:32         ` [PATCH 2 of 3] Define $(RPM) for use in Makefile Ian Campbell
2010-12-08 18:19         ` [Xen-API] [PATCH 0 of 3] XCP: Allow XCP to use ocaml library bindings in Xen unstable (which will become Xen 4.1) -- xen-dist-ocaml.hg Dave Scott
2010-12-07 14:32       ` [PATCH 0 of 5] XCP: Allow XCP to use ocaml library bindings in Xen unstable (which will become Xen 4.1) -- xen-api-libs.hg Ian Campbell
2010-12-07 14:33         ` [PATCH 1 of 5] Query rpm to find build directories Ian Campbell
     [not found]         ` <patchbomb.1291732379-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>
2010-12-07 14:33           ` [PATCH 2 of 5] xc: split xc non-upstream bindings into xcext module Ian Campbell
2010-12-07 14:33           ` [PATCH 3 of 5] add configure-time option to use system installed Xen library bindings Ian Campbell
2010-12-07 14:33           ` [PATCH 5 of 5] REBASE-4.1: xiu: add xenctrlosdep backend Ian Campbell
2010-12-07 14:33         ` [PATCH 4 of 5] REBASE-4.1: default to using system installed bindings if Xen is available Ian Campbell
2010-12-07 14:33       ` [PATCH 0 of 6] XCP: Allow XCP to use ocaml library bindings in Xen unstable (which will become Xen 4.1) -- xen-api.hg Ian Campbell
     [not found]         ` <patchbomb.1291732388-o4Be2W7LfRlXesXXhkcM7miJhflN2719@public.gmane.org>
2010-12-07 14:33           ` [PATCH 1 of 6] Query rpm to find build directories Ian Campbell
2010-12-07 14:33           ` [PATCH 2 of 6] xc: use Xcext library rather than Xc where appropriate Ian Campbell
2010-12-07 14:33           ` [PATCH 4 of 6] REBASE-4.1: xenops: update PV console path in xenstore Ian Campbell
2010-12-07 14:33           ` [PATCH 6 of 6] HACK: xenguest updates for Xen 4.0/4.1 Ian Campbell
2010-12-07 14:33         ` [PATCH 3 of 6] REBASE-4.1: vmops: apply CPUID to all domains Ian Campbell
2010-12-07 14:33         ` [PATCH 5 of 6] HACK/PoC: adjust for minor differences in upstream Xc bindings Ian Campbell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=21d9d889ec30f6ac5526.1291732372@zakaz.uk.xensource.com \
    --to=ian.campbell-sxgqhf6nn4dqt0dzr+alfa@public.gmane.org \
    --cc=xen-api-GuqFBffKawuULHF6PoxzQEEOCMrvLtNR@public.gmane.org \
    --cc=xen-devel-GuqFBffKawuULHF6PoxzQEEOCMrvLtNR@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.