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]);
188 * Given a keyword with %-encoding (and possibly quotes to protect
189 * spaces), return a copy of the keyword without %-encoding and
190 * without double-quotes (%22). Also, add a space at the beginning
191 * if there is not a '+'.
193 * @param in string with %-encoding
194 * @param emsg where to store the parser error message (if any)
195 * @return decodded string with leading space (or preserved plus)
198 percent_decode_keyword (const char *in, char **emsg)
206 out = GNUNET_strdup (in);
209 while (out[rpos] != '\0')
211 if (out[rpos] == '%')
213 if (1 != sscanf (&out[rpos + 1], "%2X", &hx))
220 continue; /* skip double quote */
221 out[wpos++] = (char) hx;
225 out[wpos++] = out[rpos++];
231 ret = GNUNET_strdup (out);
235 /* need to prefix with space */
236 ret = GNUNET_malloc (strlen (out) + 2);
248 * @param s an uri string
249 * @param emsg where to store the parser error message (if any)
250 * @return NULL on error, otherwise the KSK URI
252 static struct GNUNET_FS_Uri *
253 uri_ksk_parse (const char *s, char **emsg)
255 struct GNUNET_FS_Uri *ret;
265 GNUNET_assert (s != NULL);
267 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX);
268 if ( (slen <= pos) ||
269 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX,
271 (s[slen - 1] == '+') ||
273 return NULL; /* no keywords / malformed */
277 for (i = pos; i < slen; i++)
279 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
281 saw_quote = (saw_quote + 1) % 2;
285 if ((s[i] == '+') && (saw_quote == 0))
289 return NULL; /* "++" not allowed */
293 return NULL; /* quotes not balanced */
295 dup = GNUNET_strdup (s);
296 keywords = GNUNET_malloc (max * sizeof (char *));
297 for (i = slen - 1; i >= pos; i--)
299 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
301 saw_quote = (saw_quote + 1) % 2;
305 if ((dup[i] == '+') && (saw_quote == 0))
307 keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg);
308 if (NULL == keywords[max])
313 keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
314 if (NULL == keywords[max])
316 GNUNET_assert (max == 0);
318 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
320 ret->data.ksk.keywordCount = iret;
321 ret->data.ksk.keywords = keywords;
324 for (i = 0; i < max; i++)
325 GNUNET_free_non_null (keywords[i]);
326 GNUNET_free (keywords);
335 * @param s an uri string
336 * @param emsg where to store the parser error message (if any)
337 * @return NULL on error, SKS URI otherwise
339 static struct GNUNET_FS_Uri *
340 uri_sks_parse (const char *s, char **emsg)
342 struct GNUNET_FS_Uri *ret;
343 GNUNET_HashCode namespace;
347 char enc[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
349 GNUNET_assert (s != NULL);
351 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX);
352 if ( (slen <= pos) ||
353 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX,
355 (slen < pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
356 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '/') )
358 memcpy (enc, &s[pos], sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
359 enc[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
360 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (enc, &namespace))
362 identifier = GNUNET_strdup (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]);
363 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
365 ret->data.sks.namespace = namespace;
366 ret->data.sks.identifier = identifier;
374 * @param s an uri string
375 * @param emsg where to store the parser error message (if any)
376 * @return NULL on error, CHK URI otherwise
378 static struct GNUNET_FS_Uri *
379 uri_chk_parse (const char *s, char **emsg)
381 struct GNUNET_FS_Uri *ret;
382 struct FileIdentifier fi;
385 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
386 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
388 GNUNET_assert (s != NULL);
391 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX);
392 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
393 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX,
395 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
396 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
401 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
402 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
404 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
405 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
406 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
408 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
410 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
412 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
416 fi.file_length = GNUNET_htonll (fi.file_length);
418 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
426 * Convert a character back to the binary value
427 * that it represents (given base64-encoding).
429 * @param a character to convert
430 * @return offset in the "tbl" array
433 c2v (unsigned char a)
435 if ((a >= '0') && (a <= '9'))
437 if ((a >= 'A') && (a <= 'Z'))
438 return (a - 'A' + 10);
439 if ((a >= 'a') && (a <= 'z'))
440 return (a - 'a' + 36);
450 * Convert string back to binary data.
452 * @param input '\0'-terminated string
453 * @param data where to write binary data
454 * @param size how much data should be converted
455 * @return number of characters processed from input,
459 enc2bin (const char *input, void *data, size_t size)
467 if (((size * 8) % 6) != 0)
469 if (strlen (input) < len)
470 return -1; /* error! */
475 for (pos = 0; pos < size; pos++)
479 bits |= (c2v (input[len++]) << hbits);
482 (((unsigned char *) data)[pos]) = (unsigned char) bits;
491 * Structure that defines how the
492 * contents of a location URI must be
493 * assembled in memory to create or
494 * verify the signature of a location
497 struct LocUriAssembly
499 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
501 struct GNUNET_TIME_AbsoluteNBO exptime;
503 struct FileIdentifier fi;
505 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded peer;
512 * Also verifies validity of the location URI.
514 * @param s an uri string
515 * @param emsg where to store the parser error message (if any)
516 * @return NULL on error, valid LOC URI otherwise
518 static struct GNUNET_FS_Uri *
519 uri_loc_parse (const char *s, char **emsg)
521 struct GNUNET_FS_Uri *uri;
522 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
523 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
526 unsigned long long exptime;
527 struct GNUNET_TIME_Absolute et;
528 struct GNUNET_CRYPTO_RsaSignature sig;
529 struct LocUriAssembly ass;
534 GNUNET_assert (s != NULL);
536 pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX);
537 if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
538 (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX,
540 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
541 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
546 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
547 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
549 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
550 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
551 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
553 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
555 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
556 &ass.fi.chk.query)) ||
557 (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
559 &ass.fi.file_length)) )
561 ass.fi.file_length = GNUNET_htonll (ass.fi.file_length);
563 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
564 while ((s[npos] != '\0') && (s[npos] != '.'))
568 ret = enc2bin (&s[npos],
570 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
574 if (s[npos++] != '.')
576 ret = enc2bin (&s[npos],
578 sizeof (struct GNUNET_CRYPTO_RsaSignature));
582 if (s[npos++] != '.')
584 if (1 != SSCANF (&s[npos], "%llu", &exptime))
586 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
587 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_NAMESPACE_PLACEMENT);
589 ass.exptime = GNUNET_TIME_absolute_hton (et);
591 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_NAMESPACE_PLACEMENT,
597 uri = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
599 uri->data.loc.fi = ass.fi;
600 uri->data.loc.peer = ass.peer;
601 uri->data.loc.expirationTime = et;
602 uri->data.loc.contentSignature = sig;
606 GNUNET_free_non_null (addr);
612 * Convert a UTF-8 String to a URI.
614 * @param uri string to parse
615 * @param emsg where to store the parser error message (if any)
616 * @return NULL on error
618 struct GNUNET_FS_Uri *
619 GNUNET_FS_uri_parse (const char *uri,
622 struct GNUNET_FS_Uri *ret;
624 if ( (NULL != (ret = uri_chk_parse (uri, emsg))) ||
625 (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
626 (NULL != (ret = uri_sks_parse (uri, emsg))) ||
627 (NULL != (ret = uri_loc_parse (uri, emsg))) )
636 * @param uri uri to free
639 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
643 GNUNET_assert (uri != NULL);
647 for (i = 0; i < uri->data.ksk.keywordCount; i++)
648 GNUNET_free (uri->data.ksk.keywords[i]);
649 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
653 GNUNET_free (uri->data.sks.identifier);
665 * How many keywords are ANDed in this keyword URI?
667 * @param uri ksk uri to get the number of keywords from
668 * @return 0 if this is not a keyword URI
671 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
673 if (uri->type != ksk)
675 return uri->data.ksk.keywordCount;
680 * Iterate over all keywords in this keyword URI.
682 * @param uri ksk uri to get the keywords from
683 * @param iterator function to call on each keyword
684 * @param iterator_cls closure for iterator
685 * @return -1 if this is not a keyword URI, otherwise number of
686 * keywords iterated over until iterator aborted
689 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
690 GNUNET_FS_KeywordIterator iterator,
696 if (uri->type != ksk)
698 if (iterator == NULL)
699 return uri->data.ksk.keywordCount;
700 for (i = 0; i < uri->data.ksk.keywordCount; i++)
702 keyword = uri->data.ksk.keywords[i];
703 /* first character of keyword indicates
704 if it is mandatory or not */
705 if (GNUNET_OK != iterator (iterator_cls,
715 * Obtain the identity of the peer offering the data
717 * @param uri the location URI to inspect
718 * @param peer where to store the identify of the peer (presumably) offering the content
719 * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
722 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
723 struct GNUNET_PeerIdentity * peer)
725 if (uri->type != loc)
726 return GNUNET_SYSERR;
727 GNUNET_CRYPTO_hash (&uri->data.loc.peer,
728 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
735 * Obtain the URI of the content itself.
737 * @param uri location URI to get the content URI from
738 * @return NULL if argument is not a location URI
740 struct GNUNET_FS_Uri *
741 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
743 struct GNUNET_FS_Uri *ret;
745 if (uri->type != loc)
747 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
749 ret->data.chk = uri->data.loc.fi;
755 * Construct a location URI (this peer will be used for the location).
757 * @param baseURI content offered by the sender
758 * @param cfg configuration information (used to find our hostkey)
759 * @param expiration_time how long will the content be offered?
760 * @return the location URI, NULL on error
762 struct GNUNET_FS_Uri *
763 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
764 struct GNUNET_CONFIGURATION_Handle *cfg,
765 struct GNUNET_TIME_Absolute expiration_time);
769 * Canonicalize keyword URI. Performs operations such
770 * as decapitalization and removal of certain characters.
771 * (useful for search).
773 * @param uri the URI to canonicalize
774 * @return canonicalized version of the URI, NULL on error
776 struct GNUNET_FS_Uri *
777 GNUNET_FS_uri_ksk_canonicalize (const struct GNUNET_FS_Uri *uri)
779 /* FIXME: not implemented */
785 * Merge the sets of keywords from two KSK URIs.
786 * (useful for merging the canonicalized keywords with
787 * the original keywords for sharing).
789 * @param u1 first uri
790 * @param u2 second uri
791 * @return merged URI, NULL on error
793 struct GNUNET_FS_Uri *
794 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
795 const struct GNUNET_FS_Uri *u2)
805 * @param uri the URI to duplicate
806 * @return copy of the URI
808 struct GNUNET_FS_Uri *
809 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
811 struct GNUNET_FS_Uri *ret;
814 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
815 memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
819 if (ret->data.ksk.keywordCount > 0)
821 ret->data.ksk.keywords
822 = GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
823 for (i = 0; i < ret->data.ksk.keywordCount; i++)
824 ret->data.ksk.keywords[i] =
825 GNUNET_strdup (uri->data.ksk.keywords[i]);
828 ret->data.ksk.keywords = NULL; /* just to be sure */
831 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
843 * Create an FS URI from a single user-supplied string of keywords.
844 * The string is broken up at spaces into individual keywords.
845 * Keywords that start with "+" are mandatory. Double-quotes can
846 * be used to prevent breaking up strings at spaces (and also
847 * to specify non-mandatory keywords starting with "+").
849 * Keywords must contain a balanced number of double quotes and
850 * double quotes can not be used in the actual keywords (for
851 * example, the string '""foo bar""' will be turned into two
852 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
854 * @param keywords the keyword string
855 * @return an FS URI for the given keywords, NULL
856 * if keywords is not legal (i.e. empty).
858 struct GNUNET_FS_Uri *
859 GNUNET_FS_uri_ksk_create (const char *keywords)
867 * Create an FS URI from a user-supplied command line of keywords.
868 * Arguments should start with "+" to indicate mandatory
871 * @param argc number of keywords
872 * @param argv keywords (double quotes are not required for
873 * keywords containing spaces; however, double
874 * quotes are required for keywords starting with
875 * "+"); there is no mechanism for having double
876 * quotes in the actual keywords (if the user
877 * did specifically specify double quotes, the
878 * caller should convert each double quote
879 * into two single quotes).
880 * @return an FS URI for the given keywords, NULL
881 * if keywords is not legal (i.e. empty).
883 struct GNUNET_FS_Uri *
884 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
893 * Test if two URIs are equal.
895 * @param u1 one of the URIs
896 * @param u2 the other URI
897 * @return GNUNET_YES if the URIs are equal
900 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
901 const struct GNUNET_FS_Uri *u2)
907 GNUNET_assert (u1 != NULL);
908 GNUNET_assert (u2 != NULL);
909 if (u1->type != u2->type)
914 if (0 == memcmp (&u1->data.chk,
916 sizeof (struct FileIdentifier)))
920 if ((0 == memcmp (&u1->data.sks.namespace,
921 &u2->data.sks.namespace,
922 sizeof (GNUNET_HashCode))) &&
923 (0 == strcmp (u1->data.sks.identifier,
924 u2->data.sks.identifier)))
929 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
931 for (i = 0; i < u1->data.ksk.keywordCount; i++)
934 for (j = 0; j < u2->data.ksk.keywordCount; j++)
936 if (0 == strcmp (u1->data.ksk.keywords[i],
937 u2->data.ksk.keywords[j]))
943 if (ret == GNUNET_NO)
948 if (memcmp (&u1->data.loc,
950 sizeof (struct FileIdentifier) +
951 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
952 sizeof (struct GNUNET_TIME_Absolute) +
953 sizeof (unsigned short) + sizeof (unsigned short)) != 0)
963 * Is this a namespace URI?
965 * @param uri the uri to check
966 * @return GNUNET_YES if this is an SKS uri
969 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
971 return uri->type == sks;
976 * Get the ID of a namespace from the given
979 * @param uri the uri to get the namespace ID from
980 * @param nsid where to store the ID of the namespace
981 * @return GNUNET_OK on success
984 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
985 GNUNET_HashCode * nsid)
987 if (! GNUNET_FS_uri_test_sks (uri))
990 return GNUNET_SYSERR;
992 *nsid = uri->data.sks.namespace;
998 * Get the content identifier of an SKS URI.
1000 * @param uri the sks uri
1001 * @return NULL on error (not a valid SKS URI)
1004 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1006 if (!GNUNET_FS_uri_test_sks (uri))
1011 return GNUNET_strdup (uri->data.sks.identifier);
1016 * Convert namespace URI to a human readable format
1017 * (using the namespace description, if available).
1019 * @param cfg configuration to use
1020 * @param uri SKS uri to convert
1021 * @return NULL on error (not an SKS URI)
1024 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
1025 const struct GNUNET_FS_Uri *uri)
1033 * Is this a keyword URI?
1035 * @param uri the uri
1036 * @return GNUNET_YES if this is a KSK uri
1039 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1044 if (uri->type == ksk)
1046 for (i = uri->data.ksk.keywordCount - 1; i >= 0; i--)
1047 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1050 return uri->type == ksk;
1055 * Is this a file (or directory) URI?
1057 * @param uri the uri to check
1058 * @return GNUNET_YES if this is a CHK uri
1061 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1063 return uri->type == chk;
1068 * What is the size of the file that this URI
1071 * @param uri the CHK URI to inspect
1072 * @return size of the file as specified in the CHK URI
1075 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri)
1080 return GNUNET_ntohll (uri->data.chk.file_length);
1082 return GNUNET_ntohll (uri->data.loc.fi.file_length);
1086 return 0; /* unreachable */
1091 * Is this a location URI?
1093 * @param uri the uri to check
1094 * @return GNUNET_YES if this is a LOC uri
1097 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1099 return uri->type == loc;
1104 * Function called on each value in the meta data.
1105 * Adds it to the URI.
1107 * @param cls URI to update
1108 * @param type type of the meta data
1109 * @param data value of the meta data
1110 * @return GNUNET_OK (always)
1113 gather_uri_data (void *cls,
1114 EXTRACTOR_KeywordType type,
1117 struct GNUNET_FS_Uri *uri = cls;
1121 for (j = uri->data.ksk.keywordCount - 1; j >= 0; j--)
1122 if (0 == strcmp (&uri->data.ksk.keywords[j][1], data))
1124 nkword = GNUNET_malloc (strlen (data) + 2);
1125 strcpy (nkword, " "); /* not mandatory */
1126 strcat (nkword, data);
1127 uri->data.ksk.keywords[uri->data.ksk.keywordCount++] = nkword;
1133 * Construct a keyword-URI from meta-data (take all entries
1134 * in the meta-data and construct one large keyword URI
1135 * that lists all keywords that can be found in the meta-data).
1138 struct GNUNET_FS_Uri *
1139 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
1141 struct GNUNET_FS_Uri *ret;
1145 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1147 ret->data.ksk.keywordCount = 0;
1148 ret->data.ksk.keywords = NULL;
1149 ret->data.ksk.keywords
1150 = GNUNET_malloc (sizeof (char *) *
1151 GNUNET_CONTAINER_meta_data_get_contents (md, NULL, NULL));
1152 GNUNET_CONTAINER_meta_data_get_contents (md, &gather_uri_data, ret);
1159 * In URI-encoding, does the given character
1160 * need to be encoded using %-encoding?
1163 needs_percent (char c)
1165 return (!((isalnum (c)) ||
1166 (c == '-') || (c == '_') || (c == '.') || (c == '~')));
1171 * Convert a KSK URI to a string.
1173 * @param uri the URI to convert
1174 * @return NULL on error (i.e. keywordCount == 0)
1177 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1180 unsigned int keywordCount;
1187 const char *keyword;
1189 if (uri->type != ksk)
1191 keywords = uri->data.ksk.keywords;
1192 keywordCount = uri->data.ksk.keywordCount;
1194 keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1195 strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1196 for (i = 0; i < keywordCount; i++)
1198 keyword = keywords[i];
1199 slen = strlen (keyword);
1201 for (j = 0; j < slen; j++)
1203 if ((j == 0) && (keyword[j] == ' '))
1206 continue; /* skip leading space */
1208 if (needs_percent (keyword[j]))
1209 n += 2; /* will use %-encoding */
1212 ret = GNUNET_malloc (n);
1213 strcpy (ret, GNUNET_FS_URI_PREFIX);
1214 strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1215 wpos = strlen (ret);
1216 for (i = 0; i < keywordCount; i++)
1218 keyword = keywords[i];
1219 slen = strlen (keyword);
1220 for (j = 0; j < slen; j++)
1222 if ((j == 0) && (keyword[j] == ' '))
1223 continue; /* skip leading space */
1224 if (needs_percent (keyword[j]))
1226 sprintf (&ret[wpos], "%%%02X", keyword[j]);
1231 ret[wpos++] = keyword[j];
1234 if (i != keywordCount - 1)
1242 * Convert SKS URI to a string.
1244 * @param uri sks uri to convert
1245 * @return NULL on error
1248 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1250 const GNUNET_HashCode * namespace;
1251 const char *identifier;
1253 struct GNUNET_CRYPTO_HashAsciiEncoded ns;
1255 if (uri->type != sks)
1257 namespace = &uri->data.sks.namespace;
1258 identifier = uri->data.sks.identifier;
1259 GNUNET_CRYPTO_hash_to_enc (namespace, &ns);
1260 GNUNET_asprintf (&ret,
1262 GNUNET_FS_URI_PREFIX,
1263 GNUNET_FS_URI_SKS_INFIX,
1264 (const char *) &ns, identifier);
1270 * Convert a CHK URI to a string.
1272 * @param uri chk uri to convert
1273 * @return NULL on error
1276 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1278 const struct FileIdentifier * fi;
1280 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1281 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1283 if (uri->type != chk)
1285 fi = &uri->data.chk;
1286 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1287 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1289 GNUNET_asprintf (&ret,
1291 GNUNET_FS_URI_PREFIX,
1292 GNUNET_FS_URI_CHK_INFIX,
1293 (const char *) &keyhash,
1294 (const char *) &queryhash,
1295 GNUNET_ntohll (fi->file_length));
1300 * Convert binary data to a string.
1302 * @return converted data
1305 bin2enc (const void *data, size_t size)
1308 * 64 characters for encoding, 6 bits per character
1311 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=";
1319 GNUNET_assert (strlen (tbl) == 64);
1321 if (((size * 8) % 6) != 0)
1323 ret = GNUNET_malloc (len + 1);
1328 for (pos = 0; pos < size; pos++)
1330 bits |= ((((const unsigned char *) data)[pos]) << hbits);
1334 ret[len++] = tbl[bits & 63];
1340 ret[len++] = tbl[bits & 63];
1346 * Convert a LOC URI to a string.
1348 * @param uri loc uri to convert
1349 * @return NULL on error
1352 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
1355 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1356 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1360 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
1361 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
1362 peerId = bin2enc (&uri->data.loc.peer,
1363 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1364 peerSig = bin2enc (&uri->data.loc.contentSignature,
1365 sizeof (struct GNUNET_CRYPTO_RsaSignature));
1366 GNUNET_asprintf (&ret,
1367 "%s%s%s.%s.%llu.%s.%s.%llu",
1368 GNUNET_FS_URI_PREFIX,
1369 GNUNET_FS_URI_LOC_INFIX,
1370 (const char *) &keyhash,
1371 (const char *) &queryhash,
1372 (unsigned long long) GNUNET_ntohll (uri->data.loc.fi.file_length),
1375 (unsigned long long) uri->data.loc.expirationTime.value);
1376 GNUNET_free (peerSig);
1377 GNUNET_free (peerId);
1383 * Convert a URI to a UTF-8 String.
1385 * @param uri uri to convert to a string
1386 * @return the UTF-8 string
1389 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
1399 return uri_ksk_to_string (uri);
1401 return uri_sks_to_string (uri);
1403 return uri_chk_to_string (uri);
1405 return uri_loc_to_string (uri);
1416 * Construct a location URI.
1418 * @param baseURI content offered by the sender
1419 * @param sender identity of the peer with the content
1420 * @param expiration_time how long will the content be offered?
1421 * @param proto transport protocol to reach the peer
1422 * @param sas sender address size (for HELLO)
1423 * @param address sas bytes of address information
1424 * @param signer function to call for obtaining
1425 * RSA signatures for "sender".
1426 * @return the location URI
1428 struct GNUNET_ECRS_URI *
1429 GNUNET_ECRS_location_to_uri (const struct GNUNET_ECRS_URI *baseUri,
1430 const GNUNET_RSA_PublicKey * sender,
1431 GNUNET_Int32Time expirationTime,
1432 GNUNET_ECRS_SignFunction signer,
1435 struct GNUNET_ECRS_URI *uri;
1437 if (baseUri->type != chk)
1440 uri = GNUNET_malloc (sizeof (struct GNUNET_ECRS_URI));
1442 uri->data.loc.fi = baseUri->data.fi;
1443 uri->data.loc.peer = *sender;
1444 uri->data.loc.expirationTime = expirationTime;
1446 sizeof (GNUNET_EC_FileIdentifier) +
1447 sizeof (GNUNET_PeerIdentity) +
1448 sizeof (GNUNET_Int32Time),
1449 &uri->data.loc.fi, &uri->data.loc.contentSignature);