GNUnet  0.11.x
messenger_api_list_tunnels.c
Go to the documentation of this file.
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  */
27 
28 void
30 {
31  GNUNET_assert(tunnels);
32 
33  tunnels->head = NULL;
34  tunnels->tail = NULL;
35 }
36 
37 void
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
57  struct GNUNET_MESSENGER_ListTunnel *element1)
58 {
59  return ((int) element0->peer) - ((int) element1->peer);
60 }
61 
62 void
64 {
66 
67  element->peer = GNUNET_PEER_intern (peer);
68 
70  tunnels->tail, element);
71 }
72 
74 find_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 
96 int
98 {
99  return find_list_tunnels (tunnels, peer, NULL) != NULL ? GNUNET_YES : GNUNET_NO;
100 }
101 
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 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
int 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...
#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_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_MESSENGER_ListTunnel * head
struct GNUNET_MESSENGER_ListTunnel * next
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void add_to_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels, const struct GNUNET_PeerIdentity *peer)
Adds a specific peer from a tunnel to the end of the list.
void GNUNET_PEER_resolve(GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
Convert an interned PID to a normal peer identity.
Definition: peer.c:225
static int compare_list_tunnels(void *cls, struct GNUNET_MESSENGER_ListTunnel *element0, struct GNUNET_MESSENGER_ListTunnel *element1)
void clear_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels)
Clears the list of tunnels peer identities.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_MESSENGER_ListTunnel * tail
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
The identity of the host (wraps the signing key of the peer).
void init_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels)
Initializes list of tunnels peer identities as empty list.
messenger api: client and service implementation of GNUnet MESSENGER service
void GNUNET_PEER_change_rc(GNUNET_PEER_Id id, int delta)
Change the reference counter of an interned PID.
Definition: peer.c:197
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...
GNUNET_PEER_Id GNUNET_PEER_intern(const struct GNUNET_PeerIdentity *pid)
Intern an peer identity.
Definition: peer.c:107
#define GNUNET_free(ptr)
Wrapper around free.
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...