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--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  */
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 *element0,
58  struct GNUNET_MESSENGER_ListTunnel *element1)
59 {
60  return ((int) element0->peer) - ((int) element1->peer);
61 }
62 
63 void
65  const struct GNUNET_PeerIdentity *peer)
66 {
67  GNUNET_assert((tunnels) && (peer));
68 
70 
71  element->peer = GNUNET_PEER_intern (peer);
72 
74  tunnels->tail, element);
75 }
76 
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
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 
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
130  const char *path)
131 {
132  GNUNET_assert((tunnels) && (path));
133 
134  if (GNUNET_YES != GNUNET_DISK_file_test (path))
135  return;
136 
138 
140  path, GNUNET_DISK_OPEN_READ, permission
141  );
142 
143  if (!handle)
144  return;
145 
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 
161 }
162 
163 void
165  const char *path)
166 {
167  GNUNET_assert((tunnels) && (path));
168 
170 
173  );
174 
175  if (!handle)
176  return;
177 
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 
192 }
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94
#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.
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:1235
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:482
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:686
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:1425
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1306
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:622
@ 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_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_PEER_change_rc(GNUNET_PEER_Id id, int delta)
Change the reference counter of an interned PID.
Definition: peer.c:197
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
GNUNET_PEER_Id GNUNET_PEER_intern(const struct GNUNET_PeerIdentity *pid)
Intern an peer identity.
Definition: peer.c:107
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.
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)
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...
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 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 clear_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels)
Clears the list of tunnels peer identities.
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...
messenger api: client and service implementation of GNUnet MESSENGER service
Handle used to access files (and pipes).
struct GNUNET_MESSENGER_ListTunnel * next
struct GNUNET_MESSENGER_ListTunnel * tail
struct GNUNET_MESSENGER_ListTunnel * head
The identity of the host (wraps the signing key of the peer).
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.