/*
This file is part of GNUnet.
(C) 2010,2011 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
* @file ats/ats_api_scheduling.c
* @brief automatic transport selection and outbound bandwidth determination
* @author Christian Grothoff
* @author Matthias Wachs
*/
#include "platform.h"
#include "gnunet_ats_service.h"
#include "ats.h"
#define DEBUG_ATS GNUNET_EXTRA_LOGGING
#define INTERFACE_PROCESSING_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
/**
* Message in linked list we should send to the ATS service. The
* actual binary message follows this struct.
*/
struct PendingMessage
{
/**
* Kept in a DLL.
*/
struct PendingMessage *next;
/**
* Kept in a DLL.
*/
struct PendingMessage *prev;
/**
* Size of the message.
*/
size_t size;
/**
* Is this the 'ATS_START' message?
*/
int is_init;
};
/**
* Information we track per session.
*/
struct SessionRecord
{
/**
* Identity of the peer (just needed for error checking).
*/
struct GNUNET_PeerIdentity peer;
/**
* Session handle.
*/
struct Session *session;
/**
* Set to GNUNET_YES if the slot is used.
*/
int slot_used;
};
struct ATS_Network
{
struct ATS_Network * next;
struct ATS_Network * prev;
struct sockaddr *network;
struct sockaddr *netmask;
socklen_t length;
};
/**
* Handle to the ATS subsystem for bandwidth/transport scheduling information.
*/
struct GNUNET_ATS_SchedulingHandle
{
/**
* Our configuration.
*/
const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
* Callback to invoke on suggestions.
*/
GNUNET_ATS_AddressSuggestionCallback suggest_cb;
/**
* Closure for 'suggest_cb'.
*/
void *suggest_cb_cls;
/**
* Connection to ATS service.
*/
struct GNUNET_CLIENT_Connection *client;
/**
* Head of list of messages for the ATS service.
*/
struct PendingMessage *pending_head;
/**
* Tail of list of messages for the ATS service
*/
struct PendingMessage *pending_tail;
/**
* Current request for transmission to ATS.
*/
struct GNUNET_CLIENT_TransmitHandle *th;
/**
* Head of network list
*/
struct ATS_Network * net_head;
/**
* Tail of network list
*/
struct ATS_Network * net_tail;
/**
* Array of session objects (we need to translate them to numbers and back
* for the protocol; the offset in the array is the session number on the
* network). Index 0 is always NULL and reserved to represent the NULL pointer.
* Unused entries are also NULL.
*/
struct SessionRecord *session_array;
/**
* Task to trigger reconnect.
*/
GNUNET_SCHEDULER_TaskIdentifier task;
/**
* Task retrieving interfaces from the system
*/
GNUNET_SCHEDULER_TaskIdentifier interface_task;
/**
* Size of the session array.
*/
unsigned int session_array_size;
/**
* Should we reconnect to ATS due to some serious error?
*/
int reconnect;
};
/**
* Re-establish the connection to the ATS service.
*
* @param sh handle to use to re-connect.
*/
static void
reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
/**
* Re-establish the connection to the ATS service.
*
* @param cls handle to use to re-connect.
* @param tc scheduler context
*/
static void
reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct GNUNET_ATS_SchedulingHandle *sh = cls;
sh->task = GNUNET_SCHEDULER_NO_TASK;
reconnect (sh);
}
/**
* Disconnect from ATS and then reconnect.
*
* @param sh our handle
*/
static void
force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
{
sh->reconnect = GNUNET_NO;
GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
sh->client = NULL;
sh->task =
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
sh);
}
/**
* Transmit messages from the message queue to the service
* (if there are any, and if we are not already trying).
*
* @param sh handle to use
*/
static void
do_transmit (struct GNUNET_ATS_SchedulingHandle *sh);
/**
* Type of a function to call when we receive a message
* from the service.
*
* @param cls the 'struct GNUNET_ATS_SchedulingHandle'
* @param msg message received, NULL on timeout or fatal error
*/
static void
process_ats_message (