2 This file is part of GNUnet.
3 (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @brief Parses and produces uri strings.
24 * @author Igor Wronsky, Christian Grothoff
26 * GNUnet URIs are of the general form "gnunet://MODULE/IDENTIFIER".
27 * The specific structure of "IDENTIFIER" depends on the module and
28 * maybe differenciated into additional subcategories if applicable.
29 * This module only deals with fs identifiers (MODULE = "fs").
32 * This module only parses URIs for the AFS module. The FS URIs fall
33 * into four categories, "chk", "sks", "ksk" and "loc". The first three
34 * categories were named in analogy (!) to Freenet, but they do NOT
35 * work in exactly the same way. They are very similar from the user's
36 * point of view (unique file identifier, subspace, keyword), but the
37 * implementation is rather different in pretty much every detail.
38 * The concrete URI formats are:
42 * First, there are URIs that identify a file. They have the format
43 * "gnunet://fs/chk/HEX1.HEX2.SIZE". These URIs can be used to
44 * download the file. The description, filename, mime-type and other
45 * meta-data is NOT part of the file-URI since a URI uniquely
46 * identifies a resource (and the contents of the file would be the
47 * same even if it had a different description).
51 * The second category identifies entries in a namespace. The format
52 * is "gnunet://fs/sks/NAMESPACE/IDENTIFIER" where the namespace
53 * should be given in HEX. Applications may allow using a nickname
54 * for the namespace if the nickname is not ambiguous. The identifier
55 * can be either an ASCII sequence or a HEX-encoding. If the
56 * identifier is in ASCII but the format is ambiguous and could denote
57 * a HEX-string a "/" is appended to indicate ASCII encoding.
61 * The third category identifies ordinary searches. The format is
62 * "gnunet://fs/ksk/KEYWORD[+KEYWORD]*". Using the "+" syntax
63 * it is possible to encode searches with the boolean "AND" operator.
64 * "+" is used since it indicates a commutative 'and' operation and
65 * is unlikely to be used in a keyword by itself.
69 * The last category identifies a datum on a specific machine. The
70 * format is "gnunet://fs/loc/HEX1.HEX2.SIZE.PEER.SIG.EXPTIME". PEER is
71 * the BinName of the public key of the peer storing the datum. The
72 * signature (SIG) certifies that this peer has this content.
73 * HEX1, HEX2 and SIZE correspond to a 'chk' URI.
77 * The encoding for hexadecimal values is defined in the hashing.c
78 * module in the gnunetutil library and discussed there.
82 #include "gnunet_fs_service.h"
83 #include "gnunet_signatures.h"
88 * Get a unique key from a URI. This is for putting URIs
89 * into HashMaps. The key may change between FS implementations.
91 * @param uri uri to convert to a unique key
92 * @param key wherer to store the unique key
95 GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri,
96 GNUNET_HashCode * key)
101 *key = uri->data.chk.chk.query;
104 GNUNET_CRYPTO_hash (uri->data.sks.identifier,
105 strlen (uri->data.sks.identifier), key);
108 if (uri->data.ksk.keywordCount > 0)
109 GNUNET_CRYPTO_hash (uri->data.ksk.keywords[0],
110 strlen (uri->data.ksk.keywords[0]), key);
113 GNUNET_CRYPTO_hash (&uri->data.loc.fi,
114 sizeof (struct FileIdentifier) +
115 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), key);
118 memset (key, 0, sizeof (GNUNET_HashCode));
125 * Convert keyword URI to a human readable format
126 * (i.e. the search query that was used in the first place)
128 * @param uri ksk uri to convert to a string
129 * @return string with the keywords
132 GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri)
139 unsigned int keywordCount;
141 if ((uri == NULL) || (uri->type != ksk))
146 keywords = uri->data.ksk.keywords;
147 keywordCount = uri->data.ksk.keywordCount;
148 n = keywordCount + 1;
149 for (i = 0; i < keywordCount; i++)
151 keyword = keywords[i];
152 n += strlen (keyword) - 1;
153 if (NULL != strstr (&keyword[1], " "))
155 if (keyword[0] == '+')
158 ret = GNUNET_malloc (n);
160 for (i = 0; i < keywordCount; i++)
162 keyword = keywords[i];
163 if (NULL != strstr (&keyword[1], " "))
166 if (keyword[0] == '+')
167 strcat (ret, keyword);
169 strcat (ret, &keyword[1]);
174 if (keyword[0] == '+')
175 strcat (ret, keyword);
177 strcat (ret, &keyword[1]);
186 * Given a keyword with %-encoding (and possibly quotes to protect
187 * spaces), return a copy of the keyword without %-encoding and
188 * without double-quotes (%22). Also, add a space at the beginning
189 * if there is not a '+'.
191 * @param in string with %-encoding
192 * @param emsg where to store the parser error message (if any)
193 * @return decodded string with leading space (or preserved plus)
196 percent_decode_keyword (const char *in, char **emsg)
204 out = GNUNET_strdup (in);
207 while (out[rpos] != '\0')
209 if (out[rpos] == '%')
211 if (1 != sscanf (&out[rpos + 1], "%2X", &hx))
214 *emsg = GNUNET_strdup (_("`%' must be followed by HEX number"));
219 continue; /* skip double quote */
220 out[wpos++] = (char) hx;
224 out[wpos++] = out[rpos++];
230 ret = GNUNET_strdup (out);
234 /* need to prefix with space */
235 ret = GNUNET_malloc (strlen (out) + 2);
243 #define GNUNET_FS_URI_KSK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX
248 * @param s an uri string
249 * @param emsg where to store the parser error message (if any)
250 * @return NULL on error, otherwise the KSK URI
252 static struct GNUNET_FS_Uri *
253 uri_ksk_parse (const char *s, char **emsg)
255 struct GNUNET_FS_Uri *ret;
265 GNUNET_assert (s != NULL);
267 pos = strlen (GNUNET_FS_URI_KSK_PREFIX);
268 if ( (slen <= pos) ||
269 (0 != strncmp (s, GNUNET_FS_URI_KSK_PREFIX,
271 return NULL; /* not KSK URI */
272 if ( (s[slen - 1] == '+') ||
275 *emsg = GNUNET_strdup (_("Malformed KSK URI (must not begin or end with `+')"));
280 for (i = pos; i < slen; i++)
282 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
284 saw_quote = (saw_quote + 1) % 2;
288 if ((s[i] == '+') && (saw_quote == 0))
293 *emsg = GNUNET_strdup (_("`++' not allowed in KSK URI"));
300 *emsg = GNUNET_strdup (_("Quotes not balanced in KSK URI"));
304 dup = GNUNET_strdup (s);
305 keywords = GNUNET_malloc (max * sizeof (char *));
306 for (i = slen - 1; i >= pos; i--)
308 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
310 saw_quote = (saw_quote + 1) % 2;
314 if ((dup[i] == '+') && (saw_quote == 0))
316 keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg);
317 if (NULL == keywords[max])
322 keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
323 if (NULL == keywords[max])
325 GNUNET_assert (max == 0);
327 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
329 ret->data.ksk.keywordCount = iret;
330 ret->data.ksk.keywords = keywords;
333 for (i = 0; i < max; i++)
334 GNUNET_free_non_null (keywords[i]);
335 GNUNET_free (keywords);
341 #define GNUNET_FS_URI_SKS_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX
346 * @param s an uri string
347 * @param emsg where to store the parser error message (if any)
348 * @return NULL on error, SKS URI otherwise
350 static struct GNUNET_FS_Uri *
351 uri_sks_parse (const char *s, char **emsg)
353 struct GNUNET_FS_Uri *ret;
354 GNUNET_HashCode namespace;
358 char enc[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
360 GNUNET_assert (s != NULL);
362 pos = strlen ( GNUNET_FS_URI_SKS_PREFIX);
363 if ( (slen <= pos) ||
364 (0 != strncmp (s, GNUNET_FS_URI_SKS_PREFIX,
366 return NULL; /* not an SKS URI */
367 if ( (slen < pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) ||
368 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '/') )
370 *emsg = GNUNET_strdup (_("Malformed SKS URI"));
373 memcpy (enc, &s[pos], sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
374 enc[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
375 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (enc, &namespace))
377 *emsg = GNUNET_strdup (_("Malformed SKS URI"));
380 identifier = GNUNET_strdup (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]);
381 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
383 ret->data.sks.namespace = namespace;
384 ret->data.sks.identifier = identifier;
388 #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
394 * @param s an uri string
395 * @param emsg where to store the parser error message (if any)
396 * @return NULL on error, CHK URI otherwise
398 static struct GNUNET_FS_Uri *
399 uri_chk_parse (const char *s, char **emsg)
401 struct GNUNET_FS_Uri *ret;
402 struct FileIdentifier fi;
404 unsigned long long flen;
406 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
407 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
409 GNUNET_assert (s != NULL);
412 pos = strlen (GNUNET_FS_URI_CHK_PREFIX);
413 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
414 (0 != strncmp (s, GNUNET_FS_URI_CHK_PREFIX,
416 return NULL; /* not a CHK URI */
417 if ( (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
418 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
420 *emsg = GNUNET_strdup (_("Malformed CHK URI"));
425 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
426 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
428 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
429 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
430 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
432 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
434 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
436 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
440 *emsg = GNUNET_strdup (_("Malformed CHK URI"));
443 fi.file_length = GNUNET_htonll (flen);
444 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
452 * Convert a character back to the binary value
453 * that it represents (given base64-encoding).
455 * @param a character to convert
456 * @return offset in the "tbl" array
459 c2v (unsigned char a)
461 if ((a >= '0') && (a <= '9'))
463 if ((a >= 'A') && (a <= 'Z'))
464 return (a - 'A' + 10);
465 if ((a >= 'a') && (a <= 'z'))
466 return (a - 'a' + 36);
476 * Convert string back to binary data.
478 * @param input '\\0'-terminated string
479 * @param data where to write binary data
480 * @param size how much data should be converted
481 * @return number of characters processed from input,
485 enc2bin (const char *input, void *data, size_t size)
493 if (((size * 8) % 6) != 0)
495 if (strlen (input) < len)
496 return -1; /* error! */
500 for (pos = 0; pos < size; pos++)
504 bits |= (c2v (input[len++]) << hbits);
507 (((unsigned char *) data)[pos]) = (unsigned char) bits;
516 * Structure that defines how the
517 * contents of a location URI must be
518 * assembled in memory to create or
519 * verify the signature of a location
522 struct LocUriAssembly
524 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
526 struct GNUNET_TIME_AbsoluteNBO exptime;
528 struct FileIdentifier fi;
530 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded peer;
535 #define GNUNET_FS_URI_LOC_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX
539 * Also verifies validity of the location URI.
541 * @param s an uri string
542 * @param emsg where to store the parser error message (if any)
543 * @return NULL on error, valid LOC URI otherwise
545 static struct GNUNET_FS_Uri *
546 uri_loc_parse (const char *s, char **emsg)
548 struct GNUNET_FS_Uri *uri;
549 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
550 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
553 unsigned long long exptime;
554 unsigned long long flen;
555 struct GNUNET_TIME_Absolute et;
556 struct GNUNET_CRYPTO_RsaSignature sig;
557 struct LocUriAssembly ass;
561 GNUNET_assert (s != NULL);
563 pos = strlen ( GNUNET_FS_URI_LOC_PREFIX);
564 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
565 (0 != strncmp (s, GNUNET_FS_URI_LOC_PREFIX,
567 return NULL; /* not an SKS URI */
568 if ( (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
569 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
571 *emsg = GNUNET_strdup (_("SKS URI malformed"));
576 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
577 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
579 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
580 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
581 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
583 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
585 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
586 &ass.fi.chk.query)) ||
587 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
591 *emsg = GNUNET_strdup (_("SKS URI malformed"));
594 ass.fi.file_length = GNUNET_htonll (flen);
596 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
597 while ((s[npos] != '\0') && (s[npos] != '.'))
601 *emsg = GNUNET_strdup (_("SKS URI malformed"));
605 ret = enc2bin (&s[npos],
607 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
610 *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode public key)"));
614 if (s[npos++] != '.')
616 *emsg = GNUNET_strdup (_("SKS URI malformed (could not find signature)"));
619 ret = enc2bin (&s[npos],
621 sizeof (struct GNUNET_CRYPTO_RsaSignature));
624 *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode signature)"));
628 if (s[npos++] != '.')
630 *emsg = GNUNET_strdup (_("SKS URI malformed"));
633 if (1 != SSCANF (&s[npos], "%llu", &exptime))
635 *emsg = GNUNET_strdup (_("SKS URI malformed (could not parse expiration time)"));
638 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
639 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
640 et.abs_value = exptime;
641 ass.exptime = GNUNET_TIME_absolute_hton (et);
643 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT,
648 *emsg = GNUNET_strdup (_("SKS URI malformed (signature failed validation)"));
651 uri = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
653 uri->data.loc.fi = ass.fi;
654 uri->data.loc.peer = ass.peer;
655 uri->data.loc.expirationTime = et;
656 uri->data.loc.contentSignature = sig;
665 * Convert a UTF-8 String to a URI.
667 * @param uri string to parse
668 * @param emsg where to store the parser error message (if any)
669 * @return NULL on error
671 struct GNUNET_FS_Uri *
672 GNUNET_FS_uri_parse (const char *uri,
675 struct GNUNET_FS_Uri *ret;
681 if ( (NULL != (ret = uri_chk_parse (uri, emsg))) ||
682 (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
683 (NULL != (ret = uri_sks_parse (uri, emsg))) ||
684 (NULL != (ret = uri_loc_parse (uri, emsg))) )
687 *emsg = GNUNET_strdup (_("Unrecognized URI type"));
697 * @param uri uri to free
700 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
704 GNUNET_assert (uri != NULL);
708 for (i = 0; i < uri->data.ksk.keywordCount; i++)
709 GNUNET_free (uri->data.ksk.keywords[i]);
710 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
714 GNUNET_free (uri->data.sks.identifier);
726 * How many keywords are ANDed in this keyword URI?
728 * @param uri ksk uri to get the number of keywords from
729 * @return 0 if this is not a keyword URI
732 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
734 if (uri->type != ksk)
736 return uri->data.ksk.keywordCount;
741 * Iterate over all keywords in this keyword URI.
743 * @param uri ksk uri to get the keywords from
744 * @param iterator function to call on each keyword
745 * @param iterator_cls closure for iterator
746 * @return -1 if this is not a keyword URI, otherwise number of
747 * keywords iterated over until iterator aborted
750 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
751 GNUNET_FS_KeywordIterator iterator,
757 if (uri->type != ksk)
759 if (iterator == NULL)
760 return uri->data.ksk.keywordCount;
761 for (i = 0; i < uri->data.ksk.keywordCount; i++)
763 keyword = uri->data.ksk.keywords[i];
764 /* first character of keyword indicates
765 if it is mandatory or not */
766 if (GNUNET_OK != iterator (iterator_cls,
776 * Add the given keyword to the set of keywords represented by the URI.
777 * Does nothing if the keyword is already present.
779 * @param uri ksk uri to modify
780 * @param keyword keyword to add
781 * @param is_mandatory is this keyword mandatory?
784 GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri,
792 GNUNET_assert (uri->type == ksk);
793 for (i = 0; i < uri->data.ksk.keywordCount; i++)
795 old = uri->data.ksk.keywords[i];
796 if (0 == strcmp (&old[1], keyword))
800 is_mandatory ? "+%s" : " %s",
802 GNUNET_array_append (uri->data.ksk.keywords,
803 uri->data.ksk.keywordCount,
809 * Remove the given keyword from the set of keywords represented by the URI.
810 * Does nothing if the keyword is not present.
812 * @param uri ksk uri to modify
813 * @param keyword keyword to add
816 GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri,
822 GNUNET_assert (uri->type == ksk);
823 for (i = 0; i < uri->data.ksk.keywordCount; i++)
825 old = uri->data.ksk.keywords[i];
826 if (0 == strcmp (&old[1], keyword))
828 uri->data.ksk.keywords[i] = uri->data.ksk.keywords[uri->data.ksk.keywordCount-1];
829 GNUNET_array_grow (uri->data.ksk.keywords,
830 uri->data.ksk.keywordCount,
831 uri->data.ksk.keywordCount - 1);
840 * Obtain the identity of the peer offering the data
842 * @param uri the location URI to inspect
843 * @param peer where to store the identify of the peer (presumably) offering the content
844 * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
847 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
848 struct GNUNET_PeerIdentity * peer)
850 if (uri->type != loc)
851 return GNUNET_SYSERR;
852 GNUNET_CRYPTO_hash (&uri->data.loc.peer,
853 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
860 * Obtain the expiration of the LOC URI.
862 * @param uri location URI to get the expiration from
863 * @return expiration time of the URI
865 struct GNUNET_TIME_Absolute
866 GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri)
868 GNUNET_assert (uri->type == loc);
869 return uri->data.loc.expirationTime;
875 * Obtain the URI of the content itself.
877 * @param uri location URI to get the content URI from
878 * @return NULL if argument is not a location URI
880 struct GNUNET_FS_Uri *
881 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
883 struct GNUNET_FS_Uri *ret;
885 if (uri->type != loc)
887 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
889 ret->data.chk = uri->data.loc.fi;
895 * Construct a location URI (this peer will be used for the location).
897 * @param baseUri content offered by the sender
898 * @param cfg configuration information (used to find our hostkey)
899 * @param expiration_time how long will the content be offered?
900 * @return the location URI, NULL on error
902 struct GNUNET_FS_Uri *
903 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
904 const struct GNUNET_CONFIGURATION_Handle *cfg,
905 struct GNUNET_TIME_Absolute expiration_time)
907 struct GNUNET_FS_Uri *uri;
908 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
909 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
911 struct LocUriAssembly ass;
913 if (baseUri->type != chk)
916 GNUNET_CONFIGURATION_get_value_filename (cfg,
918 "HOSTKEY", &keyfile))
920 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
922 ("Lacking key configuration settings.\n"));
925 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
926 if (my_private_key == NULL)
928 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
929 _("Could not access hostkey file `%s'.\n"),
931 GNUNET_free (keyfile);
934 GNUNET_free (keyfile);
935 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
936 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
937 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
938 ass.exptime = GNUNET_TIME_absolute_hton (expiration_time);
939 ass.fi = baseUri->data.chk;
940 ass.peer = my_public_key;
941 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
943 uri->data.loc.fi = baseUri->data.chk;
944 uri->data.loc.expirationTime = expiration_time;
945 uri->data.loc.peer = my_public_key;
946 GNUNET_assert (GNUNET_OK ==
947 GNUNET_CRYPTO_rsa_sign (my_private_key,
949 &uri->data.loc.contentSignature));
950 GNUNET_CRYPTO_rsa_key_free (my_private_key);
956 * Create an SKS URI from a namespace and an identifier.
958 * @param ns namespace
959 * @param id identifier
960 * @param emsg where to store an error message
961 * @return an FS URI for the given namespace and identifier
963 struct GNUNET_FS_Uri *
964 GNUNET_FS_uri_sks_create (struct GNUNET_FS_Namespace *ns,
968 struct GNUNET_FS_Uri *ns_uri;
969 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
971 ns_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
973 GNUNET_CRYPTO_rsa_key_get_public (ns->key,
975 GNUNET_CRYPTO_hash (&pk,
977 &ns_uri->data.sks.namespace);
978 ns_uri->data.sks.identifier = GNUNET_strdup (id);
984 * Create an SKS URI from a namespace ID and an identifier.
986 * @param nsid namespace ID
987 * @param id identifier
988 * @return an FS URI for the given namespace and identifier
990 struct GNUNET_FS_Uri *
991 GNUNET_FS_uri_sks_create_from_nsid (GNUNET_HashCode *nsid,
994 struct GNUNET_FS_Uri *ns_uri;
996 ns_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
998 ns_uri->data.sks.namespace = *nsid;
999 ns_uri->data.sks.identifier = GNUNET_strdup (id);
1005 * Canonicalize a keyword.
1007 * @param in input string (the keyword)
1008 * @return canonicalized keyword
1011 canonicalize_keyword (const char *in)
1017 ret = GNUNET_strdup (in);
1020 while ('\0' != *rpos)
1022 switch (tolower( (unsigned char) *rpos))
1033 /* skip characters listed above */
1055 /* convert characters listed above to lower case */
1056 *wpos = tolower( (unsigned char)*rpos);
1063 /* keep characters listed above without changes */
1068 /* replace characters listed above with '_' */
1080 * Canonicalize keyword URI. Performs operations such
1081 * as decapitalization and removal of certain characters.
1082 * (useful for search).
1084 * @param uri the URI to canonicalize
1085 * @return canonicalized version of the URI, NULL on error
1087 struct GNUNET_FS_Uri *
1088 GNUNET_FS_uri_ksk_canonicalize (const struct GNUNET_FS_Uri *uri)
1090 struct GNUNET_FS_Uri *ret;
1095 kc = uri->data.ksk.keywordCount;
1096 kl = GNUNET_malloc (kc*sizeof(char*));
1098 kl[i] = canonicalize_keyword (uri->data.ksk.keywords[i]);
1099 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
1101 ret->data.ksk.keywordCount = kc;
1102 ret->data.ksk.keywords = kl;
1108 * Merge the sets of keywords from two KSK URIs.
1109 * (useful for merging the canonicalized keywords with
1110 * the original keywords for sharing).
1112 * @param u1 first uri
1113 * @param u2 second uri
1114 * @return merged URI, NULL on error
1116 struct GNUNET_FS_Uri *
1117 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
1118 const struct GNUNET_FS_Uri *u2)
1120 struct GNUNET_FS_Uri *ret;
1128 if ( (u1 == NULL) && (u2 == NULL) )
1131 return GNUNET_FS_uri_dup (u2);
1133 return GNUNET_FS_uri_dup (u1);
1134 if ( (u1->type != ksk) ||
1140 kc = u1->data.ksk.keywordCount;
1141 kl = GNUNET_malloc ((kc+u2->data.ksk.keywordCount)*sizeof(char*));
1142 for (i=0;i<u1->data.ksk.keywordCount;i++)
1143 kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]);
1144 for (i=0;i<u2->data.ksk.keywordCount;i++)
1146 kp = u2->data.ksk.keywords[i];
1148 for (j=0;j<u1->data.ksk.keywordCount;j++)
1149 if (0 == strcmp(kp + 1,
1158 kl[kc++] = GNUNET_strdup (kp);
1160 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
1162 ret->data.ksk.keywordCount = kc;
1163 ret->data.ksk.keywords = kl;
1171 * @param uri the URI to duplicate
1172 * @return copy of the URI
1174 struct GNUNET_FS_Uri *
1175 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
1177 struct GNUNET_FS_Uri *ret;
1182 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1183 memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
1187 if (ret->data.ksk.keywordCount >= GNUNET_MAX_MALLOC_CHECKED / sizeof (char*))
1193 if (ret->data.ksk.keywordCount > 0)
1195 ret->data.ksk.keywords
1196 = GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
1197 for (i = 0; i < ret->data.ksk.keywordCount; i++)
1198 ret->data.ksk.keywords[i] =
1199 GNUNET_strdup (uri->data.ksk.keywords[i]);
1202 ret->data.ksk.keywords = NULL; /* just to be sure */
1205 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
1217 * Create an FS URI from a single user-supplied string of keywords.
1218 * The string is broken up at spaces into individual keywords.
1219 * Keywords that start with "+" are mandatory. Double-quotes can
1220 * be used to prevent breaking up strings at spaces (and also
1221 * to specify non-mandatory keywords starting with "+").
1223 * Keywords must contain a balanced number of double quotes and
1224 * double quotes can not be used in the actual keywords (for
1225 * example, the string '""foo bar""' will be turned into two
1226 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
1228 * @param keywords the keyword string
1229 * @param emsg where to store an error message
1230 * @return an FS URI for the given keywords, NULL
1231 * if keywords is not legal (i.e. empty).
1233 struct GNUNET_FS_Uri *
1234 GNUNET_FS_uri_ksk_create (const char *keywords,
1238 unsigned int num_Words;
1241 struct GNUNET_FS_Uri *uri;
1245 if (keywords == NULL)
1247 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1251 searchString = GNUNET_strdup (keywords);
1256 while ('\0' != *pos)
1258 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1262 else if (0 == inWord)
1268 saw_quote = (saw_quote + 1) % 2;
1273 GNUNET_free (searchString);
1274 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1279 GNUNET_free (searchString);
1280 *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
1283 keywordarr = GNUNET_malloc (num_Words * sizeof (char *));
1287 while ('\0' != *pos)
1289 if ((saw_quote == 0) && (isspace ( (unsigned char) *pos)))
1294 else if (0 == inWord)
1296 keywordarr[num_Words] = pos;
1301 saw_quote = (saw_quote + 1) % 2;
1305 GNUNET_FS_uri_ksk_create_from_args (num_Words,
1306 (const char **) keywordarr);
1307 GNUNET_free (keywordarr);
1308 GNUNET_free (searchString);
1314 * Create an FS URI from a user-supplied command line of keywords.
1315 * Arguments should start with "+" to indicate mandatory
1318 * @param argc number of keywords
1319 * @param argv keywords (double quotes are not required for
1320 * keywords containing spaces; however, double
1321 * quotes are required for keywords starting with
1322 * "+"); there is no mechanism for having double
1323 * quotes in the actual keywords (if the user
1324 * did specifically specify double quotes, the
1325 * caller should convert each double quote
1326 * into two single quotes).
1327 * @return an FS URI for the given keywords, NULL
1328 * if keywords is not legal (i.e. empty).
1330 struct GNUNET_FS_Uri *
1331 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
1335 struct GNUNET_FS_Uri *uri;
1336 const char *keyword;
1344 /* allow URI to be given as one and only keyword and
1345 handle accordingly */
1348 (strlen(argv[0]) > strlen(GNUNET_FS_URI_PREFIX)) &&
1349 (0 == strncmp(argv[0], GNUNET_FS_URI_PREFIX, strlen(GNUNET_FS_URI_PREFIX)) ) &&
1350 (NULL != (uri = GNUNET_FS_uri_parse(argv[0], &emsg)) ) )
1352 GNUNET_free_non_null (emsg);
1353 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1355 uri->data.ksk.keywordCount = argc;
1356 uri->data.ksk.keywords = GNUNET_malloc (argc * sizeof (char *));
1357 for (i = 0; i < argc; i++)
1360 if (keyword[0] == '+')
1361 val = GNUNET_strdup (keyword);
1363 GNUNET_asprintf (&val, " %s", keyword);
1374 uri->data.ksk.keywords[i] = val;
1381 * Test if two URIs are equal.
1383 * @param u1 one of the URIs
1384 * @param u2 the other URI
1385 * @return GNUNET_YES if the URIs are equal
1388 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
1389 const struct GNUNET_FS_Uri *u2)
1395 GNUNET_assert (u1 != NULL);
1396 GNUNET_assert (u2 != NULL);
1397 if (u1->type != u2->type)
1402 if (0 == memcmp (&u1->data.chk,
1404 sizeof (struct FileIdentifier)))
1408 if ((0 == memcmp (&u1->data.sks.namespace,
1409 &u2->data.sks.namespace,
1410 sizeof (GNUNET_HashCode))) &&
1411 (0 == strcmp (u1->data.sks.identifier,
1412 u2->data.sks.identifier)))
1417 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
1419 for (i = 0; i < u1->data.ksk.keywordCount; i++)
1422 for (j = 0; j < u2->data.ksk.keywordCount; j++)
1424 if (0 == strcmp (u1->data.ksk.keywords[i],
1425 u2->data.ksk.keywords[j]))
1431 if (ret == GNUNET_NO)
1436 if (memcmp (&u1->data.loc,
1438 sizeof (struct FileIdentifier) +
1439 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1440 sizeof (struct GNUNET_TIME_Absolute) +
1441 sizeof (unsigned short) + sizeof (unsigned short)) != 0)
1451 * Is this a namespace URI?
1453 * @param uri the uri to check
1454 * @return GNUNET_YES if this is an SKS uri
1457 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
1459 return uri->type == sks;
1464 * Get the ID of a namespace from the given
1467 * @param uri the uri to get the namespace ID from
1468 * @param nsid where to store the ID of the namespace
1469 * @return GNUNET_OK on success
1472 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
1473 GNUNET_HashCode * nsid)
1475 if (! GNUNET_FS_uri_test_sks (uri))
1478 return GNUNET_SYSERR;
1480 *nsid = uri->data.sks.namespace;
1486 * Get the content identifier of an SKS URI.
1488 * @param uri the sks uri
1489 * @return NULL on error (not a valid SKS URI)
1492 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1494 if (!GNUNET_FS_uri_test_sks (uri))
1499 return GNUNET_strdup (uri->data.sks.identifier);
1504 * Convert namespace URI to a human readable format
1505 * (using the namespace description, if available).
1507 * @param cfg configuration to use
1508 * @param uri SKS uri to convert
1509 * @return NULL on error (not an SKS URI)
1512 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
1513 const struct GNUNET_FS_Uri *uri)
1518 if (uri->type != sks)
1520 name = GNUNET_PSEUDONYM_id_to_name (cfg, &uri->data.sks.namespace);
1522 return GNUNET_FS_uri_to_string (uri);
1523 GNUNET_asprintf (&ret,
1526 uri->data.sks.identifier);
1533 * Is this a keyword URI?
1535 * @param uri the uri
1536 * @return GNUNET_YES if this is a KSK uri
1539 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1544 if (uri->type == ksk)
1546 for (i = uri->data.ksk.keywordCount - 1; i >= 0; i--)
1547 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1550 return uri->type == ksk;
1555 * Is this a file (or directory) URI?
1557 * @param uri the uri to check
1558 * @return GNUNET_YES if this is a CHK uri
1561 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1563 return uri->type == chk;
1568 * What is the size of the file that this URI
1571 * @param uri the CHK URI to inspect
1572 * @return size of the file as specified in the CHK URI
1575 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri)
1580 return GNUNET_ntohll (uri->data.chk.file_length);
1582 return GNUNET_ntohll (uri->data.loc.fi.file_length);
1586 return 0; /* unreachable */
1591 * Is this a location URI?
1593 * @param uri the uri to check
1594 * @return GNUNET_YES if this is a LOC uri
1597 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1599 return uri->type == loc;
1604 * Function called on each value in the meta data.
1605 * Adds it to the URI.
1607 * @param cls URI to update
1608 * @param plugin_name name of the plugin that produced this value;
1609 * special values can be used (i.e. '<zlib>' for zlib being
1610 * used in the main libextractor library and yielding
1612 * @param type libextractor-type describing the meta data
1613 * @param format basic format information about data
1614 * @param data_mime_type mime-type of data (not of the original file);
1615 * can be NULL (if mime-type is not known)
1616 * @param data actual meta-data found
1617 * @param data_len number of bytes in data
1618 * @return 0 (always)
1621 gather_uri_data (void *cls,
1622 const char *plugin_name,
1623 enum EXTRACTOR_MetaType type,
1624 enum EXTRACTOR_MetaFormat format,
1625 const char *data_mime_type,
1629 struct GNUNET_FS_Uri *uri = cls;
1633 if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
1634 (format != EXTRACTOR_METAFORMAT_C_STRING) )
1636 for (j = uri->data.ksk.keywordCount - 1; j >= 0; j--)
1637 if (0 == strcmp (&uri->data.ksk.keywords[j][1], data))
1639 GNUNET_asprintf (&nkword,
1640 " %s", /* space to mark as 'non mandatory' */
1642 uri->data.ksk.keywords[uri->data.ksk.keywordCount++] = nkword;
1648 * Construct a keyword-URI from meta-data (take all entries
1649 * in the meta-data and construct one large keyword URI
1650 * that lists all keywords that can be found in the meta-data).
1652 * @param md metadata to use
1653 * @return NULL on error, otherwise a KSK URI
1655 struct GNUNET_FS_Uri *
1656 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
1658 struct GNUNET_FS_Uri *ret;
1663 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1665 ent = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
1668 ret->data.ksk.keywords
1669 = GNUNET_malloc (sizeof (char *) * ent);
1670 GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
1677 * In URI-encoding, does the given character
1678 * need to be encoded using %-encoding?
1681 needs_percent (char c)
1683 return (!((isalnum ( (unsigned char) c)) ||
1684 (c == '-') || (c == '_') || (c == '.') || (c == '~')));
1689 * Convert a KSK URI to a string.
1691 * @param uri the URI to convert
1692 * @return NULL on error (i.e. keywordCount == 0)
1695 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1698 unsigned int keywordCount;
1705 const char *keyword;
1707 if (uri->type != ksk)
1709 keywords = uri->data.ksk.keywords;
1710 keywordCount = uri->data.ksk.keywordCount;
1712 keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1713 strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1714 for (i = 0; i < keywordCount; i++)
1716 keyword = keywords[i];
1717 slen = strlen (keyword);
1719 for (j = 0; j < slen; j++)
1721 if ((j == 0) && (keyword[j] == ' '))
1724 continue; /* skip leading space */
1726 if (needs_percent (keyword[j]))
1727 n += 2; /* will use %-encoding */
1730 ret = GNUNET_malloc (n);
1731 strcpy (ret, GNUNET_FS_URI_PREFIX);
1732 strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1733 wpos = strlen (ret);
1734 for (i = 0; i < keywordCount; i++)
1736 keyword = keywords[i];
1737 slen = strlen (keyword);
1738 for (j = 0; j < slen; j++)
1740 if ((j == 0) && (keyword[j] == ' '))
1741 continue; /* skip leading space */
1742 if (needs_percent (keyword[j]))
1744 sprintf (&ret[wpos], "%%%02X", keyword[j]);
1749 ret[wpos++] = keyword[j];
1752 if (i != keywordCount - 1)
1760 * Convert SKS URI to a string.
1762 * @param uri sks uri to convert
1763 * @return NULL on error
1766 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1768 const GNUNET_HashCode * namespace;
1769 const char *identifier;
1771 struct GNUNET_CRYPTO_HashAsciiEncoded ns;
1773 if (uri->type != sks)
1775 namespace = &uri->data.sks.namespace;
1776 identifier = uri->data.sks.identifier;
1777 GNUNET_CRYPTO_hash_to_enc (namespace, &ns);
1778 GNUNET_asprintf (&ret,
1780 GNUNET_FS_URI_PREFIX,
1781 GNUNET_FS_URI_SKS_INFIX,
1782 (const char *) &ns, identifier);
1788 * Convert a CHK URI to a string.
1790 * @param uri chk uri to convert
1791 * @return NULL on error
1794 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1796 const struct FileIdentifier * fi;
1798 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1799 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1801 if (uri->type != chk)
1803 fi = &uri->data.chk;
1804 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1805 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1807 GNUNET_asprintf (&ret,
1809 GNUNET_FS_URI_PREFIX,
1810 GNUNET_FS_URI_CHK_INFIX,
1811 (const char *) &keyhash,
1812 (const char *) &queryhash,
1813 GNUNET_ntohll (fi->file_length));
1818 * Convert binary data to a string.
1820 * @param data binary data to convert
1821 * @param size number of bytes in data
1822 * @return converted data
1825 bin2enc (const void *data, size_t size)
1828 * 64 characters for encoding, 6 bits per character
1831 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=";
1839 GNUNET_assert (strlen (tbl) == 64);
1841 if (((size * 8) % 6) != 0)
1843 ret = GNUNET_malloc (len + 1);
1848 for (pos = 0; pos < size; pos++)
1850 bits |= ((((const unsigned char *) data)[pos]) << hbits);
1854 ret[len++] = tbl[bits & 63];
1860 ret[len] = tbl[bits & 63];
1866 * Convert a LOC URI to a string.
1868 * @param uri loc uri to convert
1869 * @return NULL on error
1872 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
1875 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1876 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1880 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
1881 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
1882 peerId = bin2enc (&uri->data.loc.peer,
1883 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1884 peerSig = bin2enc (&uri->data.loc.contentSignature,
1885 sizeof (struct GNUNET_CRYPTO_RsaSignature));
1886 GNUNET_asprintf (&ret,
1887 "%s%s%s.%s.%llu.%s.%s.%llu",
1888 GNUNET_FS_URI_PREFIX,
1889 GNUNET_FS_URI_LOC_INFIX,
1890 (const char *) &keyhash,
1891 (const char *) &queryhash,
1892 (unsigned long long) GNUNET_ntohll (uri->data.loc.fi.file_length),
1895 (unsigned long long) uri->data.loc.expirationTime.abs_value);
1896 GNUNET_free (peerSig);
1897 GNUNET_free (peerId);
1903 * Convert a URI to a UTF-8 String.
1905 * @param uri uri to convert to a string
1906 * @return the UTF-8 string
1909 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
1919 return uri_ksk_to_string (uri);
1921 return uri_sks_to_string (uri);
1923 return uri_chk_to_string (uri);
1925 return uri_loc_to_string (uri);
1932 /* end of fs_uri.c */