diff options
Diffstat (limited to 'src/mesh/mesh_path.c')
-rw-r--r-- | src/mesh/mesh_path.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/src/mesh/mesh_path.c b/src/mesh/mesh_path.c new file mode 100644 index 000000000..05444655a --- /dev/null +++ b/src/mesh/mesh_path.c | |||
@@ -0,0 +1,125 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2001 - 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | 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 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file mesh/mesh_path.c | ||
23 | * @brief Path handling functions | ||
24 | * @author Bartlomiej Polot | ||
25 | */ | ||
26 | |||
27 | #include "mesh2.h" | ||
28 | #include "mesh_path.h" | ||
29 | |||
30 | |||
31 | /** | ||
32 | * Create a new path | ||
33 | * | ||
34 | * @param length How many hops will the path have. | ||
35 | * | ||
36 | * @return A newly allocated path with a peer array of the specified length. | ||
37 | */ | ||
38 | struct MeshPeerPath * | ||
39 | path_new (unsigned int length) | ||
40 | { | ||
41 | struct MeshPeerPath *p; | ||
42 | |||
43 | p = GNUNET_malloc (sizeof (struct MeshPeerPath)); | ||
44 | if (length > 0) | ||
45 | { | ||
46 | p->length = length; | ||
47 | p->peers = GNUNET_malloc (length * sizeof (GNUNET_PEER_Id)); | ||
48 | } | ||
49 | return p; | ||
50 | } | ||
51 | |||
52 | |||
53 | /** | ||
54 | * Invert the path | ||
55 | * | ||
56 | * @param path the path to invert | ||
57 | */ | ||
58 | void | ||
59 | path_invert (struct MeshPeerPath *path) | ||
60 | { | ||
61 | GNUNET_PEER_Id aux; | ||
62 | unsigned int i; | ||
63 | |||
64 | for (i = 0; i < path->length / 2; i++) | ||
65 | { | ||
66 | aux = path->peers[i]; | ||
67 | path->peers[i] = path->peers[path->length - i - 1]; | ||
68 | path->peers[path->length - i - 1] = aux; | ||
69 | } | ||
70 | } | ||
71 | |||
72 | |||
73 | /** | ||
74 | * Duplicate a path, incrementing short peer's rc. | ||
75 | * | ||
76 | * @param path The path to duplicate. | ||
77 | */ | ||
78 | struct MeshPeerPath * | ||
79 | path_duplicate (struct MeshPeerPath *path) | ||
80 | { | ||
81 | struct MeshPeerPath *aux; | ||
82 | unsigned int i; | ||
83 | |||
84 | aux = path_new (path->length); | ||
85 | memcpy (aux->peers, path->peers, path->length * sizeof (GNUNET_PEER_Id)); | ||
86 | for (i = 0; i < path->length; i++) | ||
87 | GNUNET_PEER_change_rc (path->peers[i], 1); | ||
88 | return aux; | ||
89 | } | ||
90 | |||
91 | |||
92 | /** | ||
93 | * Get the length of a path. | ||
94 | * | ||
95 | * @param path The path to measure, with the local peer at any point of it. | ||
96 | * | ||
97 | * @return Number of hops to reach destination. | ||
98 | * UINT_MAX in case the peer is not in the path. | ||
99 | */ | ||
100 | unsigned int | ||
101 | path_get_length (struct MeshPeerPath *path) | ||
102 | { | ||
103 | if (NULL == path) | ||
104 | return UINT_MAX; | ||
105 | return path->length; | ||
106 | } | ||
107 | |||
108 | |||
109 | /** | ||
110 | * Destroy the path and free any allocated resources linked to it | ||
111 | * | ||
112 | * @param p the path to destroy | ||
113 | * | ||
114 | * @return GNUNET_OK on success | ||
115 | */ | ||
116 | int | ||
117 | path_destroy (struct MeshPeerPath *p) | ||
118 | { | ||
119 | if (NULL == p) | ||
120 | return GNUNET_OK; | ||
121 | GNUNET_PEER_decrement_rcs (p->peers, p->length); | ||
122 | GNUNET_free_non_null (p->peers); | ||
123 | GNUNET_free (p); | ||
124 | return GNUNET_OK; | ||
125 | } | ||