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.
17 * @file testbed/testbed_api_underlay.c
18 * @brief testbed underlay API implementation
19 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
22 #include "testbed_api_peers.h"
31 * next pointer for list
33 struct LinkProperty *next;
36 * the peer whose link is defined by these properties
38 struct GNUNET_TESTBED_Peer *peer;
41 * latency of the link in microseconds
46 * data loss on the link expressed as percentage
51 * bandwidth of the link in kilobytes per second
58 * Container for holding a peer in whitelist/blacklist
65 struct ListEntry *next;
70 struct GNUNET_TESTBED_Peer *peer;
75 * Model for configuring underlay links of a peer
78 struct GNUNET_TESTBED_UnderlayLinkModel
81 * The peer associated with this model
83 struct GNUNET_TESTBED_Peer *peer;
86 * List of peers in the list
88 struct ListEntry *entries;
91 * list of link properties
93 struct LinkProperty *props;
96 * the type of this model
98 enum GNUNET_TESTBED_UnderlayLinkModelType type;
103 * Function to free resources of list entries
105 * @param model the model
108 free_entries (struct GNUNET_TESTBED_UnderlayLinkModel *model)
112 while (NULL != (e = model->entries))
114 model->entries = e->next;
121 * Function to free resources of link properties added to the given model
123 * @param model the model
126 free_link_properties (struct GNUNET_TESTBED_UnderlayLinkModel *model)
128 struct LinkProperty *p;
130 while (NULL != (p = model->props))
132 model->props = p->next;
139 * Create a GNUNET_TESTBED_UnderlayLinkModel for the given peer. A peer can
140 * have ONLY ONE model and it can be either a blacklist or whitelist based one.
143 * @param peer the peer for which the model has to be created
144 * @param type the type of the model
147 struct GNUNET_TESTBED_UnderlayLinkModel *
148 GNUNET_TESTBED_underlaylinkmodel_create (struct GNUNET_TESTBED_Peer *peer,
149 enum GNUNET_TESTBED_UnderlayLinkModelType type)
151 struct GNUNET_TESTBED_UnderlayLinkModel *m;
153 GNUNET_assert (0 == peer->underlay_model_exists);
154 m = GNUNET_new (struct GNUNET_TESTBED_UnderlayLinkModel);
155 peer->underlay_model_exists = 1;
162 * Add a peer to the given model. Underlay connections to the given peer will
163 * be permitted if the model is whitelist based; otherwise they will not be
167 * @param model the model
168 * @param peer the peer to add
171 GNUNET_TESTBED_underlaylinkmodel_add_peer (struct GNUNET_TESTBED_UnderlayLinkModel *model,
172 struct GNUNET_TESTBED_Peer *peer)
174 struct ListEntry *entry;
176 entry = GNUNET_new (struct ListEntry);
178 entry->next = model->entries;
179 model->entries = entry;
184 * Set the metrics for a link to the given peer in the underlay model. The link
185 * SHOULD be permittable according to the given model.
188 * @param model the model
189 * @param peer the other end peer of the link
190 * @param latency latency of the link in microseconds
191 * @param loss data loss of the link expressed as a percentage
192 * @param bandwidth bandwidth of the link in kilobytes per second [kB/s]
195 GNUNET_TESTBED_underlaylinkmodel_set_link (struct GNUNET_TESTBED_UnderlayLinkModel *model,
196 struct GNUNET_TESTBED_Peer *peer,
201 struct LinkProperty *prop;
203 prop = GNUNET_new (struct LinkProperty);
205 prop->latency = latency;
207 prop->bandwidth = bandwidth;
208 prop->next = model->props;
214 * Free the resources of the model. Use this function only if the model has not
215 * be committed and has to be unallocated. The peer can then have another model
219 * @param model the model to unallocate
222 GNUNET_TESTBED_underlaylinkmodel_free (struct GNUNET_TESTBED_UnderlayLinkModel *model)
224 model->peer->underlay_model_exists = 0;
225 free_entries (model);
226 free_link_properties (model);
232 * Commit the model. The model is freed in this function(!).
235 * @param model the model to commit
238 GNUNET_TESTBED_underlaylinkmodel_commit (struct GNUNET_TESTBED_UnderlayLinkModel *model)
240 /* FIXME: Marshal the model into a message */
242 /* do not reset the value of model->peer->underlay_model_exists */
243 free_entries (model);
244 free_link_properties (model);