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);
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)) ||
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;
399 unsigned long long flen;
401 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
402 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
404 GNUNET_assert (s != NULL);
407 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX);
408 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
409 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX,
411 return NULL; /* not a CHK URI */
412 if ( (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
413 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
415 *emsg = GNUNET_strdup (_("Malformed CHK URI"));
420 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
421 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
423 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
424 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
425 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
427 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
429 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
431 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
435 *emsg = GNUNET_strdup (_("Malformed CHK URI"));
438 fi.file_length = GNUNET_htonll (flen);
439 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
447 * Convert a character back to the binary value
448 * that it represents (given base64-encoding).
450 * @param a character to convert
451 * @return offset in the "tbl" array
454 c2v (unsigned char a)
456 if ((a >= '0') && (a <= '9'))
458 if ((a >= 'A') && (a <= 'Z'))
459 return (a - 'A' + 10);
460 if ((a >= 'a') && (a <= 'z'))
461 return (a - 'a' + 36);
471 * Convert string back to binary data.
473 * @param input '\\0'-terminated string
474 * @param data where to write binary data
475 * @param size how much data should be converted
476 * @return number of characters processed from input,
480 enc2bin (const char *input, void *data, size_t size)
488 if (((size * 8) % 6) != 0)
490 if (strlen (input) < len)
491 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 unsigned long long flen;
548 struct GNUNET_TIME_Absolute et;
549 struct GNUNET_CRYPTO_RsaSignature sig;
550 struct LocUriAssembly ass;
554 GNUNET_assert (s != NULL);
556 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX);
557 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
558 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX,
560 return NULL; /* not an SKS URI */
561 if ( (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
562 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
564 *emsg = GNUNET_strdup (_("SKS URI malformed"));
569 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
570 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
572 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
573 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
574 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
576 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
578 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
579 &ass.fi.chk.query)) ||
580 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
584 *emsg = GNUNET_strdup (_("SKS URI malformed"));
587 ass.fi.file_length = GNUNET_htonll (flen);
589 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
590 while ((s[npos] != '\0') && (s[npos] != '.'))
594 *emsg = GNUNET_strdup (_("SKS URI malformed"));
598 ret = enc2bin (&s[npos],
600 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
603 *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode public key)"));
607 if (s[npos++] != '.')
609 *emsg = GNUNET_strdup (_("SKS URI malformed (could not find signature)"));
612 ret = enc2bin (&s[npos],
614 sizeof (struct GNUNET_CRYPTO_RsaSignature));
617 *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode signature)"));
621 if (s[npos++] != '.')
623 *emsg = GNUNET_strdup (_("SKS URI malformed"));
626 if (1 != SSCANF (&s[npos], "%llu", &exptime))
628 *emsg = GNUNET_strdup (_("SKS URI malformed (could not parse expiration time)"));
631 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
632 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
634 ass.exptime = GNUNET_TIME_absolute_hton (et);
636 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT,
641 *emsg = GNUNET_strdup (_("SKS URI malformed (signature failed validation)"));
644 uri = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
646 uri->data.loc.fi = ass.fi;
647 uri->data.loc.peer = ass.peer;
648 uri->data.loc.expirationTime = et;
649 uri->data.loc.contentSignature = sig;
658 * Convert a UTF-8 String to a URI.
660 * @param uri string to parse
661 * @param emsg where to store the parser error message (if any)
662 * @return NULL on error
664 struct GNUNET_FS_Uri *
665 GNUNET_FS_uri_parse (const char *uri,
668 struct GNUNET_FS_Uri *ret;
674 if ( (NULL != (ret = uri_chk_parse (uri, emsg))) ||
675 (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
676 (NULL != (ret = uri_sks_parse (uri, emsg))) ||
677 (NULL != (ret = uri_loc_parse (uri, emsg))) )
680 *emsg = GNUNET_strdup (_("Unrecognized URI type"));
690 * @param uri uri to free
693 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
697 GNUNET_assert (uri != NULL);
701 for (i = 0; i < uri->data.ksk.keywordCount; i++)
702 GNUNET_free (uri->data.ksk.keywords[i]);
703 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
707 GNUNET_free (uri->data.sks.identifier);
719 * How many keywords are ANDed in this keyword URI?
721 * @param uri ksk uri to get the number of keywords from
722 * @return 0 if this is not a keyword URI
725 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
727 if (uri->type != ksk)
729 return uri->data.ksk.keywordCount;
734 * Iterate over all keywords in this keyword URI.
736 * @param uri ksk uri to get the keywords from
737 * @param iterator function to call on each keyword
738 * @param iterator_cls closure for iterator
739 * @return -1 if this is not a keyword URI, otherwise number of
740 * keywords iterated over until iterator aborted
743 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
744 GNUNET_FS_KeywordIterator iterator,
750 if (uri->type != ksk)
752 if (iterator == NULL)
753 return uri->data.ksk.keywordCount;
754 for (i = 0; i < uri->data.ksk.keywordCount; i++)
756 keyword = uri->data.ksk.keywords[i];
757 /* first character of keyword indicates
758 if it is mandatory or not */
759 if (GNUNET_OK != iterator (iterator_cls,
769 * Obtain the identity of the peer offering the data
771 * @param uri the location URI to inspect
772 * @param peer where to store the identify of the peer (presumably) offering the content
773 * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
776 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
777 struct GNUNET_PeerIdentity * peer)
779 if (uri->type != loc)
780 return GNUNET_SYSERR;
781 GNUNET_CRYPTO_hash (&uri->data.loc.peer,
782 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
789 * Obtain the expiration of the LOC URI.
791 * @param uri location URI to get the expiration from
792 * @return expiration time of the URI
794 struct GNUNET_TIME_Absolute
795 GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri)
797 GNUNET_assert (uri->type == loc);
798 return uri->data.loc.expirationTime;
804 * Obtain the URI of the content itself.
806 * @param uri location URI to get the content URI from
807 * @return NULL if argument is not a location URI
809 struct GNUNET_FS_Uri *
810 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
812 struct GNUNET_FS_Uri *ret;
814 if (uri->type != loc)
816 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
818 ret->data.chk = uri->data.loc.fi;
824 * Construct a location URI (this peer will be used for the location).
826 * @param baseUri content offered by the sender
827 * @param cfg configuration information (used to find our hostkey)
828 * @param expiration_time how long will the content be offered?
829 * @return the location URI, NULL on error
831 struct GNUNET_FS_Uri *
832 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
833 const struct GNUNET_CONFIGURATION_Handle *cfg,
834 struct GNUNET_TIME_Absolute expiration_time)
836 struct GNUNET_FS_Uri *uri;
837 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
838 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
840 struct LocUriAssembly ass;
842 if (baseUri->type != chk)
845 GNUNET_CONFIGURATION_get_value_filename (cfg,
847 "HOSTKEY", &keyfile))
849 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
851 ("Lacking key configuration settings.\n"));
854 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
855 if (my_private_key == NULL)
857 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
858 _("Could not access hostkey file `%s'.\n"),
860 GNUNET_free (keyfile);
863 GNUNET_free (keyfile);
864 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
865 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
866 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
867 ass.exptime = GNUNET_TIME_absolute_hton (expiration_time);
868 ass.fi = baseUri->data.chk;
869 ass.peer = my_public_key;
870 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
872 uri->data.loc.fi = baseUri->data.chk;
873 uri->data.loc.expirationTime = expiration_time;
874 uri->data.loc.peer = my_public_key;
875 GNUNET_assert (GNUNET_OK ==
876 GNUNET_CRYPTO_rsa_sign (my_private_key,
878 &uri->data.loc.contentSignature));
879 GNUNET_CRYPTO_rsa_key_free (my_private_key);
885 * Create an SKS URI from a namespace and an identifier.
887 * @param ns namespace
888 * @param id identifier
889 * @param emsg where to store an error message
890 * @return an FS URI for the given namespace and identifier
892 struct GNUNET_FS_Uri *
893 GNUNET_FS_uri_sks_create (struct GNUNET_FS_Namespace *ns,
897 struct GNUNET_FS_Uri *ns_uri;
898 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
900 ns_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
902 GNUNET_CRYPTO_rsa_key_get_public (ns->key,
904 GNUNET_CRYPTO_hash (&pk,
906 &ns_uri->data.sks.namespace);
907 ns_uri->data.sks.identifier = GNUNET_strdup (id);
913 * Canonicalize a keyword.
915 * @param in input string (the keyword)
916 * @return canonicalized keyword
919 canonicalize_keyword (const char *in)
925 ret = GNUNET_strdup (in);
928 while ('\0' != *rpos)
930 switch (tolower( (unsigned char) *rpos))
941 /* skip characters listed above */
964 /* convert characters listed above to lower case */
965 *wpos = tolower( (unsigned char)*rpos);
971 /* keep characters listed above without changes */
976 /* replace characters listed above with '_' */
987 * Canonicalize keyword URI. Performs operations such
988 * as decapitalization and removal of certain characters.
989 * (useful for search).
991 * @param uri the URI to canonicalize
992 * @return canonicalized version of the URI, NULL on error
994 struct GNUNET_FS_Uri *
995 GNUNET_FS_uri_ksk_canonicalize (const struct GNUNET_FS_Uri *uri)
997 struct GNUNET_FS_Uri *ret;
1002 kc = uri->data.ksk.keywordCount;
1003 kl = GNUNET_malloc (kc*sizeof(char*));
1005 kl[i] = canonicalize_keyword (uri->data.ksk.keywords[i]);
1006 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
1008 ret->data.ksk.keywordCount = kc;
1009 ret->data.ksk.keywords = kl;
1015 * Merge the sets of keywords from two KSK URIs.
1016 * (useful for merging the canonicalized keywords with
1017 * the original keywords for sharing).
1019 * @param u1 first uri
1020 * @param u2 second uri
1021 * @return merged URI, NULL on error
1023 struct GNUNET_FS_Uri *
1024 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
1025 const struct GNUNET_FS_Uri *u2)
1027 struct GNUNET_FS_Uri *ret;
1035 if ( (u1 == NULL) && (u2 == NULL) )
1038 return GNUNET_FS_uri_dup (u2);
1040 return GNUNET_FS_uri_dup (u1);
1041 if ( (u1->type != ksk) ||
1047 kc = u1->data.ksk.keywordCount;
1048 kl = GNUNET_malloc ((kc+u2->data.ksk.keywordCount)*sizeof(char*));
1049 for (i=0;i<u1->data.ksk.keywordCount;i++)
1050 kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]);
1051 for (i=0;i<u2->data.ksk.keywordCount;i++)
1053 kp = u2->data.ksk.keywords[i];
1055 for (j=0;j<u1->data.ksk.keywordCount;j++)
1056 if (0 == strcmp(kp + 1,
1065 kl[kc++] = GNUNET_strdup (kp - 1);
1067 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
1069 ret->data.ksk.keywordCount = kc;
1070 ret->data.ksk.keywords = kl;
1078 * @param uri the URI to duplicate
1079 * @return copy of the URI
1081 struct GNUNET_FS_Uri *
1082 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
1084 struct GNUNET_FS_Uri *ret;
1089 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1090 memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
1094 if (ret->data.ksk.keywordCount >= GNUNET_MAX_MALLOC_CHECKED / sizeof (char*))
1100 if (ret->data.ksk.keywordCount > 0)
1102 ret->data.ksk.keywords
1103 = GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
1104 for (i = 0; i < ret->data.ksk.keywordCount; i++)
1105 ret->data.ksk.keywords[i] =
1106 GNUNET_strdup (uri->data.ksk.keywords[i]);
1109 ret->data.ksk.keywords = NULL; /* just to be sure */
1112 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
1124 * Create an FS URI from a single user-supplied string of keywords.
1125 * The string is broken up at spaces into individual keywords.
1126 * Keywords that start with "+" are mandatory. Double-quotes can
1127 * be used to prevent breaking up strings at spaces (and also
1128 * to specify non-mandatory keywords starting with "+").
1130 * Keywords must contain a balanced number of double quotes and
1131 * double quotes can not be used in the actual keywords (for
1132 * example, the string '""foo bar""' will be turned into two
1133 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
1135 * @param keywords the keyword string
1136 * @param emsg where to store an error message
1137 * @return an FS URI for the given keywords, NULL
1138 * if keywords is not legal (i.e. empty).
1140 struct GNUNET_FS_Uri *
1141 GNUNET_FS_uri_ksk_create (const char *keywords,
1145 unsigned int num_Words;
1148 struct GNUNET_FS_Uri *uri;
1152 if (keywords == NULL)
1157 searchString = GNUNET_strdup (keywords);
1162 while ('\0' != *pos)
1164 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1168 else if (0 == inWord)
1174 saw_quote = (saw_quote + 1) % 2;
1179 GNUNET_free (searchString);
1180 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1185 GNUNET_free (searchString);
1186 *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
1189 keywordarr = GNUNET_malloc (num_Words * sizeof (char *));
1193 while ('\0' != *pos)
1195 if ((saw_quote == 0) && (isspace ( (unsigned char) *pos)))
1200 else if (0 == inWord)
1202 keywordarr[num_Words] = pos;
1207 saw_quote = (saw_quote + 1) % 2;
1211 GNUNET_FS_uri_ksk_create_from_args (num_Words,
1212 (const char **) keywordarr);
1213 GNUNET_free (keywordarr);
1214 GNUNET_free (searchString);
1220 * Create an FS URI from a user-supplied command line of keywords.
1221 * Arguments should start with "+" to indicate mandatory
1224 * @param argc number of keywords
1225 * @param argv keywords (double quotes are not required for
1226 * keywords containing spaces; however, double
1227 * quotes are required for keywords starting with
1228 * "+"); there is no mechanism for having double
1229 * quotes in the actual keywords (if the user
1230 * did specifically specify double quotes, the
1231 * caller should convert each double quote
1232 * into two single quotes).
1233 * @return an FS URI for the given keywords, NULL
1234 * if keywords is not legal (i.e. empty).
1236 struct GNUNET_FS_Uri *
1237 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
1241 struct GNUNET_FS_Uri *uri;
1242 const char *keyword;
1250 /* allow URI to be given as one and only keyword and
1251 handle accordingly */
1254 (strlen(argv[0]) > strlen(GNUNET_FS_URI_PREFIX)) &&
1255 (0 == strncmp(argv[0], GNUNET_FS_URI_PREFIX, strlen(GNUNET_FS_URI_PREFIX)) ) &&
1256 (NULL != (uri = GNUNET_FS_uri_parse(argv[0], &emsg)) ) )
1258 GNUNET_free_non_null (emsg);
1259 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1261 uri->data.ksk.keywordCount = argc;
1262 uri->data.ksk.keywords = GNUNET_malloc (argc * sizeof (char *));
1263 for (i = 0; i < argc; i++)
1266 if (keyword[0] == '+')
1267 val = GNUNET_strdup (keyword);
1269 GNUNET_asprintf (&val, " %s", keyword);
1280 uri->data.ksk.keywords[i] = val;
1287 * Test if two URIs are equal.
1289 * @param u1 one of the URIs
1290 * @param u2 the other URI
1291 * @return GNUNET_YES if the URIs are equal
1294 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
1295 const struct GNUNET_FS_Uri *u2)
1301 GNUNET_assert (u1 != NULL);
1302 GNUNET_assert (u2 != NULL);
1303 if (u1->type != u2->type)
1308 if (0 == memcmp (&u1->data.chk,
1310 sizeof (struct FileIdentifier)))
1314 if ((0 == memcmp (&u1->data.sks.namespace,
1315 &u2->data.sks.namespace,
1316 sizeof (GNUNET_HashCode))) &&
1317 (0 == strcmp (u1->data.sks.identifier,
1318 u2->data.sks.identifier)))
1323 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
1325 for (i = 0; i < u1->data.ksk.keywordCount; i++)
1328 for (j = 0; j < u2->data.ksk.keywordCount; j++)
1330 if (0 == strcmp (u1->data.ksk.keywords[i],
1331 u2->data.ksk.keywords[j]))
1337 if (ret == GNUNET_NO)
1342 if (memcmp (&u1->data.loc,
1344 sizeof (struct FileIdentifier) +
1345 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1346 sizeof (struct GNUNET_TIME_Absolute) +
1347 sizeof (unsigned short) + sizeof (unsigned short)) != 0)
1357 * Is this a namespace URI?
1359 * @param uri the uri to check
1360 * @return GNUNET_YES if this is an SKS uri
1363 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
1365 return uri->type == sks;
1370 * Get the ID of a namespace from the given
1373 * @param uri the uri to get the namespace ID from
1374 * @param nsid where to store the ID of the namespace
1375 * @return GNUNET_OK on success
1378 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
1379 GNUNET_HashCode * nsid)
1381 if (! GNUNET_FS_uri_test_sks (uri))
1384 return GNUNET_SYSERR;
1386 *nsid = uri->data.sks.namespace;
1392 * Get the content identifier of an SKS URI.
1394 * @param uri the sks uri
1395 * @return NULL on error (not a valid SKS URI)
1398 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1400 if (!GNUNET_FS_uri_test_sks (uri))
1405 return GNUNET_strdup (uri->data.sks.identifier);
1410 * Convert namespace URI to a human readable format
1411 * (using the namespace description, if available).
1413 * @param cfg configuration to use
1414 * @param uri SKS uri to convert
1415 * @return NULL on error (not an SKS URI)
1418 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
1419 const struct GNUNET_FS_Uri *uri)
1424 if (uri->type != sks)
1426 name = GNUNET_PSEUDONYM_id_to_name (cfg, &uri->data.sks.namespace);
1428 return GNUNET_FS_uri_to_string (uri);
1429 GNUNET_asprintf (&ret,
1432 uri->data.sks.identifier);
1439 * Is this a keyword URI?
1441 * @param uri the uri
1442 * @return GNUNET_YES if this is a KSK uri
1445 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1450 if (uri->type == ksk)
1452 for (i = uri->data.ksk.keywordCount - 1; i >= 0; i--)
1453 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1456 return uri->type == ksk;
1461 * Is this a file (or directory) URI?
1463 * @param uri the uri to check
1464 * @return GNUNET_YES if this is a CHK uri
1467 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1469 return uri->type == chk;
1474 * What is the size of the file that this URI
1477 * @param uri the CHK URI to inspect
1478 * @return size of the file as specified in the CHK URI
1481 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri)
1486 return GNUNET_ntohll (uri->data.chk.file_length);
1488 return GNUNET_ntohll (uri->data.loc.fi.file_length);
1492 return 0; /* unreachable */
1497 * Is this a location URI?
1499 * @param uri the uri to check
1500 * @return GNUNET_YES if this is a LOC uri
1503 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1505 return uri->type == loc;
1510 * Function called on each value in the meta data.
1511 * Adds it to the URI.
1513 * @param cls URI to update
1514 * @param plugin_name name of the plugin that produced this value;
1515 * special values can be used (i.e. '<zlib>' for zlib being
1516 * used in the main libextractor library and yielding
1518 * @param type libextractor-type describing the meta data
1519 * @param format basic format information about data
1520 * @param data_mime_type mime-type of data (not of the original file);
1521 * can be NULL (if mime-type is not known)
1522 * @param data actual meta-data found
1523 * @param data_len number of bytes in data
1524 * @return 0 (always)
1527 gather_uri_data (void *cls,
1528 const char *plugin_name,
1529 enum EXTRACTOR_MetaType type,
1530 enum EXTRACTOR_MetaFormat format,
1531 const char *data_mime_type,
1535 struct GNUNET_FS_Uri *uri = cls;
1539 if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
1540 (format != EXTRACTOR_METAFORMAT_C_STRING) )
1542 for (j = uri->data.ksk.keywordCount - 1; j >= 0; j--)
1543 if (0 == strcmp (&uri->data.ksk.keywords[j][1], data))
1545 nkword = GNUNET_malloc (strlen (data) + 2);
1546 strcpy (nkword, " "); /* not mandatory */
1547 strcat (nkword, data);
1548 uri->data.ksk.keywords[uri->data.ksk.keywordCount++] = nkword;
1554 * Construct a keyword-URI from meta-data (take all entries
1555 * in the meta-data and construct one large keyword URI
1556 * that lists all keywords that can be found in the meta-data).
1558 * @param md metadata to use
1559 * @return NULL on error, otherwise a KSK URI
1561 struct GNUNET_FS_Uri *
1562 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
1564 struct GNUNET_FS_Uri *ret;
1568 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1570 ret->data.ksk.keywordCount = 0;
1571 ret->data.ksk.keywords = NULL;
1572 ret->data.ksk.keywords
1573 = GNUNET_malloc (sizeof (char *) *
1574 GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL));
1575 GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
1582 * In URI-encoding, does the given character
1583 * need to be encoded using %-encoding?
1586 needs_percent (char c)
1588 return (!((isalnum ( (unsigned char) c)) ||
1589 (c == '-') || (c == '_') || (c == '.') || (c == '~')));
1594 * Convert a KSK URI to a string.
1596 * @param uri the URI to convert
1597 * @return NULL on error (i.e. keywordCount == 0)
1600 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1603 unsigned int keywordCount;
1610 const char *keyword;
1612 if (uri->type != ksk)
1614 keywords = uri->data.ksk.keywords;
1615 keywordCount = uri->data.ksk.keywordCount;
1617 keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1618 strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1619 for (i = 0; i < keywordCount; i++)
1621 keyword = keywords[i];
1622 slen = strlen (keyword);
1624 for (j = 0; j < slen; j++)
1626 if ((j == 0) && (keyword[j] == ' '))
1629 continue; /* skip leading space */
1631 if (needs_percent (keyword[j]))
1632 n += 2; /* will use %-encoding */
1635 ret = GNUNET_malloc (n);
1636 strcpy (ret, GNUNET_FS_URI_PREFIX);
1637 strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1638 wpos = strlen (ret);
1639 for (i = 0; i < keywordCount; i++)
1641 keyword = keywords[i];
1642 slen = strlen (keyword);
1643 for (j = 0; j < slen; j++)
1645 if ((j == 0) && (keyword[j] == ' '))
1646 continue; /* skip leading space */
1647 if (needs_percent (keyword[j]))
1649 sprintf (&ret[wpos], "%%%02X", keyword[j]);
1654 ret[wpos++] = keyword[j];
1657 if (i != keywordCount - 1)
1665 * Convert SKS URI to a string.
1667 * @param uri sks uri to convert
1668 * @return NULL on error
1671 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1673 const GNUNET_HashCode * namespace;
1674 const char *identifier;
1676 struct GNUNET_CRYPTO_HashAsciiEncoded ns;
1678 if (uri->type != sks)
1680 namespace = &uri->data.sks.namespace;
1681 identifier = uri->data.sks.identifier;
1682 GNUNET_CRYPTO_hash_to_enc (namespace, &ns);
1683 GNUNET_asprintf (&ret,
1685 GNUNET_FS_URI_PREFIX,
1686 GNUNET_FS_URI_SKS_INFIX,
1687 (const char *) &ns, identifier);
1693 * Convert a CHK URI to a string.
1695 * @param uri chk uri to convert
1696 * @return NULL on error
1699 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1701 const struct FileIdentifier * fi;
1703 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1704 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1706 if (uri->type != chk)
1708 fi = &uri->data.chk;
1709 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1710 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1712 GNUNET_asprintf (&ret,
1714 GNUNET_FS_URI_PREFIX,
1715 GNUNET_FS_URI_CHK_INFIX,
1716 (const char *) &keyhash,
1717 (const char *) &queryhash,
1718 GNUNET_ntohll (fi->file_length));
1723 * Convert binary data to a string.
1725 * @param data binary data to convert
1726 * @param size number of bytes in data
1727 * @return converted data
1730 bin2enc (const void *data, size_t size)
1733 * 64 characters for encoding, 6 bits per character
1736 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=";
1744 GNUNET_assert (strlen (tbl) == 64);
1746 if (((size * 8) % 6) != 0)
1748 ret = GNUNET_malloc (len + 1);
1753 for (pos = 0; pos < size; pos++)
1755 bits |= ((((const unsigned char *) data)[pos]) << hbits);
1759 ret[len++] = tbl[bits & 63];
1765 ret[len] = tbl[bits & 63];
1771 * Convert a LOC URI to a string.
1773 * @param uri loc uri to convert
1774 * @return NULL on error
1777 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
1780 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1781 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1785 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
1786 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
1787 peerId = bin2enc (&uri->data.loc.peer,
1788 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1789 peerSig = bin2enc (&uri->data.loc.contentSignature,
1790 sizeof (struct GNUNET_CRYPTO_RsaSignature));
1791 GNUNET_asprintf (&ret,
1792 "%s%s%s.%s.%llu.%s.%s.%llu",
1793 GNUNET_FS_URI_PREFIX,
1794 GNUNET_FS_URI_LOC_INFIX,
1795 (const char *) &keyhash,
1796 (const char *) &queryhash,
1797 (unsigned long long) GNUNET_ntohll (uri->data.loc.fi.file_length),
1800 (unsigned long long) uri->data.loc.expirationTime.value);
1801 GNUNET_free (peerSig);
1802 GNUNET_free (peerId);
1808 * Convert a URI to a UTF-8 String.
1810 * @param uri uri to convert to a string
1811 * @return the UTF-8 string
1814 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
1824 return uri_ksk_to_string (uri);
1826 return uri_sks_to_string (uri);
1828 return uri_chk_to_string (uri);
1830 return uri_loc_to_string (uri);
1837 /* end of fs_uri.c */