- use tunnel encryption state to select decryption key
[oweals/gnunet.git] / src / testbed / testbed_api_underlay.c
1 /*
2   This file is part of GNUnet.
3   (C) 2008--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 testbed/testbed_api_underlay.c
23  * @brief testbed underlay API implementation
24  * @author Sree Harsha Totakura <sreeharsha@totakura.in> 
25  */
26
27 #include "testbed_api_peers.h"
28
29
30 /**
31  * An underlay link
32  */
33 struct LinkProperty
34 {
35   /**
36    * next pointer for list
37    */
38   struct LinkProperty *next;
39
40   /**
41    * the peer whose link is defined by these properties
42    */
43   struct GNUNET_TESTBED_Peer *peer;
44
45   /**
46    * latency of the link in microseconds
47    */
48   uint32_t latency;
49   
50   /**
51    * data loss on the link expressed as percentage
52    */
53   uint32_t loss;
54
55   /**
56    * bandwidth of the link in kilobytes per second
57    */
58   uint32_t bandwidth;
59 };
60
61
62 /**
63  * Container for holding a peer in whitelist/blacklist
64  */
65 struct ListEntry
66 {
67   /**
68    * the next pointer
69    */
70   struct ListEntry *next;
71
72   /**
73    * the peer
74    */
75   struct GNUNET_TESTBED_Peer *peer;
76 };
77
78
79 /**
80  * Model for configuring underlay links of a peer
81  * @ingroup underlay
82  */
83 struct GNUNET_TESTBED_UnderlayLinkModel
84 {
85   /**
86    * The peer associated with this model
87    */
88   struct GNUNET_TESTBED_Peer *peer;
89
90   /**
91    * List of peers in the list
92    */
93   struct ListEntry *entries;
94
95   /**
96    * list of link properties
97    */
98   struct LinkProperty *props;
99
100   /**
101    * the type of this model
102    */
103   enum GNUNET_TESTBED_UnderlayLinkModelType type;
104 }
105
106
107 /**
108  * Function to free resources of list entries
109  *
110  * @param model the model
111  */
112 static void
113 free_entries (struct GNUNET_TESTBED_UnderlayLinkModel *model)
114 {
115   struct ListEntry *e;
116
117   while (NULL != (e = model->entries))
118   {
119     model->entries = e->next;
120     GNUNET_free (e);
121   }
122 }
123
124
125 /**
126  * Function to free resources of link properties added to the given model
127  *
128  * @param model the model
129  */
130 static void
131 free_link_properties (struct GNUNET_TESTBED_UnderlayLinkModel *model)
132 {
133   struct LinkProperty *p;
134
135   while (NULL != (p = model->props))
136   {
137     model->props = p->next;
138     GNUNET_free (p);
139   }
140 }
141
142
143 /**
144  * Create a GNUNET_TESTBED_UnderlayLinkModel for the given peer.  A peer can
145  * have ONLY ONE model and it can be either a blacklist or whitelist based one.
146  *
147  * @ingroup underlay
148  * @param peer the peer for which the model has to be created
149  * @param type the type of the model
150  * @return the model
151  */
152 struct GNUNET_TESTBED_UnderlayLinkModel *
153 GNUNET_TESTBED_underlaylinkmodel_create (struct GNUNET_TESTBED_Peer *peer,
154                                          enum GNUNET_TESTBED_UnderlayLinkModelType type)
155 {
156   struct GNUNET_TESTBED_UnderlayLinkModel *m;
157   
158   GNUNET_assert (0 == peer->underlay_model_exists);
159   m = GNUNET_new (struct GNUNET_TESTBED_UnderlayLinkModel);
160   peer->underlay_model_exists = 1;
161   m->type = type;
162   return m;
163 }
164
165
166 /**
167  * Add a peer to the given model.  Underlay connections to the given peer will
168  * be permitted if the model is whitelist based; otherwise they will not be
169  * permitted.
170  *
171  * @ingroup underlay
172  * @param model the model
173  * @param peer the peer to add
174  */
175 void
176 GNUNET_TESTBED_underlaylinkmodel_add_peer (struct GNUNET_TESTBED_UnderlayLinkModel *model,
177                                            struct GNUNET_TESTBED_Peer *peer)
178 {
179   struct ListEntry *entry;
180
181   entry = GNUNET_new (struct ListEntry);
182   entry->peer = peer;
183   entry->next = model->entries;
184   model->entries = entry;
185 }
186
187
188 /**
189  * Set the metrics for a link to the given peer in the underlay model.  The link
190  * SHOULD be permittable according to the given model.
191  * 
192  * @ingroup underlay
193  * @param model the model
194  * @param peer the other end peer of the link
195  * @param latency latency of the link in microseconds
196  * @param loss data loss of the link expressed as a percentage
197  * @param bandwidth bandwidth of the link in kilobytes per second [kB/s]
198  */
199 void
200 GNUNET_TESTBED_underlaylinkmodel_set_link (struct GNUNET_TESTBED_UnderlayLinkModel *model,
201                                            struct GNUNET_TESTBED_Peer *peer,
202                                            uint32_t latency,
203                                            uint32_t loss,
204                                            uint32_t bandwidth)
205 {
206   struct LinkProperty *prop;
207
208   prop = GNUNET_new (struct LinkProperty);
209   prop->peer = peer;
210   prop->latency = latency;
211   prop->loss = loss;
212   prop->bandwidth = bandwidth;
213   prop->next = model->props;
214   model->props = prop;
215 }
216
217
218 /**
219  * Free the resources of the model.  Use this function only if the model has not
220  * be committed and has to be unallocated.  The peer can then have another model
221  * created.
222  *
223  * @ingroup underlay
224  * @param model the model to unallocate
225  */
226 void
227 GNUNET_TESTBED_underlaylinkmodel_free (struct GNUNET_TESTBED_UnderlayLinkModel *model)
228 {
229   model->peer->underlay_model_exists = 0;
230   free_entries (model);
231   free_link_properties (model);
232   gnunet_free (model);
233 }
234
235
236 /**
237  * Commit the model.  The model is freed in this function(!).
238  *
239  * @ingroup underlay
240  * @param model the model to commit
241  */
242 void
243 GNUNET_TESTBED_underlaylinkmodel_commit (struct GNUNET_TESTBED_UnderlayLinkModel *model)
244 {
245   /* FIXME: Marshal the model into a message */
246   GNUNET_break (0);
247   /* do not reset the value of model->peer->underlay_model_exists */
248   free_entries (model);
249   free_link_properties (model);
250   GNUNET_free (model);
251 }