diff options
Diffstat (limited to 'src/messenger/messenger_api_list_tunnels.c')
-rw-r--r-- | src/messenger/messenger_api_list_tunnels.c | 192 |
1 files changed, 0 insertions, 192 deletions
diff --git a/src/messenger/messenger_api_list_tunnels.c b/src/messenger/messenger_api_list_tunnels.c deleted file mode 100644 index 7e77d8f2f..000000000 --- a/src/messenger/messenger_api_list_tunnels.c +++ /dev/null | |||
@@ -1,192 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2020--2021 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 | * @author Tobias Frisch | ||
22 | * @file src/messenger/messenger_api_list_tunnels.c | ||
23 | * @brief messenger api: client and service implementation of GNUnet MESSENGER service | ||
24 | */ | ||
25 | |||
26 | #include "messenger_api_list_tunnels.h" | ||
27 | |||
28 | void | ||
29 | init_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels) | ||
30 | { | ||
31 | GNUNET_assert(tunnels); | ||
32 | |||
33 | tunnels->head = NULL; | ||
34 | tunnels->tail = NULL; | ||
35 | } | ||
36 | |||
37 | void | ||
38 | clear_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels) | ||
39 | { | ||
40 | GNUNET_assert(tunnels); | ||
41 | |||
42 | struct GNUNET_MESSENGER_ListTunnel *element; | ||
43 | |||
44 | for (element = tunnels->head; element; element = tunnels->head) | ||
45 | { | ||
46 | GNUNET_CONTAINER_DLL_remove(tunnels->head, tunnels->tail, element); | ||
47 | GNUNET_PEER_change_rc (element->peer, -1); | ||
48 | GNUNET_free(element); | ||
49 | } | ||
50 | |||
51 | tunnels->head = NULL; | ||
52 | tunnels->tail = NULL; | ||
53 | } | ||
54 | |||
55 | static int | ||
56 | compare_list_tunnels (void *cls, | ||
57 | struct GNUNET_MESSENGER_ListTunnel *element0, | ||
58 | struct GNUNET_MESSENGER_ListTunnel *element1) | ||
59 | { | ||
60 | return ((int) element0->peer) - ((int) element1->peer); | ||
61 | } | ||
62 | |||
63 | void | ||
64 | add_to_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels, | ||
65 | const struct GNUNET_PeerIdentity *peer) | ||
66 | { | ||
67 | GNUNET_assert((tunnels) && (peer)); | ||
68 | |||
69 | struct GNUNET_MESSENGER_ListTunnel *element = GNUNET_new(struct GNUNET_MESSENGER_ListTunnel); | ||
70 | |||
71 | element->peer = GNUNET_PEER_intern (peer); | ||
72 | |||
73 | GNUNET_CONTAINER_DLL_insert_sorted(struct GNUNET_MESSENGER_ListTunnel, compare_list_tunnels, NULL, tunnels->head, | ||
74 | tunnels->tail, element); | ||
75 | } | ||
76 | |||
77 | struct GNUNET_MESSENGER_ListTunnel* | ||
78 | find_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels, | ||
79 | const struct GNUNET_PeerIdentity *peer, | ||
80 | size_t *index) | ||
81 | { | ||
82 | GNUNET_assert((tunnels) && (peer)); | ||
83 | |||
84 | struct GNUNET_MESSENGER_ListTunnel *element; | ||
85 | struct GNUNET_PeerIdentity pid; | ||
86 | |||
87 | if (index) | ||
88 | *index = 0; | ||
89 | |||
90 | for (element = tunnels->head; element; element = element->next) | ||
91 | { | ||
92 | GNUNET_PEER_resolve (element->peer, &pid); | ||
93 | |||
94 | if (0 == GNUNET_memcmp(&pid, peer)) | ||
95 | return element; | ||
96 | |||
97 | if (index) | ||
98 | (*index) = (*index) + 1; | ||
99 | } | ||
100 | |||
101 | return NULL; | ||
102 | } | ||
103 | |||
104 | int | ||
105 | contains_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels, | ||
106 | const struct GNUNET_PeerIdentity *peer) | ||
107 | { | ||
108 | GNUNET_assert((tunnels) && (peer)); | ||
109 | |||
110 | return find_list_tunnels (tunnels, peer, NULL) != NULL ? GNUNET_YES : GNUNET_NO; | ||
111 | } | ||
112 | |||
113 | struct GNUNET_MESSENGER_ListTunnel* | ||
114 | remove_from_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels, | ||
115 | struct GNUNET_MESSENGER_ListTunnel *element) | ||
116 | { | ||
117 | GNUNET_assert((tunnels) && (element)); | ||
118 | |||
119 | struct GNUNET_MESSENGER_ListTunnel *next = element->next; | ||
120 | |||
121 | GNUNET_CONTAINER_DLL_remove(tunnels->head, tunnels->tail, element); | ||
122 | GNUNET_PEER_change_rc (element->peer, -1); | ||
123 | GNUNET_free(element); | ||
124 | |||
125 | return next; | ||
126 | } | ||
127 | |||
128 | void | ||
129 | load_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels, | ||
130 | const char *path) | ||
131 | { | ||
132 | GNUNET_assert((tunnels) && (path)); | ||
133 | |||
134 | if (GNUNET_YES != GNUNET_DISK_file_test (path)) | ||
135 | return; | ||
136 | |||
137 | enum GNUNET_DISK_AccessPermissions permission = (GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE); | ||
138 | |||
139 | struct GNUNET_DISK_FileHandle *handle = GNUNET_DISK_file_open( | ||
140 | path, GNUNET_DISK_OPEN_READ, permission | ||
141 | ); | ||
142 | |||
143 | if (!handle) | ||
144 | return; | ||
145 | |||
146 | GNUNET_DISK_file_seek(handle, 0, GNUNET_DISK_SEEK_SET); | ||
147 | |||
148 | struct GNUNET_PeerIdentity peer; | ||
149 | ssize_t len; | ||
150 | |||
151 | do { | ||
152 | len = GNUNET_DISK_file_read(handle, &peer, sizeof(peer)); | ||
153 | |||
154 | if (len != sizeof(peer)) | ||
155 | break; | ||
156 | |||
157 | add_to_list_tunnels(tunnels, &peer); | ||
158 | } while (len == sizeof(peer)); | ||
159 | |||
160 | GNUNET_DISK_file_close(handle); | ||
161 | } | ||
162 | |||
163 | void | ||
164 | save_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels, | ||
165 | const char *path) | ||
166 | { | ||
167 | GNUNET_assert((tunnels) && (path)); | ||
168 | |||
169 | enum GNUNET_DISK_AccessPermissions permission = (GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE); | ||
170 | |||
171 | struct GNUNET_DISK_FileHandle *handle = GNUNET_DISK_file_open( | ||
172 | path, GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_WRITE, permission | ||
173 | ); | ||
174 | |||
175 | if (!handle) | ||
176 | return; | ||
177 | |||
178 | GNUNET_DISK_file_seek(handle, 0, GNUNET_DISK_SEEK_SET); | ||
179 | |||
180 | struct GNUNET_MESSENGER_ListTunnel *element; | ||
181 | struct GNUNET_PeerIdentity pid; | ||
182 | |||
183 | for (element = tunnels->head; element; element = element->next) | ||
184 | { | ||
185 | GNUNET_PEER_resolve (element->peer, &pid); | ||
186 | |||
187 | GNUNET_DISK_file_write(handle, &pid, sizeof(pid)); | ||
188 | } | ||
189 | |||
190 | GNUNET_DISK_file_sync(handle); | ||
191 | GNUNET_DISK_file_close(handle); | ||
192 | } | ||