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)];
411 GNUNET_assert (s != NULL);
413 pos = strlen (GNUNET_FS_URI_CHK_PREFIX);
414 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
415 (0 != strncmp (s, GNUNET_FS_URI_CHK_PREFIX,
417 return NULL; /* not a CHK URI */
418 if ( (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
419 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
421 *emsg = GNUNET_strdup (_("Malformed CHK URI"));
426 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
427 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
429 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
430 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
431 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
433 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
435 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
437 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
441 *emsg = GNUNET_strdup (_("Malformed CHK URI"));
444 fi.file_length = GNUNET_htonll (flen);
445 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
453 * Convert a character back to the binary value
454 * that it represents (given base64-encoding).
456 * @param a character to convert
457 * @return offset in the "tbl" array
460 c2v (unsigned char a)
462 if ((a >= '0') && (a <= '9'))
464 if ((a >= 'A') && (a <= 'Z'))
465 return (a - 'A' + 10);
466 if ((a >= 'a') && (a <= 'z'))
467 return (a - 'a' + 36);
477 * Convert string back to binary data.
479 * @param input '\\0'-terminated string
480 * @param data where to write binary data
481 * @param size how much data should be converted
482 * @return number of characters processed from input,
486 enc2bin (const char *input, void *data, size_t size)
494 if (((size * 8) % 6) != 0)
496 if (strlen (input) < len)
497 return -1; /* error! */
501 for (pos = 0; pos < size; pos++)
505 bits |= (c2v (input[len++]) << hbits);
508 (((unsigned char *) data)[pos]) = (unsigned char) bits;
517 * Structure that defines how the
518 * contents of a location URI must be
519 * assembled in memory to create or
520 * verify the signature of a location
523 struct LocUriAssembly
525 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
527 struct GNUNET_TIME_AbsoluteNBO exptime;
529 struct FileIdentifier fi;
531 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded peer;
536 #define GNUNET_FS_URI_LOC_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX
540 * Also verifies validity of the location URI.
542 * @param s an uri string
543 * @param emsg where to store the parser error message (if any)
544 * @return NULL on error, valid LOC URI otherwise
546 static struct GNUNET_FS_Uri *
547 uri_loc_parse (const char *s, char **emsg)
549 struct GNUNET_FS_Uri *uri;
550 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
551 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
554 unsigned long long exptime;
555 unsigned long long flen;
556 struct GNUNET_TIME_Absolute et;
557 struct GNUNET_CRYPTO_RsaSignature sig;
558 struct LocUriAssembly ass;
562 GNUNET_assert (s != NULL);
564 pos = strlen ( GNUNET_FS_URI_LOC_PREFIX);
565 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
566 (0 != strncmp (s, GNUNET_FS_URI_LOC_PREFIX,
568 return NULL; /* not an SKS URI */
569 if ( (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
570 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
572 *emsg = GNUNET_strdup (_("SKS URI malformed"));
577 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
578 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
580 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
581 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
582 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
584 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
586 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
587 &ass.fi.chk.query)) ||
588 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
592 *emsg = GNUNET_strdup (_("SKS URI malformed"));
595 ass.fi.file_length = GNUNET_htonll (flen);
597 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
598 while ((s[npos] != '\0') && (s[npos] != '.'))
602 *emsg = GNUNET_strdup (_("SKS URI malformed"));
606 ret = enc2bin (&s[npos],
608 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
611 *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode public key)"));
615 if (s[npos++] != '.')
617 *emsg = GNUNET_strdup (_("SKS URI malformed (could not find signature)"));
620 ret = enc2bin (&s[npos],
622 sizeof (struct GNUNET_CRYPTO_RsaSignature));
625 *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode signature)"));
629 if (s[npos++] != '.')
631 *emsg = GNUNET_strdup (_("SKS URI malformed"));
634 if (1 != SSCANF (&s[npos], "%llu", &exptime))
636 *emsg = GNUNET_strdup (_("SKS URI malformed (could not parse expiration time)"));
639 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
640 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
641 et.abs_value = exptime;
642 ass.exptime = GNUNET_TIME_absolute_hton (et);
644 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT,
649 *emsg = GNUNET_strdup (_("SKS URI malformed (signature failed validation)"));
652 uri = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
654 uri->data.loc.fi = ass.fi;
655 uri->data.loc.peer = ass.peer;
656 uri->data.loc.expirationTime = et;
657 uri->data.loc.contentSignature = sig;
666 * Convert a UTF-8 String to a URI.
668 * @param uri string to parse
669 * @param emsg where to store the parser error message (if any)
670 * @return NULL on error
672 struct GNUNET_FS_Uri *
673 GNUNET_FS_uri_parse (const char *uri,
676 struct GNUNET_FS_Uri *ret;
682 if ( (NULL != (ret = uri_chk_parse (uri, emsg))) ||
683 (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
684 (NULL != (ret = uri_sks_parse (uri, emsg))) ||
685 (NULL != (ret = uri_loc_parse (uri, emsg))) )
688 *emsg = GNUNET_strdup (_("Unrecognized URI type"));
698 * @param uri uri to free
701 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
705 GNUNET_assert (uri != NULL);
709 for (i = 0; i < uri->data.ksk.keywordCount; i++)
710 GNUNET_free (uri->data.ksk.keywords[i]);
711 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
715 GNUNET_free (uri->data.sks.identifier);
727 * How many keywords are ANDed in this keyword URI?
729 * @param uri ksk uri to get the number of keywords from
730 * @return 0 if this is not a keyword URI
733 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
735 if (uri->type != ksk)
737 return uri->data.ksk.keywordCount;
742 * Iterate over all keywords in this keyword URI.
744 * @param uri ksk uri to get the keywords from
745 * @param iterator function to call on each keyword
746 * @param iterator_cls closure for iterator
747 * @return -1 if this is not a keyword URI, otherwise number of
748 * keywords iterated over until iterator aborted
751 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
752 GNUNET_FS_KeywordIterator iterator,
758 if (uri->type != ksk)
760 if (iterator == NULL)
761 return uri->data.ksk.keywordCount;
762 for (i = 0; i < uri->data.ksk.keywordCount; i++)
764 keyword = uri->data.ksk.keywords[i];
765 /* first character of keyword indicates
766 if it is mandatory or not */
767 if (GNUNET_OK != iterator (iterator_cls,
777 * Add the given keyword to the set of keywords represented by the URI.
778 * Does nothing if the keyword is already present.
780 * @param uri ksk uri to modify
781 * @param keyword keyword to add
782 * @param is_mandatory is this keyword mandatory?
785 GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri,
793 GNUNET_assert (uri->type == ksk);
794 for (i = 0; i < uri->data.ksk.keywordCount; i++)
796 old = uri->data.ksk.keywords[i];
797 if (0 == strcmp (&old[1], keyword))
801 is_mandatory ? "+%s" : " %s",
803 GNUNET_array_append (uri->data.ksk.keywords,
804 uri->data.ksk.keywordCount,
810 * Remove the given keyword from the set of keywords represented by the URI.
811 * Does nothing if the keyword is not present.
813 * @param uri ksk uri to modify
814 * @param keyword keyword to add
817 GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri,
823 GNUNET_assert (uri->type == ksk);
824 for (i = 0; i < uri->data.ksk.keywordCount; i++)
826 old = uri->data.ksk.keywords[i];
827 if (0 == strcmp (&old[1], keyword))
829 uri->data.ksk.keywords[i] = uri->data.ksk.keywords[uri->data.ksk.keywordCount-1];
830 GNUNET_array_grow (uri->data.ksk.keywords,
831 uri->data.ksk.keywordCount,
832 uri->data.ksk.keywordCount - 1);
841 * Obtain the identity of the peer offering the data
843 * @param uri the location URI to inspect
844 * @param peer where to store the identify of the peer (presumably) offering the content
845 * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
848 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
849 struct GNUNET_PeerIdentity * peer)
851 if (uri->type != loc)
852 return GNUNET_SYSERR;
853 GNUNET_CRYPTO_hash (&uri->data.loc.peer,
854 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
861 * Obtain the expiration of the LOC URI.
863 * @param uri location URI to get the expiration from
864 * @return expiration time of the URI
866 struct GNUNET_TIME_Absolute
867 GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri)
869 GNUNET_assert (uri->type == loc);
870 return uri->data.loc.expirationTime;
876 * Obtain the URI of the content itself.
878 * @param uri location URI to get the content URI from
879 * @return NULL if argument is not a location URI
881 struct GNUNET_FS_Uri *
882 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
884 struct GNUNET_FS_Uri *ret;
886 if (uri->type != loc)
888 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
890 ret->data.chk = uri->data.loc.fi;
896 * Construct a location URI (this peer will be used for the location).
898 * @param baseUri content offered by the sender
899 * @param cfg configuration information (used to find our hostkey)
900 * @param expiration_time how long will the content be offered?
901 * @return the location URI, NULL on error
903 struct GNUNET_FS_Uri *
904 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
905 const struct GNUNET_CONFIGURATION_Handle *cfg,
906 struct GNUNET_TIME_Absolute expiration_time)
908 struct GNUNET_FS_Uri *uri;
909 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
910 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
912 struct LocUriAssembly ass;
914 if (baseUri->type != chk)
917 GNUNET_CONFIGURATION_get_value_filename (cfg,
919 "HOSTKEY", &keyfile))
921 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
923 ("Lacking key configuration settings.\n"));
926 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
927 if (my_private_key == NULL)
929 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
930 _("Could not access hostkey file `%s'.\n"),
932 GNUNET_free (keyfile);
935 GNUNET_free (keyfile);
936 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
937 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
938 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
939 ass.exptime = GNUNET_TIME_absolute_hton (expiration_time);
940 ass.fi = baseUri->data.chk;
941 ass.peer = my_public_key;
942 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
944 uri->data.loc.fi = baseUri->data.chk;
945 uri->data.loc.expirationTime = expiration_time;
946 uri->data.loc.peer = my_public_key;
947 GNUNET_assert (GNUNET_OK ==
948 GNUNET_CRYPTO_rsa_sign (my_private_key,
950 &uri->data.loc.contentSignature));
951 GNUNET_CRYPTO_rsa_key_free (my_private_key);
957 * Create an SKS URI from a namespace and an identifier.
959 * @param ns namespace
960 * @param id identifier
961 * @param emsg where to store an error message
962 * @return an FS URI for the given namespace and identifier
964 struct GNUNET_FS_Uri *
965 GNUNET_FS_uri_sks_create (struct GNUNET_FS_Namespace *ns,
969 struct GNUNET_FS_Uri *ns_uri;
970 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
972 ns_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
974 GNUNET_CRYPTO_rsa_key_get_public (ns->key,
976 GNUNET_CRYPTO_hash (&pk,
978 &ns_uri->data.sks.namespace);
979 ns_uri->data.sks.identifier = GNUNET_strdup (id);
985 * Create an SKS URI from a namespace ID and an identifier.
987 * @param nsid namespace ID
988 * @param id identifier
989 * @return an FS URI for the given namespace and identifier
991 struct GNUNET_FS_Uri *
992 GNUNET_FS_uri_sks_create_from_nsid (GNUNET_HashCode *nsid,
995 struct GNUNET_FS_Uri *ns_uri;
997 ns_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
999 ns_uri->data.sks.namespace = *nsid;
1000 ns_uri->data.sks.identifier = GNUNET_strdup (id);
1006 * Canonicalize a keyword.
1008 * @param in input string (the keyword)
1009 * @return canonicalized keyword
1012 canonicalize_keyword (const char *in)
1018 ret = GNUNET_strdup (in);
1021 while ('\0' != *rpos)
1023 switch (tolower( (unsigned char) *rpos))
1034 /* skip characters listed above */
1056 /* convert characters listed above to lower case */
1057 *wpos = tolower( (unsigned char)*rpos);
1064 /* keep characters listed above without changes */
1069 /* replace characters listed above with '_' */
1081 * Canonicalize keyword URI. Performs operations such
1082 * as decapitalization and removal of certain characters.
1083 * (useful for search).
1085 * @param uri the URI to canonicalize
1086 * @return canonicalized version of the URI, NULL on error
1088 struct GNUNET_FS_Uri *
1089 GNUNET_FS_uri_ksk_canonicalize (const struct GNUNET_FS_Uri *uri)
1091 struct GNUNET_FS_Uri *ret;
1096 kc = uri->data.ksk.keywordCount;
1097 kl = GNUNET_malloc (kc*sizeof(char*));
1099 kl[i] = canonicalize_keyword (uri->data.ksk.keywords[i]);
1100 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
1102 ret->data.ksk.keywordCount = kc;
1103 ret->data.ksk.keywords = kl;
1109 * Merge the sets of keywords from two KSK URIs.
1110 * (useful for merging the canonicalized keywords with
1111 * the original keywords for sharing).
1113 * @param u1 first uri
1114 * @param u2 second uri
1115 * @return merged URI, NULL on error
1117 struct GNUNET_FS_Uri *
1118 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
1119 const struct GNUNET_FS_Uri *u2)
1121 struct GNUNET_FS_Uri *ret;
1129 if ( (u1 == NULL) && (u2 == NULL) )
1132 return GNUNET_FS_uri_dup (u2);
1134 return GNUNET_FS_uri_dup (u1);
1135 if ( (u1->type != ksk) ||
1141 kc = u1->data.ksk.keywordCount;
1142 kl = GNUNET_malloc ((kc+u2->data.ksk.keywordCount)*sizeof(char*));
1143 for (i=0;i<u1->data.ksk.keywordCount;i++)
1144 kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]);
1145 for (i=0;i<u2->data.ksk.keywordCount;i++)
1147 kp = u2->data.ksk.keywords[i];
1149 for (j=0;j<u1->data.ksk.keywordCount;j++)
1150 if (0 == strcmp(kp + 1,
1159 kl[kc++] = GNUNET_strdup (kp);
1161 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
1163 ret->data.ksk.keywordCount = kc;
1164 ret->data.ksk.keywords = kl;
1172 * @param uri the URI to duplicate
1173 * @return copy of the URI
1175 struct GNUNET_FS_Uri *
1176 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
1178 struct GNUNET_FS_Uri *ret;
1183 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1184 memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
1188 if (ret->data.ksk.keywordCount >= GNUNET_MAX_MALLOC_CHECKED / sizeof (char*))
1194 if (ret->data.ksk.keywordCount > 0)
1196 ret->data.ksk.keywords
1197 = GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
1198 for (i = 0; i < ret->data.ksk.keywordCount; i++)
1199 ret->data.ksk.keywords[i] =
1200 GNUNET_strdup (uri->data.ksk.keywords[i]);
1203 ret->data.ksk.keywords = NULL; /* just to be sure */
1206 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
1218 * Create an FS URI from a single user-supplied string of keywords.
1219 * The string is broken up at spaces into individual keywords.
1220 * Keywords that start with "+" are mandatory. Double-quotes can
1221 * be used to prevent breaking up strings at spaces (and also
1222 * to specify non-mandatory keywords starting with "+").
1224 * Keywords must contain a balanced number of double quotes and
1225 * double quotes can not be used in the actual keywords (for
1226 * example, the string '""foo bar""' will be turned into two
1227 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
1229 * @param keywords the keyword string
1230 * @param emsg where to store an error message
1231 * @return an FS URI for the given keywords, NULL
1232 * if keywords is not legal (i.e. empty).
1234 struct GNUNET_FS_Uri *
1235 GNUNET_FS_uri_ksk_create (const char *keywords,
1239 unsigned int num_Words;
1242 struct GNUNET_FS_Uri *uri;
1246 if (keywords == NULL)
1248 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1252 searchString = GNUNET_strdup (keywords);
1257 while ('\0' != *pos)
1259 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1263 else if (0 == inWord)
1269 saw_quote = (saw_quote + 1) % 2;
1274 GNUNET_free (searchString);
1275 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1280 GNUNET_free (searchString);
1281 *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
1284 keywordarr = GNUNET_malloc (num_Words * sizeof (char *));
1288 while ('\0' != *pos)
1290 if ((saw_quote == 0) && (isspace ( (unsigned char) *pos)))
1295 else if (0 == inWord)
1297 keywordarr[num_Words] = pos;
1302 saw_quote = (saw_quote + 1) % 2;
1306 GNUNET_FS_uri_ksk_create_from_args (num_Words,
1307 (const char **) keywordarr);
1308 GNUNET_free (keywordarr);
1309 GNUNET_free (searchString);
1315 * Create an FS URI from a user-supplied command line of keywords.
1316 * Arguments should start with "+" to indicate mandatory
1319 * @param argc number of keywords
1320 * @param argv keywords (double quotes are not required for
1321 * keywords containing spaces; however, double
1322 * quotes are required for keywords starting with
1323 * "+"); there is no mechanism for having double
1324 * quotes in the actual keywords (if the user
1325 * did specifically specify double quotes, the
1326 * caller should convert each double quote
1327 * into two single quotes).
1328 * @return an FS URI for the given keywords, NULL
1329 * if keywords is not legal (i.e. empty).
1331 struct GNUNET_FS_Uri *
1332 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
1336 struct GNUNET_FS_Uri *uri;
1337 const char *keyword;
1345 /* allow URI to be given as one and only keyword and
1346 handle accordingly */
1349 (strlen(argv[0]) > strlen(GNUNET_FS_URI_PREFIX)) &&
1350 (0 == strncmp(argv[0], GNUNET_FS_URI_PREFIX, strlen(GNUNET_FS_URI_PREFIX)) ) &&
1351 (NULL != (uri = GNUNET_FS_uri_parse(argv[0], &emsg)) ) )
1353 GNUNET_free_non_null (emsg);
1354 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1356 uri->data.ksk.keywordCount = argc;
1357 uri->data.ksk.keywords = GNUNET_malloc (argc * sizeof (char *));
1358 for (i = 0; i < argc; i++)
1361 if (keyword[0] == '+')
1362 val = GNUNET_strdup (keyword);
1364 GNUNET_asprintf (&val, " %s", keyword);
1375 uri->data.ksk.keywords[i] = val;
1382 * Test if two URIs are equal.
1384 * @param u1 one of the URIs
1385 * @param u2 the other URI
1386 * @return GNUNET_YES if the URIs are equal
1389 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
1390 const struct GNUNET_FS_Uri *u2)
1396 GNUNET_assert (u1 != NULL);
1397 GNUNET_assert (u2 != NULL);
1398 if (u1->type != u2->type)
1403 if (0 == memcmp (&u1->data.chk,
1405 sizeof (struct FileIdentifier)))
1409 if ((0 == memcmp (&u1->data.sks.namespace,
1410 &u2->data.sks.namespace,
1411 sizeof (GNUNET_HashCode))) &&
1412 (0 == strcmp (u1->data.sks.identifier,
1413 u2->data.sks.identifier)))
1418 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
1420 for (i = 0; i < u1->data.ksk.keywordCount; i++)
1423 for (j = 0; j < u2->data.ksk.keywordCount; j++)
1425 if (0 == strcmp (u1->data.ksk.keywords[i],
1426 u2->data.ksk.keywords[j]))
1432 if (ret == GNUNET_NO)
1437 if (memcmp (&u1->data.loc,
1439 sizeof (struct FileIdentifier) +
1440 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1441 sizeof (struct GNUNET_TIME_Absolute) +
1442 sizeof (unsigned short) + sizeof (unsigned short)) != 0)
1452 * Is this a namespace URI?
1454 * @param uri the uri to check
1455 * @return GNUNET_YES if this is an SKS uri
1458 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
1460 return uri->type == sks;
1465 * Get the ID of a namespace from the given
1468 * @param uri the uri to get the namespace ID from
1469 * @param nsid where to store the ID of the namespace
1470 * @return GNUNET_OK on success
1473 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
1474 GNUNET_HashCode * nsid)
1476 if (! GNUNET_FS_uri_test_sks (uri))
1479 return GNUNET_SYSERR;
1481 *nsid = uri->data.sks.namespace;
1487 * Get the content identifier of an SKS URI.
1489 * @param uri the sks uri
1490 * @return NULL on error (not a valid SKS URI)
1493 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1495 if (!GNUNET_FS_uri_test_sks (uri))
1500 return GNUNET_strdup (uri->data.sks.identifier);
1505 * Convert namespace URI to a human readable format
1506 * (using the namespace description, if available).
1508 * @param cfg configuration to use
1509 * @param uri SKS uri to convert
1510 * @return NULL on error (not an SKS URI)
1513 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
1514 const struct GNUNET_FS_Uri *uri)
1519 if (uri->type != sks)
1521 name = GNUNET_PSEUDONYM_id_to_name (cfg, &uri->data.sks.namespace);
1523 return GNUNET_FS_uri_to_string (uri);
1524 GNUNET_asprintf (&ret,
1527 uri->data.sks.identifier);
1534 * Is this a keyword URI?
1536 * @param uri the uri
1537 * @return GNUNET_YES if this is a KSK uri
1540 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1545 if (uri->type == ksk)
1547 for (i = uri->data.ksk.keywordCount - 1; i >= 0; i--)
1548 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1551 return uri->type == ksk;
1556 * Is this a file (or directory) URI?
1558 * @param uri the uri to check
1559 * @return GNUNET_YES if this is a CHK uri
1562 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1564 return uri->type == chk;
1569 * What is the size of the file that this URI
1572 * @param uri the CHK URI to inspect
1573 * @return size of the file as specified in the CHK URI
1576 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri)
1581 return GNUNET_ntohll (uri->data.chk.file_length);
1583 return GNUNET_ntohll (uri->data.loc.fi.file_length);
1587 return 0; /* unreachable */
1592 * Is this a location URI?
1594 * @param uri the uri to check
1595 * @return GNUNET_YES if this is a LOC uri
1598 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1600 return uri->type == loc;
1605 * Function called on each value in the meta data.
1606 * Adds it to the URI.
1608 * @param cls URI to update
1609 * @param plugin_name name of the plugin that produced this value;
1610 * special values can be used (i.e. '<zlib>' for zlib being
1611 * used in the main libextractor library and yielding
1613 * @param type libextractor-type describing the meta data
1614 * @param format basic format information about data
1615 * @param data_mime_type mime-type of data (not of the original file);
1616 * can be NULL (if mime-type is not known)
1617 * @param data actual meta-data found
1618 * @param data_len number of bytes in data
1619 * @return 0 (always)
1622 gather_uri_data (void *cls,
1623 const char *plugin_name,
1624 enum EXTRACTOR_MetaType type,
1625 enum EXTRACTOR_MetaFormat format,
1626 const char *data_mime_type,
1630 struct GNUNET_FS_Uri *uri = cls;
1634 if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
1635 (format != EXTRACTOR_METAFORMAT_C_STRING) )
1637 for (j = uri->data.ksk.keywordCount - 1; j >= 0; j--)
1638 if (0 == strcmp (&uri->data.ksk.keywords[j][1], data))
1640 GNUNET_asprintf (&nkword,
1641 " %s", /* space to mark as 'non mandatory' */
1643 uri->data.ksk.keywords[uri->data.ksk.keywordCount++] = nkword;
1649 * Construct a keyword-URI from meta-data (take all entries
1650 * in the meta-data and construct one large keyword URI
1651 * that lists all keywords that can be found in the meta-data).
1653 * @param md metadata to use
1654 * @return NULL on error, otherwise a KSK URI
1656 struct GNUNET_FS_Uri *
1657 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
1659 struct GNUNET_FS_Uri *ret;
1664 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1666 ent = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
1669 ret->data.ksk.keywords
1670 = GNUNET_malloc (sizeof (char *) * ent);
1671 GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
1678 * In URI-encoding, does the given character
1679 * need to be encoded using %-encoding?
1682 needs_percent (char c)
1684 return (!((isalnum ( (unsigned char) c)) ||
1685 (c == '-') || (c == '_') || (c == '.') || (c == '~')));
1690 * Convert a KSK URI to a string.
1692 * @param uri the URI to convert
1693 * @return NULL on error (i.e. keywordCount == 0)
1696 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1699 unsigned int keywordCount;
1706 const char *keyword;
1708 if (uri->type != ksk)
1710 keywords = uri->data.ksk.keywords;
1711 keywordCount = uri->data.ksk.keywordCount;
1713 keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1714 strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1715 for (i = 0; i < keywordCount; i++)
1717 keyword = keywords[i];
1718 slen = strlen (keyword);
1720 for (j = 0; j < slen; j++)
1722 if ((j == 0) && (keyword[j] == ' '))
1725 continue; /* skip leading space */
1727 if (needs_percent (keyword[j]))
1728 n += 2; /* will use %-encoding */
1731 ret = GNUNET_malloc (n);
1732 strcpy (ret, GNUNET_FS_URI_PREFIX);
1733 strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1734 wpos = strlen (ret);
1735 for (i = 0; i < keywordCount; i++)
1737 keyword = keywords[i];
1738 slen = strlen (keyword);
1739 for (j = 0; j < slen; j++)
1741 if ((j == 0) && (keyword[j] == ' '))
1742 continue; /* skip leading space */
1743 if (needs_percent (keyword[j]))
1745 sprintf (&ret[wpos], "%%%02X", keyword[j]);
1750 ret[wpos++] = keyword[j];
1753 if (i != keywordCount - 1)
1761 * Convert SKS URI to a string.
1763 * @param uri sks uri to convert
1764 * @return NULL on error
1767 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1769 const GNUNET_HashCode * namespace;
1770 const char *identifier;
1772 struct GNUNET_CRYPTO_HashAsciiEncoded ns;
1774 if (uri->type != sks)
1776 namespace = &uri->data.sks.namespace;
1777 identifier = uri->data.sks.identifier;
1778 GNUNET_CRYPTO_hash_to_enc (namespace, &ns);
1779 GNUNET_asprintf (&ret,
1781 GNUNET_FS_URI_PREFIX,
1782 GNUNET_FS_URI_SKS_INFIX,
1783 (const char *) &ns, identifier);
1789 * Convert a CHK URI to a string.
1791 * @param uri chk uri to convert
1792 * @return NULL on error
1795 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1797 const struct FileIdentifier * fi;
1799 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1800 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1802 if (uri->type != chk)
1804 fi = &uri->data.chk;
1805 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1806 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1808 GNUNET_asprintf (&ret,
1810 GNUNET_FS_URI_PREFIX,
1811 GNUNET_FS_URI_CHK_INFIX,
1812 (const char *) &keyhash,
1813 (const char *) &queryhash,
1814 GNUNET_ntohll (fi->file_length));
1819 * Convert binary data to a string.
1821 * @param data binary data to convert
1822 * @param size number of bytes in data
1823 * @return converted data
1826 bin2enc (const void *data, size_t size)
1829 * 64 characters for encoding, 6 bits per character
1832 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=";
1840 GNUNET_assert (strlen (tbl) == 64);
1842 if (((size * 8) % 6) != 0)
1844 ret = GNUNET_malloc (len + 1);
1849 for (pos = 0; pos < size; pos++)
1851 bits |= ((((const unsigned char *) data)[pos]) << hbits);
1855 ret[len++] = tbl[bits & 63];
1861 ret[len] = tbl[bits & 63];
1867 * Convert a LOC URI to a string.
1869 * @param uri loc uri to convert
1870 * @return NULL on error
1873 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
1876 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1877 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1881 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
1882 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
1883 peerId = bin2enc (&uri->data.loc.peer,
1884 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1885 peerSig = bin2enc (&uri->data.loc.contentSignature,
1886 sizeof (struct GNUNET_CRYPTO_RsaSignature));
1887 GNUNET_asprintf (&ret,
1888 "%s%s%s.%s.%llu.%s.%s.%llu",
1889 GNUNET_FS_URI_PREFIX,
1890 GNUNET_FS_URI_LOC_INFIX,
1891 (const char *) &keyhash,
1892 (const char *) &queryhash,
1893 (unsigned long long) GNUNET_ntohll (uri->data.loc.fi.file_length),
1896 (unsigned long long) uri->data.loc.expirationTime.abs_value);
1897 GNUNET_free (peerSig);
1898 GNUNET_free (peerId);
1904 * Convert a URI to a UTF-8 String.
1906 * @param uri uri to convert to a string
1907 * @return the UTF-8 string
1910 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
1920 return uri_ksk_to_string (uri);
1922 return uri_sks_to_string (uri);
1924 return uri_chk_to_string (uri);
1926 return uri_loc_to_string (uri);
1933 /* end of fs_uri.c */