aboutsummaryrefslogtreecommitdiff
path: root/src/social
diff options
context:
space:
mode:
authorGabor X Toth <*@tg-x.net>2016-05-11 20:12:03 +0000
committerGabor X Toth <*@tg-x.net>2016-05-11 20:12:03 +0000
commit641c9c191ec41b1725d9df01af4101c3423772bb (patch)
treece059045b73da3f934eb4b84007a292483dba60a /src/social
parent92c6c99b247329f15e09118604f143f35516001c (diff)
downloadgnunet-641c9c191ec41b1725d9df01af4101c3423772bb.tar.gz
gnunet-641c9c191ec41b1725d9df01af4101c3423772bb.zip
social: place load/save
Diffstat (limited to 'src/social')
-rw-r--r--src/social/gnunet-service-social.c148
1 files changed, 62 insertions, 86 deletions
diff --git a/src/social/gnunet-service-social.c b/src/social/gnunet-service-social.c
index ede1b0104..8dd51e610 100644
--- a/src/social/gnunet-service-social.c
+++ b/src/social/gnunet-service-social.c
@@ -933,16 +933,14 @@ place_recv_save_method (void *cls,
933 plc->file_offset = 0; 933 plc->file_offset = 0;
934 plc->file_save = GNUNET_NO; 934 plc->file_save = GNUNET_NO;
935 935
936 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii; 936 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&plc->pub_key);
937 memcpy (&place_pub_hash_ascii.encoding,
938 GNUNET_h2s_full (&plc->pub_key_hash), sizeof (place_pub_hash_ascii));
939
940 char *filename = NULL; 937 char *filename = NULL;
941 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part", 938 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part",
942 dir_social, DIR_SEPARATOR, 939 dir_social, DIR_SEPARATOR,
943 "files", DIR_SEPARATOR, 940 "files", DIR_SEPARATOR,
944 place_pub_hash_ascii.encoding, DIR_SEPARATOR, 941 place_pub_str, DIR_SEPARATOR,
945 GNUNET_ntohll (msg->message_id)); 942 GNUNET_ntohll (msg->message_id));
943 GNUNET_free (place_pub_str);
946 944
947 /* save if does not already exist */ 945 /* save if does not already exist */
948 if (GNUNET_YES != GNUNET_DISK_file_test (filename)) 946 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
@@ -979,16 +977,14 @@ place_recv_save_data (void *cls,
979 if (GNUNET_YES != plc->file_save) 977 if (GNUNET_YES != plc->file_save)
980 return; 978 return;
981 979
982 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii; 980 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&plc->pub_key);
983 memcpy (&place_pub_hash_ascii.encoding,
984 GNUNET_h2s_full (&plc->pub_key_hash), sizeof (place_pub_hash_ascii));
985
986 char *filename = NULL; 981 char *filename = NULL;
987 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part", 982 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part",
988 dir_social, DIR_SEPARATOR, 983 dir_social, DIR_SEPARATOR,
989 "files", DIR_SEPARATOR, 984 "files", DIR_SEPARATOR,
990 place_pub_hash_ascii.encoding, DIR_SEPARATOR, 985 place_pub_str, DIR_SEPARATOR,
991 GNUNET_ntohll (msg->message_id)); 986 GNUNET_ntohll (msg->message_id));
987 GNUNET_free (place_pub_str);
992 GNUNET_DISK_directory_create_for_file (filename); 988 GNUNET_DISK_directory_create_for_file (filename);
993 struct GNUNET_DISK_FileHandle * 989 struct GNUNET_DISK_FileHandle *
994 fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_WRITE, 990 fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_WRITE,
@@ -1026,16 +1022,14 @@ place_recv_save_eom (void *cls,
1026 if (GNUNET_YES != plc->file_save) 1022 if (GNUNET_YES != plc->file_save)
1027 return; 1023 return;
1028 1024
1029 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii; 1025 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&plc->pub_key);
1030 memcpy (&place_pub_hash_ascii.encoding,
1031 GNUNET_h2s_full (&plc->pub_key_hash), sizeof (place_pub_hash_ascii));
1032
1033 char *fn = NULL; 1026 char *fn = NULL;
1034 GNUNET_asprintf (&fn, "%s%c%s%c%s%c%" PRIu64, 1027 GNUNET_asprintf (&fn, "%s%c%s%c%s%c%" PRIu64,
1035 dir_social, DIR_SEPARATOR, 1028 dir_social, DIR_SEPARATOR,
1036 "files", DIR_SEPARATOR, 1029 "files", DIR_SEPARATOR,
1037 place_pub_hash_ascii.encoding, DIR_SEPARATOR, 1030 place_pub_str, DIR_SEPARATOR,
1038 GNUNET_ntohll (msg->message_id)); 1031 GNUNET_ntohll (msg->message_id));
1032 GNUNET_free (place_pub_str);
1039 char *fn_part = NULL; 1033 char *fn_part = NULL;
1040 GNUNET_asprintf (&fn_part, "%s.part", fn); 1034 GNUNET_asprintf (&fn_part, "%s.part", fn);
1041 1035
@@ -1201,26 +1195,14 @@ app_place_save (const char *app_id,
1201 if (NULL == dir_places) 1195 if (NULL == dir_places)
1202 return GNUNET_SYSERR; 1196 return GNUNET_SYSERR;
1203 1197
1204 struct GNUNET_HashCode ego_pub_hash; 1198 char *ego_pub_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ereq->ego_pub_key);
1205 struct GNUNET_HashCode place_pub_hash; 1199 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&ereq->place_pub_key);
1206 GNUNET_CRYPTO_hash (&ereq->ego_pub_key, sizeof (ereq->ego_pub_key),
1207 &ego_pub_hash);
1208 GNUNET_CRYPTO_hash (&ereq->place_pub_key, sizeof (ereq->place_pub_key),
1209 &place_pub_hash);
1210
1211 struct GNUNET_CRYPTO_HashAsciiEncoded ego_pub_hash_ascii;
1212 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii;
1213 memcpy (&ego_pub_hash_ascii.encoding,
1214 GNUNET_h2s_full (&ego_pub_hash), sizeof (ego_pub_hash_ascii));
1215 memcpy (&place_pub_hash_ascii.encoding,
1216 GNUNET_h2s_full (&place_pub_hash), sizeof (place_pub_hash_ascii));
1217
1218 char *filename = NULL; 1200 char *filename = NULL;
1219 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s", 1201 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
1220 dir_social, DIR_SEPARATOR, 1202 dir_social, DIR_SEPARATOR,
1221 "places", DIR_SEPARATOR, 1203 "places", DIR_SEPARATOR,
1222 ego_pub_hash_ascii.encoding, DIR_SEPARATOR, 1204 ego_pub_str, DIR_SEPARATOR,
1223 place_pub_hash_ascii.encoding); 1205 place_pub_str);
1224 int ret = GNUNET_DISK_directory_create_for_file (filename); 1206 int ret = GNUNET_DISK_directory_create_for_file (filename);
1225 if (GNUNET_OK != ret 1207 if (GNUNET_OK != ret
1226 || 0 > GNUNET_DISK_fn_write (filename, ereq, ntohs (ereq->header.size), 1208 || 0 > GNUNET_DISK_fn_write (filename, ereq, ntohs (ereq->header.size),
@@ -1238,8 +1220,8 @@ app_place_save (const char *app_id,
1238 dir_social, DIR_SEPARATOR, 1220 dir_social, DIR_SEPARATOR,
1239 "apps", DIR_SEPARATOR, 1221 "apps", DIR_SEPARATOR,
1240 app_id, DIR_SEPARATOR, 1222 app_id, DIR_SEPARATOR,
1241 ego_pub_hash_ascii.encoding, DIR_SEPARATOR, 1223 ego_pub_str, DIR_SEPARATOR,
1242 place_pub_hash_ascii.encoding); 1224 place_pub_str);
1243 ret = GNUNET_DISK_directory_create_for_file (filename); 1225 ret = GNUNET_DISK_directory_create_for_file (filename);
1244 if (GNUNET_OK != ret 1226 if (GNUNET_OK != ret
1245 || 0 > GNUNET_DISK_fn_write (filename, "", 0, 1227 || 0 > GNUNET_DISK_fn_write (filename, "", 0,
@@ -1251,6 +1233,8 @@ app_place_save (const char *app_id,
1251 } 1233 }
1252 GNUNET_free (filename); 1234 GNUNET_free (filename);
1253 } 1235 }
1236 GNUNET_free (ego_pub_str);
1237 GNUNET_free (place_pub_str);
1254 return ret; 1238 return ret;
1255} 1239}
1256 1240
@@ -1265,21 +1249,18 @@ app_place_remove (const char *app_id,
1265 GNUNET_CRYPTO_hash (ego_pub_key, sizeof (*ego_pub_key), &ego_pub_hash); 1249 GNUNET_CRYPTO_hash (ego_pub_key, sizeof (*ego_pub_key), &ego_pub_hash);
1266 GNUNET_CRYPTO_hash (place_pub_key, sizeof (*place_pub_key), &place_pub_hash); 1250 GNUNET_CRYPTO_hash (place_pub_key, sizeof (*place_pub_key), &place_pub_hash);
1267 1251
1268 struct GNUNET_CRYPTO_HashAsciiEncoded ego_pub_hash_ascii; 1252 char *ego_pub_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (ego_pub_key);
1269 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii; 1253 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (place_pub_key);
1270 memcpy (&ego_pub_hash_ascii.encoding,
1271 GNUNET_h2s_full (&ego_pub_hash), sizeof (ego_pub_hash_ascii));
1272 memcpy (&place_pub_hash_ascii.encoding,
1273 GNUNET_h2s_full (&place_pub_hash), sizeof (place_pub_hash_ascii));
1274
1275 char *app_place_filename = NULL; 1254 char *app_place_filename = NULL;
1276 GNUNET_asprintf (&app_place_filename, 1255 GNUNET_asprintf (&app_place_filename,
1277 "%s%c" "%s%c" "%s%c" "%s%c" "%s", 1256 "%s%c" "%s%c" "%s%c" "%s%c" "%s",
1278 dir_social, DIR_SEPARATOR, 1257 dir_social, DIR_SEPARATOR,
1279 "apps", DIR_SEPARATOR, 1258 "apps", DIR_SEPARATOR,
1280 app_id, DIR_SEPARATOR, 1259 app_id, DIR_SEPARATOR,
1281 ego_pub_hash_ascii.encoding, DIR_SEPARATOR, 1260 ego_pub_str, DIR_SEPARATOR,
1282 place_pub_hash_ascii.encoding); 1261 place_pub_str);
1262 GNUNET_free (ego_pub_str);
1263 GNUNET_free (place_pub_str);
1283 1264
1284 struct GNUNET_HashCode app_id_hash; 1265 struct GNUNET_HashCode app_id_hash;
1285 GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash); 1266 GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);
@@ -3291,110 +3272,105 @@ path_basename (const char *path)
3291struct PlaceLoadClosure 3272struct PlaceLoadClosure
3292{ 3273{
3293 const char *app_id; 3274 const char *app_id;
3294 const char *ego_pub_hash_str; 3275 const char *ego_pub_str;
3295}; 3276};
3296 3277
3297 3278
3298/** Load a place file */ 3279/** Load a place file */
3299int 3280int
3300file_place_load (void *cls, const char *filename) 3281file_place_load (void *cls, const char *place_filename)
3301{ 3282{
3302 char *app_id = cls; 3283 struct PlaceLoadClosure *plcls = cls;
3284
3285 const char *place_pub_str = path_basename (place_filename);
3286 if (NULL == place_pub_str)
3287 {
3288 GNUNET_break (0);
3289 return GNUNET_OK;
3290 }
3291
3292 char *filename = NULL;
3293 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
3294 dir_social, DIR_SEPARATOR,
3295 "places", DIR_SEPARATOR,
3296 plcls->ego_pub_str, DIR_SEPARATOR,
3297 place_pub_str);
3298
3303 uint64_t file_size = 0; 3299 uint64_t file_size = 0;
3304 if (GNUNET_OK != 3300 if (GNUNET_OK !=
3305 GNUNET_DISK_file_size (filename, &file_size, GNUNET_YES, GNUNET_YES) 3301 GNUNET_DISK_file_size (filename, &file_size, GNUNET_YES, GNUNET_YES)
3306 || file_size < sizeof (struct HostEnterRequest)) 3302 || file_size < sizeof (struct PlaceEnterRequest))
3307 return GNUNET_OK; 3303 return GNUNET_OK;
3308 3304
3309 struct PlaceEnterRequest *ereq = GNUNET_malloc (file_size); 3305 struct PlaceEnterRequest *ereq = GNUNET_malloc (file_size);
3310 ssize_t read_size = GNUNET_DISK_fn_read (filename, ereq, file_size); 3306 ssize_t read_size = GNUNET_DISK_fn_read (filename, ereq, file_size);
3311 if (read_size < 0 || read_size < sizeof (*ereq)) 3307 if (read_size < 0 || read_size < sizeof (*ereq))
3308 {
3309 GNUNET_free (ereq);
3312 return GNUNET_OK; 3310 return GNUNET_OK;
3311 }
3313 3312
3314 uint16_t ereq_size = ntohs (ereq->header.size); 3313 uint16_t ereq_size = ntohs (ereq->header.size);
3315 if (read_size != ereq_size) 3314 if (read_size != ereq_size)
3315 {
3316 GNUNET_free (ereq);
3316 return GNUNET_OK; 3317 return GNUNET_OK;
3318 }
3317 3319
3318 switch (ntohs (ereq->header.type)) 3320 switch (ntohs (ereq->header.type))
3319 { 3321 {
3320 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER: 3322 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
3321 if (ereq_size < sizeof (struct HostEnterRequest)) 3323 if (ereq_size < sizeof (struct HostEnterRequest))
3324 {
3325 GNUNET_free (ereq);
3322 return GNUNET_OK; 3326 return GNUNET_OK;
3327 }
3323 struct HostEnterRequest *hreq = (struct HostEnterRequest *) ereq; 3328 struct HostEnterRequest *hreq = (struct HostEnterRequest *) ereq;
3324 host_enter (hreq, NULL); 3329 host_enter (hreq, NULL);
3325 break; 3330 break;
3326 3331
3327 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER: 3332 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
3328 if (ereq_size < sizeof (struct GuestEnterRequest)) 3333 if (ereq_size < sizeof (struct GuestEnterRequest))
3334 {
3335 GNUNET_free (ereq);
3329 return GNUNET_OK; 3336 return GNUNET_OK;
3337 }
3330 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) ereq; 3338 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) ereq;
3331 guest_enter (greq, NULL); 3339 guest_enter (greq, NULL);
3332 break; 3340 break;
3333 3341
3334 default: 3342 default:
3343 GNUNET_free (ereq);
3335 return GNUNET_OK; 3344 return GNUNET_OK;
3336 } 3345 }
3337 3346
3338 app_place_add (app_id, ereq);
3339 return GNUNET_OK;
3340}
3341
3342
3343/** Load an ego place file */
3344int
3345file_ego_place_load (void *cls, const char *place_filename)
3346{
3347 struct PlaceLoadClosure *plcls = cls;
3348
3349 const char *place_pub_hash_str = path_basename (place_filename);
3350 if (NULL == place_pub_hash_str)
3351 {
3352 GNUNET_break (0);
3353 return GNUNET_OK;
3354 }
3355
3356 char *filename = NULL;
3357 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
3358 dir_social, DIR_SEPARATOR,
3359 "places", DIR_SEPARATOR,
3360 plcls->ego_pub_hash_str, DIR_SEPARATOR,
3361 place_pub_hash_str);
3362
3363 struct PlaceEnterRequest ereq[GNUNET_SERVER_MAX_MESSAGE_SIZE];
3364
3365 int read_size = GNUNET_DISK_fn_read (filename, &ereq,
3366 GNUNET_SERVER_MAX_MESSAGE_SIZE);
3367 GNUNET_free (filename);
3368
3369 if (read_size < (ssize_t) sizeof (ereq))
3370 return GNUNET_OK;
3371
3372 app_place_add (plcls->app_id, ereq); 3347 app_place_add (plcls->app_id, ereq);
3348 GNUNET_free (ereq);
3373 return GNUNET_OK; 3349 return GNUNET_OK;
3374} 3350}
3375 3351
3376 3352
3377/** 3353/**
3378 * Read @e place_pub_hash_str entries in @a dir_ego 3354 * Read @e place_pub_str entries in @a dir_ego
3379 * 3355 *
3380 * @param dir_ego 3356 * @param dir_ego
3381 * Data directory of an application ego. 3357 * Data directory of an application ego.
3382 * $GNUNET_DATA_HOME/social/apps/$app_id/$ego_pub_hash_str/ 3358 * $GNUNET_DATA_HOME/social/apps/$app_id/$ego_pub_str/
3383 */ 3359 */
3384int 3360int
3385scan_app_ego_dir (void *cls, const char *dir_ego) 3361scan_app_ego_dir (void *cls, const char *dir_ego)
3386{ 3362{
3387 struct PlaceLoadClosure *plcls = cls; 3363 struct PlaceLoadClosure *plcls = cls;
3388 plcls->ego_pub_hash_str = path_basename (dir_ego); 3364 plcls->ego_pub_str = path_basename (dir_ego);
3389 3365
3390 if (NULL != plcls->ego_pub_hash_str) 3366 if (NULL != plcls->ego_pub_str)
3391 GNUNET_DISK_directory_scan (dir_ego, file_ego_place_load, plcls); 3367 GNUNET_DISK_directory_scan (dir_ego, file_place_load, plcls);
3392 3368
3393 return GNUNET_OK; 3369 return GNUNET_OK;
3394} 3370}
3395 3371
3396/** 3372/**
3397 * Read @e ego_pub_hash_str entries in @a dir_app 3373 * Read @e ego_pub_str entries in @a dir_app
3398 * 3374 *
3399 * @param dir_app 3375 * @param dir_app
3400 * Data directory of an application. 3376 * Data directory of an application.