aboutsummaryrefslogtreecommitdiff
path: root/src/fs/fs_uri.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2009-08-16 09:31:25 +0000
committerChristian Grothoff <christian@grothoff.org>2009-08-16 09:31:25 +0000
commitab1934147f74c835b525864b770795f858e37b89 (patch)
tree193ae3f0682dbee68881e24261e8228429998a8d /src/fs/fs_uri.c
parent596fe6fcff78fc66f2ed7e8bd46475feef9c6210 (diff)
downloadgnunet-ab1934147f74c835b525864b770795f858e37b89.tar.gz
gnunet-ab1934147f74c835b525864b770795f858e37b89.zip
move-hacknig
Diffstat (limited to 'src/fs/fs_uri.c')
-rw-r--r--src/fs/fs_uri.c367
1 files changed, 309 insertions, 58 deletions
diff --git a/src/fs/fs_uri.c b/src/fs/fs_uri.c
index cf0e8a067..f2351af1b 100644
--- a/src/fs/fs_uri.c
+++ b/src/fs/fs_uri.c
@@ -182,8 +182,6 @@ GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri)
182} 182}
183 183
184 184
185
186
187/** 185/**
188 * Given a keyword with %-encoding (and possibly quotes to protect 186 * Given a keyword with %-encoding (and possibly quotes to protect
189 * spaces), return a copy of the keyword without %-encoding and 187 * spaces), return a copy of the keyword without %-encoding and
@@ -762,7 +760,127 @@ GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
762struct GNUNET_FS_Uri * 760struct GNUNET_FS_Uri *
763GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri, 761GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
764 struct GNUNET_CONFIGURATION_Handle *cfg, 762 struct GNUNET_CONFIGURATION_Handle *cfg,
765 struct GNUNET_TIME_Absolute expiration_time); 763 struct GNUNET_TIME_Absolute expiration_time)
764{
765 struct GNUNET_FS_Uri *uri;
766 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
767 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
768 char *keyfile;
769 struct LocUriAssembly ass;
770
771 if (baseUri->type != chk)
772 return NULL;
773 if (GNUNET_OK !=
774 GNUNET_CONFIGURATION_get_value_filename (cfg,
775 "GNUNETD",
776 "HOSTKEY", &keyfile))
777 {
778 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
779 _
780 ("Lacking key configuration settings.\n"));
781 return NULL;
782 }
783 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
784 if (my_private_key == NULL)
785 {
786 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
787 _("Could not access hostkey file `%s'.\n"),
788 keyfile);
789 GNUNET_free (keyfile);
790 return NULL;
791 }
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));
800 uri->type = loc;
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,
806 &ass.purpose,
807 &uri->data.loc.contentSignature));
808 GNUNET_CRYPTO_rsa_key_free (my_private_key);
809 return uri;
810}
811
812
813/**
814 * Canonicalize a keyword.
815 *
816 * @param in input string (the keyword)
817 * @return canonicalized keyword
818 */
819static char *
820canonicalize_keyword (const char *in)
821{
822 char *ret;
823 char *wpos;
824 const char *rpos;
825
826 ret = GNUNET_strdup (in);
827 wpos = ret;
828 rpos = in;
829 while ('\0' != *rpos)
830 {
831 switch (tolower(*rpos))
832 {
833 case 'a':
834 case 'e':
835 case 'i':
836 case 'o':
837 case 'u':
838 case ' ':
839 case '\t':
840 case '\n':
841 case '\r':
842 /* skip characters listed above */
843 rpos++;
844 break;
845 case 'b':
846 case 'c':
847 case 'd':
848 case 'f':
849 case 'g':
850 case 'h':
851 case 'j':
852 case 'k':
853 case 'l':
854 case 'm':
855 case 'n':
856 case 'p':
857 case 'r':
858 case 's':
859 case 't':
860 case 'v':
861 case 'w':
862 case 'x':
863 case 'y':
864 case 'z':
865 /* convert characters listed above to lower case */
866 *wpos = tolower(*rpos);
867 wpos++;
868 case '!':
869 case '.':
870 case '?':
871 case '-':
872 /* keep characters listed above without changes */
873 *wpos = *rpos;
874 wpos++;
875 default:
876 /* replace characters listed above with '_' */
877 *wpos = '_';
878 wpos++;
879 }
880 rpos++;
881 }
882 return ret;
883}
766 884
767 885
768/** 886/**
@@ -776,8 +894,20 @@ GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
776struct GNUNET_FS_Uri * 894struct GNUNET_FS_Uri *
777GNUNET_FS_uri_ksk_canonicalize (const struct GNUNET_FS_Uri *uri) 895GNUNET_FS_uri_ksk_canonicalize (const struct GNUNET_FS_Uri *uri)
778{ 896{
779 /* FIXME: not implemented */ 897 struct GNUNET_FS_Uri *ret;
780 return NULL; 898 unsigned int kc;
899 unsigned int i;
900 char **kl;
901
902 kc = uri->data.ksk.keywordCount;
903 kl = GNUNET_malloc (kc*sizeof(char*));
904 for (i=0;i<kc;i++)
905 kl[i] = canonicalize_keyword (uri->data.ksk.keywords[i]);
906 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
907 ret->type = ksk;
908 ret->data.ksk.keywordCount = kc;
909 ret->data.ksk.keywords = kl;
910 return ret;
781} 911}
782 912
783 913
@@ -794,8 +924,45 @@ struct GNUNET_FS_Uri *
794GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1, 924GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
795 const struct GNUNET_FS_Uri *u2) 925 const struct GNUNET_FS_Uri *u2)
796{ 926{
797 /* FIXME */ 927 struct GNUNET_FS_Uri *ret;
798 return NULL; 928 unsigned int kc;
929 unsigned int i;
930 unsigned int j;
931 int found;
932 const char *kp;
933 char **kl;
934
935 if ( (u1->type != ksk) ||
936 (u2->type != ksk) )
937 {
938 GNUNET_break (0);
939 return NULL;
940 }
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++)
946 {
947 kp = u2->data.ksk.keywords[i];
948 found = 0;
949 for (j=0;j<u1->data.ksk.keywordCount;j++)
950 if (0 == strcmp(kp + 1,
951 kl[j]+1))
952 {
953 found = 1;
954 if (kp[0] == '+')
955 kl[j][0] = '+';
956 break;
957 }
958 if (0 == found)
959 kl[kc++] = GNUNET_strdup (kp - 1);
960 }
961 ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
962 ret->type = ksk;
963 ret->data.ksk.keywordCount = kc;
964 ret->data.ksk.keywords = kl;
965 return ret;
799} 966}
800 967
801 968
@@ -852,14 +1019,86 @@ GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
852 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'. 1019 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
853 * 1020 *
854 * @param keywords the keyword string 1021 * @param keywords the keyword string
1022 * @param emsg where to store an error message
855 * @return an FS URI for the given keywords, NULL 1023 * @return an FS URI for the given keywords, NULL
856 * if keywords is not legal (i.e. empty). 1024 * if keywords is not legal (i.e. empty).
857 */ 1025 */
858struct GNUNET_FS_Uri * 1026struct GNUNET_FS_Uri *
859GNUNET_FS_uri_ksk_create (const char *keywords) 1027GNUNET_FS_uri_ksk_create (const char *keywords,
1028 char **emsg)
860{ 1029{
861 /* FIXME */ 1030 char **keywordarr;
862 return NULL; 1031 unsigned int num_Words;
1032 int inWord;
1033 char *pos;
1034 struct GNUNET_FS_Uri *uri;
1035 char *searchString;
1036 int saw_quote;
1037
1038 if (keywords == NULL)
1039 {
1040 GNUNET_break (0);
1041 return NULL;
1042 }
1043 searchString = GNUNET_strdup (keywords);
1044 num_Words = 0;
1045 inWord = 0;
1046 saw_quote = 0;
1047 pos = searchString;
1048 while ('\0' != *pos)
1049 {
1050 if ((saw_quote == 0) && (isspace (*pos)))
1051 {
1052 inWord = 0;
1053 }
1054 else if (0 == inWord)
1055 {
1056 inWord = 1;
1057 ++num_Words;
1058 }
1059 if ('"' == *pos)
1060 saw_quote = (saw_quote + 1) % 2;
1061 pos++;
1062 }
1063 if (num_Words == 0)
1064 {
1065 GNUNET_free (searchString);
1066 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1067 return NULL;
1068 }
1069 if (saw_quote != 0)
1070 {
1071 GNUNET_free (searchString);
1072 *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
1073 return NULL;
1074 }
1075 keywordarr = GNUNET_malloc (num_Words * sizeof (char *));
1076 num_Words = 0;
1077 inWord = 0;
1078 pos = searchString;
1079 while ('\0' != *pos)
1080 {
1081 if ((saw_quote == 0) && (isspace (*pos)))
1082 {
1083 inWord = 0;
1084 *pos = '\0';
1085 }
1086 else if (0 == inWord)
1087 {
1088 keywordarr[num_Words] = pos;
1089 inWord = 1;
1090 ++num_Words;
1091 }
1092 if ('"' == *pos)
1093 saw_quote = (saw_quote + 1) % 2;
1094 pos++;
1095 }
1096 uri =
1097 GNUNET_FS_uri_ksk_create_from_args (num_Words,
1098 (const char **) keywordarr);
1099 GNUNET_free (keywordarr);
1100 GNUNET_free (searchString);
1101 return uri;
863} 1102}
864 1103
865 1104
@@ -884,8 +1123,49 @@ struct GNUNET_FS_Uri *
884GNUNET_FS_uri_ksk_create_from_args (unsigned int argc, 1123GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
885 const char **argv) 1124 const char **argv)
886{ 1125{
887 /* FIXME */ 1126 unsigned int i;
888 return NULL; 1127 struct GNUNET_FS_Uri *uri;
1128 const char *keyword;
1129 char *val;
1130 const char *r;
1131 char *w;
1132 char *emsg;
1133
1134 if (argc == 0)
1135 return NULL;
1136 /* allow URI to be given as one and only keyword and
1137 handle accordingly */
1138 emsg = NULL;
1139 if ( (argc == 1) &&
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)) ) )
1143 return uri;
1144 GNUNET_free_non_null (emsg);
1145 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1146 uri->type = ksk;
1147 uri->data.ksk.keywordCount = argc;
1148 uri->data.ksk.keywords = GNUNET_malloc (argc * sizeof (char *));
1149 for (i = 0; i < argc; i++)
1150 {
1151 keyword = argv[i];
1152 if (keyword[0] == '+')
1153 val = GNUNET_strdup (keyword);
1154 else
1155 GNUNET_asprintf (&val, " %s", keyword);
1156 r = val;
1157 w = val;
1158 while ('\0' != *r)
1159 {
1160 if ('"' == *r)
1161 r++;
1162 else
1163 *(w++) = *(r++);
1164 }
1165 *w = '\0';
1166 uri->data.ksk.keywords[i] = val;
1167 }
1168 return uri;
889} 1169}
890 1170
891 1171
@@ -1024,8 +1304,20 @@ char *
1024GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg, 1304GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
1025 const struct GNUNET_FS_Uri *uri) 1305 const struct GNUNET_FS_Uri *uri)
1026{ 1306{
1027 /* FIXME */ 1307 char *ret;
1028 return NULL; 1308 char *name;
1309
1310 if (uri->type != sks)
1311 return NULL;
1312 name = GNUNET_PSEUDONYM_id_to_name (cfg, &uri->data.sks.namespace);
1313 if (name == NULL)
1314 return GNUNET_FS_uri_to_string (uri);
1315 GNUNET_asprintf (&ret,
1316 "%s: %s",
1317 name,
1318 uri->data.sks.identifier);
1319 GNUNET_free (name);
1320 return ret;
1029} 1321}
1030 1322
1031 1323
@@ -1299,6 +1591,8 @@ uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1299/** 1591/**
1300 * Convert binary data to a string. 1592 * Convert binary data to a string.
1301 * 1593 *
1594 * @param data binary data to convert
1595 * @param size number of bytes in data
1302 * @return converted data 1596 * @return converted data
1303 */ 1597 */
1304static char * 1598static char *
@@ -1409,47 +1703,4 @@ GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
1409 } 1703 }
1410} 1704}
1411 1705
1412 1706/* end of fs_uri.c */
1413#if 0
1414
1415/**
1416 * Construct a location URI.
1417 *
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
1427 */
1428struct GNUNET_ECRS_URI *
1429GNUNET_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,
1433 void *signer_cls)
1434{
1435 struct GNUNET_ECRS_URI *uri;
1436
1437 if (baseUri->type != chk)
1438 return NULL;
1439
1440 uri = GNUNET_malloc (sizeof (struct GNUNET_ECRS_URI));
1441 uri->type = loc;
1442 uri->data.loc.fi = baseUri->data.fi;
1443 uri->data.loc.peer = *sender;
1444 uri->data.loc.expirationTime = expirationTime;
1445 signer (signer_cls,
1446 sizeof (GNUNET_EC_FileIdentifier) +
1447 sizeof (GNUNET_PeerIdentity) +
1448 sizeof (GNUNET_Int32Time),
1449 &uri->data.loc.fi, &uri->data.loc.contentSignature);
1450 return uri;
1451}
1452
1453#endif
1454
1455/* end of uri.c */