OverSim
I3Identifier.cc
Go to the documentation of this file.
1 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 
24 #include "I3Identifier.h"
25 #include "SHA1.h"
26 
27 using namespace std;
28 
29 void I3Identifier::initKey(int prefixL, int keyL)
30 {
31  prefixLength = prefixL;
32  keyLength = keyL;
33  key = new unsigned char[keyLength / 8];
34 }
35 
37 {
39 }
40 
41 I3Identifier::I3Identifier(int prefixL, int keyL)
42 {
43  initKey(prefixL, keyL);
44 }
45 
47 {
49  *this = id;
50 }
51 
53 {
55  createFromHash(s);
56 }
57 
59 {
60  memset(key, 0, keyLength / 8);
61 }
62 
64 {
65  for (int i = 0; i < keyLength / 8; i++) {
66  if (key[i] != 0) return true;
67  }
68  return false;
69 }
70 
72 {
73  return prefixLength;
74 }
75 
77 {
78  return keyLength;
79 }
80 
82 {
83  return memcmp(key, id.key, keyLength / 8);
84 }
85 
87 {
88  return compareTo(id) < 0;
89 }
90 
92 {
93  return compareTo(id) > 0;
94 }
95 
97 {
98  return compareTo(id) == 0;
99 }
100 
102 {
103  memcpy(key, id.key, keyLength / 8);
104  name = id.name;
105  return *this;
106 }
107 
108 bool I3Identifier::isMatch(const I3Identifier &id) const
109 {
110  return memcmp(key, id.key, prefixLength / 8) == 0;
111 }
112 
114 {
115  int index;
116 
117  for (index = 0; index < keyLength; index++) {
118  if (key[index] != id.key[index]) break;
119  }
120 
121  return (keyLength - index) * 256 + (key[index] ^ id.key[index]);
122 }
123 
125 {
126  return OverlayKey(key, prefixLength / 8);
127 }
128 
129 void I3Identifier::createFromHash(const std::string &p, const std::string &o)
130 {
131  uint8_t temp[20];
132  CSHA1 sha1;
133  int size1, size2;
134 
135  sha1.Reset();
136  sha1.Update((uint8_t*)p.c_str(), p.size());
137  sha1.Final();
138  sha1.GetHash(temp);
139 
140  clear();
141  size1 = prefixLength / 8;
142  if (size1 > 20) size1 = 20;
143  memcpy(key, temp, size1);
144 
145  name = p + ":0";
146 
147  if (o.size() == 0) return;
148 
149  name = p + ":" + o;
150 
151  sha1.Reset();
152  sha1.Update((uint8_t*)o.c_str(), o.size());
153  sha1.Final();
154  sha1.GetHash(temp);
155 
156  clear();
157  size2 = (keyLength - prefixLength) / 8;
158  if (size2 > 20) size2 = 20;
159  memcpy(key + size1, temp, size2);
160 }
161 
163 {
164  createRandomPrefix();
165  createRandomSuffix();
166 }
167 
169 {
170  for (int i = 0; i < prefixLength / 8; i++) {
171  key[i] = intrand(256);
172  }
173 }
174 
176 {
177  for (int i = prefixLength / 8; i < keyLength / 8; i++) {
178  key[i] = intrand(256);
179  }
180 }
181 
182 int I3Identifier::length() const {
183  return 16 + keyLength;
184 }
185 
186 void I3Identifier::setName(std::string s) {
187  name = s;
188 }
189 
190 std::string I3Identifier::getName() {
191  return name;
192 }
193 
194 
195 std::ostream& operator<<(std::ostream& os, const I3Identifier& id)
196 {
197  bool allzeros;
198  const char hex[] = "0123456789abcdef";
199  string s0, s1;
200 
201  if (id.name.length() != 0) {
202  os << "(" << id.name << ") ";
203  }
204 
205  for (int i = 0; i < id.prefixLength / 8; i++) {
206  os << hex[id.key[i] >> 4];
207  os << hex[id.key[i] & 0xf];
208  }
209  os << ':';
210 
211  allzeros = true;
212  for (int i = id.prefixLength / 8; i < id.keyLength / 8; i++) {
213  if (id.key[i] != 0) {
214  allzeros = false;
215  break;
216  }
217  }
218  if (allzeros) {
219  os << "0...";
220  } else {
221  for (int i = id.prefixLength / 8; i < id.keyLength / 8; i++) {
222  os << hex[id.key[i] >> 4];
223  os << hex[id.key[i] & 0xf];
224  }
225  }
226  return os;
227 }
228 
229 
231 {
232  if (key == 0) {
233  cout << "Warning: key already deleted." << endl;
234  }
235  delete[] key;
236  key = 0;
237 }