aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_peerstore_service.h50
-rw-r--r--src/peerstore/Makefile.am3
-rw-r--r--src/peerstore/peerstore_api.c200
3 files changed, 241 insertions, 12 deletions
diff --git a/src/include/gnunet_peerstore_service.h b/src/include/gnunet_peerstore_service.h
index 37d3ca7fe..6196d8202 100644
--- a/src/include/gnunet_peerstore_service.h
+++ b/src/include/gnunet_peerstore_service.h
@@ -122,6 +122,11 @@ struct GNUNET_PEERSTORE_Handle;
122struct GNUNET_PEERSTORE_StoreContext; 122struct GNUNET_PEERSTORE_StoreContext;
123 123
124/** 124/**
125 * Context for the info handler.
126 */
127struct GNUNET_PEERSTORE_NotifyContext;
128
129/**
125 * Single PEERSTORE record 130 * Single PEERSTORE record
126 */ 131 */
127struct GNUNET_PEERSTORE_Record 132struct GNUNET_PEERSTORE_Record
@@ -185,6 +190,48 @@ typedef void (*GNUNET_PEERSTORE_Processor) (
185 const struct GNUNET_PEERSTORE_Record *record, 190 const struct GNUNET_PEERSTORE_Record *record,
186 const char *emsg); 191 const char *emsg);
187 192
193/**
194 * Function called by PEERSTORE when notifying a client about a changed hello.
195 *
196 * @param cls closure
197 * @param hello_uri Hello uri.
198 */
199typedef void (*GNUNET_PEERSTORE_hello_notify_cb) (
200 void *cls,
201 const struct GNUNET_PeerIdentity *peer,
202 const struct GNUNET_MessageHeader *hello,
203 const char *err_msg);
204
205/**
206 * Call a method whenever our known information about peers
207 * changes. Initially calls the given function for all known
208 * peers and then only signals changes.
209 *
210 * If @a include_friend_only is set to #GNUNET_YES peerinfo will include HELLO
211 * messages which are intended for friend to friend mode and which do not
212 * have to be gossiped. Otherwise these messages are skipped. //FIXME Not implemented atm!
213 *
214 * @param h Handle to the PEERSTORE service
215 * @param include_friend_only include HELLO messages for friends only (not used at the moment)
216 * @param callback the method to call for getting the hello.
217 * @param callback_cls closure for @a callback
218 * @return NULL on error
219 */
220struct GNUNET_PEERSTORE_NotifyContext *
221GNUNET_PEERSTORE_hello_changed_notify (struct GNUNET_PEERSTORE_Handle *h,
222 int include_friend_only,
223 GNUNET_PEERSTORE_hello_notify_cb callback,
224 void *callback_cls);
225
226
227/**
228 * Stop notifying about changes.
229 *
230 * @param nc context to stop notifying
231 */
232void
233GNUNET_PEERSTORE_hello_changed_notify_cancel (struct
234 GNUNET_PEERSTORE_NotifyContext *nc);
188 235
189 236
190/** 237/**
@@ -210,7 +257,8 @@ GNUNET_PEERSTORE_hello_add (struct GNUNET_PEERSTORE_Handle *h,
210 * @param huc The context for storing a hello. 257 * @param huc The context for storing a hello.
211 */ 258 */
212void 259void
213GNUNET_PEERSTORE_hello_add_cancel (struct GNUNET_PEERSTORE_StoreHelloContext *huc); 260GNUNET_PEERSTORE_hello_add_cancel (struct
261 GNUNET_PEERSTORE_StoreHelloContext *huc);
214 262
215 263
216/** 264/**
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
46libgnunetpeerstore_la_LIBADD = \ 46libgnunetpeerstore_la_LIBADD = \
47 $(top_builddir)/src/util/libgnunetutil.la 47 $(top_builddir)/src/util/libgnunetutil.la \
48 $(top_builddir)/src/hello/libgnunethello.la
48libgnunetpeerstore_la_LDFLAGS = \ 49libgnunetpeerstore_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 */
267struct 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 */
267struct GNUNET_PEERSTORE_StoreHelloContext 298struct 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
1048static void
1049hello_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
1076static void
1077watch_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
1100static void
1101set_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
1138struct GNUNET_PEERSTORE_NotifyContext *
1139GNUNET_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 */
1169void
1170GNUNET_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
1014static void 1191static 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
1073static void 1250static void
1074merge_uri (void *cls, 1251merge_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
1164void 1342void
1165GNUNET_PEERSTORE_hello_add_cancel (struct GNUNET_PEERSTORE_StoreHelloContext *huc) 1343GNUNET_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 */