Lumiera  0.pre.03
»edit your freedom«
luid.c
Go to the documentation of this file.
1 /*
2  LUID - Lumiera unique identifiers
3 
4  Copyright (C) Lumiera.org
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
29 #include "lib/luid.h"
30 
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <time.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <stdint.h>
38 
39 
40 
41 void
42 lumiera_uid_set_ptr (lumiera_uid* luid, void* ptr)
43 {
44  memset (luid, 0, 16);
45  *(void**)luid = ptr;
46 }
47 
48 
49 void*
51 {
52  return *(void**)luid;
53 }
54 
55 
56 void
58 {
59  static int fd = -2;
60  if (!luid)
61  return;
62 
63  if (fd == -2)
64  {
65  fd = open ("/dev/urandom", O_RDONLY);
66  /* on linux /dev/random would be way to slow for our purpose, so we comment that out for now.
67  other unixiods offer a /dev/random which has the same semantics as linux /dev/urandom has,
68  configuration should do this right some day.
69  if (fd == -1)
70  fd = open ("/dev/random", O_RDONLY);
71  */
72  if (fd >= 0)
73  fcntl (fd, F_SETFD, FD_CLOEXEC);
74  else
75  srand (getpid () + time (NULL));
76  }
77 
78  do
79  {
80  if (fd < 0)
81  {
82  for (int i = 0; i < 16; ++i)
83  ((unsigned char*)luid)[i] = (unsigned char)(rand()>>7);
84  }
85  else
86  {
87  if (read (fd, luid, 16) < 16)
88  abort ();
89  }
90  }
91  /* we identify generic pointers by having some zeros in the LUID,
92  * Accidentally, but very unlikely this might happen within a random LUID;
93  * just regenerate in this case */
94  while (!*(((intptr_t*)luid)+1));
95 }
96 
97 
98 void
100 {
101  memcpy (dest, src, 16);
102 }
103 
104 
105 int
106 lumiera_uid_eq (const lumiera_uid* luida, const lumiera_uid* luidb)
107 {
108  return !memcmp (luida, luidb, 16);
109 }
110 
111 size_t
113 {
114  return *(size_t*)luid;
115 }
116 
117 /*
118 // Local Variables:
119 // mode: C
120 // c-file-style: "gnu"
121 // indent-tabs-mode: nil
122 // End:
123 */
size_t lumiera_uid_hash(const lumiera_uid *luid)
Generate a hash sum over an luid.
Definition: luid.c:112
Common functions for handling of time values.
void * lumiera_uid_ptr_get(const lumiera_uid *luid)
Retrieve a generic pointer stored in a luid.
Definition: luid.c:50
Lumiera unique object identifier.
void lumiera_uid_gen(lumiera_uid *luid)
Generate a new luid.
Definition: luid.c:57
void lumiera_uid_copy(lumiera_uid *dest, lumiera_uid *src)
Copy an luid.
Definition: luid.c:99
int lumiera_uid_eq(const lumiera_uid *luida, const lumiera_uid *luidb)
Test 2 luid&#39;s for equality.
Definition: luid.c:106
unsigned char lumiera_uid[16]
storage for a Lumiera unique ID, based on a 128bit random number
Definition: hash-value.h:45
void lumiera_uid_set_ptr(lumiera_uid *luid, void *ptr)
Store a generic pointer in a luid.
Definition: luid.c:42