diff options
author | t3sserakt <t3ss@posteo.de> | 2023-08-03 10:57:39 +0200 |
---|---|---|
committer | t3sserakt <t3ss@posteo.de> | 2023-08-03 10:57:39 +0200 |
commit | 55a25efeae2ddfc12e507bc97c57d21f40bf4da4 (patch) | |
tree | 7e5d15322d241c0ab02eb31cb6f74814e8afc047 /src/peerstore | |
parent | 0f8b71280614e9a69d7703d88cc35108e6cec052 (diff) | |
download | gnunet-55a25efeae2ddfc12e507bc97c57d21f40bf4da4.tar.gz gnunet-55a25efeae2ddfc12e507bc97c57d21f40bf4da4.zip |
NEWS: Added api to get notified when hellos are stored with peerstore service.
Diffstat (limited to 'src/peerstore')
-rw-r--r-- | src/peerstore/Makefile.am | 3 | ||||
-rw-r--r-- | src/peerstore/peerstore_api.c | 200 |
2 files changed, 192 insertions, 11 deletions
diff --git a/src/peerstore/Makefile.am b/src/peerstore/Makefile.am index 41fa18b98..b73bd2477 100644 --- a/src/peerstore/Makefile.am +++ b/src/peerstore/Makefile.am | |||
@@ -44,7 +44,8 @@ libgnunetpeerstore_la_SOURCES = \ | |||
44 | peerstore_api.c \ | 44 | peerstore_api.c \ |
45 | peerstore_common.c | 45 | peerstore_common.c |
46 | libgnunetpeerstore_la_LIBADD = \ | 46 | libgnunetpeerstore_la_LIBADD = \ |
47 | $(top_builddir)/src/util/libgnunetutil.la | 47 | $(top_builddir)/src/util/libgnunetutil.la \ |
48 | $(top_builddir)/src/hello/libgnunethello.la | ||
48 | libgnunetpeerstore_la_LDFLAGS = \ | 49 | libgnunetpeerstore_la_LDFLAGS = \ |
49 | $(GN_LIBINTL) \ | 50 | $(GN_LIBINTL) \ |
50 | $(GN_LIB_LDFLAGS) | 51 | $(GN_LIB_LDFLAGS) |
diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c index 3905b14e4..72742c919 100644 --- a/src/peerstore/peerstore_api.c +++ b/src/peerstore/peerstore_api.c | |||
@@ -262,6 +262,37 @@ struct GNUNET_PEERSTORE_WatchContext | |||
262 | }; | 262 | }; |
263 | 263 | ||
264 | /** | 264 | /** |
265 | * Context for the info handler. | ||
266 | */ | ||
267 | struct GNUNET_PEERSTORE_NotifyContext | ||
268 | { | ||
269 | /** | ||
270 | * Peerstore handle. | ||
271 | */ | ||
272 | struct GNUNET_PEERSTORE_Handle *h; | ||
273 | |||
274 | /** | ||
275 | * Function to call with information. | ||
276 | */ | ||
277 | GNUNET_PEERSTORE_hello_notify_cb callback; | ||
278 | |||
279 | /** | ||
280 | * Closure for @e callback. | ||
281 | */ | ||
282 | void *callback_cls; | ||
283 | |||
284 | /** | ||
285 | * Iteration context to iterate through all the stored hellos. | ||
286 | */ | ||
287 | struct GNUNET_PEERSTORE_IterateContext *ic; | ||
288 | |||
289 | /** | ||
290 | * Is this request canceled. | ||
291 | */ | ||
292 | unsigned int canceled; | ||
293 | }; | ||
294 | |||
295 | /** | ||
265 | * Context for a add hello uri request. | 296 | * Context for a add hello uri request. |
266 | */ | 297 | */ |
267 | struct GNUNET_PEERSTORE_StoreHelloContext | 298 | struct GNUNET_PEERSTORE_StoreHelloContext |
@@ -1009,6 +1040,152 @@ GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h, | |||
1009 | } | 1040 | } |
1010 | 1041 | ||
1011 | 1042 | ||
1043 | /******************************************************************************/ | ||
1044 | /******************* HELLO FUNCTIONS *********************/ | ||
1045 | /******************************************************************************/ | ||
1046 | |||
1047 | |||
1048 | static void | ||
1049 | hello_updated (void *cls, | ||
1050 | const struct GNUNET_PEERSTORE_Record *record, | ||
1051 | const char *emsg) | ||
1052 | { | ||
1053 | struct GNUNET_PEERSTORE_NotifyContext *nc = cls; | ||
1054 | struct GNUNET_PEERSTORE_Handle *h = nc->h; | ||
1055 | const struct GNUNET_MessageHeader *hello; | ||
1056 | const char *val; | ||
1057 | |||
1058 | if (NULL != emsg) | ||
1059 | { | ||
1060 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1061 | "Got failure from PEERSTORE: %s\n", | ||
1062 | emsg); | ||
1063 | nc->callback (nc->callback_cls, NULL, NULL, emsg); | ||
1064 | return; | ||
1065 | } | ||
1066 | hello = record->value; | ||
1067 | if ((0 == record->value_size) || ('\0' != val[record->value_size - 1])) | ||
1068 | { | ||
1069 | GNUNET_break (0); | ||
1070 | return; | ||
1071 | } | ||
1072 | nc->callback (nc->callback_cls, &record->peer, hello, NULL); | ||
1073 | } | ||
1074 | |||
1075 | |||
1076 | static void | ||
1077 | watch_cancel (void *cls, | ||
1078 | const struct GNUNET_PEERSTORE_Record *record, | ||
1079 | const char *emsg) | ||
1080 | { | ||
1081 | struct GNUNET_PEERSTORE_NotifyContext *nc = cls; | ||
1082 | struct GNUNET_PEERSTORE_Handle *h = nc->h; | ||
1083 | struct GNUNET_HashCode keyhash; | ||
1084 | struct GNUNET_PEERSTORE_WatchContext *wc; | ||
1085 | |||
1086 | if (NULL != record) | ||
1087 | { | ||
1088 | PEERSTORE_hash_key ("peerstore", &record->peer, record->key, &keyhash); | ||
1089 | wc = GNUNET_CONTAINER_multihashmap_get (h->watches, &keyhash); | ||
1090 | if (NULL != wc) | ||
1091 | GNUNET_PEERSTORE_watch_cancel (wc); | ||
1092 | } | ||
1093 | else | ||
1094 | { | ||
1095 | GNUNET_free (nc); | ||
1096 | } | ||
1097 | } | ||
1098 | |||
1099 | |||
1100 | static void | ||
1101 | set_watch (void *cls, | ||
1102 | const struct GNUNET_PEERSTORE_Record *record, | ||
1103 | const char *emsg) | ||
1104 | { | ||
1105 | struct GNUNET_PEERSTORE_NotifyContext *nc = cls; | ||
1106 | struct GNUNET_PEERSTORE_Handle *h = nc->h; | ||
1107 | struct GNUNET_PEERSTORE_WatchContext *wc; | ||
1108 | const struct GNUNET_MessageHeader *hello; | ||
1109 | const char *val; | ||
1110 | |||
1111 | if (NULL != emsg) | ||
1112 | { | ||
1113 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1114 | "Got failure from PEERSTORE: %s\n", | ||
1115 | emsg); | ||
1116 | nc->callback (nc->callback_cls, NULL, NULL, emsg); | ||
1117 | return; | ||
1118 | } | ||
1119 | if (NULL == record) | ||
1120 | return; | ||
1121 | |||
1122 | wc = GNUNET_PEERSTORE_watch (h, | ||
1123 | "peerstore", | ||
1124 | &record->peer, | ||
1125 | GNUNET_PEERSTORE_HELLO_KEY, | ||
1126 | &hello_updated, | ||
1127 | nc); | ||
1128 | hello = record->value; | ||
1129 | if ((0 == record->value_size) || ('\0' != val[record->value_size - 1])) | ||
1130 | { | ||
1131 | GNUNET_break (0); | ||
1132 | return; | ||
1133 | } | ||
1134 | nc->callback (nc->callback_cls, &record->peer, hello, NULL); | ||
1135 | } | ||
1136 | |||
1137 | |||
1138 | struct GNUNET_PEERSTORE_NotifyContext * | ||
1139 | GNUNET_PEERSTORE_hello_changed_notify (struct GNUNET_PEERSTORE_Handle *h, | ||
1140 | int include_friend_only, | ||
1141 | GNUNET_PEERSTORE_hello_notify_cb callback, | ||
1142 | void *callback_cls) | ||
1143 | { | ||
1144 | struct GNUNET_PEERSTORE_NotifyContext *nc; | ||
1145 | struct GNUNET_PEERSTORE_IterateContext *ic; | ||
1146 | |||
1147 | nc = GNUNET_new (struct GNUNET_PEERSTORE_NotifyContext); | ||
1148 | nc->callback = callback; | ||
1149 | nc->callback_cls = callback_cls; | ||
1150 | nc->h = h; | ||
1151 | |||
1152 | ic = GNUNET_PEERSTORE_iterate (h, | ||
1153 | "peerstore", | ||
1154 | NULL, | ||
1155 | GNUNET_PEERSTORE_HELLO_KEY, | ||
1156 | &set_watch, | ||
1157 | nc); | ||
1158 | nc->ic = ic; | ||
1159 | |||
1160 | return nc; | ||
1161 | } | ||
1162 | |||
1163 | |||
1164 | /** | ||
1165 | * Stop notifying about changes. | ||
1166 | * | ||
1167 | * @param nc context to stop notifying | ||
1168 | */ | ||
1169 | void | ||
1170 | GNUNET_PEERSTORE_hello_changed_notify_cancel (struct | ||
1171 | GNUNET_PEERSTORE_NotifyContext *nc) | ||
1172 | { | ||
1173 | struct GNUNET_PEERSTORE_IterateContext *ic; | ||
1174 | struct GNUNET_PEERSTORE_Handle *h = nc->h; | ||
1175 | |||
1176 | if (GNUNET_NO == nc->canceled && NULL != nc->ic) | ||
1177 | { | ||
1178 | nc->canceled = GNUNET_YES; | ||
1179 | GNUNET_PEERSTORE_iterate_cancel (nc->ic); | ||
1180 | ic = GNUNET_PEERSTORE_iterate (h, | ||
1181 | "peerstore", | ||
1182 | NULL, | ||
1183 | GNUNET_PEERSTORE_HELLO_KEY, | ||
1184 | &watch_cancel, | ||
1185 | nc); | ||
1186 | nc->ic = ic; | ||
1187 | } | ||
1188 | } | ||
1012 | 1189 | ||
1013 | 1190 | ||
1014 | static void | 1191 | static void |
@@ -1037,7 +1214,7 @@ merge_success (void *cls, int success) | |||
1037 | return; | 1214 | return; |
1038 | } | 1215 | } |
1039 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1216 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1040 | "Got notified during storing hello uri!\n"); | 1217 | "Got notified during storing hello uri!\n"); |
1041 | } | 1218 | } |
1042 | 1219 | ||
1043 | 1220 | ||
@@ -1072,8 +1249,8 @@ store_hello (struct GNUNET_PEERSTORE_StoreHelloContext *huc, | |||
1072 | 1249 | ||
1073 | static void | 1250 | static void |
1074 | merge_uri (void *cls, | 1251 | merge_uri (void *cls, |
1075 | const struct GNUNET_PEERSTORE_Record *record, | 1252 | const struct GNUNET_PEERSTORE_Record *record, |
1076 | const char *emsg) | 1253 | const char *emsg) |
1077 | { | 1254 | { |
1078 | struct GNUNET_PEERSTORE_StoreHelloContext *huc = cls; | 1255 | struct GNUNET_PEERSTORE_StoreHelloContext *huc = cls; |
1079 | struct GNUNET_PEERSTORE_Handle *h = huc->h; | 1256 | struct GNUNET_PEERSTORE_Handle *h = huc->h; |
@@ -1150,27 +1327,30 @@ GNUNET_PEERSTORE_hello_add (struct GNUNET_PEERSTORE_Handle *h, | |||
1150 | builder = GNUNET_HELLO_builder_from_msg (msg); | 1327 | builder = GNUNET_HELLO_builder_from_msg (msg); |
1151 | pid = GNUNET_HELLO_builder_get_id (builder); | 1328 | pid = GNUNET_HELLO_builder_get_id (builder); |
1152 | ic = GNUNET_PEERSTORE_iterate (h, | 1329 | ic = GNUNET_PEERSTORE_iterate (h, |
1153 | "peerstore", | 1330 | "peerstore", |
1154 | pid, | 1331 | pid, |
1155 | GNUNET_PEERSTORE_HELLO_KEY, | 1332 | GNUNET_PEERSTORE_HELLO_KEY, |
1156 | &merge_uri, | 1333 | &merge_uri, |
1157 | huc); | 1334 | huc); |
1158 | GNUNET_HELLO_builder_free (builder); | 1335 | GNUNET_HELLO_builder_free (builder); |
1159 | huc->ic = ic; | 1336 | huc->ic = ic; |
1160 | 1337 | ||
1161 | return huc; | 1338 | return huc; |
1162 | } | 1339 | } |
1163 | 1340 | ||
1341 | |||
1164 | void | 1342 | void |
1165 | GNUNET_PEERSTORE_hello_add_cancel (struct GNUNET_PEERSTORE_StoreHelloContext *huc) | 1343 | GNUNET_PEERSTORE_hello_add_cancel (struct |
1344 | GNUNET_PEERSTORE_StoreHelloContext *huc) | ||
1166 | { | 1345 | { |
1167 | struct GNUNET_PEERSTORE_StoreContext *sc; | 1346 | struct GNUNET_PEERSTORE_StoreContext *sc; |
1168 | 1347 | ||
1169 | GNUNET_PEERSTORE_iterate_cancel (huc->ic); | 1348 | GNUNET_PEERSTORE_iterate_cancel (huc->ic); |
1170 | GNUNET_PEERSTORE_watch_cancel (huc->wc); | 1349 | GNUNET_PEERSTORE_watch_cancel (huc->wc); |
1171 | while (NULL != (sc = huc->sc_head)) | 1350 | while (NULL != (sc = huc->sc_head)) |
1172 | GNUNET_PEERSTORE_store_cancel (sc); | 1351 | GNUNET_PEERSTORE_store_cancel (sc); |
1173 | GNUNET_free (huc); | 1352 | GNUNET_free (huc); |
1174 | } | 1353 | } |
1175 | 1354 | ||
1355 | |||
1176 | /* end of peerstore_api.c */ | 1356 | /* end of peerstore_api.c */ |