2 This file is part of GNUnet.
3 Copyright (C) 2008--2013 GNUnet e.V.
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.
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.
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/>.
20 * @file testbed/testbed_api_underlay.c
21 * @brief testbed underlay API implementation
22 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25 #include "testbed_api_peers.h"
34 * next pointer for list
36 struct LinkProperty *next;
39 * the peer whose link is defined by these properties
41 struct GNUNET_TESTBED_Peer *peer;
44 * latency of the link in microseconds
49 * data loss on the link expressed as percentage
54 * bandwidth of the link in kilobytes per second
61 * Container for holding a peer in whitelist/blacklist
68 struct ListEntry *next;
73 struct GNUNET_TESTBED_Peer *peer;
78 * Model for configuring underlay links of a peer
81 struct GNUNET_TESTBED_UnderlayLinkModel
84 * The peer associated with this model
86 struct GNUNET_TESTBED_Peer *peer;
89 * List of peers in the list
91 struct ListEntry *entries;
94 * list of link properties
96 struct LinkProperty *props;
99 * the type of this model
101 enum GNUNET_TESTBED_UnderlayLinkModelType type;
106 * Function to free resources of list entries
108 * @param model the model
111 free_entries (struct GNUNET_TESTBED_UnderlayLinkModel *model)
115 while (NULL != (e = model->entries))
117 model->entries = e->next;
124 * Function to free resources of link properties added to the given model
126 * @param model the model
129 free_link_properties (struct GNUNET_TESTBED_UnderlayLinkModel *model)
131 struct LinkProperty *p;
133 while (NULL != (p = model->props))
135 model->props = p->next;
142 * Create a GNUNET_TESTBED_UnderlayLinkModel for the given peer. A peer can
143 * have ONLY ONE model and it can be either a blacklist or whitelist based one.
146 * @param peer the peer for which the model has to be created
147 * @param type the type of the model
150 struct GNUNET_TESTBED_UnderlayLinkModel *
151 GNUNET_TESTBED_underlaylinkmodel_create (struct GNUNET_TESTBED_Peer *peer,
152 enum GNUNET_TESTBED_UnderlayLinkModelType type)
154 struct GNUNET_TESTBED_UnderlayLinkModel *m;
156 GNUNET_assert (0 == peer->underlay_model_exists);
157 m = GNUNET_new (struct GNUNET_TESTBED_UnderlayLinkModel);
158 peer->underlay_model_exists = 1;
165 * Add a peer to the given model. Underlay connections to the given peer will
166 * be permitted if the model is whitelist based; otherwise they will not be
170 * @param model the model
171 * @param peer the peer to add
174 GNUNET_TESTBED_underlaylinkmodel_add_peer (struct GNUNET_TESTBED_UnderlayLinkModel *model,
175 struct GNUNET_TESTBED_Peer *peer)
177 struct ListEntry *entry;
179 entry = GNUNET_new (struct ListEntry);
181 entry->next = model->entries;
182 model->entries = entry;
187 * Set the metrics for a link to the given peer in the underlay model. The link
188 * SHOULD be permittable according to the given model.
191 * @param model the model
192 * @param peer the other end peer of the link
193 * @param latency latency of the link in microseconds
194 * @param loss data loss of the link expressed as a percentage
195 * @param bandwidth bandwidth of the link in kilobytes per second [kB/s]
198 GNUNET_TESTBED_underlaylinkmodel_set_link (struct GNUNET_TESTBED_UnderlayLinkModel *model,
199 struct GNUNET_TESTBED_Peer *peer,
204 struct LinkProperty *prop;
206 prop = GNUNET_new (struct LinkProperty);
208 prop->latency = latency;
210 prop->bandwidth = bandwidth;
211 prop->next = model->props;
217 * Free the resources of the model. Use this function only if the model has not
218 * be committed and has to be unallocated. The peer can then have another model
222 * @param model the model to unallocate
225 GNUNET_TESTBED_underlaylinkmodel_free (struct GNUNET_TESTBED_UnderlayLinkModel *model)
227 model->peer->underlay_model_exists = 0;
228 free_entries (model);
229 free_link_properties (model);
235 * Commit the model. The model is freed in this function(!).
238 * @param model the model to commit
241 GNUNET_TESTBED_underlaylinkmodel_commit (struct GNUNET_TESTBED_UnderlayLinkModel *model)
243 /* FIXME: Marshal the model into a message */
245 /* do not reset the value of model->peer->underlay_model_exists */
246 free_entries (model);
247 free_link_properties (model);