aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/messenger_api_list_tunnels.c
diff options
context:
space:
mode:
authorTheJackiMonster <thejackimonster@gmail.com>2020-11-12 17:22:36 +0100
committerTheJackiMonster <thejackimonster@gmail.com>2020-11-12 17:22:36 +0100
commitfec34163a1f17729c190022a2bf747f48e34f07a (patch)
tree9c2884555c795f9f70ec1e0c8d76dbb50aba1f2f /src/messenger/messenger_api_list_tunnels.c
parent63fe195e40e55f13ab29e3ba578e97017fc4cc48 (diff)
parent8bf864c25bda97c1448b709a76a168834753ff86 (diff)
downloadgnunet-fec34163a1f17729c190022a2bf747f48e34f07a.tar.gz
gnunet-fec34163a1f17729c190022a2bf747f48e34f07a.zip
-merge branch 'jacki/messenger'
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
Diffstat (limited to 'src/messenger/messenger_api_list_tunnels.c')
-rw-r--r--src/messenger/messenger_api_list_tunnels.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/messenger/messenger_api_list_tunnels.c b/src/messenger/messenger_api_list_tunnels.c
new file mode 100644
index 000000000..13d8c1906
--- /dev/null
+++ b/src/messenger/messenger_api_list_tunnels.c
@@ -0,0 +1,112 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020 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
28void
29init_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels)
30{
31 GNUNET_assert(tunnels);
32
33 tunnels->head = NULL;
34 tunnels->tail = NULL;
35}
36
37void
38clear_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
55static int
56compare_list_tunnels (void *cls, struct GNUNET_MESSENGER_ListTunnel *element0,
57 struct GNUNET_MESSENGER_ListTunnel *element1)
58{
59 return ((int) element0->peer) - ((int) element1->peer);
60}
61
62void
63add_to_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels, const struct GNUNET_PeerIdentity *peer)
64{
65 struct GNUNET_MESSENGER_ListTunnel *element = GNUNET_new(struct GNUNET_MESSENGER_ListTunnel);
66
67 element->peer = GNUNET_PEER_intern (peer);
68
69 GNUNET_CONTAINER_DLL_insert_sorted(struct GNUNET_MESSENGER_ListTunnel, compare_list_tunnels, NULL, tunnels->head,
70 tunnels->tail, element);
71}
72
73struct GNUNET_MESSENGER_ListTunnel*
74find_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels, const struct GNUNET_PeerIdentity *peer, size_t *index)
75{
76 struct GNUNET_MESSENGER_ListTunnel *element;
77 struct GNUNET_PeerIdentity pid;
78
79 if (index)
80 *index = 0;
81
82 for (element = tunnels->head; element; element = element->next)
83 {
84 GNUNET_PEER_resolve (element->peer, &pid);
85
86 if (0 == GNUNET_memcmp(&pid, peer))
87 return element;
88
89 if (index)
90 (*index) = (*index) + 1;
91 }
92
93 return NULL;
94}
95
96int
97contains_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels, const struct GNUNET_PeerIdentity *peer)
98{
99 return find_list_tunnels (tunnels, peer, NULL) != NULL ? GNUNET_YES : GNUNET_NO;
100}
101
102struct GNUNET_MESSENGER_ListTunnel*
103remove_from_list_tunnels (struct GNUNET_MESSENGER_ListTunnels *tunnels, struct GNUNET_MESSENGER_ListTunnel *element)
104{
105 struct GNUNET_MESSENGER_ListTunnel *next = element->next;
106
107 GNUNET_CONTAINER_DLL_remove(tunnels->head, tunnels->tail, element);
108 GNUNET_PEER_change_rc (element->peer, -1);
109 GNUNET_free(element);
110
111 return next;
112}