aboutsummaryrefslogtreecommitdiff
path: root/src/gns/gnunet-gns.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-09-20 10:10:04 +0000
committerChristian Grothoff <christian@grothoff.org>2012-09-20 10:10:04 +0000
commit1aa42a97fb4310dd72a31aad114b6ebe26aebd8a (patch)
treeed076a8a4dd413efa4cbd67ec6482834b7ebc581 /src/gns/gnunet-gns.c
parent37c4704049df39026b5fb043bcebcd7a15ecf1a2 (diff)
downloadgnunet-1aa42a97fb4310dd72a31aad114b6ebe26aebd8a.tar.gz
gnunet-1aa42a97fb4310dd72a31aad114b6ebe26aebd8a.zip
-core review
Diffstat (limited to 'src/gns/gnunet-gns.c')
-rw-r--r--src/gns/gnunet-gns.c211
1 files changed, 81 insertions, 130 deletions
diff --git a/src/gns/gnunet-gns.c b/src/gns/gnunet-gns.c
index 0524b3def..f06bf0d7b 100644
--- a/src/gns/gnunet-gns.c
+++ b/src/gns/gnunet-gns.c
@@ -58,7 +58,7 @@ static char *auth_name;
58/** 58/**
59 * raw output 59 * raw output
60 */ 60 */
61static int raw = 0; 61static int raw;
62 62
63static enum GNUNET_GNS_RecordType rtype; 63static enum GNUNET_GNS_RecordType rtype;
64 64
@@ -71,8 +71,6 @@ static struct GNUNET_GNS_ShortenRequest *shorten_request;
71/* Handle to get authority request */ 71/* Handle to get authority request */
72static struct GNUNET_GNS_GetAuthRequest *getauth_request; 72static struct GNUNET_GNS_GetAuthRequest *getauth_request;
73 73
74/* shutdown task */
75static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
76 74
77/** 75/**
78 * Task run on shutdown. Cleans up everything. 76 * Task run on shutdown. Cleans up everything.
@@ -99,61 +97,61 @@ do_shutdown (void *cls,
99 97
100 98
101static void 99static void
102process_shorten_result(void* cls, const char* nshort) 100process_shorten_result (void* cls, const char* nshort)
103{ 101{
102 const char *original_name = cls;
103
104 shorten_request = NULL; 104 shorten_request = NULL;
105 if (raw) 105 if (raw)
106 printf("%s", nshort); 106 printf("%s", nshort);
107 else 107 else
108 printf("%s shortened to %s\n", (char*) cls, nshort); 108 printf("%s shortened to %s\n", original_name, nshort);
109 GNUNET_SCHEDULER_cancel (shutdown_task); 109 GNUNET_SCHEDULER_shutdown ();
110 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
111} 110}
112 111
112
113static void 113static void
114process_lookup_result(void* cls, uint32_t rd_count, 114process_lookup_result (void* cls, uint32_t rd_count,
115 const struct GNUNET_NAMESTORE_RecordData *rd) 115 const struct GNUNET_NAMESTORE_RecordData *rd)
116{ 116{
117 int i; 117 const char* name = cls;
118 char* name = (char*) cls; 118 uint32_t i;
119 const char* typename; 119 const char* typename;
120 char* string_val; 120 char* string_val;
121 lookup_request = NULL; 121
122 122 lookup_request = NULL;
123 if (!raw) { 123 if (!raw)
124 {
124 if (rd_count == 0) 125 if (rd_count == 0)
125 printf("No results.\n"); 126 printf("No results.\n");
126 else 127 else
127 printf("%s:\n", name); 128 printf("%s:\n", name);
128 } 129 }
129
130
131
132 for (i=0; i<rd_count; i++) 130 for (i=0; i<rd_count; i++)
133 { 131 {
134 typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type); 132 typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
135 string_val = GNUNET_NAMESTORE_value_to_string(rd[i].record_type, 133 string_val = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
136 rd[i].data, 134 rd[i].data,
137 rd[i].data_size); 135 rd[i].data_size);
138 if (raw) 136 if (raw)
139 printf("%s\n", string_val); 137 printf ("%s\n", string_val);
140 else 138 else
141 printf("Got %s record: %s\n", typename, string_val); 139 printf ("Got `%s' record: %s\n", typename, string_val);
142 140 GNUNET_free_non_null (string_val);
143 } 141 }
144 GNUNET_SCHEDULER_cancel (shutdown_task); 142 GNUNET_SCHEDULER_shutdown ();
145 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
146} 143}
147 144
145
148static void 146static void
149process_auth_result(void* cls, const char* auth) 147process_auth_result (void* cls, const char* auth)
150{ 148{
151 getauth_request = NULL; 149 getauth_request = NULL;
152 printf ("%s\n", auth); 150 printf ("%s\n", auth);
153 GNUNET_SCHEDULER_cancel (shutdown_task); 151 GNUNET_SCHEDULER_shutdown ();
154 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
155} 152}
156 153
154
157/** 155/**
158 * Main function that will be run. 156 * Main function that will be run.
159 * 157 *
@@ -177,124 +175,84 @@ run (void *cls, char *const *args, const char *cfgfile,
177 struct GNUNET_CRYPTO_ShortHashCode *private_zone = NULL; 175 struct GNUNET_CRYPTO_ShortHashCode *private_zone = NULL;
178 struct GNUNET_CRYPTO_ShortHashCode *shorten_zone = NULL; 176 struct GNUNET_CRYPTO_ShortHashCode *shorten_zone = NULL;
179 177
180 shorten_request = NULL; 178 gns = GNUNET_GNS_connect (cfg);
181 lookup_request = NULL; 179 if (NULL == gns)
182 getauth_request = NULL; 180 {
183 181 fprintf (stderr,
182 _("Failed to connect to GNS\n"));
183 return;
184 }
184 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns", 185 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
185 "ZONEKEY", &keyfile)) 186 "ZONEKEY", &keyfile))
186 { 187 {
187 if (!raw) 188 if (! raw)
188 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 189 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
189 "No private key for root zone found, using default!\n"); 190 "No private key for root zone found, using default!\n");
190 zone = NULL;
191 } 191 }
192 else 192 else
193 { 193 {
194 if (GNUNET_YES == GNUNET_DISK_file_test (keyfile)) 194 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
195 { 195 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
196 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile); 196 GNUNET_CRYPTO_short_hash (&pkey,
197 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey); 197 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
198 GNUNET_CRYPTO_short_hash(&pkey, 198 &user_zone);
199 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), 199 zone = &user_zone;
200 &user_zone); 200 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
201 zone = &user_zone; 201 if (!raw)
202 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename); 202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 if (!raw) 203 "Using zone: %s!\n", &zonename);
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 204 GNUNET_CRYPTO_rsa_key_free (key);
205 "Using zone: %s!\n", &zonename);
206 GNUNET_CRYPTO_rsa_key_free(key);
207 }
208 GNUNET_free(keyfile); 205 GNUNET_free(keyfile);
209 } 206 }
210 207
211 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns", 208 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
212 "SHORTEN_ZONEKEY", &keyfile)) 209 "SHORTEN_ZONEKEY", &keyfile))
213 { 210 {
214 if (!raw) 211 if (! raw)
215 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 212 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
216 "No shorten key found!\n"); 213 "No shorten key found!\n");
217 shorten_key = NULL;
218 } 214 }
219 else 215 else
220 { 216 {
221 if (GNUNET_YES == GNUNET_DISK_file_test (keyfile)) 217 shorten_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
222 { 218 GNUNET_CRYPTO_rsa_key_get_public (shorten_key, &pkey);
223 shorten_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile); 219 shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
224 GNUNET_CRYPTO_rsa_key_get_public (shorten_key, &pkey); 220 GNUNET_CRYPTO_short_hash(&pkey,
225 shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode)); 221 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
226 GNUNET_CRYPTO_short_hash(&pkey, 222 shorten_zone);
227 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), 223 GNUNET_CRYPTO_short_hash_to_enc (shorten_zone, &zonename);
228 shorten_zone); 224 if (! raw)
229 GNUNET_CRYPTO_short_hash_to_enc (shorten_zone, &zonename); 225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 if (!raw) 226 "Using shorten zone: %s!\n", &zonename);
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 227 GNUNET_free (keyfile);
232 "Using shorten zone: %s!\n", &zonename);
233
234 }
235 GNUNET_free(keyfile);
236 } 228 }
237 229
238
239 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns", 230 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
240 "PRIVATE_ZONEKEY", &keyfile)) 231 "PRIVATE_ZONEKEY", &keyfile))
241 { 232 {
242 if (!raw) 233 if (! raw)
243 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
244 "No private zone key file name specified in configuration!\n"); 235 "No private zone key file name specified in configuration!\n");
245 private_key = NULL;
246 } 236 }
247 else 237 else
248 { 238 {
249 if (GNUNET_YES == GNUNET_DISK_file_test (keyfile)) 239 private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
250 { 240 GNUNET_CRYPTO_rsa_key_get_public (private_key, &pkey);
251 private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile); 241 private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
252 GNUNET_CRYPTO_rsa_key_get_public (private_key, &pkey); 242 GNUNET_CRYPTO_short_hash(&pkey,
253 private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode)); 243 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
254 GNUNET_CRYPTO_short_hash(&pkey, 244 private_zone);
255 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), 245 GNUNET_CRYPTO_short_hash_to_enc (private_zone, &zonename);
256 private_zone); 246 if (! raw)
257 GNUNET_CRYPTO_short_hash_to_enc (private_zone, &zonename); 247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258 if (!raw) 248 "Using private zone: %s!\n", &zonename);
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Using private zone: %s!\n", &zonename);
261 }
262 else
263 {
264 /* FIXME: shouldn't we just create the private key in this case? */
265 if (!raw)
266 fprintf (stderr,
267 _("Key file `%s' for private zone does not exist!\n"),
268 keyfile);
269
270 }
271 GNUNET_free(keyfile);
272 if (NULL != private_key)
273 {
274 GNUNET_CRYPTO_rsa_key_free (private_key);
275 private_key = NULL;
276 }
277 } 249 }
278 250
279
280 gns = GNUNET_GNS_connect (cfg);
281 if (NULL != lookup_type) 251 if (NULL != lookup_type)
282 rtype = GNUNET_NAMESTORE_typename_to_number (lookup_type); 252 rtype = GNUNET_NAMESTORE_typename_to_number (lookup_type);
283 else 253 else
284 rtype = GNUNET_GNS_RECORD_A; 254 rtype = GNUNET_GNS_RECORD_A;
285 255
286 if (NULL == gns)
287 {
288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
289 _("Failed to connect to GNS\n"));
290 if (NULL != shorten_key)
291 {
292 GNUNET_CRYPTO_rsa_key_free (shorten_key);
293 shorten_key = NULL;
294 }
295 return;
296 }
297
298 if ((NULL != shorten_name) && (NULL != shorten_zone) && (NULL != private_zone)) 256 if ((NULL != shorten_name) && (NULL != shorten_zone) && (NULL != private_zone))
299 { 257 {
300 shorten_request = GNUNET_GNS_shorten_zone (gns, shorten_name, 258 shorten_request = GNUNET_GNS_shorten_zone (gns, shorten_name,
@@ -304,31 +262,25 @@ run (void *cls, char *const *args, const char *cfgfile,
304 &process_shorten_result, 262 &process_shorten_result,
305 shorten_name); 263 shorten_name);
306 } 264 }
307
308 if (NULL != lookup_name) 265 if (NULL != lookup_name)
309 { 266 {
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "Lookup\n");
312 lookup_request = GNUNET_GNS_lookup_zone (gns, lookup_name, 267 lookup_request = GNUNET_GNS_lookup_zone (gns, lookup_name,
313 zone, 268 zone,
314 rtype, 269 rtype,
315 GNUNET_NO, //Use DHT 270 GNUNET_NO, /* Use DHT */
316 shorten_key, 271 shorten_key,
317 &process_lookup_result, lookup_name); 272 &process_lookup_result, lookup_name);
318 } 273 }
319
320 if (NULL != auth_name) 274 if (NULL != auth_name)
321 { 275 {
322 getauth_request = GNUNET_GNS_get_authority(gns, auth_name, 276 getauth_request = GNUNET_GNS_get_authority (gns, auth_name,
323 &process_auth_result, auth_name); 277 &process_auth_result, auth_name);
324 } 278 }
325 279
326 if (NULL != shorten_key) 280 if (NULL != shorten_key)
327 GNUNET_CRYPTO_rsa_key_free (shorten_key); 281 GNUNET_CRYPTO_rsa_key_free (shorten_key);
328
329 if (NULL != shorten_zone) 282 if (NULL != shorten_zone)
330 GNUNET_free (shorten_zone); 283 GNUNET_free (shorten_zone);
331
332 if (NULL != private_zone) 284 if (NULL != private_zone)
333 GNUNET_free (private_zone); 285 GNUNET_free (private_zone);
334 286
@@ -336,15 +288,14 @@ run (void *cls, char *const *args, const char *cfgfile,
336 (NULL == shorten_name) && 288 (NULL == shorten_name) &&
337 (NULL == lookup_name)) 289 (NULL == lookup_name))
338 { 290 {
339 if (!raw) 291 fprintf (stderr,
340 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 292 _("Please specify lookup, shorten or authority operation!\n"));
341 "Please specify lookup, shorten or authority operation!\n");
342 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); 293 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
343 return; 294 return;
344 } 295 }
345 296
346 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 297 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
347 &do_shutdown, NULL); 298 &do_shutdown, NULL);
348} 299}
349 300
350 301