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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file testbed/testbed_api_underlay.c
23 * @brief testbed underlay API implementation
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
27 #include "testbed_api_peers.h"
35 * next pointer for list
37 struct LinkProperty *next;
40 * the peer whose link is defined by these properties
42 struct GNUNET_TESTBED_Peer *peer;
45 * latency of the link in microseconds
50 * data loss on the link expressed as percentage
55 * bandwidth of the link in kilobytes per second
62 * 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 {
83 * The peer associated with this model
85 struct GNUNET_TESTBED_Peer *peer;
88 * List of peers in the list
90 struct ListEntry *entries;
93 * list of link properties
95 struct LinkProperty *props;
98 * the type of this model
100 enum GNUNET_TESTBED_UnderlayLinkModelType type;
105 * Function to free resources of list entries
107 * @param model the model
110 free_entries(struct GNUNET_TESTBED_UnderlayLinkModel *model)
114 while (NULL != (e = model->entries))
116 model->entries = e->next;
123 * Function to free resources of link properties added to the given model
125 * @param model the model
128 free_link_properties(struct GNUNET_TESTBED_UnderlayLinkModel *model)
130 struct LinkProperty *p;
132 while (NULL != (p = model->props))
134 model->props = p->next;
141 * Create a GNUNET_TESTBED_UnderlayLinkModel for the given peer. A peer can
142 * have ONLY ONE model and it can be either a blacklist or whitelist based one.
145 * @param peer the peer for which the model has to be created
146 * @param type the type of the model
149 struct GNUNET_TESTBED_UnderlayLinkModel *
150 GNUNET_TESTBED_underlaylinkmodel_create(struct GNUNET_TESTBED_Peer *peer,
151 enum GNUNET_TESTBED_UnderlayLinkModelType type)
153 struct GNUNET_TESTBED_UnderlayLinkModel *m;
155 GNUNET_assert(0 == peer->underlay_model_exists);
156 m = GNUNET_new(struct GNUNET_TESTBED_UnderlayLinkModel);
157 peer->underlay_model_exists = 1;
164 * Add a peer to the given model. Underlay connections to the given peer will
165 * be permitted if the model is whitelist based; otherwise they will not be
169 * @param model the model
170 * @param peer the peer to add
173 GNUNET_TESTBED_underlaylinkmodel_add_peer(struct GNUNET_TESTBED_UnderlayLinkModel *model,
174 struct GNUNET_TESTBED_Peer *peer)
176 struct ListEntry *entry;
178 entry = GNUNET_new(struct ListEntry);
180 entry->next = model->entries;
181 model->entries = entry;
186 * Set the metrics for a link to the given peer in the underlay model. The link
187 * SHOULD be permittable according to the given model.
190 * @param model the model
191 * @param peer the other end peer of the link
192 * @param latency latency of the link in microseconds
193 * @param loss data loss of the link expressed as a percentage
194 * @param bandwidth bandwidth of the link in kilobytes per second [kB/s]
197 GNUNET_TESTBED_underlaylinkmodel_set_link(struct GNUNET_TESTBED_UnderlayLinkModel *model,
198 struct GNUNET_TESTBED_Peer *peer,
203 struct LinkProperty *prop;
205 prop = GNUNET_new(struct LinkProperty);
207 prop->latency = latency;
209 prop->bandwidth = bandwidth;
210 prop->next = model->props;
216 * Free the resources of the model. Use this function only if the model has not
217 * be committed and has to be unallocated. The peer can then have another model
221 * @param model the model to unallocate
224 GNUNET_TESTBED_underlaylinkmodel_free(struct GNUNET_TESTBED_UnderlayLinkModel *model)
226 model->peer->underlay_model_exists = 0;
228 free_link_properties(model);
234 * Commit the model. The model is freed in this function(!).
237 * @param model the model to commit
240 GNUNET_TESTBED_underlaylinkmodel_commit(struct GNUNET_TESTBED_UnderlayLinkModel *model)
242 /* FIXME: Marshal the model into a message */
244 /* do not reset the value of model->peer->underlay_model_exists */
246 free_link_properties(model);