#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 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_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
*/
GNUNET_SCHEDULER_TaskIdentifier task;
+ /**
+ * Task retrieving interfaces from the system
+ */
+
+ GNUNET_SCHEDULER_TaskIdentifier interface_task;
+
+
/**
* Size of the session array.
*/
find_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
const struct GNUNET_PeerIdentity *peer)
{
-#if DEBUG_ATS
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Find session %u from peer %s in %p\n",
- (unsigned int) session_id,
- GNUNET_i2s (peer),
- sh);
-#endif
+
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
+ "Find session %u from peer %s in %p\n",
+ (unsigned int) session_id, GNUNET_i2s (peer), sh);
+
if (session_id >= sh->session_array_size)
{
GNUNET_break (0);
if (sh->session_array[session_id].session == NULL)
{
GNUNET_break (0 ==
- memcmp (peer, &sh->session_array[session_id].peer,
- sizeof (struct GNUNET_PeerIdentity)));
+ memcmp (peer, &sh->session_array[session_id].peer,
+ sizeof (struct GNUNET_PeerIdentity)));
return NULL;
}
if (0 !=
memcmp (peer, &sh->session_array[session_id].peer,
- sizeof (struct GNUNET_PeerIdentity)))
+ sizeof (struct GNUNET_PeerIdentity)))
{
GNUNET_break (0);
sh->reconnect = GNUNET_YES;
unsigned int i;
unsigned int f;
-#if DEBUG_ATS
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Get session ID for session %p from peer %s in %p\n",
- session,
- GNUNET_i2s (peer),
- sh);
-#endif
+
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
+ "Get session ID for session %p from peer %s in %p\n", session,
+ GNUNET_i2s (peer), sh);
+
if (NULL == session)
return 0;
f = 0;
{
if (session == sh->session_array[i].session)
{
- GNUNET_assert (0 == memcmp (peer, &sh->session_array[i].peer,
- sizeof (struct GNUNET_PeerIdentity)));
+ GNUNET_assert (0 ==
+ memcmp (peer, &sh->session_array[i].peer,
+ sizeof (struct GNUNET_PeerIdentity)));
return i;
}
if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
sh->session_array[f].session = session;
sh->session_array[f].peer = *peer;
sh->session_array[f].slot_used = GNUNET_YES;
-#if DEBUG_ATS
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Assigning session ID %u for session %p of peer %s in %p\n",
- f,
- session,
- GNUNET_i2s (peer),
- sh);
-#endif
+
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
+ "Assigning session ID %u for session %p of peer %s in %p\n", f,
+ session, GNUNET_i2s (peer), sh);
+
return f;
}
remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
const struct GNUNET_PeerIdentity *peer)
{
-#if DEBUG_ATS
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Remove sessionID %u from peer %s in %p\n",
- (unsigned int) session_id,
- GNUNET_i2s (peer),
- sh);
-#endif
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
+ "Remove sessionID %u from peer %s in %p\n", "ats-scheduling-api",
+ (unsigned int) session_id, GNUNET_i2s (peer), sh);
if (0 == session_id)
return;
GNUNET_assert (session_id < sh->session_array_size);
GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
- GNUNET_assert (0 == memcmp (peer, &sh->session_array[session_id].peer,
+ GNUNET_assert (0 ==
+ memcmp (peer, &sh->session_array[session_id].peer,
sizeof (struct GNUNET_PeerIdentity)));
sh->session_array[session_id].session = NULL;
}
release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
const struct GNUNET_PeerIdentity *peer)
{
-#if DEBUG_ATS
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Release sessionID %u from peer %s in %p\n",
- (unsigned int) session_id,
- GNUNET_i2s (peer),
- sh);
-#endif
+
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
+ "Release sessionID %u from peer %s in %p\n",
+ (unsigned int) session_id, GNUNET_i2s (peer), sh);
+
if (session_id >= sh->session_array_size)
{
GNUNET_break (0);
/* this slot should have been removed from remove_session before */
GNUNET_assert (sh->session_array[session_id].session == NULL);
- if (0 != memcmp (peer, &sh->session_array[session_id].peer,
+ if (0 !=
+ memcmp (peer, &sh->session_array[session_id].peer,
sizeof (struct GNUNET_PeerIdentity)))
{
GNUNET_break (0);
uint16_t plugin_name_length;
uint32_t ats_count;
struct GNUNET_HELLO_Address address;
- struct Session * s;
+ struct Session *s;
if (NULL == msg)
{
force_reconnect (sh);
return;
}
- uint32_t session_id = ntohl (m->session_id);
+ uint32_t session_id = ntohl (m->session_id);
if (session_id == 0)
s = NULL;
s = find_session (sh, session_id, &m->peer);
if (s == NULL)
{
-#if DEBUG_ATS
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "ATS tries to use outdated session `%s'\n",
- GNUNET_i2s(&m->peer));
-#endif
+
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
+ "ATS tries to use outdated session `%s'\n",
+ GNUNET_i2s (&m->peer));
GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
GNUNET_TIME_UNIT_FOREVER_REL);
return;
address.address = plugin_address;
address.address_length = plugin_address_length;
address.transport_name = plugin_name;
+
+ if ((s == NULL) && (0 == address.address_length))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "ATS returned invalid address for peer `%s' transport `%s' address length %i, session_id %i\n",
+ GNUNET_i2s (&address.peer), address.transport_name,
+ plugin_address_length, session_id);
+ GNUNET_break_op (0);
+ GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
+ GNUNET_TIME_UNIT_FOREVER_REL);
+ return;
+ }
+
sh->suggest_cb (sh->suggest_cb_cls, &address, s, m->bandwidth_out,
m->bandwidth_in, atsi, ats_count);
do_transmit (sh);
}
+/**
+ * delete the current network list
+ */
+
+static void
+delete_networks (struct GNUNET_ATS_SchedulingHandle *sh)
+{
+ struct ATS_Network * cur = sh->net_head;
+ while (cur != NULL)
+ {
+ GNUNET_CONTAINER_DLL_remove(sh->net_head, sh->net_tail, cur);
+ GNUNET_free (cur);
+ cur = sh->net_head;
+ }
+}
+
+
+static int
+interface_proc (void *cls, const char *name,
+ int isDefault,
+ const struct sockaddr *
+ addr,
+ const struct sockaddr *
+ broadcast_addr,
+ const struct sockaddr *
+ netmask, socklen_t addrlen)
+{
+ struct GNUNET_ATS_SchedulingHandle * sh = cls;
+ /* Calculate network */
+ struct ATS_Network *net = NULL;
+
+ /* Skipping IPv4 loopback addresses since we have special check */
+ if (addr->sa_family == AF_INET)
+ {
+ struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
+
+ if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
+ return GNUNET_OK;
+ }
+ /* Skipping IPv6 loopback addresses since we have special check */
+ if (addr->sa_family == AF_INET6)
+ {
+ struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
+ if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
+ return GNUNET_OK;
+ }
+
+ if (addr->sa_family == AF_INET)
+ {
+ struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
+ struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
+ struct sockaddr_in *tmp = NULL;
+ struct sockaddr_in network4;
+
+ net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
+ tmp = (struct sockaddr_in *) &net[1];
+ net->network = (struct sockaddr *) &tmp[0];
+ net->netmask = (struct sockaddr *) &tmp[1];
+ net->length = addrlen;
+
+ memset (&network4, 0, sizeof (network4));
+ network4.sin_family = AF_INET;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ network4.sin_len = sizeof (network4);
+#endif
+ network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
+
+ memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
+ memcpy (net->network, &network4, sizeof (struct sockaddr_in));
+ }
+
+ if (addr->sa_family == AF_INET6)
+ {
+ struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
+ struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask;
+ struct sockaddr_in6 * tmp = NULL;
+ struct sockaddr_in6 network6;
+
+ net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
+ tmp = (struct sockaddr_in6 *) &net[1];
+ net->network = (struct sockaddr *) &tmp[0];
+ net->netmask = (struct sockaddr *) &tmp[1];
+ net->length = addrlen;
+
+ memset (&network6, 0, sizeof (network6));
+ network6.sin6_family = AF_INET6;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ network6.sin6_len = sizeof (network6);
+#endif
+ int c = 0;
+ uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
+ uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
+ uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
+ for (c = 0; c < 4; c++)
+ net_elem[c] = addr_elem[c] & mask_elem[c];
+
+ memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
+ memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
+ }
+
+ /* Store in list */
+ if (net != NULL)
+ {
+#if VERBOSE_ATS
+ char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
+ GNUNET_a2s((struct sockaddr *) net->network, addrlen),
+ netmask);
+ GNUNET_free (netmask);
+# endif
+ GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
+ }
+ return GNUNET_OK;
+}
+
+
+
+/**
+ * Periodically get list of addresses
+ * @param cls closure
+ * @param tc Task context
+ */
+static void
+get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_ATS_SchedulingHandle * sh = cls;
+ sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
+ delete_networks (sh);
+ GNUNET_OS_network_interfaces_list(interface_proc, sh);
+ sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
+ get_addresses,
+ sh);
+}
+
+/**
+ * Returns where the address is located: LAN or WAN or ...
+ * @param sh the scheduling handle
+ * @param addr address
+ * @param addrlen address length
+ * @return location as GNUNET_ATS_Information
+ */
+
+const struct GNUNET_ATS_Information
+GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
+{
+ GNUNET_assert (sh != NULL);
+ struct GNUNET_ATS_Information ats;
+ struct ATS_Network * cur = sh->net_head;
+ int type = GNUNET_ATS_NET_UNSPECIFIED;
+
+ if (addr->sa_family == AF_UNIX)
+ {
+ type = GNUNET_ATS_NET_LOOPBACK;
+ }
+
+ /* IPv4 loopback check */
+ if (addr->sa_family == AF_INET)
+ {
+ struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
+
+ if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
+ type = GNUNET_ATS_NET_LOOPBACK;
+ }
+ /* IPv6 loopback check */
+ if (addr->sa_family == AF_INET6)
+ {
+ struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
+ if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
+ type = GNUNET_ATS_NET_LOOPBACK;
+ }
+
+ /* Check local networks */
+ while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
+ {
+ if (addrlen != cur->length)
+ {
+ cur = cur->next;
+ continue;
+ }
+
+ if (addr->sa_family == AF_INET)
+ {
+ struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
+ struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
+ struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
+
+ if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
+ {
+ char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
+ "`%s' is in network `%s'\n",
+ GNUNET_a2s ((const struct sockaddr *)a4, addrlen),
+ net);
+ GNUNET_free (net);
+ type = GNUNET_ATS_NET_LAN;
+ }
+ }
+ if (addr->sa_family == AF_INET6)
+ {
+ struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
+ struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
+ struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
+
+ int res = GNUNET_YES;
+ int c = 0;
+ uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
+ uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
+ uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
+ for (c = 0; c < 4; c++)
+ if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
+ res = GNUNET_NO;
+
+ if (res == GNUNET_YES)
+ {
+ char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net6, addrlen));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
+ GNUNET_a2s ((const struct sockaddr *) a6, addrlen),
+ net);
+ GNUNET_free (net);
+ type = GNUNET_ATS_NET_LAN;
+ }
+ }
+ cur = cur->next;
+ }
+
+ /* no local network found for this address, default: WAN */
+ if (type == GNUNET_ATS_NET_UNSPECIFIED)
+ type = GNUNET_ATS_NET_WAN;
+
+#if VERBOSE
+ const char * range;
+ switch (type) {
+ case GNUNET_ATS_NET_WAN:
+ range = "WAN";
+ break;
+ case GNUNET_ATS_NET_LAN:
+ range = "LAN";
+ break;
+ case GNUNET_ATS_NET_LOOPBACK:
+ range = "LOOPBACK";
+ break;
+ default:
+
+ break;
+ }
+#endif
+
+ ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
+ ats.value = htonl (type);
+ return (const struct GNUNET_ATS_Information) ats;
+}
/**
* Initialize the ATS subsystem.
sh->suggest_cb = suggest_cb;
sh->suggest_cb_cls = suggest_cb_cls;
GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
+ GNUNET_OS_network_interfaces_list(interface_proc, sh);
+ sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
+ get_addresses,
+ sh);
reconnect (sh);
return sh;
}
GNUNET_SCHEDULER_cancel (sh->task);
sh->task = GNUNET_SCHEDULER_NO_TASK;
}
+
+ delete_networks (sh);
+ if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
+ {
+ GNUNET_SCHEDULER_cancel(sh->interface_task);
+ sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
+ }
GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
GNUNET_free (sh);
+ sh = NULL;
}
size_t namelen;
size_t msize;
- namelen = (address->transport_name == NULL) ? 0 : strlen (address->transport_name) + 1;
+ if (address == NULL)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ if ((address == NULL) && (session == NULL))
+ {
+ GNUNET_break (0);
+ return;
+ }
+
+ namelen =
+ (address->transport_name ==
+ NULL) ? 0 : strlen (address->transport_name) + 1;
msize =
sizeof (struct AddressUpdateMessage) + address->address_length +
ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
void
GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
const struct GNUNET_HELLO_Address *address,
- struct Session *session,
- int in_use)
+ struct Session *session, int in_use)
{
struct PendingMessage *p;
struct AddressUseMessage *m;
size_t msize;
GNUNET_assert (NULL != address);
- namelen = (address->transport_name == NULL) ? 0 : strlen (address->transport_name) + 1;
+ namelen =
+ (address->transport_name ==
+ NULL) ? 0 : strlen (address->transport_name) + 1;
msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
(address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
GNUNET_assert (address->transport_name != NULL);
namelen = strlen (address->transport_name) + 1;
GNUNET_assert (namelen > 1);
- msize = sizeof (struct AddressDestroyedMessage) + address->address_length + namelen;
+ msize =
+ sizeof (struct AddressDestroyedMessage) + address->address_length +
+ namelen;
if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
(address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
(namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))