GNUnet 0.22.2
messenger_api_list_tunnels.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020--2024 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 */
27
28void
30{
31 GNUNET_assert (tunnels);
32
33 tunnels->head = NULL;
34 tunnels->tail = NULL;
35}
36
37
38void
40{
41 struct GNUNET_MESSENGER_ListTunnel *element;
42
43 GNUNET_assert (tunnels);
44
45 element = tunnels->head;
46 while (element)
47 element = remove_from_list_tunnels (tunnels, element);
48
49 tunnels->head = NULL;
50 tunnels->tail = NULL;
51}
52
53
54static int
56 struct GNUNET_MESSENGER_ListTunnel *element0,
57 struct GNUNET_MESSENGER_ListTunnel *element1)
58{
59 struct GNUNET_PeerIdentity peer0, peer1;
60
61 GNUNET_assert ((element0) && (element1));
62
63 GNUNET_PEER_resolve (element0->peer, &peer0);
64 GNUNET_PEER_resolve (element1->peer, &peer1);
65
66 return GNUNET_memcmp (&peer0, &peer1);
67}
68
69
70void
72 const struct GNUNET_PeerIdentity *peer,
73 const struct GNUNET_HashCode *hash)
74{
75 struct GNUNET_MESSENGER_ListTunnel *element;
76
77 GNUNET_assert ((tunnels) && (peer));
78
79 element = GNUNET_new (struct GNUNET_MESSENGER_ListTunnel);
80
81 element->peer = GNUNET_PEER_intern (peer);
82 element->hash = hash ? GNUNET_memdup (hash, sizeof (struct GNUNET_HashCode)) :
83 NULL;
84
85 memset (&(element->connection), 0, sizeof (element->connection));
86
88 compare_list_tunnels, NULL, tunnels->head,
89 tunnels->tail, element);
90}
91
92
95 const struct GNUNET_PeerIdentity *peer,
96 size_t *index)
97{
98 struct GNUNET_MESSENGER_ListTunnel *element;
100
101 GNUNET_assert ((tunnels) && (peer));
102
103 if (index)
104 *index = 0;
105
106 for (element = tunnels->head; element; element = element->next)
107 {
108 GNUNET_PEER_resolve (element->peer, &pid);
109
110 if (0 == GNUNET_memcmp (&pid, peer))
111 return element;
112
113 if (index)
114 (*index) = (*index) + 1;
115 }
116
117 return NULL;
118}
119
120
123 const struct GNUNET_PeerIdentity *peer)
124{
125 struct GNUNET_MESSENGER_ListTunnel *element;
127
128 GNUNET_assert ((tunnels) && (peer));
129
130 for (element = tunnels->head; element; element = element->next)
131 {
132 GNUNET_PEER_resolve (element->peer, &pid);
133
134 if (0 != GNUNET_memcmp (&pid, peer))
135 return element;
136 }
137
138 return NULL;
139}
140
141
145 const struct GNUNET_PeerIdentity *peer,
147{
148 struct GNUNET_MESSENGER_ListTunnel *element;
150
151 GNUNET_assert ((tunnels) && (peer) && (flag));
152
153 for (element = tunnels->head; element; element = element->next)
154 {
155 if ((element->connection.flags & flag) != flag)
156 continue;
157
158 GNUNET_PEER_resolve (element->peer, &pid);
159
160 if (0 == GNUNET_memcmp (&pid, peer))
161 return GNUNET_OK;
162 }
163
164 return GNUNET_SYSERR;
165}
166
167
168void
170 const struct GNUNET_PeerIdentity *peer,
171 const struct GNUNET_HashCode *hash)
172{
173 struct GNUNET_MESSENGER_ListTunnel *element;
174
175 GNUNET_assert ((tunnels) && (peer));
176
177 element = find_list_tunnels (tunnels, peer, NULL);
178 if (! element)
179 return;
180
181 if (element->hash)
182 {
183 if (hash)
184 GNUNET_memcpy (element->hash, hash, sizeof(struct GNUNET_HashCode));
185 else
186 {
187 GNUNET_free (element->hash);
188 element->hash = NULL;
189 }
190 }
191 else if (hash)
192 element->hash = GNUNET_memdup (hash, sizeof(struct GNUNET_HashCode));
193}
194
195
198 const struct GNUNET_PeerIdentity *peer)
199{
200 GNUNET_assert ((tunnels) && (peer));
201
202 return find_list_tunnels (tunnels, peer, NULL) != NULL ? GNUNET_YES :
203 GNUNET_NO;
204}
205
206
209 struct GNUNET_MESSENGER_ListTunnel *element)
210{
212
213 GNUNET_assert ((tunnels) && (element));
214
215 next = element->next;
216
217 if ((tunnels->head) && (tunnels->tail))
218 GNUNET_CONTAINER_DLL_remove (tunnels->head, tunnels->tail, element);
219
220 if (element->hash)
221 GNUNET_free (element->hash);
222
223 GNUNET_PEER_change_rc (element->peer, -1);
224 GNUNET_free (element);
225 return next;
226}
227
228
229void
231 const char *path)
232{
234
235 GNUNET_assert ((tunnels) && (path));
236
237 if (GNUNET_YES != GNUNET_DISK_file_test (path))
238 return;
239
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load list of tunnels from path: %s\n",
241 path);
242
243 {
244 enum GNUNET_DISK_AccessPermissions permission;
245
248 }
249
250 if (! handle)
251 return;
252
254
255 {
256 struct GNUNET_PeerIdentity peer;
257 ssize_t len;
258
259 do {
260 len = GNUNET_DISK_file_read (handle, &peer, sizeof(peer));
261
262 if (len != sizeof(peer))
263 break;
264
265 add_to_list_tunnels (tunnels, &peer, NULL);
266 } while (len == sizeof(peer));
267 }
268
270}
271
272
273void
275 const char *path)
276{
278
279 GNUNET_assert ((tunnels) && (path));
280
281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Save list of tunnels to path: %s\n",
282 path);
283
284 {
285 enum GNUNET_DISK_AccessPermissions permission;
286
290 }
291
292 if (! handle)
293 return;
294
296
297 {
298 struct GNUNET_MESSENGER_ListTunnel *element;
300
301 for (element = tunnels->head; element; element = element->next)
302 {
303 GNUNET_PEER_resolve (element->peer, &pid);
304
306 }
307 }
308
311}
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
static struct GNUNET_VPN_Handle * handle
Handle to vpn service.
Definition: gnunet-vpn.c:35
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1215
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
Definition: disk.c:506
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:682
off_t GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h, off_t offset, enum GNUNET_DISK_Seek whence)
Move the read/write pointer in a file.
Definition: disk.c:206
GNUNET_DISK_AccessPermissions
File access permissions, UNIX-style.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_sync(const struct GNUNET_DISK_FileHandle *h)
Write file changes to disk.
Definition: disk.c:1408
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1289
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:646
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
@ GNUNET_DISK_SEEK_SET
Seek an absolute position (from the start of the file).
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_log(kind,...)
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#define GNUNET_CONTAINER_DLL_insert_sorted(TYPE, comparator, comparator_cls, head, tail, element)
Insertion sort of element into DLL from head to tail sorted by comparator.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
GNUNET_MESSENGER_ConnectionFlags
Enum for the different supported flags used to specify connection handling.
void GNUNET_PEER_change_rc(GNUNET_PEER_Id id, int delta)
Change the reference counter of an interned PID.
Definition: peer.c:192
void GNUNET_PEER_resolve(GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
Convert an interned PID to a normal peer identity.
Definition: peer.c:220
GNUNET_PEER_Id GNUNET_PEER_intern(const struct GNUNET_PeerIdentity *pid)
Intern an peer identity.
Definition: peer.c:108
void load_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels, const char *path)
Loads the list of tunnels peer identities from a file under a given path.
struct GNUNET_MESSENGER_ListTunnel * find_list_tunnels_alternate(struct GNUNET_MESSENGER_ListTunnels *tunnels, const struct GNUNET_PeerIdentity *peer)
Searches linearly through the list of tunnels peer identities for matching against a specific peer id...
void init_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels)
Initializes list of tunnels peer identities as empty list.
void save_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels, const char *path)
Saves the list of tunnels peer identities to a file under a given path.
static int compare_list_tunnels(void *cls, struct GNUNET_MESSENGER_ListTunnel *element0, struct GNUNET_MESSENGER_ListTunnel *element1)
void update_to_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_HashCode *hash)
Updates a specific peer from a tunnel in the list.
enum GNUNET_GenericReturnValue verify_list_tunnels_flag_token(const struct GNUNET_MESSENGER_ListTunnels *tunnels, const struct GNUNET_PeerIdentity *peer, enum GNUNET_MESSENGER_ConnectionFlags flag)
Verifies that a specific tunnel selected by its peer identity in a list of tunnels is the first in or...
void add_to_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_HashCode *hash)
Adds a specific peer from a tunnel to the end of the list.
struct GNUNET_MESSENGER_ListTunnel * find_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels, const struct GNUNET_PeerIdentity *peer, size_t *index)
Searches linearly through the list of tunnels peer identities for matching a specific peer identity a...
void clear_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels)
Clears the list of tunnels peer identities.
enum GNUNET_GenericReturnValue contains_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels, const struct GNUNET_PeerIdentity *peer)
Tests linearly if the list of tunnels peer identities contains a specific peer identity and returns G...
struct GNUNET_MESSENGER_ListTunnel * remove_from_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels, struct GNUNET_MESSENGER_ListTunnel *element)
Removes a specific element from the list of tunnels peer identities and returns the next element in t...
Handle used to access files (and pipes).
A 512-bit hashcode.
struct GNUNET_MESSENGER_ListTunnel * next
struct GNUNET_MESSENGER_MessageConnection connection
struct GNUNET_MESSENGER_ListTunnel * tail
struct GNUNET_MESSENGER_ListTunnel * head
uint32_t flags
The flags about the connections of a peer.
The identity of the host (wraps the signing key of the peer).