OSDN Git Service

import v0.2.5
[openpts/openpts.git] / src / uuid_libuuid.c
1 /*
2  * This file is part of the OpenPTS project.
3  *
4  * The Initial Developer of the Original Code is International
5  * Business Machines Corporation. Portions created by IBM
6  * Corporation are Copyright (C) 2010 International Business
7  * Machines Corporation. All Rights Reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the Common Public License as published by
11  * IBM Corporation; either version 1 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * Common Public License for more details.
18  *
19  * You should have received a copy of the Common Public License
20  * along with this program; if not, a copy can be viewed at
21  * http://www.opensource.org/licenses/cpl1.0.php.
22  */
23
24 /**
25  * \file src/uuid.c
26  * \brief UUID wrapper (libuuid part)
27  * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
28  * @date 2010-11-29
29  * cleanup 2011-01-20,21 SM
30  *
31  * Linux uses libuuid
32  *
33  * Program  UUID   Description        When            
34  * ---------------------------------------------------------------------------------------------------
35  * ptscd    CID    Colelctor ID       System install     => /var/lib/openpts/uuid (UUID of sign key)
36  *          RM     RM ID              RM Gen xid in RM,  path /var/lib/openpts/$UUID/rm_files
37  *          RunID  ID of this daemon  Daemon start       => /var/lib/openpts/run_uuid
38  * openpts  VID    Verifier ID        1st run            => uuid=XXX in 'HOME/.openpts/openpts.conf' file
39  *          RM                                           => 'HOME/.openpts/hostname/$UUID/rm_files
40  *
41  * Unit Test: check_uuid.c
42  *
43  */
44
45 #include <stdio.h>
46 #include <string.h>
47 #include <time.h>
48 #include <sys/stat.h>
49
50 // DIR
51 #include <unistd.h>
52 #include <dirent.h>
53
54 #include <uuid.h>
55
56 #include <openpts.h>
57
58 #define SEP_LINE "------------------------------------------------------------------------------------"
59
60 /******************************/
61 /* PTS_UUID                   */
62 /******************************/
63
64 /**
65  * Create new UUID (DCE1.1 v1 time and node base)
66  */
67 PTS_UUID *newUuid() {
68     uuid_t uu;
69     PTS_UUID *uuid;
70
71     uuid = malloc(sizeof(PTS_UUID));  // BYTE[16]
72     if (uuid == NULL) {
73         ERROR("no memory\n");
74         return NULL;
75     }
76
77     uuid_generate_time(uu);
78     memcpy(uuid, uu, 16);
79
80     return (PTS_UUID *)uuid;
81 }
82
83 /**
84  * free UUID
85  */
86 void freeUuid(PTS_UUID *uuid) {
87     /* check */
88     if (uuid == NULL) {
89         ERROR("null input\n");
90         return;
91     }
92
93     free(uuid);
94 }
95
96
97 /**
98  * String -> UUID 
99  */
100 PTS_UUID *getUuidFromString(char *str) {
101     PTS_UUID *uuid;
102     uuid_t uu;
103     int rc;
104
105     rc = uuid_parse(str, uu);
106     if (rc != 0) {
107         ERROR("getUuidFromString() - uuid_parse fail, rc=%d, UUID='%s'\n",
108             rc, str);
109         return NULL;
110     }
111
112     uuid = malloc(sizeof(PTS_UUID));
113     if (uuid == NULL) {
114         ERROR("\n");
115         return NULL;
116     }
117     memcpy(uuid, uu, 16);
118
119     return uuid;
120 }
121
122 /**
123  * UUID -> String 
124  */
125 char * getStringOfUuid(PTS_UUID *uuid) {
126     char *str_uuid;
127     uuid_t uu;
128
129     str_uuid = malloc(37);
130     if (str_uuid == NULL) {
131         ERROR("no memory\n");
132         return NULL;
133     }
134
135     memcpy(uu, uuid, 16);
136
137     uuid_unparse(uu, str_uuid);
138
139     return str_uuid;
140 }
141
142
143 /**
144  * get Time 
145  *
146 Linux
147 struct tm
148   int tm_sec;                    Seconds.     [0-60] (1 leap second)
149   int tm_min;                    Minutes.     [0-59] 
150   int tm_hour;                   Hours.       [0-23] 
151   int tm_mday;                   Day.         [1-31] 
152   int tm_mon;                    Month.       [0-11] 
153   int tm_year;                   Year - 1900.  
154   int tm_wday;                   Day of week. [0-6] 
155   int tm_yday;                   Days in year.[0-365] 
156   int tm_isdst;                  DST.         [-1/0/1]
157   long int __tm_gmtoff;          Seconds east of UTC.  
158   __const char *__tm_zone;       Timezone abbreviation.
159
160 PTS 
161 typedef struct {
162     PTS_UInt32 sec;     //
163     PTS_UInt32 min;             //
164     PTS_UInt32 hour;    //
165     PTS_UInt32 mday;    //
166     PTS_UInt32 mon;             //
167     PTS_UInt32 year;    //
168     PTS_UInt32 wday;    //
169     PTS_UInt32 yday;    //
170     PTS_Bool isDst;
171 } PTS_DateTime;
172
173  */
174 PTS_DateTime * getDateTimeOfUuid(PTS_UUID *uuid) {
175     uuid_t uu;
176     PTS_DateTime *pdt;
177     time_t t;
178     struct timeval tv;
179     struct tm time;
180
181     /* check */
182     if (uuid == NULL) {
183         ERROR("null input\n");
184         return NULL;
185     }
186
187     /* get time */
188     memcpy(uu, uuid, 16);
189     t = uuid_time(uu, &tv);
190     // TODO gmtime or local?
191     gmtime_r((const time_t *) &t, &time);
192
193     pdt = malloc(sizeof(PTS_DateTime));
194     if (pdt == NULL) {
195         ERROR("no memory\n");
196         return NULL;
197     }
198     memcpy(pdt, &time, (9*4));
199
200     return pdt;
201 }
202
203 /**
204  * get current time
205  */
206 PTS_DateTime * getDateTime() {
207     PTS_DateTime *pdt;
208     time_t t;
209     struct tm ttm;
210
211     /* get time */
212     time(&t);
213     // TODO gmtime or local?
214     gmtime_r((const time_t *) &t, &ttm);
215
216     pdt = malloc(sizeof(PTS_DateTime));
217     if (pdt == NULL) {
218         ERROR("no memory\n");
219         return NULL;
220     }
221     memcpy(pdt, &ttm, (9*4));
222
223     return pdt;
224 }
225
226