Versioned UUID framework to start
[distributed.git] / src / libs / net / uuid.h
blob8d2ff59c7913f67be5b9d682825b5b367064e40c
1 //
2 // Copyright (C) 2008 Francesco Salvestrini
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #ifndef LIBS_NET_UUID_H
20 #define LIBS_NET_UUID_H
22 #include "config.h"
24 #include <string>
25 #include "libs/misc/exception.h"
27 namespace Net {
29 class UUID {
30 public:
31 typedef enum {
32 UNKNOWN = 0,
33 IETF_RFC4122_TIMEBASED = 1,
34 DCE_SECURITY_POSIX_UID = 2,
35 IETF_RFC4122_NAMEBASED_MD5 = 3,
36 IETF_RFC4122_RANDOM = 4,
37 IETF_RFC4122_NAMEBASED_SHA1 = 5
38 } version_t;
40 UUID(void);
41 ~UUID(void);
43 UUID(const std::string & u);
44 UUID(version_t version);
46 void clear(void);
48 // Operators
49 operator std::string(void);
50 bool operator ==(const UUID & rhs);
51 UUID & operator =(const UUID & rhs);
52 friend std::ostream & operator <<(std::ostream & stream,
53 UUID obj);
54 friend std::istream & operator >>(std::istream & stream,
55 UUID & obj);
57 // Exceptions
58 class invalid_version : public Exception {
59 public:
60 version_t version;
62 invalid_version(version_t ver,
63 const std::string & description) :
64 Exception(description),
65 version(ver) {};
67 ~invalid_version(void) throw () {};
69 protected:
70 invalid_version(void);
71 private:
72 // nothing more for the time being
75 class invalid_hexfield : public Exception {
76 public:
77 uint8_t position;
79 invalid_hexfield(uint8_t pos,
80 const std::string & description) :
81 Exception(description),
82 position(pos)
83 {};
85 ~invalid_hexfield(void) throw () {};
87 protected:
88 invalid_hexfield(void);
89 private:
90 // nothing more for the time being
93 class formatting_problem : public Exception {
94 public:
95 version_t version;
97 formatting_problem(version_t ver,
98 const std::string & description) :
99 Exception(description),
100 version(ver)
103 ~formatting_problem(void) throw () {};
105 protected:
106 formatting_problem(void);
107 private:
108 // nothing more for the time being
111 class scrambling_problem : public Exception {
112 public:
113 version_t version;
115 scrambling_problem(version_t ver,
116 const std::string & description) :
117 Exception(description),
118 version(ver)
121 ~scrambling_problem(void) throw () {};
123 protected:
124 scrambling_problem(void);
125 private:
126 // nothing more for the time being
129 class generic_problem : public Exception {
130 public:
131 generic_problem(const std::string & description) :
132 Exception(description)
135 ~generic_problem(void) throw () {};
137 protected:
138 generic_problem(void);
139 private:
140 // nothing more for the time being
143 protected:
145 private:
146 // From RFC4122
148 // The fields are encoded as 16 octets, with the sizes and order of the
149 // fields defined above, and with each field encoded with the Most
150 // Significant Byte first (known as network byte order). Note that the
151 // field names, particularly for multiplexed fields, follow historical
152 // practice.
154 // 0 1 2 3
155 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
156 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
157 // | time_low |
158 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159 // | time_mid | time_hi_and_version |
160 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161 // |clk_seq_hi_res | clk_seq_low | node (0-1) |
162 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163 // | node (2-5) |
164 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
166 struct uuid {
167 uint32_t time_low;
168 uint16_t time_mid;
169 uint16_t time_hi_and_version;
170 uint16_t clock_seq;
171 uint8_t node[6];
172 } uuid_;
174 bool grab_4hex(const std::string & u,
175 std::string::size_type s,
176 std::string::size_type e,
177 uint16_t & v);
178 bool grab_8hex(const std::string & u,
179 std::string::size_type s,
180 std::string::size_type e,
181 uint32_t & v);
182 bool grab_12hex(const std::string & u,
183 std::string::size_type s,
184 std::string::size_type e,
185 uint8_t * v);
187 void format_uuid_tb(void);
188 void format_uuid_nb(void);
189 void format_uuid_random(void);
190 void scramble_sha1(void);
191 void scramble_md5(void);
195 #endif // LIBS_NET_UUID_H