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 2, 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);
247 * @param s an uri string
248 * @param emsg where to store the parser error message (if any)
249 * @return NULL on error, otherwise the KSK URI
251 static struct GNUNET_FS_Uri *
252 uri_ksk_parse (const char *s, char **emsg)
254 struct GNUNET_FS_Uri *ret;
264 GNUNET_assert (s != NULL);
266 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX);
267 if ( (slen <= pos) ||
268 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX,
270 return NULL; /* not KSK URI */
271 if ( (s[slen - 1] == '+') ||
274 *emsg = GNUNET_strdup (_("Malformed KSK URI (must not begin or end with `+')"));
279 for (i = pos; i < slen; i++)
281 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
283 saw_quote = (saw_quote + 1) % 2;
287 if ((s[i] == '+') && (saw_quote == 0))
292 *emsg = GNUNET_strdup (_("`++' not allowed in KSK URI"));
299 *emsg = GNUNET_strdup (_("Quotes not balanced in KSK URI"));
303 dup = GNUNET_strdup (s);
304 keywords = GNUNET_malloc (max * sizeof (char *));
305 for (i = slen - 1; i >= pos; i--)
307 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
309 saw_quote = (saw_quote + 1) % 2;
313 if ((dup[i] == '+') && (saw_quote == 0))
315 keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg);
316 if (NULL == keywords[max])
321 keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
322 if (NULL == keywords[max])
324 GNUNET_assert (max == 0);
326 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
328 ret->data.ksk.keywordCount = iret;
329 ret->data.ksk.keywords = keywords;
332 for (i = 0; i < max; i++)
333 GNUNET_free_non_null (keywords[i]);
334 GNUNET_free (keywords);
343 * @param s an uri string
344 * @param emsg where to store the parser error message (if any)
345 * @return NULL on error, SKS URI otherwise
347 static struct GNUNET_FS_Uri *
348 uri_sks_parse (const char *s, char **emsg)
350 struct GNUNET_FS_Uri *ret;
351 GNUNET_HashCode namespace;
355 char enc[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
357 GNUNET_assert (s != NULL);
359 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX);
360 if ( (slen <= pos) ||
361 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX,
363 return NULL; /* not an SKS URI */
364 if ( (slen < pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
365 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '/') )
367 *emsg = GNUNET_strdup (_("Malformed SKS URI"));
370 memcpy (enc, &s[pos], sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
371 enc[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
372 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (enc, &namespace))
374 *emsg = GNUNET_strdup (_("Malformed SKS URI"));
377 identifier = GNUNET_strdup (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]);
378 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
380 ret->data.sks.namespace = namespace;
381 ret->data.sks.identifier = identifier;
389 * @param s an uri string
390 * @param emsg where to store the parser error message (if any)
391 * @return NULL on error, CHK URI otherwise
393 static struct GNUNET_FS_Uri *
394 uri_chk_parse (const char *s, char **emsg)
396 struct GNUNET_FS_Uri *ret;
397 struct FileIdentifier fi;
400 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
401 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
403 GNUNET_assert (s != NULL);
406 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX);
407 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
408 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX,
410 return NULL; /* not a CHK URI */
411 if ( (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
412 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
414 *emsg = GNUNET_strdup (_("Malformed CHK URI"));
419 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
420 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
422 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
423 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
424 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
426 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
428 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
430 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
434 *emsg = GNUNET_strdup (_("Malformed CHK URI"));
437 fi.file_length = GNUNET_htonll (fi.file_length);
438 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
446 * Convert a character back to the binary value
447 * that it represents (given base64-encoding).
449 * @param a character to convert
450 * @return offset in the "tbl" array
453 c2v (unsigned char a)
455 if ((a >= '0') && (a <= '9'))
457 if ((a >= 'A') && (a <= 'Z'))
458 return (a - 'A' + 10);
459 if ((a >= 'a') && (a <= 'z'))
460 return (a - 'a' + 36);
470 * Convert string back to binary data.
472 * @param input '\0'-terminated string
473 * @param data where to write binary data
474 * @param size how much data should be converted
475 * @return number of characters processed from input,
479 enc2bin (const char *input, void *data, size_t size)
487 if (((size * 8) % 6) != 0)
489 if (strlen (input) < len)
490 return -1; /* error! */
495 for (pos = 0; pos < size; pos++)
499 bits |= (c2v (input[len++]) << hbits);
502 (((unsigned char *) data)[pos]) = (unsigned char) bits;
511 * Structure that defines how the
512 * contents of a location URI must be
513 * assembled in memory to create or
514 * verify the signature of a location
517 struct LocUriAssembly
519 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
521 struct GNUNET_TIME_AbsoluteNBO exptime;
523 struct FileIdentifier fi;
525 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded peer;
532 * Also verifies validity of the location URI.
534 * @param s an uri string
535 * @param emsg where to store the parser error message (if any)
536 * @return NULL on error, valid LOC URI otherwise
538 static struct GNUNET_FS_Uri *
539 uri_loc_parse (const char *s, char **emsg)
541 struct GNUNET_FS_Uri *uri;
542 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
543 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
546 unsigned long long exptime;
547 struct GNUNET_TIME_Absolute et;
548 struct GNUNET_CRYPTO_RsaSignature sig;
549 struct LocUriAssembly ass;
553 GNUNET_assert (s != NULL);
555 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX);
556 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
557 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX,
559 return NULL; /* not an SKS URI */
560 if ( (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
561 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
563 *emsg = GNUNET_strdup (_("SKS URI malformed"));
568 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
569 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
571 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
572 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
573 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
575 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
577 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
578 &ass.fi.chk.query)) ||
579 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
581 &ass.fi.file_length)) )
583 *emsg = GNUNET_strdup (_("SKS URI malformed"));
586 ass.fi.file_length = GNUNET_htonll (ass.fi.file_length);
588 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
589 while ((s[npos] != '\0') && (s[npos] != '.'))
593 *emsg = GNUNET_strdup (_("SKS URI malformed"));
597 ret = enc2bin (&s[npos],
599 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
602 *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode public key)"));
606 if (s[npos++] != '.')
608 *emsg = GNUNET_strdup (_("SKS URI malformed (could not find signature)"));
611 ret = enc2bin (&s[npos],
613 sizeof (struct GNUNET_CRYPTO_RsaSignature));
616 *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode signature)"));
620 if (s[npos++] != '.')
622 *emsg = GNUNET_strdup (_("SKS URI malformed"));
625 if (1 != SSCANF (&s[npos], "%llu", &exptime))
627 *emsg = GNUNET_strdup (_("SKS URI malformed (could not parse expiration time)"));
630 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
631 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_NAMESPACE_PLACEMENT);
633 ass.exptime = GNUNET_TIME_absolute_hton (et);
635 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_NAMESPACE_PLACEMENT,
640 *emsg = GNUNET_strdup (_("SKS URI malformed (signature failed validation)"));
643 uri = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
645 uri->data.loc.fi = ass.fi;
646 uri->data.loc.peer = ass.peer;
647 uri->data.loc.expirationTime = et;
648 uri->data.loc.contentSignature = sig;
657 * Convert a UTF-8 String to a URI.
659 * @param uri string to parse
660 * @param emsg where to store the parser error message (if any)
661 * @return NULL on error
663 struct GNUNET_FS_Uri *
664 GNUNET_FS_uri_parse (const char *uri,
667 struct GNUNET_FS_Uri *ret;
673 if ( (NULL != (ret = uri_chk_parse (uri, emsg))) ||
674 (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
675 (NULL != (ret = uri_sks_parse (uri, emsg))) ||
676 (NULL != (ret = uri_loc_parse (uri, emsg))) )
679 *emsg = GNUNET_strdup (_("Unrecognized URI type"));
689 * @param uri uri to free
692 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
696 GNUNET_assert (uri != NULL);
700 for (i = 0; i < uri->data.ksk.keywordCount; i++)
701 GNUNET_free (uri->data.ksk.keywords[i]);
702 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
706 GNUNET_free (uri->data.sks.identifier);
718 * How many keywords are ANDed in this keyword URI?
720 * @param uri ksk uri to get the number of keywords from
721 * @return 0 if this is not a keyword URI
724 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
726 if (uri->type != ksk)
728 return uri->data.ksk.keywordCount;
733 * Iterate over all keywords in this keyword URI.
735 * @param uri ksk uri to get the keywords from
736 * @param iterator function to call on each keyword
737 * @param iterator_cls closure for iterator
738 * @return -1 if this is not a keyword URI, otherwise number of
739 * keywords iterated over until iterator aborted
742 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
743 GNUNET_FS_KeywordIterator iterator,
749 if (uri->type != ksk)
751 if (iterator == NULL)
752 return uri->data.ksk.keywordCount;
753 for (i = 0; i < uri->data.ksk.keywordCount; i++)
755 keyword = uri->data.ksk.keywords[i];
756 /* first character of keyword indicates
757 if it is mandatory or not */
758 if (GNUNET_OK != iterator (iterator_cls,
768 * Obtain the identity of the peer offering the data
770 * @param uri the location URI to inspect
771 * @param peer where to store the identify of the peer (presumably) offering the content
772 * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
775 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
776 struct GNUNET_PeerIdentity * peer)
778 if (uri->type != loc)
779 return GNUNET_SYSERR;
780 GNUNET_CRYPTO_hash (&uri->data.loc.peer,
781 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
788 * Obtain the URI of the content itself.
790 * @param uri location URI to get the content URI from
791 * @return NULL if argument is not a location URI
793 struct GNUNET_FS_Uri *
794 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
796 struct GNUNET_FS_Uri *ret;
798 if (uri->type != loc)
800 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
802 ret->data.chk = uri->data.loc.fi;
808 * Construct a location URI (this peer will be used for the location).
810 * @param baseURI content offered by the sender
811 * @param cfg configuration information (used to find our hostkey)
812 * @param expiration_time how long will the content be offered?
813 * @return the location URI, NULL on error
815 struct GNUNET_FS_Uri *
816 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
817 struct GNUNET_CONFIGURATION_Handle *cfg,
818 struct GNUNET_TIME_Absolute expiration_time)
820 struct GNUNET_FS_Uri *uri;
821 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
822 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
824 struct LocUriAssembly ass;
826 if (baseUri->type != chk)
829 GNUNET_CONFIGURATION_get_value_filename (cfg,
831 "HOSTKEY", &keyfile))
833 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
835 ("Lacking key configuration settings.\n"));
838 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
839 if (my_private_key == NULL)
841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
842 _("Could not access hostkey file `%s'.\n"),
844 GNUNET_free (keyfile);
847 GNUNET_free (keyfile);
848 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
849 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
850 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_NAMESPACE_PLACEMENT);
851 ass.exptime = GNUNET_TIME_absolute_hton (expiration_time);
852 ass.fi = baseUri->data.chk;
853 ass.peer = my_public_key;
854 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
856 uri->data.loc.fi = baseUri->data.chk;
857 uri->data.loc.expirationTime = expiration_time;
858 uri->data.loc.peer = my_public_key;
859 GNUNET_assert (GNUNET_OK ==
860 GNUNET_CRYPTO_rsa_sign (my_private_key,
862 &uri->data.loc.contentSignature));
863 GNUNET_CRYPTO_rsa_key_free (my_private_key);
869 * Canonicalize a keyword.
871 * @param in input string (the keyword)
872 * @return canonicalized keyword
875 canonicalize_keyword (const char *in)
881 ret = GNUNET_strdup (in);
884 while ('\0' != *rpos)
886 switch (tolower(*rpos))
897 /* skip characters listed above */
920 /* convert characters listed above to lower case */
921 *wpos = tolower(*rpos);
927 /* keep characters listed above without changes */
931 /* replace characters listed above with '_' */
942 * Canonicalize keyword URI. Performs operations such
943 * as decapitalization and removal of certain characters.
944 * (useful for search).
946 * @param uri the URI to canonicalize
947 * @return canonicalized version of the URI, NULL on error
949 struct GNUNET_FS_Uri *
950 GNUNET_FS_uri_ksk_canonicalize (const struct GNUNET_FS_Uri *uri)
952 struct GNUNET_FS_Uri *ret;
957 kc = uri->data.ksk.keywordCount;
958 kl = GNUNET_malloc (kc*sizeof(char*));
960 kl[i] = canonicalize_keyword (uri->data.ksk.keywords[i]);
961 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
963 ret->data.ksk.keywordCount = kc;
964 ret->data.ksk.keywords = kl;
970 * Merge the sets of keywords from two KSK URIs.
971 * (useful for merging the canonicalized keywords with
972 * the original keywords for sharing).
974 * @param u1 first uri
975 * @param u2 second uri
976 * @return merged URI, NULL on error
978 struct GNUNET_FS_Uri *
979 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
980 const struct GNUNET_FS_Uri *u2)
982 struct GNUNET_FS_Uri *ret;
990 if ( (u1->type != ksk) ||
996 kc = u1->data.ksk.keywordCount;
997 kl = GNUNET_malloc ((kc+u2->data.ksk.keywordCount)*sizeof(char*));
998 for (i=0;i<u1->data.ksk.keywordCount;i++)
999 kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]);
1000 for (i=0;i<u2->data.ksk.keywordCount;i++)
1002 kp = u2->data.ksk.keywords[i];
1004 for (j=0;j<u1->data.ksk.keywordCount;j++)
1005 if (0 == strcmp(kp + 1,
1014 kl[kc++] = GNUNET_strdup (kp - 1);
1016 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
1018 ret->data.ksk.keywordCount = kc;
1019 ret->data.ksk.keywords = kl;
1027 * @param uri the URI to duplicate
1028 * @return copy of the URI
1030 struct GNUNET_FS_Uri *
1031 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
1033 struct GNUNET_FS_Uri *ret;
1036 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1037 memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
1041 if (ret->data.ksk.keywordCount > 0)
1043 ret->data.ksk.keywords
1044 = GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
1045 for (i = 0; i < ret->data.ksk.keywordCount; i++)
1046 ret->data.ksk.keywords[i] =
1047 GNUNET_strdup (uri->data.ksk.keywords[i]);
1050 ret->data.ksk.keywords = NULL; /* just to be sure */
1053 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
1065 * Create an FS URI from a single user-supplied string of keywords.
1066 * The string is broken up at spaces into individual keywords.
1067 * Keywords that start with "+" are mandatory. Double-quotes can
1068 * be used to prevent breaking up strings at spaces (and also
1069 * to specify non-mandatory keywords starting with "+").
1071 * Keywords must contain a balanced number of double quotes and
1072 * double quotes can not be used in the actual keywords (for
1073 * example, the string '""foo bar""' will be turned into two
1074 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
1076 * @param keywords the keyword string
1077 * @param emsg where to store an error message
1078 * @return an FS URI for the given keywords, NULL
1079 * if keywords is not legal (i.e. empty).
1081 struct GNUNET_FS_Uri *
1082 GNUNET_FS_uri_ksk_create (const char *keywords,
1086 unsigned int num_Words;
1089 struct GNUNET_FS_Uri *uri;
1093 if (keywords == NULL)
1098 searchString = GNUNET_strdup (keywords);
1103 while ('\0' != *pos)
1105 if ((saw_quote == 0) && (isspace (*pos)))
1109 else if (0 == inWord)
1115 saw_quote = (saw_quote + 1) % 2;
1120 GNUNET_free (searchString);
1121 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1126 GNUNET_free (searchString);
1127 *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
1130 keywordarr = GNUNET_malloc (num_Words * sizeof (char *));
1134 while ('\0' != *pos)
1136 if ((saw_quote == 0) && (isspace (*pos)))
1141 else if (0 == inWord)
1143 keywordarr[num_Words] = pos;
1148 saw_quote = (saw_quote + 1) % 2;
1152 GNUNET_FS_uri_ksk_create_from_args (num_Words,
1153 (const char **) keywordarr);
1154 GNUNET_free (keywordarr);
1155 GNUNET_free (searchString);
1161 * Create an FS URI from a user-supplied command line of keywords.
1162 * Arguments should start with "+" to indicate mandatory
1165 * @param argc number of keywords
1166 * @param argv keywords (double quotes are not required for
1167 * keywords containing spaces; however, double
1168 * quotes are required for keywords starting with
1169 * "+"); there is no mechanism for having double
1170 * quotes in the actual keywords (if the user
1171 * did specifically specify double quotes, the
1172 * caller should convert each double quote
1173 * into two single quotes).
1174 * @return an FS URI for the given keywords, NULL
1175 * if keywords is not legal (i.e. empty).
1177 struct GNUNET_FS_Uri *
1178 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
1182 struct GNUNET_FS_Uri *uri;
1183 const char *keyword;
1191 /* allow URI to be given as one and only keyword and
1192 handle accordingly */
1195 (strlen(argv[0]) > strlen(GNUNET_FS_URI_PREFIX)) &&
1196 (strncmp(argv[0], GNUNET_FS_URI_PREFIX, strlen(GNUNET_FS_URI_PREFIX)) ) &&
1197 (NULL != (uri = GNUNET_FS_uri_parse(argv[0], &emsg)) ) )
1199 GNUNET_free_non_null (emsg);
1200 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1202 uri->data.ksk.keywordCount = argc;
1203 uri->data.ksk.keywords = GNUNET_malloc (argc * sizeof (char *));
1204 for (i = 0; i < argc; i++)
1207 if (keyword[0] == '+')
1208 val = GNUNET_strdup (keyword);
1210 GNUNET_asprintf (&val, " %s", keyword);
1221 uri->data.ksk.keywords[i] = val;
1228 * Test if two URIs are equal.
1230 * @param u1 one of the URIs
1231 * @param u2 the other URI
1232 * @return GNUNET_YES if the URIs are equal
1235 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
1236 const struct GNUNET_FS_Uri *u2)
1242 GNUNET_assert (u1 != NULL);
1243 GNUNET_assert (u2 != NULL);
1244 if (u1->type != u2->type)
1249 if (0 == memcmp (&u1->data.chk,
1251 sizeof (struct FileIdentifier)))
1255 if ((0 == memcmp (&u1->data.sks.namespace,
1256 &u2->data.sks.namespace,
1257 sizeof (GNUNET_HashCode))) &&
1258 (0 == strcmp (u1->data.sks.identifier,
1259 u2->data.sks.identifier)))
1264 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
1266 for (i = 0; i < u1->data.ksk.keywordCount; i++)
1269 for (j = 0; j < u2->data.ksk.keywordCount; j++)
1271 if (0 == strcmp (u1->data.ksk.keywords[i],
1272 u2->data.ksk.keywords[j]))
1278 if (ret == GNUNET_NO)
1283 if (memcmp (&u1->data.loc,
1285 sizeof (struct FileIdentifier) +
1286 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1287 sizeof (struct GNUNET_TIME_Absolute) +
1288 sizeof (unsigned short) + sizeof (unsigned short)) != 0)
1298 * Is this a namespace URI?
1300 * @param uri the uri to check
1301 * @return GNUNET_YES if this is an SKS uri
1304 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
1306 return uri->type == sks;
1311 * Get the ID of a namespace from the given
1314 * @param uri the uri to get the namespace ID from
1315 * @param nsid where to store the ID of the namespace
1316 * @return GNUNET_OK on success
1319 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
1320 GNUNET_HashCode * nsid)
1322 if (! GNUNET_FS_uri_test_sks (uri))
1325 return GNUNET_SYSERR;
1327 *nsid = uri->data.sks.namespace;
1333 * Get the content identifier of an SKS URI.
1335 * @param uri the sks uri
1336 * @return NULL on error (not a valid SKS URI)
1339 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1341 if (!GNUNET_FS_uri_test_sks (uri))
1346 return GNUNET_strdup (uri->data.sks.identifier);
1351 * Convert namespace URI to a human readable format
1352 * (using the namespace description, if available).
1354 * @param cfg configuration to use
1355 * @param uri SKS uri to convert
1356 * @return NULL on error (not an SKS URI)
1359 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
1360 const struct GNUNET_FS_Uri *uri)
1365 if (uri->type != sks)
1367 name = GNUNET_PSEUDONYM_id_to_name (cfg, &uri->data.sks.namespace);
1369 return GNUNET_FS_uri_to_string (uri);
1370 GNUNET_asprintf (&ret,
1373 uri->data.sks.identifier);
1380 * Is this a keyword URI?
1382 * @param uri the uri
1383 * @return GNUNET_YES if this is a KSK uri
1386 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1391 if (uri->type == ksk)
1393 for (i = uri->data.ksk.keywordCount - 1; i >= 0; i--)
1394 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1397 return uri->type == ksk;
1402 * Is this a file (or directory) URI?
1404 * @param uri the uri to check
1405 * @return GNUNET_YES if this is a CHK uri
1408 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1410 return uri->type == chk;
1415 * What is the size of the file that this URI
1418 * @param uri the CHK URI to inspect
1419 * @return size of the file as specified in the CHK URI
1422 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri)
1427 return GNUNET_ntohll (uri->data.chk.file_length);
1429 return GNUNET_ntohll (uri->data.loc.fi.file_length);
1433 return 0; /* unreachable */
1438 * Is this a location URI?
1440 * @param uri the uri to check
1441 * @return GNUNET_YES if this is a LOC uri
1444 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1446 return uri->type == loc;
1451 * Function called on each value in the meta data.
1452 * Adds it to the URI.
1454 * @param cls URI to update
1455 * @param type type of the meta data
1456 * @param data value of the meta data
1457 * @return GNUNET_OK (always)
1460 gather_uri_data (void *cls,
1461 EXTRACTOR_KeywordType type,
1464 struct GNUNET_FS_Uri *uri = cls;
1468 for (j = uri->data.ksk.keywordCount - 1; j >= 0; j--)
1469 if (0 == strcmp (&uri->data.ksk.keywords[j][1], data))
1471 nkword = GNUNET_malloc (strlen (data) + 2);
1472 strcpy (nkword, " "); /* not mandatory */
1473 strcat (nkword, data);
1474 uri->data.ksk.keywords[uri->data.ksk.keywordCount++] = nkword;
1480 * Construct a keyword-URI from meta-data (take all entries
1481 * in the meta-data and construct one large keyword URI
1482 * that lists all keywords that can be found in the meta-data).
1485 struct GNUNET_FS_Uri *
1486 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
1488 struct GNUNET_FS_Uri *ret;
1492 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1494 ret->data.ksk.keywordCount = 0;
1495 ret->data.ksk.keywords = NULL;
1496 ret->data.ksk.keywords
1497 = GNUNET_malloc (sizeof (char *) *
1498 GNUNET_CONTAINER_meta_data_get_contents (md, NULL, NULL));
1499 GNUNET_CONTAINER_meta_data_get_contents (md, &gather_uri_data, ret);
1506 * In URI-encoding, does the given character
1507 * need to be encoded using %-encoding?
1510 needs_percent (char c)
1512 return (!((isalnum (c)) ||
1513 (c == '-') || (c == '_') || (c == '.') || (c == '~')));
1518 * Convert a KSK URI to a string.
1520 * @param uri the URI to convert
1521 * @return NULL on error (i.e. keywordCount == 0)
1524 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1527 unsigned int keywordCount;
1534 const char *keyword;
1536 if (uri->type != ksk)
1538 keywords = uri->data.ksk.keywords;
1539 keywordCount = uri->data.ksk.keywordCount;
1541 keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1542 strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1543 for (i = 0; i < keywordCount; i++)
1545 keyword = keywords[i];
1546 slen = strlen (keyword);
1548 for (j = 0; j < slen; j++)
1550 if ((j == 0) && (keyword[j] == ' '))
1553 continue; /* skip leading space */
1555 if (needs_percent (keyword[j]))
1556 n += 2; /* will use %-encoding */
1559 ret = GNUNET_malloc (n);
1560 strcpy (ret, GNUNET_FS_URI_PREFIX);
1561 strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1562 wpos = strlen (ret);
1563 for (i = 0; i < keywordCount; i++)
1565 keyword = keywords[i];
1566 slen = strlen (keyword);
1567 for (j = 0; j < slen; j++)
1569 if ((j == 0) && (keyword[j] == ' '))
1570 continue; /* skip leading space */
1571 if (needs_percent (keyword[j]))
1573 sprintf (&ret[wpos], "%%%02X", keyword[j]);
1578 ret[wpos++] = keyword[j];
1581 if (i != keywordCount - 1)
1589 * Convert SKS URI to a string.
1591 * @param uri sks uri to convert
1592 * @return NULL on error
1595 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1597 const GNUNET_HashCode * namespace;
1598 const char *identifier;
1600 struct GNUNET_CRYPTO_HashAsciiEncoded ns;
1602 if (uri->type != sks)
1604 namespace = &uri->data.sks.namespace;
1605 identifier = uri->data.sks.identifier;
1606 GNUNET_CRYPTO_hash_to_enc (namespace, &ns);
1607 GNUNET_asprintf (&ret,
1609 GNUNET_FS_URI_PREFIX,
1610 GNUNET_FS_URI_SKS_INFIX,
1611 (const char *) &ns, identifier);
1617 * Convert a CHK URI to a string.
1619 * @param uri chk uri to convert
1620 * @return NULL on error
1623 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1625 const struct FileIdentifier * fi;
1627 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1628 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1630 if (uri->type != chk)
1632 fi = &uri->data.chk;
1633 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1634 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1636 GNUNET_asprintf (&ret,
1638 GNUNET_FS_URI_PREFIX,
1639 GNUNET_FS_URI_CHK_INFIX,
1640 (const char *) &keyhash,
1641 (const char *) &queryhash,
1642 GNUNET_ntohll (fi->file_length));
1647 * Convert binary data to a string.
1649 * @param data binary data to convert
1650 * @param size number of bytes in data
1651 * @return converted data
1654 bin2enc (const void *data, size_t size)
1657 * 64 characters for encoding, 6 bits per character
1660 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=";
1668 GNUNET_assert (strlen (tbl) == 64);
1670 if (((size * 8) % 6) != 0)
1672 ret = GNUNET_malloc (len + 1);
1677 for (pos = 0; pos < size; pos++)
1679 bits |= ((((const unsigned char *) data)[pos]) << hbits);
1683 ret[len++] = tbl[bits & 63];
1689 ret[len++] = tbl[bits & 63];
1695 * Convert a LOC URI to a string.
1697 * @param uri loc uri to convert
1698 * @return NULL on error
1701 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
1704 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1705 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1709 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
1710 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
1711 peerId = bin2enc (&uri->data.loc.peer,
1712 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1713 peerSig = bin2enc (&uri->data.loc.contentSignature,
1714 sizeof (struct GNUNET_CRYPTO_RsaSignature));
1715 GNUNET_asprintf (&ret,
1716 "%s%s%s.%s.%llu.%s.%s.%llu",
1717 GNUNET_FS_URI_PREFIX,
1718 GNUNET_FS_URI_LOC_INFIX,
1719 (const char *) &keyhash,
1720 (const char *) &queryhash,
1721 (unsigned long long) GNUNET_ntohll (uri->data.loc.fi.file_length),
1724 (unsigned long long) uri->data.loc.expirationTime.value);
1725 GNUNET_free (peerSig);
1726 GNUNET_free (peerId);
1732 * Convert a URI to a UTF-8 String.
1734 * @param uri uri to convert to a string
1735 * @return the UTF-8 string
1738 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
1748 return uri_ksk_to_string (uri);
1750 return uri_sks_to_string (uri);
1752 return uri_chk_to_string (uri);
1754 return uri_loc_to_string (uri);
1761 /* end of fs_uri.c */