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_lib.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.fi.chk.query;
103 GNUNET_hash (uri->data.sks.identifier,
104 strlen (uri->data.sks.identifier), key);
107 if (uri->data.ksk.keywordCount > 0)
108 GNUNET_hash (uri->data.ksk.keywords[0],
109 strlen (uri->data.ksk.keywords[0]), key);
112 GNUNET_hash (&uri->data.loc.fi,
113 sizeof (GNUNET_EC_FileIdentifier) +
114 sizeof (GNUNET_RSA_PublicKey), key);
117 memset (key, 0, sizeof (GNUNET_HashCode));
124 * Convert a URI to a UTF-8 String.
126 * @param uri uri to convert to a string
127 * @return the UTF-8 string
130 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri);
134 * Convert keyword URI to a human readable format
135 * (i.e. the search query that was used in the first place)
137 * @param uri ksk uri to convert to a string
138 * @return string with the keywords
141 GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri);
144 * Convert a UTF-8 String to a URI.
146 * @param uri string to parse
147 * @param emsg where to store the parser error message (if any)
148 * @return NULL on error
150 struct GNUNET_FS_Uri *
151 GNUNET_FS_uri_parse (const char *uri,
157 * @param uri uri to free
160 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
164 GNUNET_assert (uri != NULL);
168 for (i = 0; i < uri->data.ksk.keywordCount; i++)
169 GNUNET_free (uri->data.ksk.keywords[i]);
170 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
174 GNUNET_free (uri->data.sks.identifier);
186 * How many keywords are ANDed in this keyword URI?
188 * @param uri ksk uri to get the number of keywords from
189 * @return 0 if this is not a keyword URI
192 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
194 if (uri->type != ksk)
196 return uri->data.ksk.keywordCount;
201 * Iterate over all keywords in this keyword URI.
203 * @param uri ksk uri to get the keywords from
204 * @param iterator function to call on each keyword
205 * @param iterator_cls closure for iterator
206 * @return -1 if this is not a keyword URI, otherwise number of
207 * keywords iterated over until iterator aborted
210 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
211 GNUNET_FS_KeywordIterator iterator,
217 if (uri->type != ksk)
219 if (iterator == NULL)
220 return uri->data.ksk.keywordCount;
221 for (i = 0; i < uri->data.ksk.keywordCount; i++)
223 keyword = uri->data.ksk.keywords[i];
224 /* first character of keyword indicates
225 if it is mandatory or not */
226 if (GNUNET_OK != iterator (&keyword[1], keyword[0] == '+', cls))
234 * Obtain the identity of the peer offering the data
236 * @param uri the location URI to inspect
237 * @param peer where to store the identify of the peer (presumably) offering the content
238 * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
241 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
242 struct GNUNET_PeerIdentity * peer)
244 if (uri->type != loc)
245 return GNUNET_SYSERR;
246 GNUNET_hash (&uri->data.loc.peer, sizeof (GNUNET_RSA_PublicKey),
253 * Obtain the URI of the content itself.
255 * @param uri location URI to get the content URI from
256 * @return NULL if argument is not a location URI
258 struct GNUNET_FS_Uri *
259 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
261 struct GNUNET_ECRS_Uri *ret;
263 if (uri->type != loc)
265 ret = GNUNET_malloc (sizeof (struct GNUNET_ECRS_Uri));
267 ret->data.chk = uri->data.loc.fi;
273 * Construct a location URI (this peer will be used for the location).
275 * @param baseURI content offered by the sender
276 * @param cfg configuration information (used to find our hostkey)
277 * @param expiration_time how long will the content be offered?
278 * @return the location URI, NULL on error
280 struct GNUNET_FS_Uri *
281 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
282 struct GNUNET_CONFIGURATION_Handle *cfg,
283 struct GNUNET_TIME_Absolute expiration_time);
287 * Canonicalize keyword URI. Performs operations such
288 * as decapitalization and removal of certain characters.
289 * (useful for search).
291 * @param uri the URI to canonicalize
292 * @return canonicalized version of the URI, NULL on error
294 struct GNUNET_FS_Uri *
295 GNUNET_FS_uri_ksk_canonicalize (const struct GNUNET_FS_Uri *uri);
299 * Merge the sets of keywords from two KSK URIs.
300 * (useful for merging the canonicalized keywords with
301 * the original keywords for sharing).
303 * @param u1 first uri
304 * @param u2 second uri
305 * @return merged URI, NULL on error
307 struct GNUNET_FS_Uri *
308 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
309 const struct GNUNET_FS_Uri *u2);
315 * @param uri the URI to duplicate
316 * @return copy of the URI
318 struct GNUNET_FS_Uri *
319 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
321 struct GNUNET_ECRS_URI *ret;
324 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
325 memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
329 if (ret->data.ksk.keywordCount > 0)
331 ret->data.ksk.keywords
332 = GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
333 for (i = 0; i < ret->data.ksk.keywordCount; i++)
334 ret->data.ksk.keywords[i] =
335 GNUNET_strdup (uri->data.ksk.keywords[i]);
338 ret->data.ksk.keywords = NULL; /* just to be sure */
341 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
353 * Create an FS URI from a single user-supplied string of keywords.
354 * The string is broken up at spaces into individual keywords.
355 * Keywords that start with "+" are mandatory. Double-quotes can
356 * be used to prevent breaking up strings at spaces (and also
357 * to specify non-mandatory keywords starting with "+").
359 * Keywords must contain a balanced number of double quotes and
360 * double quotes can not be used in the actual keywords (for
361 * example, the string '""foo bar""' will be turned into two
362 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
364 * @param keywords the keyword string
365 * @return an FS URI for the given keywords, NULL
366 * if keywords is not legal (i.e. empty).
368 struct GNUNET_FS_Uri *
369 GNUNET_FS_uri_ksk_create (const char *keywords);
373 * Create an FS URI from a user-supplied command line of keywords.
374 * Arguments should start with "+" to indicate mandatory
377 * @param argc number of keywords
378 * @param argv keywords (double quotes are not required for
379 * keywords containing spaces; however, double
380 * quotes are required for keywords starting with
381 * "+"); there is no mechanism for having double
382 * quotes in the actual keywords (if the user
383 * did specifically specify double quotes, the
384 * caller should convert each double quote
385 * into two single quotes).
386 * @return an FS URI for the given keywords, NULL
387 * if keywords is not legal (i.e. empty).
389 struct GNUNET_FS_Uri *
390 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
395 * Test if two URIs are equal.
397 * @param u1 one of the URIs
398 * @param u2 the other URI
399 * @return GNUNET_YES if the URIs are equal
402 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
403 const struct GNUNET_FS_Uri *u2)
409 GNUNET_assert (uri1 != NULL);
410 GNUNET_assert (uri2 != NULL);
411 if (uri1->type != uri2->type)
416 if (0 == memcmp (&uri1->data.chk,
418 sizeof (struct FileIdentifier)))
422 if ((0 == memcmp (&uri1->data.sks.namespace,
423 &uri2->data.sks.namespace,
424 sizeof (GNUNET_HashCode))) &&
425 (0 == strcmp (uri1->data.sks.identifier,
426 uri2->data.sks.identifier)))
431 if (uri1->data.ksk.keywordCount != uri2->data.ksk.keywordCount)
433 for (i = 0; i < uri1->data.ksk.keywordCount; i++)
436 for (j = 0; j < uri2->data.ksk.keywordCount; j++)
438 if (0 == strcmp (uri1->data.ksk.keywords[i],
439 uri2->data.ksk.keywords[j]))
445 if (ret == GNUNET_NO)
450 if (memcmp (&uri1->data.loc,
452 sizeof (struct FileIdentifier) +
453 sizeof (GNUNET_RSA_PublicKey) +
454 sizeof (struct GNUNET_TIME_Absolute) +
455 sizeof (unsigned short) + sizeof (unsigned short)) != 0)
465 * Is this a namespace URI?
467 * @param uri the uri to check
468 * @return GNUNET_YES if this is an SKS uri
471 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
473 return uri->type == sks;
478 * Get the ID of a namespace from the given
481 * @param uri the uri to get the namespace ID from
482 * @param nsid where to store the ID of the namespace
483 * @return GNUNET_OK on success
486 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
487 GNUNET_HashCode * nsid)
489 if (! GNUNET_FS_uri_test_sks (uri))
492 return GNUNET_SYSERR;
494 *id = uri->data.sks.namespace;
500 * Get the content identifier of an SKS URI.
502 * @param uri the sks uri
503 * @return NULL on error (not a valid SKS URI)
506 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
508 if (!GNUNET_FS_uri_test_sks (uri))
513 return GNUNET_strdup (uri->data.sks.identifier);
518 * Convert namespace URI to a human readable format
519 * (using the namespace description, if available).
521 * @param cfg configuration to use
522 * @param uri SKS uri to convert
523 * @return NULL on error (not an SKS URI)
526 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
527 const struct GNUNET_FS_Uri *uri);
531 * Is this a keyword URI?
534 * @return GNUNET_YES if this is a KSK uri
537 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
542 if (uri->type == ksk)
544 for (i = uri->data.ksk.keywordCount - 1; i >= 0; i--)
545 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
548 return uri->type == ksk;
553 * Is this a file (or directory) URI?
555 * @param uri the uri to check
556 * @return GNUNET_YES if this is a CHK uri
559 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
561 return uri->type == chk;
566 * What is the size of the file that this URI
569 * @param uri the CHK URI to inspect
570 * @return size of the file as specified in the CHK URI
573 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri)
578 return GNUNET_ntohll (uri->data.chk.file_length);
580 return GNUNET_ntohll (uri->data.loc.fi.file_length);
584 return 0; /* unreachable */
589 * Is this a location URI?
591 * @param uri the uri to check
592 * @return GNUNET_YES if this is a LOC uri
595 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
597 return uri->type == loc;
602 * Function called on each value in the meta data.
603 * Adds it to the URI.
605 * @param cls URI to update
606 * @param type type of the meta data
607 * @param data value of the meta data
608 * @return GNUNET_OK (always)
611 gather_uri_data (void *cls,
612 EXTRACTOR_KeywordType type,
615 struct GNUNET_FS_Uri *uri = cls;
619 for (j = uri->data.ksk.keywordCount - 1; j >= 0; j--)
620 if (0 == strcmp (&uri->data.ksk.keywords[j][1], data))
622 nkword = GNUNET_malloc (strlen (data) + 2);
623 strcpy (nkword, " "); /* not mandatory */
624 strcat (nkword, data);
625 uri->data.ksk.keywords[uri->data.ksk.keywordCount++] = nkword;
631 * Construct a keyword-URI from meta-data (take all entries
632 * in the meta-data and construct one large keyword URI
633 * that lists all keywords that can be found in the meta-data).
636 struct GNUNET_FS_Uri *
637 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_MetaData *md)
639 struct GNUNET_FS_Uri *ret;
643 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
645 ret->data.ksk.keywordCount = 0;
646 ret->data.ksk.keywords = NULL;
647 ret->data.ksk.keywords
648 = GNUNET_malloc (sizeof (char *) *
649 GNUNET_meta_data_get_contents (md, NULL, NULL));
650 GNUNET_meta_data_get_contents (md, &gather_uri_data, ret);
662 * In URI-encoding, does the given character
663 * need to be encoded using %-encoding?
666 needs_percent (char c)
668 return (!((isalnum (c)) ||
669 (c == '-') || (c == '_') || (c == '.') || (c == '~')));
673 * Generate a keyword URI.
674 * @return NULL on error (i.e. keywordCount == 0)
677 createKeywordURI (char **keywords, unsigned int keywordCount)
688 keywordCount + strlen (GNUNET_ECRS_URI_PREFIX) +
689 strlen (GNUNET_ECRS_SEARCH_INFIX) + 1;
690 for (i = 0; i < keywordCount; i++)
692 keyword = keywords[i];
693 slen = strlen (keyword);
695 for (j = 0; j < slen; j++)
697 if ((j == 0) && (keyword[j] == ' '))
700 continue; /* skip leading space */
702 if (needs_percent (keyword[j]))
703 n += 2; /* will use %-encoding */
706 ret = GNUNET_malloc (n);
707 strcpy (ret, GNUNET_ECRS_URI_PREFIX);
708 strcat (ret, GNUNET_ECRS_SEARCH_INFIX);
710 for (i = 0; i < keywordCount; i++)
712 keyword = keywords[i];
713 slen = strlen (keyword);
714 for (j = 0; j < slen; j++)
716 if ((j == 0) && (keyword[j] == ' '))
717 continue; /* skip leading space */
718 if (needs_percent (keyword[j]))
720 sprintf (&ret[wpos], "%%%02X", keyword[j]);
725 ret[wpos++] = keyword[j];
728 if (i != keywordCount - 1)
735 * Generate a subspace URI.
738 createSubspaceURI (const GNUNET_HashCode * namespace, const char *identifier)
745 sizeof (GNUNET_EncName) + strlen (GNUNET_ECRS_URI_PREFIX) +
746 strlen (GNUNET_ECRS_SUBSPACE_INFIX) + 1 + strlen (identifier);
747 ret = GNUNET_malloc (n);
748 GNUNET_hash_to_enc (namespace, &ns);
749 GNUNET_snprintf (ret, n,
751 GNUNET_ECRS_URI_PREFIX, GNUNET_ECRS_SUBSPACE_INFIX,
752 (const char *) &ns, identifier);
757 * Generate a file URI.
760 createFileURI (const GNUNET_EC_FileIdentifier * fi)
763 GNUNET_EncName keyhash;
764 GNUNET_EncName queryhash;
767 GNUNET_hash_to_enc (&fi->chk.key, &keyhash);
768 GNUNET_hash_to_enc (&fi->chk.query, &queryhash);
771 strlen (GNUNET_ECRS_URI_PREFIX) + 2 * sizeof (GNUNET_EncName) + 8 + 16 +
772 32 + strlen (GNUNET_ECRS_FILE_INFIX);
773 ret = GNUNET_malloc (n);
774 GNUNET_snprintf (ret,
777 GNUNET_ECRS_URI_PREFIX,
778 GNUNET_ECRS_FILE_INFIX,
779 (char *) &keyhash, (char *) &queryhash,
780 GNUNET_ntohll (fi->file_length));
784 #include "bincoder.c"
787 * Create a (string) location URI from a Location.
790 createLocURI (const Location * loc)
794 GNUNET_EncName keyhash;
795 GNUNET_EncName queryhash;
799 GNUNET_hash_to_enc (&loc->fi.chk.key, &keyhash);
800 GNUNET_hash_to_enc (&loc->fi.chk.query, &queryhash);
802 peerId = bin2enc (&loc->peer, sizeof (GNUNET_RSA_PublicKey));
803 peerSig = bin2enc (&loc->contentSignature, sizeof (GNUNET_RSA_Signature));
804 ret = GNUNET_malloc (n);
805 GNUNET_snprintf (ret,
807 "%s%s%s.%s.%llu.%s.%s.%u",
808 GNUNET_ECRS_URI_PREFIX,
809 GNUNET_ECRS_LOCATION_INFIX,
812 GNUNET_ntohll (loc->fi.file_length),
813 peerId, peerSig, loc->expirationTime);
814 GNUNET_free (peerSig);
815 GNUNET_free (peerId);
820 * Convert a URI to a UTF-8 String.
823 GNUNET_ECRS_uri_to_string (const struct GNUNET_ECRS_URI *uri)
827 GNUNET_GE_BREAK (NULL, 0);
833 return createKeywordURI (uri->data.ksk.keywords,
834 uri->data.ksk.keywordCount);
836 return createSubspaceURI (&uri->data.sks.namespace,
837 uri->data.sks.identifier);
839 return createFileURI (&uri->data.fi);
841 return createLocURI (&uri->data.loc);
843 GNUNET_GE_BREAK (NULL, 0);
849 * Convert keyword URI to a human readable format
850 * (i.e. the search query that was used in the first place)
853 GNUNET_ECRS_ksk_uri_to_human_readable_string (const struct GNUNET_ECRS_URI
861 unsigned int keywordCount;
863 if ((uri == NULL) || (uri->type != ksk))
865 GNUNET_GE_BREAK (NULL, 0);
868 keywords = uri->data.ksk.keywords;
869 keywordCount = uri->data.ksk.keywordCount;
870 n = keywordCount + 1;
871 for (i = 0; i < keywordCount; i++)
873 keyword = keywords[i];
874 n += strlen (keyword) - 1;
875 if (NULL != strstr (&keyword[1], " "))
877 if (keyword[0] == '+')
880 ret = GNUNET_malloc (n);
882 for (i = 0; i < keywordCount; i++)
884 keyword = keywords[i];
885 if (NULL != strstr (&keyword[1], " "))
888 if (keyword[0] == '+')
889 strcat (ret, keyword);
891 strcat (ret, &keyword[1]);
896 if (keyword[0] == '+')
897 strcat (ret, keyword);
899 strcat (ret, &keyword[1]);
907 * Given a keyword with %-encoding (and possibly quotes to protect
908 * spaces), return a copy of the keyword without %-encoding and
909 * without double-quotes (%22). Also, add a space at the beginning
910 * if there is not a '+'.
913 percent_decode_keyword (const char *in)
921 out = GNUNET_strdup (in);
924 while (out[rpos] != '\0')
926 if (out[rpos] == '%')
928 if (1 != sscanf (&out[rpos + 1], "%2X", &hx))
935 continue; /* skip double quote */
936 out[wpos++] = (char) hx;
940 out[wpos++] = out[rpos++];
946 ret = GNUNET_strdup (out);
950 /* need to prefix with space */
951 ret = GNUNET_malloc (strlen (out) + 2);
960 * Parses an ECRS search URI.
962 * @param uri an uri string
963 * @param keyword will be set to an array with the keywords
964 * @return GNUNET_SYSERR if this is not a search URI, otherwise
965 * the number of keywords placed in the array
968 parseKeywordURI (struct GNUNET_GE_Context *ectx, const char *uri,
979 GNUNET_GE_ASSERT (ectx, uri != NULL);
982 pos = strlen (GNUNET_ECRS_URI_PREFIX);
984 if (0 != strncmp (uri, GNUNET_ECRS_URI_PREFIX, pos))
985 return GNUNET_SYSERR;
987 strncmp (&uri[pos], GNUNET_ECRS_SEARCH_INFIX,
988 strlen (GNUNET_ECRS_SEARCH_INFIX)))
989 return GNUNET_SYSERR;
990 pos += strlen (GNUNET_ECRS_SEARCH_INFIX);
997 if ((uri[slen - 1] == '+') || (uri[pos] == '+'))
998 return GNUNET_SYSERR; /* no keywords / malformed */
1002 for (i = pos; i < slen; i++)
1004 if ((uri[i] == '%') && (&uri[i] == strstr (&uri[i], "%22")))
1006 saw_quote = (saw_quote + 1) % 2;
1010 if ((uri[i] == '+') && (saw_quote == 0))
1013 if (uri[i - 1] == '+')
1014 return GNUNET_SYSERR; /* "++" not allowed */
1018 return GNUNET_SYSERR; /* quotes not balanced */
1020 dup = GNUNET_strdup (uri);
1021 (*keywords) = GNUNET_malloc (ret * sizeof (char *));
1022 for (i = 0; i < ret; i++)
1023 (*keywords)[i] = NULL;
1024 for (i = slen - 1; i >= pos; i--)
1026 if ((uri[i] == '%') && (&uri[i] == strstr (&uri[i], "%22")))
1028 saw_quote = (saw_quote + 1) % 2;
1032 if ((dup[i] == '+') && (saw_quote == 0))
1034 (*keywords)[--ret] = percent_decode_keyword (&dup[i + 1]);
1035 if (NULL == (*keywords)[ret])
1040 (*keywords)[--ret] = percent_decode_keyword (&dup[pos]);
1041 if (NULL == (*keywords)[ret])
1043 GNUNET_GE_ASSERT (ectx, ret == 0);
1047 for (i = 0; i < ret; i++)
1048 GNUNET_free_non_null ((*keywords)[i]);
1049 GNUNET_free (*keywords);
1052 return GNUNET_SYSERR;
1056 * Parses an AFS namespace / subspace identifier URI.
1058 * @param uri an uri string
1059 * @param namespace set to the namespace ID
1060 * @param identifier set to the ID in the namespace
1061 * @return GNUNET_OK on success, GNUNET_SYSERR if this is not a namespace URI
1064 parseSubspaceURI (struct GNUNET_GE_Context *ectx,
1066 GNUNET_HashCode * namespace, char **identifier)
1072 GNUNET_GE_ASSERT (ectx, uri != NULL);
1074 slen = strlen (uri);
1075 pos = strlen (GNUNET_ECRS_URI_PREFIX);
1077 if (0 != strncmp (uri, GNUNET_ECRS_URI_PREFIX, pos))
1078 return GNUNET_SYSERR;
1079 if (0 != strncmp (&uri[pos],
1080 GNUNET_ECRS_SUBSPACE_INFIX,
1081 strlen (GNUNET_ECRS_SUBSPACE_INFIX)))
1082 return GNUNET_SYSERR;
1083 pos += strlen (GNUNET_ECRS_SUBSPACE_INFIX);
1084 if ((slen < pos + sizeof (GNUNET_EncName) + 1) ||
1085 (!((uri[pos + sizeof (GNUNET_EncName) - 1] == '/') ||
1086 (uri[pos + sizeof (GNUNET_EncName) - 1] == '\\'))))
1087 return GNUNET_SYSERR;
1089 up = GNUNET_strdup (uri);
1090 up[pos + sizeof (GNUNET_EncName) - 1] = '\0';
1091 if ((GNUNET_OK != GNUNET_enc_to_hash (&up[pos], namespace)))
1094 return GNUNET_SYSERR;
1096 *identifier = GNUNET_strdup (&up[pos + sizeof (GNUNET_EncName)]);
1102 * Parses an URI that identifies a file
1104 * @param uri an uri string
1105 * @param fi the file identifier
1106 * @return GNUNET_OK on success, GNUNET_SYSERR if this is not a file URI
1109 parseFileURI (struct GNUNET_GE_Context *ectx, const char *uri,
1110 GNUNET_EC_FileIdentifier * fi)
1116 GNUNET_GE_ASSERT (ectx, uri != NULL);
1118 slen = strlen (uri);
1119 pos = strlen (GNUNET_ECRS_URI_PREFIX);
1121 if (0 != strncmp (uri, GNUNET_ECRS_URI_PREFIX, pos))
1122 return GNUNET_SYSERR;
1124 strncmp (&uri[pos], GNUNET_ECRS_FILE_INFIX,
1125 strlen (GNUNET_ECRS_FILE_INFIX)))
1126 return GNUNET_SYSERR;
1127 pos += strlen (GNUNET_ECRS_FILE_INFIX);
1128 if ((slen < pos + 2 * sizeof (GNUNET_EncName) + 1) ||
1129 (uri[pos + sizeof (GNUNET_EncName) - 1] != '.') ||
1130 (uri[pos + sizeof (GNUNET_EncName) * 2 - 1] != '.'))
1131 return GNUNET_SYSERR;
1133 dup = GNUNET_strdup (uri);
1134 dup[pos + sizeof (GNUNET_EncName) - 1] = '\0';
1135 dup[pos + sizeof (GNUNET_EncName) * 2 - 1] = '\0';
1136 if ((GNUNET_OK != GNUNET_enc_to_hash (&dup[pos],
1138 (GNUNET_OK != GNUNET_enc_to_hash (&dup[pos + sizeof (GNUNET_EncName)],
1140 (1 != SSCANF (&dup[pos + sizeof (GNUNET_EncName) * 2],
1141 "%llu", &fi->file_length)))
1144 return GNUNET_SYSERR;
1147 fi->file_length = GNUNET_htonll (fi->file_length);
1152 * Parses an URI that identifies a location (and file).
1153 * Also verifies validity of the location URI.
1155 * @param uri an uri string
1156 * @param loc where to store the location
1157 * @return GNUNET_OK on success, GNUNET_SYSERR if this is not a file URI
1160 parseLocationURI (struct GNUNET_GE_Context *ectx, const char *uri,
1171 GNUNET_GE_ASSERT (ectx, uri != NULL);
1173 slen = strlen (uri);
1174 pos = strlen (GNUNET_ECRS_URI_PREFIX);
1176 if (0 != strncmp (uri, GNUNET_ECRS_URI_PREFIX, pos))
1177 return GNUNET_SYSERR;
1178 if (0 != strncmp (&uri[pos],
1179 GNUNET_ECRS_LOCATION_INFIX,
1180 strlen (GNUNET_ECRS_LOCATION_INFIX)))
1181 return GNUNET_SYSERR;
1182 pos += strlen (GNUNET_ECRS_LOCATION_INFIX);
1183 if ((slen < pos + 2 * sizeof (GNUNET_EncName) + 1) ||
1184 (uri[pos + sizeof (GNUNET_EncName) - 1] != '.') ||
1185 (uri[pos + sizeof (GNUNET_EncName) * 2 - 1] != '.'))
1186 return GNUNET_SYSERR;
1188 dup = GNUNET_strdup (uri);
1189 dup[pos + sizeof (GNUNET_EncName) - 1] = '\0';
1190 dup[pos + sizeof (GNUNET_EncName) * 2 - 1] = '\0';
1191 npos = pos + sizeof (GNUNET_EncName) * 2;
1192 while ((uri[npos] != '\0') && (uri[npos] != '.'))
1194 if (dup[npos] == '\0')
1197 if ((GNUNET_OK != GNUNET_enc_to_hash (&dup[pos],
1198 &loc->fi.chk.key)) ||
1199 (GNUNET_OK != GNUNET_enc_to_hash (&dup[pos + sizeof (GNUNET_EncName)],
1200 &loc->fi.chk.query)) ||
1201 (1 != SSCANF (&dup[pos + sizeof (GNUNET_EncName) * 2],
1202 "%llu", &loc->fi.file_length)))
1204 loc->fi.file_length = GNUNET_htonll (loc->fi.file_length);
1205 ret = enc2bin (&dup[npos], &loc->peer, sizeof (GNUNET_RSA_PublicKey));
1209 if (dup[npos++] != '.')
1212 enc2bin (&dup[npos], &loc->contentSignature,
1213 sizeof (GNUNET_RSA_Signature));
1217 if (dup[npos++] != '.')
1219 if (1 != SSCANF (&dup[npos], "%u", &loc->expirationTime))
1221 /* Finally: verify sigs! */
1222 if (GNUNET_OK != GNUNET_RSA_verify (&loc->fi,
1223 sizeof (GNUNET_EC_FileIdentifier) +
1224 sizeof (GNUNET_PeerIdentity) +
1225 sizeof (GNUNET_Int32Time),
1226 &loc->contentSignature, &loc->peer))
1232 GNUNET_free_non_null (addr);
1233 return GNUNET_SYSERR;
1237 * Convert a UTF-8 String to a URI.
1240 GNUNET_ECRS_string_to_uri (struct GNUNET_GE_Context * ectx, const char *uri)
1245 ret = GNUNET_malloc (sizeof (URI));
1246 if (GNUNET_OK == parseFileURI (ectx, uri, &ret->data.fi))
1251 if (GNUNET_OK == parseSubspaceURI (ectx,
1253 &ret->data.sks.namespace,
1254 &ret->data.sks.identifier))
1259 if (GNUNET_OK == parseLocationURI (ectx, uri, &ret->data.loc))
1264 len = parseKeywordURI (ectx, uri, &ret->data.ksk.keywords);
1271 ret->data.ksk.keywordCount = len;
1278 * Construct a location URI.
1280 * @param baseURI content offered by the sender
1281 * @param sender identity of the peer with the content
1282 * @param expiration_time how long will the content be offered?
1283 * @param proto transport protocol to reach the peer
1284 * @param sas sender address size (for HELLO)
1285 * @param address sas bytes of address information
1286 * @param signer function to call for obtaining
1287 * RSA signatures for "sender".
1288 * @return the location URI
1290 struct GNUNET_ECRS_URI *
1291 GNUNET_ECRS_location_to_uri (const struct GNUNET_ECRS_URI *baseUri,
1292 const GNUNET_RSA_PublicKey * sender,
1293 GNUNET_Int32Time expirationTime,
1294 GNUNET_ECRS_SignFunction signer,
1297 struct GNUNET_ECRS_URI *uri;
1299 if (baseUri->type != chk)
1302 uri = GNUNET_malloc (sizeof (struct GNUNET_ECRS_URI));
1304 uri->data.loc.fi = baseUri->data.fi;
1305 uri->data.loc.peer = *sender;
1306 uri->data.loc.expirationTime = expirationTime;
1308 sizeof (GNUNET_EC_FileIdentifier) +
1309 sizeof (GNUNET_PeerIdentity) +
1310 sizeof (GNUNET_Int32Time),
1311 &uri->data.loc.fi, &uri->data.loc.contentSignature);