diff options
Diffstat (limited to 'src/service/cadet/gnunet-service-cadet_hello.c')
-rw-r--r-- | src/service/cadet/gnunet-service-cadet_hello.c | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/src/service/cadet/gnunet-service-cadet_hello.c b/src/service/cadet/gnunet-service-cadet_hello.c new file mode 100644 index 000000000..cc28f0fde --- /dev/null +++ b/src/service/cadet/gnunet-service-cadet_hello.c | |||
@@ -0,0 +1,195 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2014, 2017 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file cadet/gnunet-service-cadet_hello.c | ||
22 | * @brief spread knowledge about how to contact us (get HELLO from peerinfo), | ||
23 | * and remember HELLOs of other peers we have an interest in | ||
24 | * @author Bartlomiej Polot | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | #include "gnunet_common.h" | ||
28 | #include "platform.h" | ||
29 | #include "gnunet_util_lib.h" | ||
30 | #include "gnunet_statistics_service.h" | ||
31 | #include "gnunet_peerstore_service.h" | ||
32 | #include "cadet_protocol.h" | ||
33 | #include "gnunet-service-cadet.h" | ||
34 | #include "gnunet-service-cadet_dht.h" | ||
35 | #include "gnunet-service-cadet_hello.h" | ||
36 | #include "gnunet-service-cadet_peer.h" | ||
37 | |||
38 | #define LOG(level, ...) GNUNET_log_from (level, "cadet-hll", __VA_ARGS__) | ||
39 | |||
40 | /** | ||
41 | * Hello message of local peer. | ||
42 | */ | ||
43 | static struct GNUNET_MessageHeader *mine; | ||
44 | |||
45 | /** | ||
46 | * Handle to the PEERSTORE service. | ||
47 | */ | ||
48 | static struct GNUNET_PEERSTORE_Handle *peerstore; | ||
49 | |||
50 | /** | ||
51 | * Our peerstore notification context. We use notification | ||
52 | * to instantly learn about new peers as they are discovered. | ||
53 | */ | ||
54 | static struct GNUNET_PEERSTORE_Monitor *peerstore_notify; | ||
55 | |||
56 | |||
57 | /** | ||
58 | * Process each hello message received from peerinfo. | ||
59 | * | ||
60 | * @param cls Closure (unused). | ||
61 | * @param id Identity of the peer. | ||
62 | * @param hello Hello of the peer. | ||
63 | * @param err_msg Error message. | ||
64 | */ | ||
65 | static void | ||
66 | got_hello (void *cls, | ||
67 | const struct GNUNET_PEERSTORE_Record *record, | ||
68 | const char *err_msg) | ||
69 | { | ||
70 | struct CadetPeer *peer; | ||
71 | struct GNUNET_HELLO_Builder *builder; | ||
72 | struct GNUNET_MessageHeader *hello; | ||
73 | |||
74 | if (NULL == record->value) | ||
75 | { | ||
76 | GNUNET_PEERSTORE_monitor_next (peerstore_notify, 1); | ||
77 | return; | ||
78 | } | ||
79 | hello = record->value; | ||
80 | if (0 == GNUNET_memcmp (&record->peer, | ||
81 | &my_full_id)) | ||
82 | { | ||
83 | GNUNET_free (mine); | ||
84 | builder = GNUNET_HELLO_builder_from_msg (hello); | ||
85 | mine = GNUNET_HELLO_builder_to_dht_hello_msg (builder, | ||
86 | my_private_key, | ||
87 | GNUNET_TIME_UNIT_ZERO); | ||
88 | GNUNET_HELLO_builder_free (builder); | ||
89 | GCD_hello_update (); | ||
90 | GNUNET_PEERSTORE_monitor_next (peerstore_notify, 1); | ||
91 | return; | ||
92 | } | ||
93 | |||
94 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
95 | "Hello for %s (%d bytes), expires on %s\n", | ||
96 | GNUNET_i2s (&record->peer), | ||
97 | ntohs (hello->size), | ||
98 | GNUNET_STRINGS_absolute_time_to_string ( | ||
99 | GNUNET_HELLO_builder_get_expiration_time (hello))); | ||
100 | peer = GCP_get (&record->peer, | ||
101 | GNUNET_YES); | ||
102 | GCP_set_hello (peer, | ||
103 | hello); | ||
104 | GNUNET_PEERSTORE_monitor_next (peerstore_notify, 1); | ||
105 | } | ||
106 | |||
107 | |||
108 | static void | ||
109 | error_cb (void *cls) | ||
110 | { | ||
111 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
112 | "Error in PEERSTORE monitoring\n"); | ||
113 | } | ||
114 | |||
115 | |||
116 | static void | ||
117 | sync_cb (void *cls) | ||
118 | { | ||
119 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
120 | "Done with initial PEERSTORE iteration during monitoring\n"); | ||
121 | } | ||
122 | |||
123 | |||
124 | /** | ||
125 | * Initialize the hello subsystem. | ||
126 | * | ||
127 | * @param c Configuration. | ||
128 | */ | ||
129 | void | ||
130 | GCH_init (const struct GNUNET_CONFIGURATION_Handle *c) | ||
131 | { | ||
132 | GNUNET_assert (NULL == peerstore_notify); | ||
133 | peerstore = GNUNET_PEERSTORE_connect (c); | ||
134 | peerstore_notify = | ||
135 | GNUNET_PEERSTORE_monitor_start (c, | ||
136 | GNUNET_YES, | ||
137 | "peerstore", | ||
138 | NULL, | ||
139 | GNUNET_PEERSTORE_HELLO_KEY, | ||
140 | &error_cb, | ||
141 | NULL, | ||
142 | &sync_cb, | ||
143 | NULL, | ||
144 | &got_hello, | ||
145 | NULL); | ||
146 | } | ||
147 | |||
148 | |||
149 | /** | ||
150 | * Shut down the hello subsystem. | ||
151 | */ | ||
152 | void | ||
153 | GCH_shutdown () | ||
154 | { | ||
155 | if (NULL != peerstore_notify) | ||
156 | { | ||
157 | GNUNET_PEERSTORE_monitor_stop (peerstore_notify); | ||
158 | peerstore_notify = NULL; | ||
159 | } | ||
160 | if (NULL != peerstore) | ||
161 | { | ||
162 | GNUNET_PEERSTORE_disconnect (peerstore); | ||
163 | peerstore = NULL; | ||
164 | } | ||
165 | if (NULL != mine) | ||
166 | { | ||
167 | GNUNET_free (mine); | ||
168 | mine = NULL; | ||
169 | } | ||
170 | } | ||
171 | |||
172 | |||
173 | /** | ||
174 | * Get own hello message. | ||
175 | * | ||
176 | * @return Own hello message. | ||
177 | */ | ||
178 | const struct GNUNET_MessageHeader * | ||
179 | GCH_get_mine (void) | ||
180 | { | ||
181 | struct GNUNET_HELLO_Builder *builder; | ||
182 | |||
183 | if (NULL == mine) | ||
184 | { | ||
185 | builder = GNUNET_HELLO_builder_new (&my_full_id); | ||
186 | mine = GNUNET_HELLO_builder_to_dht_hello_msg (builder, | ||
187 | my_private_key, | ||
188 | GNUNET_TIME_UNIT_ZERO); | ||
189 | GNUNET_HELLO_builder_free (builder); | ||
190 | } | ||
191 | return mine; | ||
192 | } | ||
193 | |||
194 | |||
195 | /* end of gnunet-service-cadet-new_hello.c */ | ||