diff options
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/include/gnunet_protocols.h | 12 | ||||
-rw-r--r-- | src/include/gnunet_psycstore_service.h | 3 | ||||
-rw-r--r-- | src/psycstore/Makefile.am | 72 | ||||
-rw-r--r-- | src/psycstore/gnunet-service-psycstore.c | 169 | ||||
-rw-r--r-- | src/psycstore/psycstore.conf | 10 | ||||
-rw-r--r-- | src/psycstore/psycstore.h | 57 | ||||
-rw-r--r-- | src/psycstore/psycstore_api.c | 451 | ||||
-rw-r--r-- | src/psycstore/test_psycstore.c | 158 | ||||
-rw-r--r-- | src/psycstore/test_psycstore.conf | 6 |
11 files changed, 938 insertions, 2 deletions
diff --git a/configure.ac b/configure.ac index d77a3b139..c676164be 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -1281,6 +1281,7 @@ src/gns/nss/Makefile | |||
1281 | src/hello/Makefile | 1281 | src/hello/Makefile |
1282 | src/identity/Makefile | 1282 | src/identity/Makefile |
1283 | src/identity/identity.conf | 1283 | src/identity/identity.conf |
1284 | src/psycstore/Makefile | ||
1284 | src/include/Makefile | 1285 | src/include/Makefile |
1285 | src/include/gnunet_directories.h | 1286 | src/include/gnunet_directories.h |
1286 | src/integration-tests/Makefile | 1287 | src/integration-tests/Makefile |
diff --git a/src/Makefile.am b/src/Makefile.am index 20a34afa3..4eb5aeb19 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
@@ -60,6 +60,7 @@ SUBDIRS = \ | |||
60 | namestore \ | 60 | namestore \ |
61 | vpn \ | 61 | vpn \ |
62 | gns \ | 62 | gns \ |
63 | psycstore \ | ||
63 | fs \ | 64 | fs \ |
64 | exit \ | 65 | exit \ |
65 | pt \ | 66 | pt \ |
diff --git a/src/include/gnunet_protocols.h b/src/include/gnunet_protocols.h index 3332bebd3..6c2530d96 100644 --- a/src/include/gnunet_protocols.h +++ b/src/include/gnunet_protocols.h | |||
@@ -1935,12 +1935,22 @@ extern "C" | |||
1935 | #define GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT 644 | 1935 | #define GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT 644 |
1936 | 1936 | ||
1937 | 1937 | ||
1938 | /******************************************************************************* | ||
1939 | * PSYCSTORE message types | ||
1940 | ******************************************************************************/ | ||
1941 | |||
1942 | /** | ||
1943 | * Generic response from PSYCstore service with success and/or error message. | ||
1944 | */ | ||
1945 | #define GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE 650 | ||
1946 | |||
1938 | 1947 | ||
1939 | /** | 1948 | /** |
1940 | * Next available: 650 | 1949 | * Next available: 670 |
1941 | */ | 1950 | */ |
1942 | 1951 | ||
1943 | 1952 | ||
1953 | |||
1944 | /* WIP: no numbers assigned yet */ | 1954 | /* WIP: no numbers assigned yet */ |
1945 | 1955 | ||
1946 | /******************************************************************************* | 1956 | /******************************************************************************* |
diff --git a/src/include/gnunet_psycstore_service.h b/src/include/gnunet_psycstore_service.h index 660ad0a2a..e3c7876a0 100644 --- a/src/include/gnunet_psycstore_service.h +++ b/src/include/gnunet_psycstore_service.h | |||
@@ -123,7 +123,8 @@ GNUNET_PSYCSTORE_counters_get (struct GNUNET_PSYCSTORE_Handle *h, | |||
123 | */ | 123 | */ |
124 | typedef void | 124 | typedef void |
125 | (*GNUNET_PSYCSTORE_ResultCallback) (void *cls, | 125 | (*GNUNET_PSYCSTORE_ResultCallback) (void *cls, |
126 | int result); | 126 | int result, |
127 | const char *err_msg); | ||
127 | 128 | ||
128 | 129 | ||
129 | /** | 130 | /** |
diff --git a/src/psycstore/Makefile.am b/src/psycstore/Makefile.am new file mode 100644 index 000000000..461ec6c4e --- /dev/null +++ b/src/psycstore/Makefile.am | |||
@@ -0,0 +1,72 @@ | |||
1 | INCLUDES = -I$(top_srcdir)/src/include | ||
2 | |||
3 | if MINGW | ||
4 | WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols | ||
5 | endif | ||
6 | |||
7 | if USE_COVERAGE | ||
8 | AM_CFLAGS = --coverage -O0 | ||
9 | XLIB = -lgcov | ||
10 | endif | ||
11 | |||
12 | pkgcfgdir= $(pkgdatadir)/config.d/ | ||
13 | |||
14 | libexecdir= $(pkglibdir)/libexec/ | ||
15 | |||
16 | pkgcfg_DATA = \ | ||
17 | psycstore.conf | ||
18 | |||
19 | |||
20 | lib_LTLIBRARIES = libgnunetpsycstore.la | ||
21 | |||
22 | libgnunetpsycstore_la_SOURCES = \ | ||
23 | psycstore_api.c \ | ||
24 | psycstore.h | ||
25 | libgnunetpsycstore_la_LIBADD = \ | ||
26 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
27 | $(GN_LIBINTL) $(XLIB) | ||
28 | libgnunetpsycstore_la_LDFLAGS = \ | ||
29 | $(GN_LIB_LDFLAGS) $(WINFLAGS) \ | ||
30 | -version-info 0:0:0 | ||
31 | libgnunetpsycstore_la_DEPENDENCIES = \ | ||
32 | $(top_builddir)/src/util/libgnunetutil.la | ||
33 | |||
34 | bin_PROGRAMS = | ||
35 | |||
36 | libexec_PROGRAMS = \ | ||
37 | gnunet-service-psycstore | ||
38 | |||
39 | gnunet_service_psycstore_SOURCES = \ | ||
40 | gnunet-service-psycstore.c | ||
41 | gnunet_service_psycstore_LDADD = \ | ||
42 | $(top_builddir)/src/statistics/libgnunetstatistics.la \ | ||
43 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
44 | $(GN_LIBINTL) | ||
45 | gnunet_service_psycstore_DEPENDENCIES = \ | ||
46 | $(top_builddir)/src/statistics/libgnunetstatistics.la \ | ||
47 | $(top_builddir)/src/util/libgnunetutil.la | ||
48 | |||
49 | if HAVE_TESTING | ||
50 | check_PROGRAMS = \ | ||
51 | test_psycstore | ||
52 | endif | ||
53 | |||
54 | if ENABLE_TEST_RUN | ||
55 | TESTS = $(check_PROGRAMS) | ||
56 | endif | ||
57 | |||
58 | test_psycstore_SOURCES = \ | ||
59 | test_psycstore.c | ||
60 | test_psycstore_LDADD = \ | ||
61 | libgnunetpsycstore.la \ | ||
62 | $(top_builddir)/src/testing/libgnunettesting.la \ | ||
63 | $(top_builddir)/src/util/libgnunetutil.la | ||
64 | test_psycstore_DEPENDENCIES = \ | ||
65 | libgnunetpsycstore.la \ | ||
66 | $(top_builddir)/src/testing/libgnunettesting.la \ | ||
67 | $(top_builddir)/src/util/libgnunetutil.la | ||
68 | |||
69 | EXTRA_DIST = \ | ||
70 | test_psycstore.conf | ||
71 | |||
72 | |||
diff --git a/src/psycstore/gnunet-service-psycstore.c b/src/psycstore/gnunet-service-psycstore.c new file mode 100644 index 000000000..977f77e06 --- /dev/null +++ b/src/psycstore/gnunet-service-psycstore.c | |||
@@ -0,0 +1,169 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
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 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file psycstore/gnunet-service-psycstore.c | ||
23 | * @brief PSYCstore service | ||
24 | * @author Gabor X Toth | ||
25 | * @author Christian Grothoff | ||
26 | * | ||
27 | * The purpose of this service is to manage private keys that | ||
28 | * represent the various egos/pseudonyms/identities of a GNUnet user. | ||
29 | * | ||
30 | */ | ||
31 | #include "platform.h" | ||
32 | #include "gnunet_util_lib.h" | ||
33 | #include "gnunet_constants.h" | ||
34 | #include "gnunet_protocols.h" | ||
35 | #include "gnunet_statistics_service.h" | ||
36 | #include "gnunet_psycstore_service.h" | ||
37 | #include "psycstore.h" | ||
38 | |||
39 | |||
40 | /** | ||
41 | * Handle to our current configuration. | ||
42 | */ | ||
43 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
44 | |||
45 | /** | ||
46 | * Handle to the statistics service. | ||
47 | */ | ||
48 | static struct GNUNET_STATISTICS_Handle *stats; | ||
49 | |||
50 | /** | ||
51 | * Notification context, simplifies client broadcasts. | ||
52 | */ | ||
53 | static struct GNUNET_SERVER_NotificationContext *nc; | ||
54 | |||
55 | /** | ||
56 | * Database file. | ||
57 | */ | ||
58 | static char *db_file; | ||
59 | |||
60 | |||
61 | /** | ||
62 | * Task run during shutdown. | ||
63 | * | ||
64 | * @param cls unused | ||
65 | * @param tc unused | ||
66 | */ | ||
67 | static void | ||
68 | shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
69 | { | ||
70 | if (NULL != nc) | ||
71 | { | ||
72 | GNUNET_SERVER_notification_context_destroy (nc); | ||
73 | nc = NULL; | ||
74 | } | ||
75 | if (NULL != stats) | ||
76 | { | ||
77 | GNUNET_STATISTICS_destroy (stats, GNUNET_NO); | ||
78 | stats = NULL; | ||
79 | } | ||
80 | GNUNET_free (db_file); | ||
81 | db_file = NULL; | ||
82 | } | ||
83 | |||
84 | |||
85 | /** | ||
86 | * Send a result code back to the client. | ||
87 | * | ||
88 | * @param client client that should receive the result code | ||
89 | * @param result_code code to transmit | ||
90 | * @param emsg error message to include (or NULL for none) | ||
91 | */ | ||
92 | static void | ||
93 | send_result_code (struct GNUNET_SERVER_Client *client, | ||
94 | uint32_t result_code, | ||
95 | const char *emsg) | ||
96 | { | ||
97 | struct GNUNET_PSYCSTORE_ResultCodeMessage *rcm; | ||
98 | size_t elen; | ||
99 | |||
100 | if (NULL == emsg) | ||
101 | elen = 0; | ||
102 | else | ||
103 | elen = strlen (emsg) + 1; | ||
104 | rcm = GNUNET_malloc (sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage) + elen); | ||
105 | rcm->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE); | ||
106 | rcm->header.size = htons (sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage) + elen); | ||
107 | rcm->result_code = htonl (result_code); | ||
108 | memcpy (&rcm[1], emsg, elen); | ||
109 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
110 | "Sending result %d (%s) to client\n", | ||
111 | (int) result_code, | ||
112 | emsg); | ||
113 | GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header, GNUNET_NO); | ||
114 | GNUNET_free (rcm); | ||
115 | } | ||
116 | |||
117 | |||
118 | /** | ||
119 | * Handle PSYCstore clients. | ||
120 | * | ||
121 | * @param cls closure | ||
122 | * @param server the initialized server | ||
123 | * @param c configuration to use | ||
124 | */ | ||
125 | static void | ||
126 | run (void *cls, | ||
127 | struct GNUNET_SERVER_Handle *server, | ||
128 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
129 | { | ||
130 | static const struct GNUNET_SERVER_MessageHandler handlers[] = { | ||
131 | {NULL, NULL, 0, 0} | ||
132 | }; | ||
133 | |||
134 | cfg = c; | ||
135 | if (GNUNET_OK != | ||
136 | GNUNET_CONFIGURATION_get_value_filename (cfg, "psycstore", | ||
137 | "DB_FILE", | ||
138 | &db_file)) | ||
139 | { | ||
140 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "psycstore", "DB_FILE"); | ||
141 | GNUNET_SCHEDULER_shutdown (); | ||
142 | return; | ||
143 | } | ||
144 | stats = GNUNET_STATISTICS_create ("psycstore", cfg); | ||
145 | GNUNET_SERVER_add_handlers (server, handlers); | ||
146 | nc = GNUNET_SERVER_notification_context_create (server, 1); | ||
147 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, | ||
148 | NULL); | ||
149 | } | ||
150 | |||
151 | |||
152 | /** | ||
153 | * The main function for the network size estimation service. | ||
154 | * | ||
155 | * @param argc number of arguments from the command line | ||
156 | * @param argv command line arguments | ||
157 | * @return 0 ok, 1 on error | ||
158 | */ | ||
159 | int | ||
160 | main (int argc, char *const *argv) | ||
161 | { | ||
162 | return (GNUNET_OK == | ||
163 | GNUNET_SERVICE_run (argc, argv, "psycstore", | ||
164 | GNUNET_SERVICE_OPTION_NONE, | ||
165 | &run, NULL)) ? 0 : 1; | ||
166 | } | ||
167 | |||
168 | |||
169 | /* end of gnunet-service-psycstore.c */ | ||
diff --git a/src/psycstore/psycstore.conf b/src/psycstore/psycstore.conf new file mode 100644 index 000000000..7bfe6e126 --- /dev/null +++ b/src/psycstore/psycstore.conf | |||
@@ -0,0 +1,10 @@ | |||
1 | [psycstore] | ||
2 | AUTOSTART = YES | ||
3 | HOME = $SERVICEHOME | ||
4 | BINARY = gnunet-service-psycstore | ||
5 | UNIXPATH = /tmp/gnunet-service-psycstore.unix | ||
6 | UNIX_MATCH_UID = NO | ||
7 | UNIX_MATCH_GID = YES | ||
8 | |||
9 | # Path to the SQLite database | ||
10 | DB_FILE = $SERVICEHOME/psycstore.sqlite | ||
diff --git a/src/psycstore/psycstore.h b/src/psycstore/psycstore.h new file mode 100644 index 000000000..f1a1c787a --- /dev/null +++ b/src/psycstore/psycstore.h | |||
@@ -0,0 +1,57 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public Liceidentity as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
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 Liceidentity for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public Liceidentity | ||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file psycstore/psycstore.h | ||
23 | * @brief Common type definitions for the PSYCstore service and API. | ||
24 | * @author Gabor X Toth | ||
25 | */ | ||
26 | |||
27 | #ifndef PSYCSTORE_H | ||
28 | #define PSYCSTORE_H | ||
29 | |||
30 | #include "gnunet_common.h" | ||
31 | |||
32 | |||
33 | GNUNET_NETWORK_STRUCT_BEGIN | ||
34 | |||
35 | /** | ||
36 | * Answer from service to client about last operation. | ||
37 | */ | ||
38 | struct GNUNET_PSYCSTORE_ResultCodeMessage | ||
39 | { | ||
40 | /** | ||
41 | * Type: GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE | ||
42 | */ | ||
43 | struct GNUNET_MessageHeader header; | ||
44 | |||
45 | /** | ||
46 | * Status code for the last operation, in NBO. | ||
47 | * (currently not used). | ||
48 | */ | ||
49 | uint32_t result_code GNUNET_PACKED; | ||
50 | |||
51 | /* followed by 0-terminated error message (on error) */ | ||
52 | |||
53 | }; | ||
54 | |||
55 | GNUNET_NETWORK_STRUCT_END | ||
56 | |||
57 | #endif | ||
diff --git a/src/psycstore/psycstore_api.c b/src/psycstore/psycstore_api.c new file mode 100644 index 000000000..b31b96c3b --- /dev/null +++ b/src/psycstore/psycstore_api.c | |||
@@ -0,0 +1,451 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public Liceidentity as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
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 Liceidentity for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public Liceidentity | ||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file psycstore/psycstore_api.c | ||
23 | * @brief API to interact with the PSYCstore service | ||
24 | * @author Gabor X Toth | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | |||
28 | #include "platform.h" | ||
29 | #include "gnunet_util_lib.h" | ||
30 | #include "gnunet_constants.h" | ||
31 | #include "gnunet_protocols.h" | ||
32 | #include "gnunet_psycstore_service.h" | ||
33 | #include "psycstore.h" | ||
34 | |||
35 | #define LOG(kind,...) GNUNET_log_from (kind, "psycstore-api",__VA_ARGS__) | ||
36 | |||
37 | |||
38 | /** | ||
39 | * Handle for an operation with the PSYCstore service. | ||
40 | */ | ||
41 | struct GNUNET_PSYCSTORE_OperationHandle | ||
42 | { | ||
43 | |||
44 | /** | ||
45 | * Main PSYCstore handle. | ||
46 | */ | ||
47 | struct GNUNET_PSYCSTORE_Handle *h; | ||
48 | |||
49 | /** | ||
50 | * We keep operations in a DLL. | ||
51 | */ | ||
52 | struct GNUNET_PSYCSTORE_OperationHandle *next; | ||
53 | |||
54 | /** | ||
55 | * We keep operations in a DLL. | ||
56 | */ | ||
57 | struct GNUNET_PSYCSTORE_OperationHandle *prev; | ||
58 | |||
59 | /** | ||
60 | * Message to send to the PSYCstore service. | ||
61 | * Allocated at the end of this struct. | ||
62 | */ | ||
63 | const struct GNUNET_MessageHeader *msg; | ||
64 | |||
65 | /** | ||
66 | * Continuation to invoke with the result of an operation. | ||
67 | */ | ||
68 | GNUNET_PSYCSTORE_ResultCallback res_cb; | ||
69 | |||
70 | /** | ||
71 | * Continuation to invoke with the result of an operation returning a fragment. | ||
72 | */ | ||
73 | GNUNET_PSYCSTORE_FragmentCallback frag_cb; | ||
74 | |||
75 | /** | ||
76 | * Continuation to invoke with the result of an operation returning a state variable. | ||
77 | */ | ||
78 | GNUNET_PSYCSTORE_StateCallback state_cb; | ||
79 | |||
80 | /** | ||
81 | * Closure for the callbacks. | ||
82 | */ | ||
83 | void *cls; | ||
84 | |||
85 | }; | ||
86 | |||
87 | |||
88 | /** | ||
89 | * Handle for the service. | ||
90 | */ | ||
91 | struct GNUNET_PSYCSTORE_Handle | ||
92 | { | ||
93 | /** | ||
94 | * Configuration to use. | ||
95 | */ | ||
96 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
97 | |||
98 | /** | ||
99 | * Socket (if available). | ||
100 | */ | ||
101 | struct GNUNET_CLIENT_Connection *client; | ||
102 | |||
103 | /** | ||
104 | * Head of active operations. | ||
105 | */ | ||
106 | struct GNUNET_PSYCSTORE_OperationHandle *op_head; | ||
107 | |||
108 | /** | ||
109 | * Tail of active operations. | ||
110 | */ | ||
111 | struct GNUNET_PSYCSTORE_OperationHandle *op_tail; | ||
112 | |||
113 | /** | ||
114 | * Currently pending transmission request, or NULL for none. | ||
115 | */ | ||
116 | struct GNUNET_CLIENT_TransmitHandle *th; | ||
117 | |||
118 | /** | ||
119 | * Task doing exponential back-off trying to reconnect. | ||
120 | */ | ||
121 | GNUNET_SCHEDULER_TaskIdentifier reconnect_task; | ||
122 | |||
123 | /** | ||
124 | * Time for next connect retry. | ||
125 | */ | ||
126 | struct GNUNET_TIME_Relative reconnect_delay; | ||
127 | |||
128 | /** | ||
129 | * Are we polling for incoming messages right now? | ||
130 | */ | ||
131 | int in_receive; | ||
132 | |||
133 | }; | ||
134 | |||
135 | |||
136 | /** | ||
137 | * Try again to connect to the PSYCstore service. | ||
138 | * | ||
139 | * @param cls handle to the PSYCstore service. | ||
140 | * @param tc scheduler context | ||
141 | */ | ||
142 | static void | ||
143 | reconnect (void *cls, | ||
144 | const struct GNUNET_SCHEDULER_TaskContext *tc); | ||
145 | |||
146 | |||
147 | /** | ||
148 | * Reschedule a connect attempt to the service. | ||
149 | * | ||
150 | * @param h transport service to reconnect | ||
151 | */ | ||
152 | static void | ||
153 | reschedule_connect (struct GNUNET_PSYCSTORE_Handle *h) | ||
154 | { | ||
155 | GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK); | ||
156 | |||
157 | if (NULL != h->th) | ||
158 | { | ||
159 | GNUNET_CLIENT_notify_transmit_ready_cancel (h->th); | ||
160 | h->th = NULL; | ||
161 | } | ||
162 | if (NULL != h->client) | ||
163 | { | ||
164 | GNUNET_CLIENT_disconnect (h->client); | ||
165 | h->client = NULL; | ||
166 | } | ||
167 | h->in_receive = GNUNET_NO; | ||
168 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
169 | "Scheduling task to reconnect to PSYCstore service in %s.\n", | ||
170 | GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, GNUNET_YES)); | ||
171 | h->reconnect_task = | ||
172 | GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h); | ||
173 | h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay); | ||
174 | } | ||
175 | |||
176 | |||
177 | /** | ||
178 | * Type of a function to call when we receive a message | ||
179 | * from the service. | ||
180 | * | ||
181 | * @param cls closure | ||
182 | * @param msg message received, NULL on timeout or fatal error | ||
183 | */ | ||
184 | static void | ||
185 | message_handler (void *cls, | ||
186 | const struct GNUNET_MessageHeader *msg) | ||
187 | { | ||
188 | struct GNUNET_PSYCSTORE_Handle *h = cls; | ||
189 | struct GNUNET_PSYCSTORE_OperationHandle *op; | ||
190 | const struct GNUNET_PSYCSTORE_ResultCodeMessage *rcm; | ||
191 | const char *str; | ||
192 | uint16_t size; | ||
193 | |||
194 | if (NULL == msg) | ||
195 | { | ||
196 | reschedule_connect (h); | ||
197 | return; | ||
198 | } | ||
199 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
200 | "Received message of type %d from PSYCstore service\n", | ||
201 | ntohs (msg->type)); | ||
202 | size = ntohs (msg->size); | ||
203 | switch (ntohs (msg->type)) | ||
204 | { | ||
205 | case GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE: | ||
206 | if (size < sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage)) | ||
207 | { | ||
208 | GNUNET_break (0); | ||
209 | reschedule_connect (h); | ||
210 | return; | ||
211 | } | ||
212 | rcm = (const struct GNUNET_PSYCSTORE_ResultCodeMessage *) msg; | ||
213 | str = (const char *) &rcm[1]; | ||
214 | if ( (size > sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage)) && | ||
215 | ('\0' != str[size - sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage) - 1]) ) | ||
216 | { | ||
217 | GNUNET_break (0); | ||
218 | reschedule_connect (h); | ||
219 | return; | ||
220 | } | ||
221 | if (size == sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage)) | ||
222 | str = NULL; | ||
223 | |||
224 | op = h->op_head; | ||
225 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
226 | h->op_tail, | ||
227 | op); | ||
228 | GNUNET_CLIENT_receive (h->client, &message_handler, h, | ||
229 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
230 | if (NULL != op->res_cb) | ||
231 | op->res_cb (op->cls, rcm->result_code , str); | ||
232 | GNUNET_free (op); | ||
233 | break; | ||
234 | default: | ||
235 | GNUNET_break (0); | ||
236 | reschedule_connect (h); | ||
237 | return; | ||
238 | } | ||
239 | } | ||
240 | |||
241 | |||
242 | /** | ||
243 | * Schedule transmission of the next message from our queue. | ||
244 | * | ||
245 | * @param h PSYCstore handle | ||
246 | */ | ||
247 | static void | ||
248 | transmit_next (struct GNUNET_PSYCSTORE_Handle *h); | ||
249 | |||
250 | |||
251 | /** | ||
252 | * Transmit next message to service. | ||
253 | * | ||
254 | * @param cls the 'struct GNUNET_PSYCSTORE_Handle'. | ||
255 | * @param size number of bytes available in buf | ||
256 | * @param buf where to copy the message | ||
257 | * @return number of bytes copied to buf | ||
258 | */ | ||
259 | static size_t | ||
260 | send_next_message (void *cls, | ||
261 | size_t size, | ||
262 | void *buf) | ||
263 | { | ||
264 | struct GNUNET_PSYCSTORE_Handle *h = cls; | ||
265 | struct GNUNET_PSYCSTORE_OperationHandle *op = h->op_head; | ||
266 | size_t ret; | ||
267 | |||
268 | h->th = NULL; | ||
269 | if (NULL == op) | ||
270 | return 0; | ||
271 | ret = ntohs (op->msg->size); | ||
272 | if (ret > size) | ||
273 | { | ||
274 | reschedule_connect (h); | ||
275 | return 0; | ||
276 | } | ||
277 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
278 | "Sending message of type %d to PSYCstore service\n", | ||
279 | ntohs (op->msg->type)); | ||
280 | memcpy (buf, op->msg, ret); | ||
281 | if ( (NULL == op->res_cb) && | ||
282 | (NULL == op->frag_cb) && | ||
283 | (NULL == op->state_cb)) | ||
284 | { | ||
285 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
286 | h->op_tail, | ||
287 | op); | ||
288 | GNUNET_free (op); | ||
289 | transmit_next (h); | ||
290 | } | ||
291 | if (GNUNET_NO == h->in_receive) | ||
292 | { | ||
293 | h->in_receive = GNUNET_YES; | ||
294 | GNUNET_CLIENT_receive (h->client, | ||
295 | &message_handler, h, | ||
296 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
297 | } | ||
298 | return ret; | ||
299 | } | ||
300 | |||
301 | |||
302 | /** | ||
303 | * Schedule transmission of the next message from our queue. | ||
304 | * | ||
305 | * @param h PSYCstore handle | ||
306 | */ | ||
307 | static void | ||
308 | transmit_next (struct GNUNET_PSYCSTORE_Handle *h) | ||
309 | { | ||
310 | struct GNUNET_PSYCSTORE_OperationHandle *op = h->op_head; | ||
311 | |||
312 | GNUNET_assert (NULL == h->th); | ||
313 | if (NULL == op) | ||
314 | return; | ||
315 | if (NULL == h->client) | ||
316 | return; | ||
317 | h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, | ||
318 | ntohs (op->msg->size), | ||
319 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
320 | GNUNET_NO, | ||
321 | &send_next_message, | ||
322 | h); | ||
323 | } | ||
324 | |||
325 | |||
326 | /** | ||
327 | * Try again to connect to the PSYCstore service. | ||
328 | * | ||
329 | * @param cls the handle to the PSYCstore service | ||
330 | * @param tc scheduler context | ||
331 | */ | ||
332 | static void | ||
333 | reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
334 | { | ||
335 | struct GNUNET_PSYCSTORE_Handle *h = cls; | ||
336 | |||
337 | h->reconnect_task = GNUNET_SCHEDULER_NO_TASK; | ||
338 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
339 | "Connecting to PSYCstore service.\n"); | ||
340 | GNUNET_assert (NULL == h->client); | ||
341 | h->client = GNUNET_CLIENT_connect ("psycstore", h->cfg); | ||
342 | GNUNET_assert (NULL != h->client); | ||
343 | /* | ||
344 | struct GNUNET_PSYCSTORE_OperationHandle *op; | ||
345 | struct GNUNET_MessageHeader msg; | ||
346 | op = GNUNET_malloc (sizeof (struct GNUNET_PSYCSTORE_OperationHandle) + | ||
347 | sizeof (struct GNUNET_MessageHeader)); | ||
348 | op->h = h; | ||
349 | op->msg = (const struct GNUNET_MessageHeader *) &op[1]; | ||
350 | msg.size = htons (sizeof (msg)); | ||
351 | msg.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_START); | ||
352 | memcpy (&op[1], &msg, sizeof (msg)); | ||
353 | GNUNET_CONTAINER_DLL_insert (h->op_head, | ||
354 | h->op_tail, | ||
355 | op); | ||
356 | transmit_next (h); | ||
357 | GNUNET_assert (NULL != h->th); | ||
358 | */ | ||
359 | } | ||
360 | |||
361 | |||
362 | /** | ||
363 | * Connect to the PSYCstore service. | ||
364 | * | ||
365 | * @param cfg the configuration to use | ||
366 | * @return handle to use | ||
367 | */ | ||
368 | struct GNUNET_PSYCSTORE_Handle * | ||
369 | GNUNET_PSYCSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
370 | { | ||
371 | struct GNUNET_PSYCSTORE_Handle *h; | ||
372 | |||
373 | h = GNUNET_new (struct GNUNET_PSYCSTORE_Handle); | ||
374 | h->cfg = cfg; | ||
375 | h->reconnect_delay = GNUNET_TIME_UNIT_ZERO; | ||
376 | h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, h); | ||
377 | return h; | ||
378 | } | ||
379 | |||
380 | |||
381 | /** | ||
382 | * Cancel a PSYCstore operation. Note that the operation MAY still | ||
383 | * be executed; this merely cancels the continuation; if the request | ||
384 | * was already transmitted, the service may still choose to complete | ||
385 | * the operation. | ||
386 | * | ||
387 | * @param op operation to cancel | ||
388 | */ | ||
389 | void | ||
390 | GNUNET_PSYCSTORE_operation_cancel (struct GNUNET_PSYCSTORE_OperationHandle *op) | ||
391 | { | ||
392 | struct GNUNET_PSYCSTORE_Handle *h = op->h; | ||
393 | |||
394 | if ( (h->op_head != op) || | ||
395 | (NULL == h->client) ) | ||
396 | { | ||
397 | /* request not active, can simply remove */ | ||
398 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
399 | h->op_tail, | ||
400 | op); | ||
401 | GNUNET_free (op); | ||
402 | return; | ||
403 | } | ||
404 | if (NULL != h->th) | ||
405 | { | ||
406 | /* request active but not yet with service, can still abort */ | ||
407 | GNUNET_CLIENT_notify_transmit_ready_cancel (h->th); | ||
408 | h->th = NULL; | ||
409 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
410 | h->op_tail, | ||
411 | op); | ||
412 | GNUNET_free (op); | ||
413 | transmit_next (h); | ||
414 | return; | ||
415 | } | ||
416 | /* request active with service, simply ensure continuations are not called */ | ||
417 | op->res_cb = NULL; | ||
418 | op->frag_cb = NULL; | ||
419 | op->state_cb = NULL; | ||
420 | } | ||
421 | |||
422 | |||
423 | /** | ||
424 | * Disconnect from PSYCstore service | ||
425 | * | ||
426 | * @param h handle to destroy | ||
427 | */ | ||
428 | void | ||
429 | GNUNET_PSYCSTORE_disconnect (struct GNUNET_PSYCSTORE_Handle *h) | ||
430 | { | ||
431 | GNUNET_assert (NULL != h); | ||
432 | GNUNET_assert (h->op_head == h->op_tail); | ||
433 | if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK) | ||
434 | { | ||
435 | GNUNET_SCHEDULER_cancel (h->reconnect_task); | ||
436 | h->reconnect_task = GNUNET_SCHEDULER_NO_TASK; | ||
437 | } | ||
438 | if (NULL != h->th) | ||
439 | { | ||
440 | GNUNET_CLIENT_notify_transmit_ready_cancel (h->th); | ||
441 | h->th = NULL; | ||
442 | } | ||
443 | if (NULL != h->client) | ||
444 | { | ||
445 | GNUNET_CLIENT_disconnect (h->client); | ||
446 | h->client = NULL; | ||
447 | } | ||
448 | GNUNET_free (h); | ||
449 | } | ||
450 | |||
451 | /* end of psycstore_api.c */ | ||
diff --git a/src/psycstore/test_psycstore.c b/src/psycstore/test_psycstore.c new file mode 100644 index 000000000..405d1622c --- /dev/null +++ b/src/psycstore/test_psycstore.c | |||
@@ -0,0 +1,158 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
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 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file psycstore/test_psycstore.c | ||
23 | * @brief Testcase for the PSYCstore service | ||
24 | * @author Gabor X Toth | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | |||
28 | #include "platform.h" | ||
29 | #include "gnunet_common.h" | ||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_psycstore_service.h" | ||
32 | #include "gnunet_testing_lib.h" | ||
33 | |||
34 | |||
35 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) | ||
36 | |||
37 | |||
38 | /** | ||
39 | * Return value from 'main'. | ||
40 | */ | ||
41 | static int res; | ||
42 | |||
43 | /** | ||
44 | * Handle to PSYCstore service. | ||
45 | */ | ||
46 | static struct GNUNET_PSYCSTORE_Handle *h; | ||
47 | |||
48 | /** | ||
49 | * Handle to PSYCstore operation. | ||
50 | */ | ||
51 | static struct GNUNET_PSYCSTORE_Operation *op; | ||
52 | |||
53 | /** | ||
54 | * Handle for task for timeout termination. | ||
55 | */ | ||
56 | static GNUNET_SCHEDULER_TaskIdentifier endbadly_task; | ||
57 | |||
58 | |||
59 | /** | ||
60 | * Clean up all resources used. | ||
61 | */ | ||
62 | static void | ||
63 | cleanup () | ||
64 | { | ||
65 | if (NULL != op) | ||
66 | { | ||
67 | GNUNET_PSYCSTORE_operation_cancel (op); | ||
68 | op = NULL; | ||
69 | } | ||
70 | if (NULL != h) | ||
71 | { | ||
72 | GNUNET_PSYCSTORE_disconnect (h); | ||
73 | h = NULL; | ||
74 | } | ||
75 | GNUNET_SCHEDULER_shutdown (); | ||
76 | } | ||
77 | |||
78 | |||
79 | /** | ||
80 | * Termiante the testcase (failure). | ||
81 | * | ||
82 | * @param cls NULL | ||
83 | * @param tc scheduler context | ||
84 | */ | ||
85 | static void | ||
86 | endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
87 | { | ||
88 | cleanup (); | ||
89 | res = 1; | ||
90 | } | ||
91 | |||
92 | |||
93 | /** | ||
94 | * Termiante the testcase (success). | ||
95 | * | ||
96 | * @param cls NULL | ||
97 | * @param tc scheduler context | ||
98 | */ | ||
99 | static void | ||
100 | end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
101 | { | ||
102 | cleanup (); | ||
103 | res = 0; | ||
104 | } | ||
105 | |||
106 | |||
107 | /** | ||
108 | * Finish the testcase (successfully). | ||
109 | */ | ||
110 | static void | ||
111 | end () | ||
112 | { | ||
113 | if (endbadly_task != GNUNET_SCHEDULER_NO_TASK) | ||
114 | { | ||
115 | GNUNET_SCHEDULER_cancel (endbadly_task); | ||
116 | endbadly_task = GNUNET_SCHEDULER_NO_TASK; | ||
117 | } | ||
118 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, | ||
119 | &end_normally, NULL); | ||
120 | } | ||
121 | |||
122 | |||
123 | /** | ||
124 | * Main function of the test, run from scheduler. | ||
125 | * | ||
126 | * @param cls NULL | ||
127 | * @param cfg configuration we use (also to connect to PSYCstore service) | ||
128 | * @param peer handle to access more of the peer (not used) | ||
129 | */ | ||
130 | static void | ||
131 | run (void *cls, | ||
132 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
133 | struct GNUNET_TESTING_Peer *peer) | ||
134 | { | ||
135 | endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, | ||
136 | &endbadly, NULL); | ||
137 | h = GNUNET_PSYCSTORE_connect (cfg); | ||
138 | GNUNET_assert (NULL != h); | ||
139 | end (); | ||
140 | } | ||
141 | |||
142 | |||
143 | int | ||
144 | main (int argc, char *argv[]) | ||
145 | { | ||
146 | res = 1; | ||
147 | if (0 != | ||
148 | GNUNET_TESTING_service_run ("test-psycstore", | ||
149 | "psycstore", | ||
150 | "test_psycstore.conf", | ||
151 | &run, | ||
152 | NULL)) | ||
153 | return 1; | ||
154 | return res; | ||
155 | } | ||
156 | |||
157 | |||
158 | /* end of test_psycstore.c */ | ||
diff --git a/src/psycstore/test_psycstore.conf b/src/psycstore/test_psycstore.conf new file mode 100644 index 000000000..1f6d279ab --- /dev/null +++ b/src/psycstore/test_psycstore.conf | |||
@@ -0,0 +1,6 @@ | |||
1 | [arm] | ||
2 | DEFAULTSERVICES = psycstore | ||
3 | UNIXPATH = /tmp/test-psycstore-service-arm.sock | ||
4 | |||
5 | [psycstore] | ||
6 | DBFILE = /tmp/test-psycstore-service.sqlite | ||