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 ecrs identifiers (MODULE = "ecrs").
32 * This module only parses URIs for the AFS module. The ECRS 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://ecrs/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://ecrs/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://ecrs/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://ecrs/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))
218 continue; /* skip double quote */
219 out[wpos++] = (char) hx;
223 out[wpos++] = out[rpos++];
229 ret = GNUNET_strdup (out);
233 /* need to prefix with space */
234 ret = GNUNET_malloc (strlen (out) + 2);
246 * @param s an uri string
247 * @param emsg where to store the parser error message (if any)
248 * @return NULL on error, otherwise the KSK URI
250 static struct GNUNET_FS_Uri *
251 uri_ksk_parse (const char *s, char **emsg)
253 struct GNUNET_FS_Uri *ret;
263 GNUNET_assert (s != NULL);
265 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX);
266 if ( (slen <= pos) ||
267 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX,
269 (s[slen - 1] == '+') ||
271 return NULL; /* no keywords / malformed */
275 for (i = pos; i < slen; i++)
277 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
279 saw_quote = (saw_quote + 1) % 2;
283 if ((s[i] == '+') && (saw_quote == 0))
287 return NULL; /* "++" not allowed */
291 return NULL; /* quotes not balanced */
293 dup = GNUNET_strdup (s);
294 keywords = GNUNET_malloc (max * sizeof (char *));
295 for (i = slen - 1; i >= pos; i--)
297 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
299 saw_quote = (saw_quote + 1) % 2;
303 if ((dup[i] == '+') && (saw_quote == 0))
305 keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg);
306 if (NULL == keywords[max])
311 keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
312 if (NULL == keywords[max])
314 GNUNET_assert (max == 0);
316 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
318 ret->data.ksk.keywordCount = iret;
319 ret->data.ksk.keywords = keywords;
322 for (i = 0; i < max; i++)
323 GNUNET_free_non_null (keywords[i]);
324 GNUNET_free (keywords);
333 * @param s an uri string
334 * @param emsg where to store the parser error message (if any)
335 * @return NULL on error, SKS URI otherwise
337 static struct GNUNET_FS_Uri *
338 uri_sks_parse (const char *s, char **emsg)
340 struct GNUNET_FS_Uri *ret;
341 GNUNET_HashCode namespace;
345 char enc[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
347 GNUNET_assert (s != NULL);
349 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX);
350 if ( (slen <= pos) ||
351 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX,
353 (slen < pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
354 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '/') )
356 memcpy (enc, &s[pos], sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
357 enc[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
358 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (enc, &namespace))
360 identifier = GNUNET_strdup (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]);
361 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
363 ret->data.sks.namespace = namespace;
364 ret->data.sks.identifier = identifier;
372 * @param s an uri string
373 * @param emsg where to store the parser error message (if any)
374 * @return NULL on error, CHK URI otherwise
376 static struct GNUNET_FS_Uri *
377 uri_chk_parse (const char *s, char **emsg)
379 struct GNUNET_FS_Uri *ret;
380 struct FileIdentifier fi;
383 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
384 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
386 GNUNET_assert (s != NULL);
389 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX);
390 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
391 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX,
393 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
394 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
399 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
400 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
402 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
403 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
404 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
406 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
408 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
410 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
414 fi.file_length = GNUNET_htonll (fi.file_length);
416 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
424 * Convert a character back to the binary value
425 * that it represents (given base64-encoding).
427 * @param a character to convert
428 * @return offset in the "tbl" array
431 c2v (unsigned char a)
433 if ((a >= '0') && (a <= '9'))
435 if ((a >= 'A') && (a <= 'Z'))
436 return (a - 'A' + 10);
437 if ((a >= 'a') && (a <= 'z'))
438 return (a - 'a' + 36);
448 * Convert string back to binary data.
450 * @param input '\0'-terminated string
451 * @param data where to write binary data
452 * @param size how much data should be converted
453 * @return number of characters processed from input,
457 enc2bin (const char *input, void *data, size_t size)
465 if (((size * 8) % 6) != 0)
467 if (strlen (input) < len)
468 return -1; /* error! */
473 for (pos = 0; pos < size; pos++)
477 bits |= (c2v (input[len++]) << hbits);
480 (((unsigned char *) data)[pos]) = (unsigned char) bits;
489 * Structure that defines how the
490 * contents of a location URI must be
491 * assembled in memory to create or
492 * verify the signature of a location
495 struct LocUriAssembly
497 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
499 struct GNUNET_TIME_AbsoluteNBO exptime;
501 struct FileIdentifier fi;
503 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded peer;
510 * Also verifies validity of the location URI.
512 * @param s an uri string
513 * @param emsg where to store the parser error message (if any)
514 * @return NULL on error, valid LOC URI otherwise
516 static struct GNUNET_FS_Uri *
517 uri_loc_parse (const char *s, char **emsg)
519 struct GNUNET_FS_Uri *uri;
520 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
521 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
524 unsigned long long exptime;
525 struct GNUNET_TIME_Absolute et;
526 struct GNUNET_CRYPTO_RsaSignature sig;
527 struct LocUriAssembly ass;
532 GNUNET_assert (s != NULL);
534 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX);
535 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
536 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX,
538 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
539 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
544 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
545 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
547 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
548 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
549 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
551 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
553 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
554 &ass.fi.chk.query)) ||
555 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
557 &ass.fi.file_length)) )
559 ass.fi.file_length = GNUNET_htonll (ass.fi.file_length);
561 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
562 while ((s[npos] != '\0') && (s[npos] != '.'))
566 ret = enc2bin (&s[npos],
568 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
572 if (s[npos++] != '.')
574 ret = enc2bin (&s[npos],
576 sizeof (struct GNUNET_CRYPTO_RsaSignature));
580 if (s[npos++] != '.')
582 if (1 != SSCANF (&s[npos], "%llu", &exptime))
584 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
585 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_NAMESPACE_PLACEMENT);
587 ass.exptime = GNUNET_TIME_absolute_hton (et);
589 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_NAMESPACE_PLACEMENT,
595 uri = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
597 uri->data.loc.fi = ass.fi;
598 uri->data.loc.peer = ass.peer;
599 uri->data.loc.expirationTime = et;
600 uri->data.loc.contentSignature = sig;
604 GNUNET_free_non_null (addr);
610 * Convert a UTF-8 String to a URI.
612 * @param uri string to parse
613 * @param emsg where to store the parser error message (if any)
614 * @return NULL on error
616 struct GNUNET_FS_Uri *
617 GNUNET_FS_uri_parse (const char *uri,
620 struct GNUNET_FS_Uri *ret;
622 if ( (NULL != (ret = uri_chk_parse (uri, emsg))) ||
623 (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
624 (NULL != (ret = uri_sks_parse (uri, emsg))) ||
625 (NULL != (ret = uri_loc_parse (uri, emsg))) )
634 * @param uri uri to free
637 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
641 GNUNET_assert (uri != NULL);
645 for (i = 0; i < uri->data.ksk.keywordCount; i++)
646 GNUNET_free (uri->data.ksk.keywords[i]);
647 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
651 GNUNET_free (uri->data.sks.identifier);
663 * How many keywords are ANDed in this keyword URI?
665 * @param uri ksk uri to get the number of keywords from
666 * @return 0 if this is not a keyword URI
669 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
671 if (uri->type != ksk)
673 return uri->data.ksk.keywordCount;
678 * Iterate over all keywords in this keyword URI.
680 * @param uri ksk uri to get the keywords from
681 * @param iterator function to call on each keyword
682 * @param iterator_cls closure for iterator
683 * @return -1 if this is not a keyword URI, otherwise number of
684 * keywords iterated over until iterator aborted
687 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
688 GNUNET_FS_KeywordIterator iterator,
694 if (uri->type != ksk)
696 if (iterator == NULL)
697 return uri->data.ksk.keywordCount;
698 for (i = 0; i < uri->data.ksk.keywordCount; i++)
700 keyword = uri->data.ksk.keywords[i];
701 /* first character of keyword indicates
702 if it is mandatory or not */
703 if (GNUNET_OK != iterator (iterator_cls,
713 * Obtain the identity of the peer offering the data
715 * @param uri the location URI to inspect
716 * @param peer where to store the identify of the peer (presumably) offering the content
717 * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
720 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
721 struct GNUNET_PeerIdentity * peer)
723 if (uri->type != loc)
724 return GNUNET_SYSERR;
725 GNUNET_CRYPTO_hash (&uri->data.loc.peer,
726 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
733 * Obtain the URI of the content itself.
735 * @param uri location URI to get the content URI from
736 * @return NULL if argument is not a location URI
738 struct GNUNET_FS_Uri *
739 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
741 struct GNUNET_FS_Uri *ret;
743 if (uri->type != loc)
745 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
747 ret->data.chk = uri->data.loc.fi;
753 * Construct a location URI (this peer will be used for the location).
755 * @param baseURI content offered by the sender
756 * @param cfg configuration information (used to find our hostkey)
757 * @param expiration_time how long will the content be offered?
758 * @return the location URI, NULL on error
760 struct GNUNET_FS_Uri *
761 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
762 struct GNUNET_CONFIGURATION_Handle *cfg,
763 struct GNUNET_TIME_Absolute expiration_time)
765 struct GNUNET_FS_Uri *uri;
766 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
767 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
769 struct LocUriAssembly ass;
771 if (baseUri->type != chk)
774 GNUNET_CONFIGURATION_get_value_filename (cfg,
776 "HOSTKEY", &keyfile))
778 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
780 ("Lacking key configuration settings.\n"));
783 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
784 if (my_private_key == NULL)
786 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
787 _("Could not access hostkey file `%s'.\n"),
789 GNUNET_free (keyfile);
792 GNUNET_free (keyfile);
793 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
794 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
795 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_NAMESPACE_PLACEMENT);
796 ass.exptime = GNUNET_TIME_absolute_hton (expiration_time);
797 ass.fi = baseUri->data.chk;
798 ass.peer = my_public_key;
799 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
801 uri->data.loc.fi = baseUri->data.chk;
802 uri->data.loc.expirationTime = expiration_time;
803 uri->data.loc.peer = my_public_key;
804 GNUNET_assert (GNUNET_OK ==
805 GNUNET_CRYPTO_rsa_sign (my_private_key,
807 &uri->data.loc.contentSignature));
808 GNUNET_CRYPTO_rsa_key_free (my_private_key);
814 * Canonicalize a keyword.
816 * @param in input string (the keyword)
817 * @return canonicalized keyword
820 canonicalize_keyword (const char *in)
826 ret = GNUNET_strdup (in);
829 while ('\0' != *rpos)
831 switch (tolower(*rpos))
842 /* skip characters listed above */
865 /* convert characters listed above to lower case */
866 *wpos = tolower(*rpos);
872 /* keep characters listed above without changes */
876 /* replace characters listed above with '_' */
887 * Canonicalize keyword URI. Performs operations such
888 * as decapitalization and removal of certain characters.
889 * (useful for search).
891 * @param uri the URI to canonicalize
892 * @return canonicalized version of the URI, NULL on error
894 struct GNUNET_FS_Uri *
895 GNUNET_FS_uri_ksk_canonicalize (const struct GNUNET_FS_Uri *uri)
897 struct GNUNET_FS_Uri *ret;
902 kc = uri->data.ksk.keywordCount;
903 kl = GNUNET_malloc (kc*sizeof(char*));
905 kl[i] = canonicalize_keyword (uri->data.ksk.keywords[i]);
906 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
908 ret->data.ksk.keywordCount = kc;
909 ret->data.ksk.keywords = kl;
915 * Merge the sets of keywords from two KSK URIs.
916 * (useful for merging the canonicalized keywords with
917 * the original keywords for sharing).
919 * @param u1 first uri
920 * @param u2 second uri
921 * @return merged URI, NULL on error
923 struct GNUNET_FS_Uri *
924 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
925 const struct GNUNET_FS_Uri *u2)
927 struct GNUNET_FS_Uri *ret;
935 if ( (u1->type != ksk) ||
941 kc = u1->data.ksk.keywordCount;
942 kl = GNUNET_malloc ((kc+u2->data.ksk.keywordCount)*sizeof(char*));
943 for (i=0;i<u1->data.ksk.keywordCount;i++)
944 kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]);
945 for (i=0;i<u2->data.ksk.keywordCount;i++)
947 kp = u2->data.ksk.keywords[i];
949 for (j=0;j<u1->data.ksk.keywordCount;j++)
950 if (0 == strcmp(kp + 1,
959 kl[kc++] = GNUNET_strdup (kp - 1);
961 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
963 ret->data.ksk.keywordCount = kc;
964 ret->data.ksk.keywords = kl;
972 * @param uri the URI to duplicate
973 * @return copy of the URI
975 struct GNUNET_FS_Uri *
976 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
978 struct GNUNET_FS_Uri *ret;
981 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
982 memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
986 if (ret->data.ksk.keywordCount > 0)
988 ret->data.ksk.keywords
989 = GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
990 for (i = 0; i < ret->data.ksk.keywordCount; i++)
991 ret->data.ksk.keywords[i] =
992 GNUNET_strdup (uri->data.ksk.keywords[i]);
995 ret->data.ksk.keywords = NULL; /* just to be sure */
998 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
1010 * Create an FS URI from a single user-supplied string of keywords.
1011 * The string is broken up at spaces into individual keywords.
1012 * Keywords that start with "+" are mandatory. Double-quotes can
1013 * be used to prevent breaking up strings at spaces (and also
1014 * to specify non-mandatory keywords starting with "+").
1016 * Keywords must contain a balanced number of double quotes and
1017 * double quotes can not be used in the actual keywords (for
1018 * example, the string '""foo bar""' will be turned into two
1019 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
1021 * @param keywords the keyword string
1022 * @param emsg where to store an error message
1023 * @return an FS URI for the given keywords, NULL
1024 * if keywords is not legal (i.e. empty).
1026 struct GNUNET_FS_Uri *
1027 GNUNET_FS_uri_ksk_create (const char *keywords,
1031 unsigned int num_Words;
1034 struct GNUNET_FS_Uri *uri;
1038 if (keywords == NULL)
1043 searchString = GNUNET_strdup (keywords);
1048 while ('\0' != *pos)
1050 if ((saw_quote == 0) && (isspace (*pos)))
1054 else if (0 == inWord)
1060 saw_quote = (saw_quote + 1) % 2;
1065 GNUNET_free (searchString);
1066 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1071 GNUNET_free (searchString);
1072 *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
1075 keywordarr = GNUNET_malloc (num_Words * sizeof (char *));
1079 while ('\0' != *pos)
1081 if ((saw_quote == 0) && (isspace (*pos)))
1086 else if (0 == inWord)
1088 keywordarr[num_Words] = pos;
1093 saw_quote = (saw_quote + 1) % 2;
1097 GNUNET_FS_uri_ksk_create_from_args (num_Words,
1098 (const char **) keywordarr);
1099 GNUNET_free (keywordarr);
1100 GNUNET_free (searchString);
1106 * Create an FS URI from a user-supplied command line of keywords.
1107 * Arguments should start with "+" to indicate mandatory
1110 * @param argc number of keywords
1111 * @param argv keywords (double quotes are not required for
1112 * keywords containing spaces; however, double
1113 * quotes are required for keywords starting with
1114 * "+"); there is no mechanism for having double
1115 * quotes in the actual keywords (if the user
1116 * did specifically specify double quotes, the
1117 * caller should convert each double quote
1118 * into two single quotes).
1119 * @return an FS URI for the given keywords, NULL
1120 * if keywords is not legal (i.e. empty).
1122 struct GNUNET_FS_Uri *
1123 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
1127 struct GNUNET_FS_Uri *uri;
1128 const char *keyword;
1136 /* allow URI to be given as one and only keyword and
1137 handle accordingly */
1140 (strlen(argv[0]) > strlen(GNUNET_FS_URI_PREFIX)) &&
1141 (strncmp(argv[0], GNUNET_FS_URI_PREFIX, strlen(GNUNET_FS_URI_PREFIX)) ) &&
1142 (NULL != (uri = GNUNET_FS_uri_parse(argv[0], &emsg)) ) )
1144 GNUNET_free_non_null (emsg);
1145 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1147 uri->data.ksk.keywordCount = argc;
1148 uri->data.ksk.keywords = GNUNET_malloc (argc * sizeof (char *));
1149 for (i = 0; i < argc; i++)
1152 if (keyword[0] == '+')
1153 val = GNUNET_strdup (keyword);
1155 GNUNET_asprintf (&val, " %s", keyword);
1166 uri->data.ksk.keywords[i] = val;
1173 * Test if two URIs are equal.
1175 * @param u1 one of the URIs
1176 * @param u2 the other URI
1177 * @return GNUNET_YES if the URIs are equal
1180 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
1181 const struct GNUNET_FS_Uri *u2)
1187 GNUNET_assert (u1 != NULL);
1188 GNUNET_assert (u2 != NULL);
1189 if (u1->type != u2->type)
1194 if (0 == memcmp (&u1->data.chk,
1196 sizeof (struct FileIdentifier)))
1200 if ((0 == memcmp (&u1->data.sks.namespace,
1201 &u2->data.sks.namespace,
1202 sizeof (GNUNET_HashCode))) &&
1203 (0 == strcmp (u1->data.sks.identifier,
1204 u2->data.sks.identifier)))
1209 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
1211 for (i = 0; i < u1->data.ksk.keywordCount; i++)
1214 for (j = 0; j < u2->data.ksk.keywordCount; j++)
1216 if (0 == strcmp (u1->data.ksk.keywords[i],
1217 u2->data.ksk.keywords[j]))
1223 if (ret == GNUNET_NO)
1228 if (memcmp (&u1->data.loc,
1230 sizeof (struct FileIdentifier) +
1231 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1232 sizeof (struct GNUNET_TIME_Absolute) +
1233 sizeof (unsigned short) + sizeof (unsigned short)) != 0)
1243 * Is this a namespace URI?
1245 * @param uri the uri to check
1246 * @return GNUNET_YES if this is an SKS uri
1249 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
1251 return uri->type == sks;
1256 * Get the ID of a namespace from the given
1259 * @param uri the uri to get the namespace ID from
1260 * @param nsid where to store the ID of the namespace
1261 * @return GNUNET_OK on success
1264 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
1265 GNUNET_HashCode * nsid)
1267 if (! GNUNET_FS_uri_test_sks (uri))
1270 return GNUNET_SYSERR;
1272 *nsid = uri->data.sks.namespace;
1278 * Get the content identifier of an SKS URI.
1280 * @param uri the sks uri
1281 * @return NULL on error (not a valid SKS URI)
1284 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1286 if (!GNUNET_FS_uri_test_sks (uri))
1291 return GNUNET_strdup (uri->data.sks.identifier);
1296 * Convert namespace URI to a human readable format
1297 * (using the namespace description, if available).
1299 * @param cfg configuration to use
1300 * @param uri SKS uri to convert
1301 * @return NULL on error (not an SKS URI)
1304 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
1305 const struct GNUNET_FS_Uri *uri)
1310 if (uri->type != sks)
1312 name = GNUNET_PSEUDONYM_id_to_name (cfg, &uri->data.sks.namespace);
1314 return GNUNET_FS_uri_to_string (uri);
1315 GNUNET_asprintf (&ret,
1318 uri->data.sks.identifier);
1325 * Is this a keyword URI?
1327 * @param uri the uri
1328 * @return GNUNET_YES if this is a KSK uri
1331 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1336 if (uri->type == ksk)
1338 for (i = uri->data.ksk.keywordCount - 1; i >= 0; i--)
1339 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1342 return uri->type == ksk;
1347 * Is this a file (or directory) URI?
1349 * @param uri the uri to check
1350 * @return GNUNET_YES if this is a CHK uri
1353 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1355 return uri->type == chk;
1360 * What is the size of the file that this URI
1363 * @param uri the CHK URI to inspect
1364 * @return size of the file as specified in the CHK URI
1367 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri)
1372 return GNUNET_ntohll (uri->data.chk.file_length);
1374 return GNUNET_ntohll (uri->data.loc.fi.file_length);
1378 return 0; /* unreachable */
1383 * Is this a location URI?
1385 * @param uri the uri to check
1386 * @return GNUNET_YES if this is a LOC uri
1389 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1391 return uri->type == loc;
1396 * Function called on each value in the meta data.
1397 * Adds it to the URI.
1399 * @param cls URI to update
1400 * @param type type of the meta data
1401 * @param data value of the meta data
1402 * @return GNUNET_OK (always)
1405 gather_uri_data (void *cls,
1406 EXTRACTOR_KeywordType type,
1409 struct GNUNET_FS_Uri *uri = cls;
1413 for (j = uri->data.ksk.keywordCount - 1; j >= 0; j--)
1414 if (0 == strcmp (&uri->data.ksk.keywords[j][1], data))
1416 nkword = GNUNET_malloc (strlen (data) + 2);
1417 strcpy (nkword, " "); /* not mandatory */
1418 strcat (nkword, data);
1419 uri->data.ksk.keywords[uri->data.ksk.keywordCount++] = nkword;
1425 * Construct a keyword-URI from meta-data (take all entries
1426 * in the meta-data and construct one large keyword URI
1427 * that lists all keywords that can be found in the meta-data).
1430 struct GNUNET_FS_Uri *
1431 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
1433 struct GNUNET_FS_Uri *ret;
1437 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1439 ret->data.ksk.keywordCount = 0;
1440 ret->data.ksk.keywords = NULL;
1441 ret->data.ksk.keywords
1442 = GNUNET_malloc (sizeof (char *) *
1443 GNUNET_CONTAINER_meta_data_get_contents (md, NULL, NULL));
1444 GNUNET_CONTAINER_meta_data_get_contents (md, &gather_uri_data, ret);
1451 * In URI-encoding, does the given character
1452 * need to be encoded using %-encoding?
1455 needs_percent (char c)
1457 return (!((isalnum (c)) ||
1458 (c == '-') || (c == '_') || (c == '.') || (c == '~')));
1463 * Convert a KSK URI to a string.
1465 * @param uri the URI to convert
1466 * @return NULL on error (i.e. keywordCount == 0)
1469 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1472 unsigned int keywordCount;
1479 const char *keyword;
1481 if (uri->type != ksk)
1483 keywords = uri->data.ksk.keywords;
1484 keywordCount = uri->data.ksk.keywordCount;
1486 keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1487 strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1488 for (i = 0; i < keywordCount; i++)
1490 keyword = keywords[i];
1491 slen = strlen (keyword);
1493 for (j = 0; j < slen; j++)
1495 if ((j == 0) && (keyword[j] == ' '))
1498 continue; /* skip leading space */
1500 if (needs_percent (keyword[j]))
1501 n += 2; /* will use %-encoding */
1504 ret = GNUNET_malloc (n);
1505 strcpy (ret, GNUNET_FS_URI_PREFIX);
1506 strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1507 wpos = strlen (ret);
1508 for (i = 0; i < keywordCount; i++)
1510 keyword = keywords[i];
1511 slen = strlen (keyword);
1512 for (j = 0; j < slen; j++)
1514 if ((j == 0) && (keyword[j] == ' '))
1515 continue; /* skip leading space */
1516 if (needs_percent (keyword[j]))
1518 sprintf (&ret[wpos], "%%%02X", keyword[j]);
1523 ret[wpos++] = keyword[j];
1526 if (i != keywordCount - 1)
1534 * Convert SKS URI to a string.
1536 * @param uri sks uri to convert
1537 * @return NULL on error
1540 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1542 const GNUNET_HashCode * namespace;
1543 const char *identifier;
1545 struct GNUNET_CRYPTO_HashAsciiEncoded ns;
1547 if (uri->type != sks)
1549 namespace = &uri->data.sks.namespace;
1550 identifier = uri->data.sks.identifier;
1551 GNUNET_CRYPTO_hash_to_enc (namespace, &ns);
1552 GNUNET_asprintf (&ret,
1554 GNUNET_FS_URI_PREFIX,
1555 GNUNET_FS_URI_SKS_INFIX,
1556 (const char *) &ns, identifier);
1562 * Convert a CHK URI to a string.
1564 * @param uri chk uri to convert
1565 * @return NULL on error
1568 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1570 const struct FileIdentifier * fi;
1572 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1573 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1575 if (uri->type != chk)
1577 fi = &uri->data.chk;
1578 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1579 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1581 GNUNET_asprintf (&ret,
1583 GNUNET_FS_URI_PREFIX,
1584 GNUNET_FS_URI_CHK_INFIX,
1585 (const char *) &keyhash,
1586 (const char *) &queryhash,
1587 GNUNET_ntohll (fi->file_length));
1592 * Convert binary data to a string.
1594 * @param data binary data to convert
1595 * @param size number of bytes in data
1596 * @return converted data
1599 bin2enc (const void *data, size_t size)
1602 * 64 characters for encoding, 6 bits per character
1605 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=";
1613 GNUNET_assert (strlen (tbl) == 64);
1615 if (((size * 8) % 6) != 0)
1617 ret = GNUNET_malloc (len + 1);
1622 for (pos = 0; pos < size; pos++)
1624 bits |= ((((const unsigned char *) data)[pos]) << hbits);
1628 ret[len++] = tbl[bits & 63];
1634 ret[len++] = tbl[bits & 63];
1640 * Convert a LOC URI to a string.
1642 * @param uri loc uri to convert
1643 * @return NULL on error
1646 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
1649 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1650 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1654 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
1655 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
1656 peerId = bin2enc (&uri->data.loc.peer,
1657 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1658 peerSig = bin2enc (&uri->data.loc.contentSignature,
1659 sizeof (struct GNUNET_CRYPTO_RsaSignature));
1660 GNUNET_asprintf (&ret,
1661 "%s%s%s.%s.%llu.%s.%s.%llu",
1662 GNUNET_FS_URI_PREFIX,
1663 GNUNET_FS_URI_LOC_INFIX,
1664 (const char *) &keyhash,
1665 (const char *) &queryhash,
1666 (unsigned long long) GNUNET_ntohll (uri->data.loc.fi.file_length),
1669 (unsigned long long) uri->data.loc.expirationTime.value);
1670 GNUNET_free (peerSig);
1671 GNUNET_free (peerId);
1677 * Convert a URI to a UTF-8 String.
1679 * @param uri uri to convert to a string
1680 * @return the UTF-8 string
1683 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
1693 return uri_ksk_to_string (uri);
1695 return uri_sks_to_string (uri);
1697 return uri_chk_to_string (uri);
1699 return uri_loc_to_string (uri);
1706 /* end of fs_uri.c */