]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - src/uuid.c
Do not export config.h
[frescor/forb.git] / src / uuid.c
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
5 /*   Universidad de Cantabria,              SPAIN                         */
6 /*   University of York,                    UK                            */
7 /*   Scuola Superiore Sant'Anna,            ITALY                         */
8 /*   Kaiserslautern University,             GERMANY                       */
9 /*   Univ. Politécnica  Valencia,           SPAIN                        */
10 /*   Czech Technical University in Prague,  CZECH REPUBLIC                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
19 /*          FRESCOR project (FP6/2005/IST/5-034026) is funded             */
20 /*       in part by the European Union Sixth Framework Programme          */
21 /*       The European Union is not liable of any use that may be          */
22 /*       made of this code.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FORB (Frescor Object Request Broker)             */
26 /*                                                                        */
27 /* FORB is free software; you can redistribute it and/or modify it        */
28 /* under terms of the GNU General Public License as published by the      */
29 /* Free Software Foundation; either version 2, or (at your option) any    */
30 /* later version.  FORB is distributed in the hope that it will be        */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
33 /* General Public License for more details. You should have received a    */
34 /* copy of the GNU General Public License along with FORB; see file       */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
36 /* Cambridge, MA 02139, USA.                                              */
37 /*                                                                        */
38 /* As a special exception, including FORB header files in a file,         */
39 /* instantiating FORB generics or templates, or linking other files       */
40 /* with FORB objects to produce an executable application, does not       */
41 /* by itself cause the resulting executable application to be covered     */
42 /* by the GNU General Public License. This exception does not             */
43 /* however invalidate any other reasons why the executable file might be  */
44 /* covered by the GNU Public License.                                     */
45 /**************************************************************************/
46
47 /**
48  * @file   uuid.c
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Tue Aug 26 09:17:31 2008
51  * 
52  * @brief  Unix version of UUID generator
53  * 
54  */
55 #include <forb/uuid.h>
56 #include <sys/time.h>
57 #include <time.h>
58 #include "sha1.h"
59 #include <sys/types.h>
60 #include <unistd.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63
64 /** 
65  * Generates universally unique ID.
66  * 
67  * @param dest Where to store the newly generated UUID.
68  * 
69  * @return Same as @a dest.
70  */
71 forb_uuid_t *forb_uuid_generate(forb_uuid_t *dest)
72 {
73         SHA1_CTX ctx;
74         struct timeval  tv;
75         pid_t pid = getpid();
76         unsigned char digest[20];
77         int i, r;
78         
79         gettimeofday(&tv, 0);
80
81         SHA1Init(&ctx);
82         SHA1Update(&ctx, (unsigned char*)&tv, sizeof(tv));
83         SHA1Update(&ctx, (unsigned char*)&pid, sizeof(pid));
84
85         /* Crank the random number generator a few times */
86         gettimeofday(&tv, 0);
87         for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--) {
88                 r = rand();
89                 SHA1Update(&ctx, (unsigned char*)&r, sizeof(r));
90         }
91
92         /* Also try to use /dev/urnadom to get some randomnes */
93         
94         /* This should be enough for localhost process.
95          * TODO: Add network addresses for distributed operation! */
96         SHA1Final(digest, &ctx);
97         memcpy(dest, digest, sizeof(forb_uuid_t));
98         return dest;
99 }
100
101 /** 
102  * Converts UUID to string.
103  * 
104  * @param dest Where to store the result.
105  * @param uuid The UUID to convert.
106  * @param n Size of @a dest buffer.
107  * 
108  * @return The same value as @a dest.
109  */
110 char *forb_uuid_to_string(char *dest, const forb_uuid_t *uuid, size_t n)
111 {
112         int i;
113         for (i = 0; i<sizeof(uuid->byte) && 2*(i+1) < n; i++)
114                 sprintf(dest + 2*i, "%02hhx", uuid->byte[i]);
115         dest[2*i] = '\0';
116         return dest;
117 }
118
119 static inline int hexval(char c)
120 {
121         if (c >= '0' && c <= '9')
122                 return c-'0';
123         if (c >= 'a' && c <= 'f')
124                 return 10+c-'a';
125         if (c >= 'A' && c <= 'F')
126                 return 10+c-'A';
127         return -1;
128 }
129
130 /** 
131  * Creates UUID from string form.
132  * 
133  * @param dest Where to store the UUID.
134  * @param string UUID in the string form.
135  * 
136  * @return @a dest in case of success, NULL in case of error.
137  */
138 forb_uuid_t *forb_uuid_from_string(forb_uuid_t *dest, const char *string)
139 {
140         int i;
141         
142         for (i = 0; i<sizeof(dest->byte); i++) {
143                 int a, b;
144                 a = hexval(string[2*i]);
145                 if (a < 0)
146                         return NULL;
147                 b = hexval(string[2*i+1]);
148                 if (b < 0)
149                         return NULL;
150
151                 dest->byte[i] = (a << 4) + b;
152         }
153         return dest;
154 }
155