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"
87 * Get a unique key from a URI. This is for putting URIs
88 * into HashMaps. The key may change between FS implementations.
90 * @param uri uri to convert to a unique key
91 * @param key wherer to store the unique key
94 GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri,
95 GNUNET_HashCode * key)
100 *key = uri->data.chk.chk.query;
103 GNUNET_CRYPTO_hash (uri->data.sks.identifier,
104 strlen (uri->data.sks.identifier), key);
107 if (uri->data.ksk.keywordCount > 0)
108 GNUNET_CRYPTO_hash (uri->data.ksk.keywords[0],
109 strlen (uri->data.ksk.keywords[0]), key);
112 GNUNET_CRYPTO_hash (&uri->data.loc.fi,
113 sizeof (struct FileIdentifier) +
114 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), key);
117 memset (key, 0, sizeof (GNUNET_HashCode));
124 * Convert keyword URI to a human readable format
125 * (i.e. the search query that was used in the first place)
127 * @param uri ksk uri to convert to a string
128 * @return string with the keywords
131 GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri)
138 unsigned int keywordCount;
140 if ((uri == NULL) || (uri->type != ksk))
145 keywords = uri->data.ksk.keywords;
146 keywordCount = uri->data.ksk.keywordCount;
147 n = keywordCount + 1;
148 for (i = 0; i < keywordCount; i++)
150 keyword = keywords[i];
151 n += strlen (keyword) - 1;
152 if (NULL != strstr (&keyword[1], " "))
154 if (keyword[0] == '+')
157 ret = GNUNET_malloc (n);
159 for (i = 0; i < keywordCount; i++)
161 keyword = keywords[i];
162 if (NULL != strstr (&keyword[1], " "))
165 if (keyword[0] == '+')
166 strcat (ret, keyword);
168 strcat (ret, &keyword[1]);
173 if (keyword[0] == '+')
174 strcat (ret, keyword);
176 strcat (ret, &keyword[1]);
187 * Given a keyword with %-encoding (and possibly quotes to protect
188 * spaces), return a copy of the keyword without %-encoding and
189 * without double-quotes (%22). Also, add a space at the beginning
190 * if there is not a '+'.
192 * @param in string with %-encoding
193 * @param emsg where to store the parser error message (if any)
194 * @return decodded string with leading space (or preserved plus)
197 percent_decode_keyword (const char *in, char **emsg)
205 out = GNUNET_strdup (in);
208 while (out[rpos] != '\0')
210 if (out[rpos] == '%')
212 if (1 != sscanf (&out[rpos + 1], "%2X", &hx))
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 (s[slen - 1] == '+') ||
272 return NULL; /* no keywords / malformed */
276 for (i = pos; i < slen; i++)
278 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
280 saw_quote = (saw_quote + 1) % 2;
284 if ((s[i] == '+') && (saw_quote == 0))
288 return NULL; /* "++" not allowed */
292 return NULL; /* quotes not balanced */
294 dup = GNUNET_strdup (s);
295 keywords = GNUNET_malloc (max * sizeof (char *));
296 for (i = slen - 1; i >= pos; i--)
298 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
300 saw_quote = (saw_quote + 1) % 2;
304 if ((dup[i] == '+') && (saw_quote == 0))
306 keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg);
307 if (NULL == keywords[max])
312 keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
313 if (NULL == keywords[max])
315 GNUNET_assert (max == 0);
317 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
319 ret->data.ksk.keywordCount = iret;
320 ret->data.ksk.keywords = keywords;
323 for (i = 0; i < max; i++)
324 GNUNET_free_non_null (keywords[i]);
325 GNUNET_free (keywords);
334 * @param s an uri string
335 * @param emsg where to store the parser error message (if any)
336 * @return NULL on error, SKS URI otherwise
338 static struct GNUNET_FS_Uri *
339 uri_sks_parse (const char *s, char **emsg)
341 struct GNUNET_FS_Uri *ret;
342 GNUNET_HashCode namespace;
346 char enc[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
348 GNUNET_assert (s != NULL);
350 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX);
351 if ( (slen <= pos) ||
352 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX,
354 (slen < pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
355 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '/') )
357 memcpy (enc, &s[pos], sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
358 enc[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
359 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (enc, &namespace))
361 identifier = GNUNET_strdup (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]);
362 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
364 ret->data.sks.namespace = namespace;
365 ret->data.sks.identifier = identifier;
373 * @param s an uri string
374 * @param emsg where to store the parser error message (if any)
375 * @return NULL on error, CHK URI otherwise
377 static struct GNUNET_FS_Uri *
378 uri_chk_parse (const char *s, char **emsg)
380 struct GNUNET_FS_Uri *ret;
381 struct FileIdentifier fi;
384 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
385 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
387 GNUNET_assert (s != NULL);
390 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX);
391 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
392 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX,
394 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
395 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
400 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
401 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
403 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
404 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
405 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
407 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
409 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
411 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
415 fi.file_length = GNUNET_htonll (fi.file_length);
417 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
426 * Also verifies validity of the location URI.
428 * @param s an uri string
429 * @param emsg where to store the parser error message (if any)
430 * @return NULL on error, valid LOC URI otherwise
432 static struct GNUNET_FS_Uri *
433 uri_loc_parse (const char *s, char **emsg)
435 struct GNUNET_FS_Uri *ret;
436 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
437 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
440 unsigned long long exptime;
445 GNUNET_assert (s != NULL);
447 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX);
448 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
449 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX,
451 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
452 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
457 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
458 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
460 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
461 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
462 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
464 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
466 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
468 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
472 fi.file_length = GNUNET_htonll (fi.file_length);
474 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
475 while ((s[npos] != '\0') && (s[npos] != '.'))
479 ret = enc2bin (&s[npos],
481 sizeof (GNUNET_RSA_PublicKey));
485 if (dup[npos++] != '.')
487 ret = enc2bin (&s[npos],
488 &loc->contentSignature,
489 sizeof (struct GNUNET_CRYPTO_RsaSignature));
493 if (dup[npos++] != '.')
495 if (1 != SSCANF (&dup[npos], "%llu", &exptime))
497 // FIXME: do something to exptime...
498 /* Finally: verify sigs! */
499 if (GNUNET_OK != GNUNET_RSA_verify (&loc->fi,
500 sizeof (struct FileIdentifier) +
501 sizeof (GNUNET_PeerIdentity) +
502 sizeof (GNUNET_Int32Time),
503 &loc->contentSignature,
507 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
509 ret->data.loc.chk = fi;
510 ret->data.loc.xx = yy;
514 GNUNET_free_non_null (addr);
520 * Convert a UTF-8 String to a URI.
522 * @param uri string to parse
523 * @param emsg where to store the parser error message (if any)
524 * @return NULL on error
526 struct GNUNET_FS_Uri *
527 GNUNET_FS_uri_parse (const char *uri,
530 struct GNUNET_FS_Uri *ret;
532 if ( (NULL != (ret = uri_chk_parse (uri, emsg))) ||
533 (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
534 (NULL != (ret = uri_sks_parse (uri, emsg))) ||
535 (NULL != (ret = uri_loc_parse (uri, emsg))) )
544 * @param uri uri to free
547 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
551 GNUNET_assert (uri != NULL);
555 for (i = 0; i < uri->data.ksk.keywordCount; i++)
556 GNUNET_free (uri->data.ksk.keywords[i]);
557 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
561 GNUNET_free (uri->data.sks.identifier);
573 * How many keywords are ANDed in this keyword URI?
575 * @param uri ksk uri to get the number of keywords from
576 * @return 0 if this is not a keyword URI
579 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
581 if (uri->type != ksk)
583 return uri->data.ksk.keywordCount;
588 * Iterate over all keywords in this keyword URI.
590 * @param uri ksk uri to get the keywords from
591 * @param iterator function to call on each keyword
592 * @param iterator_cls closure for iterator
593 * @return -1 if this is not a keyword URI, otherwise number of
594 * keywords iterated over until iterator aborted
597 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
598 GNUNET_FS_KeywordIterator iterator,
604 if (uri->type != ksk)
606 if (iterator == NULL)
607 return uri->data.ksk.keywordCount;
608 for (i = 0; i < uri->data.ksk.keywordCount; i++)
610 keyword = uri->data.ksk.keywords[i];
611 /* first character of keyword indicates
612 if it is mandatory or not */
613 if (GNUNET_OK != iterator (iterator_cls,
623 * Obtain the identity of the peer offering the data
625 * @param uri the location URI to inspect
626 * @param peer where to store the identify of the peer (presumably) offering the content
627 * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
630 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
631 struct GNUNET_PeerIdentity * peer)
633 if (uri->type != loc)
634 return GNUNET_SYSERR;
635 GNUNET_CRYPTO_hash (&uri->data.loc.peer,
636 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
643 * Obtain the URI of the content itself.
645 * @param uri location URI to get the content URI from
646 * @return NULL if argument is not a location URI
648 struct GNUNET_FS_Uri *
649 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
651 struct GNUNET_FS_Uri *ret;
653 if (uri->type != loc)
655 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
657 ret->data.chk = uri->data.loc.fi;
663 * Construct a location URI (this peer will be used for the location).
665 * @param baseURI content offered by the sender
666 * @param cfg configuration information (used to find our hostkey)
667 * @param expiration_time how long will the content be offered?
668 * @return the location URI, NULL on error
670 struct GNUNET_FS_Uri *
671 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
672 struct GNUNET_CONFIGURATION_Handle *cfg,
673 struct GNUNET_TIME_Absolute expiration_time);
677 * Canonicalize keyword URI. Performs operations such
678 * as decapitalization and removal of certain characters.
679 * (useful for search).
681 * @param uri the URI to canonicalize
682 * @return canonicalized version of the URI, NULL on error
684 struct GNUNET_FS_Uri *
685 GNUNET_FS_uri_ksk_canonicalize (const struct GNUNET_FS_Uri *uri)
687 /* FIXME: not implemented */
693 * Merge the sets of keywords from two KSK URIs.
694 * (useful for merging the canonicalized keywords with
695 * the original keywords for sharing).
697 * @param u1 first uri
698 * @param u2 second uri
699 * @return merged URI, NULL on error
701 struct GNUNET_FS_Uri *
702 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
703 const struct GNUNET_FS_Uri *u2)
713 * @param uri the URI to duplicate
714 * @return copy of the URI
716 struct GNUNET_FS_Uri *
717 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
719 struct GNUNET_FS_Uri *ret;
722 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
723 memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
727 if (ret->data.ksk.keywordCount > 0)
729 ret->data.ksk.keywords
730 = GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
731 for (i = 0; i < ret->data.ksk.keywordCount; i++)
732 ret->data.ksk.keywords[i] =
733 GNUNET_strdup (uri->data.ksk.keywords[i]);
736 ret->data.ksk.keywords = NULL; /* just to be sure */
739 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
751 * Create an FS URI from a single user-supplied string of keywords.
752 * The string is broken up at spaces into individual keywords.
753 * Keywords that start with "+" are mandatory. Double-quotes can
754 * be used to prevent breaking up strings at spaces (and also
755 * to specify non-mandatory keywords starting with "+").
757 * Keywords must contain a balanced number of double quotes and
758 * double quotes can not be used in the actual keywords (for
759 * example, the string '""foo bar""' will be turned into two
760 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
762 * @param keywords the keyword string
763 * @return an FS URI for the given keywords, NULL
764 * if keywords is not legal (i.e. empty).
766 struct GNUNET_FS_Uri *
767 GNUNET_FS_uri_ksk_create (const char *keywords)
775 * Create an FS URI from a user-supplied command line of keywords.
776 * Arguments should start with "+" to indicate mandatory
779 * @param argc number of keywords
780 * @param argv keywords (double quotes are not required for
781 * keywords containing spaces; however, double
782 * quotes are required for keywords starting with
783 * "+"); there is no mechanism for having double
784 * quotes in the actual keywords (if the user
785 * did specifically specify double quotes, the
786 * caller should convert each double quote
787 * into two single quotes).
788 * @return an FS URI for the given keywords, NULL
789 * if keywords is not legal (i.e. empty).
791 struct GNUNET_FS_Uri *
792 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
801 * Test if two URIs are equal.
803 * @param u1 one of the URIs
804 * @param u2 the other URI
805 * @return GNUNET_YES if the URIs are equal
808 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
809 const struct GNUNET_FS_Uri *u2)
815 GNUNET_assert (u1 != NULL);
816 GNUNET_assert (u2 != NULL);
817 if (u1->type != u2->type)
822 if (0 == memcmp (&u1->data.chk,
824 sizeof (struct FileIdentifier)))
828 if ((0 == memcmp (&u1->data.sks.namespace,
829 &u2->data.sks.namespace,
830 sizeof (GNUNET_HashCode))) &&
831 (0 == strcmp (u1->data.sks.identifier,
832 u2->data.sks.identifier)))
837 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
839 for (i = 0; i < u1->data.ksk.keywordCount; i++)
842 for (j = 0; j < u2->data.ksk.keywordCount; j++)
844 if (0 == strcmp (u1->data.ksk.keywords[i],
845 u2->data.ksk.keywords[j]))
851 if (ret == GNUNET_NO)
856 if (memcmp (&u1->data.loc,
858 sizeof (struct FileIdentifier) +
859 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
860 sizeof (struct GNUNET_TIME_Absolute) +
861 sizeof (unsigned short) + sizeof (unsigned short)) != 0)
871 * Is this a namespace URI?
873 * @param uri the uri to check
874 * @return GNUNET_YES if this is an SKS uri
877 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
879 return uri->type == sks;
884 * Get the ID of a namespace from the given
887 * @param uri the uri to get the namespace ID from
888 * @param nsid where to store the ID of the namespace
889 * @return GNUNET_OK on success
892 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
893 GNUNET_HashCode * nsid)
895 if (! GNUNET_FS_uri_test_sks (uri))
898 return GNUNET_SYSERR;
900 *nsid = uri->data.sks.namespace;
906 * Get the content identifier of an SKS URI.
908 * @param uri the sks uri
909 * @return NULL on error (not a valid SKS URI)
912 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
914 if (!GNUNET_FS_uri_test_sks (uri))
919 return GNUNET_strdup (uri->data.sks.identifier);
924 * Convert namespace URI to a human readable format
925 * (using the namespace description, if available).
927 * @param cfg configuration to use
928 * @param uri SKS uri to convert
929 * @return NULL on error (not an SKS URI)
932 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
933 const struct GNUNET_FS_Uri *uri)
941 * Is this a keyword URI?
944 * @return GNUNET_YES if this is a KSK uri
947 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
952 if (uri->type == ksk)
954 for (i = uri->data.ksk.keywordCount - 1; i >= 0; i--)
955 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
958 return uri->type == ksk;
963 * Is this a file (or directory) URI?
965 * @param uri the uri to check
966 * @return GNUNET_YES if this is a CHK uri
969 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
971 return uri->type == chk;
976 * What is the size of the file that this URI
979 * @param uri the CHK URI to inspect
980 * @return size of the file as specified in the CHK URI
983 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri)
988 return GNUNET_ntohll (uri->data.chk.file_length);
990 return GNUNET_ntohll (uri->data.loc.fi.file_length);
994 return 0; /* unreachable */
999 * Is this a location URI?
1001 * @param uri the uri to check
1002 * @return GNUNET_YES if this is a LOC uri
1005 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1007 return uri->type == loc;
1012 * Function called on each value in the meta data.
1013 * Adds it to the URI.
1015 * @param cls URI to update
1016 * @param type type of the meta data
1017 * @param data value of the meta data
1018 * @return GNUNET_OK (always)
1021 gather_uri_data (void *cls,
1022 EXTRACTOR_KeywordType type,
1025 struct GNUNET_FS_Uri *uri = cls;
1029 for (j = uri->data.ksk.keywordCount - 1; j >= 0; j--)
1030 if (0 == strcmp (&uri->data.ksk.keywords[j][1], data))
1032 nkword = GNUNET_malloc (strlen (data) + 2);
1033 strcpy (nkword, " "); /* not mandatory */
1034 strcat (nkword, data);
1035 uri->data.ksk.keywords[uri->data.ksk.keywordCount++] = nkword;
1041 * Construct a keyword-URI from meta-data (take all entries
1042 * in the meta-data and construct one large keyword URI
1043 * that lists all keywords that can be found in the meta-data).
1046 struct GNUNET_FS_Uri *
1047 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
1049 struct GNUNET_FS_Uri *ret;
1053 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1055 ret->data.ksk.keywordCount = 0;
1056 ret->data.ksk.keywords = NULL;
1057 ret->data.ksk.keywords
1058 = GNUNET_malloc (sizeof (char *) *
1059 GNUNET_CONTAINER_meta_data_get_contents (md, NULL, NULL));
1060 GNUNET_CONTAINER_meta_data_get_contents (md, &gather_uri_data, ret);
1067 * In URI-encoding, does the given character
1068 * need to be encoded using %-encoding?
1071 needs_percent (char c)
1073 return (!((isalnum (c)) ||
1074 (c == '-') || (c == '_') || (c == '.') || (c == '~')));
1079 * Convert a KSK URI to a string.
1081 * @param uri the URI to convert
1082 * @return NULL on error (i.e. keywordCount == 0)
1085 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1088 unsigned int keywordCount;
1095 const char *keyword;
1097 if (uri->type != ksk)
1099 keywords = uri->data.ksk.keywords;
1100 keywordCount = uri->data.ksk.keywordCount;
1102 keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1103 strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1104 for (i = 0; i < keywordCount; i++)
1106 keyword = keywords[i];
1107 slen = strlen (keyword);
1109 for (j = 0; j < slen; j++)
1111 if ((j == 0) && (keyword[j] == ' '))
1114 continue; /* skip leading space */
1116 if (needs_percent (keyword[j]))
1117 n += 2; /* will use %-encoding */
1120 ret = GNUNET_malloc (n);
1121 strcpy (ret, GNUNET_FS_URI_PREFIX);
1122 strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1123 wpos = strlen (ret);
1124 for (i = 0; i < keywordCount; i++)
1126 keyword = keywords[i];
1127 slen = strlen (keyword);
1128 for (j = 0; j < slen; j++)
1130 if ((j == 0) && (keyword[j] == ' '))
1131 continue; /* skip leading space */
1132 if (needs_percent (keyword[j]))
1134 sprintf (&ret[wpos], "%%%02X", keyword[j]);
1139 ret[wpos++] = keyword[j];
1142 if (i != keywordCount - 1)
1150 * Convert SKS URI to a string.
1152 * @param uri sks uri to convert
1153 * @return NULL on error
1156 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1158 const GNUNET_HashCode * namespace;
1159 const char *identifier;
1161 struct GNUNET_CRYPTO_HashAsciiEncoded ns;
1163 if (uri->type != sks)
1165 namespace = &uri->data.sks.namespace;
1166 identifier = uri->data.sks.identifier;
1167 GNUNET_CRYPTO_hash_to_enc (namespace, &ns);
1168 GNUNET_asprintf (&ret,
1170 GNUNET_FS_URI_PREFIX,
1171 GNUNET_FS_URI_SKS_INFIX,
1172 (const char *) &ns, identifier);
1178 * Convert a CHK URI to a string.
1180 * @param uri chk uri to convert
1181 * @return NULL on error
1184 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1186 const struct FileIdentifier * fi;
1188 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1189 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1191 if (uri->type != chk)
1193 fi = &uri->data.chk;
1194 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1195 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1197 GNUNET_asprintf (&ret,
1199 GNUNET_FS_URI_PREFIX,
1200 GNUNET_FS_URI_CHK_INFIX,
1201 (const char *) &keyhash,
1202 (const char *) &queryhash,
1203 GNUNET_ntohll (fi->file_length));
1208 * Convert binary data to a string.
1210 * @return converted data
1213 bin2enc (const void *data, size_t size)
1216 * 64 characters for encoding, 6 bits per character
1219 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=";
1227 GNUNET_assert (strlen (tbl) == 64);
1229 if (((size * 8) % 6) != 0)
1231 ret = GNUNET_malloc (len + 1);
1236 for (pos = 0; pos < size; pos++)
1238 bits |= ((((const unsigned char *) data)[pos]) << hbits);
1242 ret[len++] = tbl[bits & 63];
1248 ret[len++] = tbl[bits & 63];
1254 * Convert a character back to the binary value
1255 * that it represents (given base64-encoding).
1257 * @param a character to convert
1258 * @return offset in the "tbl" array
1261 c2v (unsigned char a)
1263 if ((a >= '0') && (a <= '9'))
1265 if ((a >= 'A') && (a <= 'Z'))
1266 return (a - 'A' + 10);
1267 if ((a >= 'a') && (a <= 'z'))
1268 return (a - 'a' + 36);
1278 * Convert string back to binary data.
1280 * @param input '\0'-terminated string
1281 * @param data where to write binary data
1282 * @param size how much data should be converted
1283 * @return number of characters processed from input,
1287 enc2bin (const char *input, void *data, size_t size)
1295 if (((size * 8) % 6) != 0)
1297 if (strlen (input) < len)
1298 return -1; /* error! */
1303 for (pos = 0; pos < size; pos++)
1307 bits |= (c2v (input[len++]) << hbits);
1310 (((unsigned char *) data)[pos]) = (unsigned char) bits;
1319 * Convert a LOC URI to a string.
1321 * @param uri loc uri to convert
1322 * @return NULL on error
1325 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
1328 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1329 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1333 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
1334 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
1335 peerId = bin2enc (&uri->data.loc.peer,
1336 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1337 peerSig = bin2enc (&uri->data.loc.contentSignature,
1338 sizeof (struct GNUNET_CRYPTO_RsaSignature));
1339 GNUNET_asprintf (&ret,
1340 "%s%s%s.%s.%llu.%s.%s.%u", // FIXME: expirationTime 64-bit???
1341 GNUNET_FS_URI_PREFIX,
1342 GNUNET_FS_URI_LOC_INFIX,
1343 (const char *) &keyhash,
1344 (const char *) &queryhash,
1345 GNUNET_ntohll (uri->data.loc.fi.file_length),
1348 uri->data.loc.expirationTime);
1349 GNUNET_free (peerSig);
1350 GNUNET_free (peerId);
1356 * Convert a URI to a UTF-8 String.
1358 * @param uri uri to convert to a string
1359 * @return the UTF-8 string
1362 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
1372 return uri_ksk_to_string (uri);
1374 return uri_sks_to_string (uri);
1376 return uri_chk_to_string (uri);
1378 return uri_loc_to_string (uri);
1389 * Construct a location URI.
1391 * @param baseURI content offered by the sender
1392 * @param sender identity of the peer with the content
1393 * @param expiration_time how long will the content be offered?
1394 * @param proto transport protocol to reach the peer
1395 * @param sas sender address size (for HELLO)
1396 * @param address sas bytes of address information
1397 * @param signer function to call for obtaining
1398 * RSA signatures for "sender".
1399 * @return the location URI
1401 struct GNUNET_ECRS_URI *
1402 GNUNET_ECRS_location_to_uri (const struct GNUNET_ECRS_URI *baseUri,
1403 const GNUNET_RSA_PublicKey * sender,
1404 GNUNET_Int32Time expirationTime,
1405 GNUNET_ECRS_SignFunction signer,
1408 struct GNUNET_ECRS_URI *uri;
1410 if (baseUri->type != chk)
1413 uri = GNUNET_malloc (sizeof (struct GNUNET_ECRS_URI));
1415 uri->data.loc.fi = baseUri->data.fi;
1416 uri->data.loc.peer = *sender;
1417 uri->data.loc.expirationTime = expirationTime;
1419 sizeof (GNUNET_EC_FileIdentifier) +
1420 sizeof (GNUNET_PeerIdentity) +
1421 sizeof (GNUNET_Int32Time),
1422 &uri->data.loc.fi, &uri->data.loc.contentSignature);