2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016 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 transport/transport_api_manipulation.c
23 * @brief library to access the low-level P2P IO service
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_transport_service.h"
33 #include "transport.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "transport-api",__VA_ARGS__)
39 * Handle for the transport service (includes all of the
40 * state for the transport service).
42 struct GNUNET_TRANSPORT_ManipulationHandle
46 * My client connection to the transport service.
48 struct GNUNET_MQ_Handle *mq;
53 const struct GNUNET_CONFIGURATION_Handle *cfg;
56 * ID of the task trying to reconnect to the service.
58 struct GNUNET_SCHEDULER_Task *reconnect_task;
61 * Delay until we try to reconnect.
63 struct GNUNET_TIME_Relative reconnect_delay;
66 * Reconnect in progress
73 * Function that will schedule the job that will try
74 * to connect us again to the client.
76 * @param h transport service to reconnect
79 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_ManipulationHandle *h);
83 * Generic error handler, called with the appropriate
84 * error code and the same closure specified at the creation of
86 * Not every message queue implementation supports an error handler.
88 * @param cls closure with the `struct GNUNET_TRANSPORT_ManipulationHandle *`
89 * @param error error code
92 mq_error_handler (void *cls,
93 enum GNUNET_MQ_Error error)
95 struct GNUNET_TRANSPORT_ManipulationHandle *h = cls;
97 LOG (GNUNET_ERROR_TYPE_DEBUG,
98 "Error receiving from transport service, disconnecting temporarily.\n");
99 h->reconnecting = GNUNET_YES;
100 disconnect_and_schedule_reconnect (h);
105 * Try again to connect to transport service.
107 * @param cls the handle to the transport service
110 reconnect (void *cls)
112 struct GNUNET_TRANSPORT_ManipulationHandle *h = cls;
113 struct GNUNET_MQ_MessageHandler handlers[] = {
114 GNUNET_MQ_handler_end ()
116 struct GNUNET_MQ_Envelope *env;
117 struct StartMessage *s;
119 h->reconnect_task = NULL;
120 LOG (GNUNET_ERROR_TYPE_DEBUG,
121 "Connecting to transport service.\n");
122 GNUNET_assert (NULL == h->mq);
123 h->reconnecting = GNUNET_NO;
124 h->mq = GNUNET_CLIENT_connect (h->cfg,
131 env = GNUNET_MQ_msg (s,
132 GNUNET_MESSAGE_TYPE_TRANSPORT_START);
133 GNUNET_MQ_send (h->mq,
139 * Function that will schedule the job that will try
140 * to connect us again to the client.
142 * @param h transport service to reconnect
145 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_ManipulationHandle *h)
147 GNUNET_assert (NULL == h->reconnect_task);
150 GNUNET_MQ_destroy (h->mq);
154 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
157 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
162 * Set transport metrics for a peer and a direction.
164 * @param handle transport handle
165 * @param peer the peer to set the metric for
166 * @param prop the performance metrics to set
167 * @param delay_in inbound delay to introduce
168 * @param delay_out outbound delay to introduce
170 * Note: Delay restrictions in receiving direction will be enforced
171 * with one message delay.
174 GNUNET_TRANSPORT_manipulation_set (struct GNUNET_TRANSPORT_ManipulationHandle *handle,
175 const struct GNUNET_PeerIdentity *peer,
176 const struct GNUNET_ATS_Properties *prop,
177 struct GNUNET_TIME_Relative delay_in,
178 struct GNUNET_TIME_Relative delay_out)
180 struct GNUNET_MQ_Envelope *env;
181 struct TrafficMetricMessage *msg;
183 if (NULL == handle->mq)
185 env = GNUNET_MQ_msg (msg,
186 GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC);
187 msg->reserved = htonl (0);
189 GNUNET_ATS_properties_hton (&msg->properties,
191 msg->delay_in = GNUNET_TIME_relative_hton (delay_in);
192 msg->delay_out = GNUNET_TIME_relative_hton (delay_out);
193 GNUNET_MQ_send (handle->mq,
199 * Connect to the transport service. Note that the connection may
200 * complete (or fail) asynchronously.
202 * @param cfg configuration to use
203 * @return NULL on error
205 struct GNUNET_TRANSPORT_ManipulationHandle *
206 GNUNET_TRANSPORT_manipulation_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
208 struct GNUNET_TRANSPORT_ManipulationHandle *h;
210 h = GNUNET_new (struct GNUNET_TRANSPORT_ManipulationHandle);
212 LOG (GNUNET_ERROR_TYPE_DEBUG,
213 "Connecting to transport service.\n");
225 * Disconnect from the transport service.
227 * @param handle handle to the service as returned from #GNUNET_TRANSPORT_manipulation_connect()
230 GNUNET_TRANSPORT_manipulation_disconnect (struct GNUNET_TRANSPORT_ManipulationHandle *handle)
232 if (NULL == handle->reconnect_task)
233 disconnect_and_schedule_reconnect (handle);
234 /* and now we stop trying to connect again... */
235 if (NULL != handle->reconnect_task)
237 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
238 handle->reconnect_task = NULL;
240 GNUNET_free (handle);
244 /* end of transport_api_manipulation.c */