From 81b92caa24a83620032438df9c5ee3ea51663a02 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 17 Jan 2015 22:20:18 +0000 Subject: simplify ATS API and plugin API by returning the network type, instead of an ATS_Information struct --- src/ats/ats_api_scheduling.c | 350 ++++++++++++---------- src/include/gnunet_ats_service.h | 16 +- src/include/gnunet_transport_plugin.h | 8 +- src/transport/gnunet-service-transport.c | 27 +- src/transport/plugin_transport_http_client.c | 23 +- src/transport/plugin_transport_http_server.c | 18 +- src/transport/plugin_transport_tcp.c | 14 +- src/transport/plugin_transport_udp.c | 20 +- src/transport/plugin_transport_udp_broadcasting.c | 29 +- src/transport/plugin_transport_unix.c | 5 +- 10 files changed, 282 insertions(+), 228 deletions(-) diff --git a/src/ats/ats_api_scheduling.c b/src/ats/ats_api_scheduling.c index d2bf06c17..496a18b12 100644 --- a/src/ats/ats_api_scheduling.c +++ b/src/ats/ats_api_scheduling.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2010,2011 Christian Grothoff (and other contributing authors) + (C) 2010-2015 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 @@ -164,12 +164,12 @@ struct GNUNET_ATS_SchedulingHandle /** * Head of network list */ - struct ATS_Network * net_head; + struct ATS_Network *net_head; /** * Tail of network list */ - struct ATS_Network * net_tail; + struct ATS_Network *net_tail; /** * Array of session objects (we need to translate them to numbers and back @@ -182,16 +182,15 @@ struct GNUNET_ATS_SchedulingHandle /** * Task to trigger reconnect. */ - struct GNUNET_SCHEDULER_Task * task; + struct GNUNET_SCHEDULER_Task *task; /** * Task retrieving interfaces from the system */ - struct GNUNET_SCHEDULER_Task * interface_task; - + struct GNUNET_SCHEDULER_Task *interface_task; /** - * Size of the session array. + * Size of the @e session_array. */ unsigned int session_array_size; @@ -218,7 +217,8 @@ reconnect (struct GNUNET_ATS_SchedulingHandle *sh); * @param tc scheduler context */ static void -reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +reconnect_task (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_ATS_SchedulingHandle *sh = cls; @@ -238,9 +238,9 @@ force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh) sh->reconnect = GNUNET_NO; GNUNET_CLIENT_disconnect (sh->client); sh->client = NULL; - sh->task = - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task, - sh); + sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, + &reconnect_task, + sh); } @@ -258,23 +258,26 @@ 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 cls the `struct GNUNET_ATS_SchedulingHandle` * @param msg message received, NULL on timeout or fatal error */ static void -process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg); +process_ats_message (void *cls, + const struct GNUNET_MessageHeader *msg); /** * We can now transmit a message to ATS. Do it. * - * @param cls the 'struct GNUNET_ATS_SchedulingHandle' + * @param cls the `struct GNUNET_ATS_SchedulingHandle` * @param size number of bytes we can transmit to ATS * @param buf where to copy the messages - * @return number of bytes copied into buf + * @return number of bytes copied into @a buf */ static size_t -transmit_message_to_ats (void *cls, size_t size, void *buf) +transmit_message_to_ats (void *cls, + size_t size, + void *buf) { struct GNUNET_ATS_SchedulingHandle *sh = cls; struct PendingMessage *p; @@ -282,7 +285,7 @@ transmit_message_to_ats (void *cls, size_t size, void *buf) char *cbuf; sh->th = NULL; - if ((size == 0) || (buf == NULL)) + if ((0 == size) || (NULL == buf)) { force_reconnect (sh); return 0; @@ -291,10 +294,14 @@ transmit_message_to_ats (void *cls, size_t size, void *buf) cbuf = buf; while ((NULL != (p = sh->pending_head)) && (p->size <= size)) { - memcpy (&cbuf[ret], &p[1], p->size); + memcpy (&cbuf[ret], + &p[1], + p->size); ret += p->size; size -= p->size; - GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p); + GNUNET_CONTAINER_DLL_remove (sh->pending_head, + sh->pending_tail, + p); GNUNET_free (p); } do_transmit (sh); @@ -336,10 +343,10 @@ do_transmit (struct GNUNET_ATS_SchedulingHandle *sh) * @return the session object (or NULL) */ static struct Session * -find_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id, +find_session (struct GNUNET_ATS_SchedulingHandle *sh, + uint32_t session_id, const struct GNUNET_PeerIdentity *peer) { - 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); @@ -396,16 +403,19 @@ find_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id, * @return the session id */ static uint32_t -find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session, - const struct GNUNET_PeerIdentity *peer) +find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh, + struct Session *session, + const struct GNUNET_PeerIdentity *peer) { unsigned int i; unsigned int f; - 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); - + 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 NOT_FOUND; f = 0; @@ -442,8 +452,9 @@ find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh, struct Session * @return the session id or NOT_FOUND for error */ static uint32_t -find_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session, - const struct GNUNET_PeerIdentity *peer) +find_session_id (struct GNUNET_ATS_SchedulingHandle *sh, + struct Session *session, + const struct GNUNET_PeerIdentity *peer) { unsigned int i; char * p2; @@ -484,15 +495,16 @@ find_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session * @param peer peer the session belongs to */ static void -remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id, +remove_session (struct GNUNET_ATS_SchedulingHandle *sh, + uint32_t session_id, const struct GNUNET_PeerIdentity *peer) { - GNUNET_assert (peer != NULL); - GNUNET_assert (sh != NULL); - - 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); + 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 (0 == session_id) return; @@ -508,7 +520,6 @@ remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id, GNUNET_i2s (peer), session_id); sh->session_array[session_id].session = NULL; - } @@ -521,7 +532,8 @@ remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id, * @param peer peer the session belongs to */ static void -release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id, +release_session (struct GNUNET_ATS_SchedulingHandle *sh, + uint32_t session_id, const struct GNUNET_PeerIdentity *peer) { @@ -565,11 +577,12 @@ process_release_message (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 cls the `struct GNUNET_ATS_SchedulingHandle` * @param msg message received, NULL on timeout or fatal error */ static void -process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg) +process_ats_message (void *cls, + const struct GNUNET_MessageHeader *msg) { struct GNUNET_ATS_SchedulingHandle *sh = cls; const struct AddressSuggestionMessage *m; @@ -688,9 +701,11 @@ reconnect (struct GNUNET_ATS_SchedulingHandle *sh) GNUNET_assert (NULL == sh->client); sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg); GNUNET_assert (NULL != sh->client); - GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh, - GNUNET_TIME_UNIT_FOREVER_REL); - if ((NULL == (p = sh->pending_head)) || (GNUNET_YES != p->is_init)) + GNUNET_CLIENT_receive (sh->client, + &process_ats_message, sh, + GNUNET_TIME_UNIT_FOREVER_REL); + if ( (NULL == (p = sh->pending_head)) || + (GNUNET_YES != p->is_init) ) { p = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct ClientStartMessage)); @@ -700,39 +715,44 @@ reconnect (struct GNUNET_ATS_SchedulingHandle *sh) init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START); init->header.size = htons (sizeof (struct ClientStartMessage)); init->start_flag = htonl (START_FLAG_SCHEDULING); - GNUNET_CONTAINER_DLL_insert (sh->pending_head, sh->pending_tail, p); + GNUNET_CONTAINER_DLL_insert (sh->pending_head, + sh->pending_tail, + p); } do_transmit (sh); } /** - * delete the current network list + * Delete the current network list. + * + * @param sh scheduling handle to clean up */ static void delete_networks (struct GNUNET_ATS_SchedulingHandle *sh) { - struct ATS_Network * cur = sh->net_head; - while (cur != NULL) + struct ATS_Network *cur; + + while (NULL != (cur = sh->net_head)) { - GNUNET_CONTAINER_DLL_remove(sh->net_head, sh->net_tail, cur); + 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, +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) + const struct sockaddr *addr, + const struct sockaddr *broadcast_addr, + const struct sockaddr *netmask, + socklen_t addrlen) { - struct GNUNET_ATS_SchedulingHandle * sh = cls; + struct GNUNET_ATS_SchedulingHandle *sh = cls; /* Calculate network */ struct ATS_Network *net = NULL; @@ -822,19 +842,23 @@ interface_proc (void *cls, const char *name, /** - * Periodically get list of addresses + * Periodically get list of network addresses from our interfaces. + * * @param cls closure * @param tc Task context */ static void -get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +get_addresses (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { - struct GNUNET_ATS_SchedulingHandle * sh = cls; + struct GNUNET_ATS_SchedulingHandle *sh = cls; + sh->interface_task = NULL; delete_networks (sh); - GNUNET_OS_network_interfaces_list(interface_proc, sh); + GNUNET_OS_network_interfaces_list (&interface_proc, + sh); sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL, - get_addresses, + &get_addresses, sh); } @@ -913,37 +937,41 @@ GNUNET_ATS_print_property_type (enum GNUNET_ATS_Property type) * @param sh the scheduling handle * @param addr address * @param addrlen address length - * @return location as GNUNET_ATS_Information + * @return type of the network the address belongs to */ -struct GNUNET_ATS_Information -GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen) +enum GNUNET_ATS_Network_Type +GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle *sh, + const struct sockaddr *addr, + socklen_t addrlen) { - GNUNET_assert (sh != NULL); - struct ATS_Network * cur = sh->net_head; - - int type = GNUNET_ATS_NET_UNSPECIFIED; - struct GNUNET_ATS_Information ats; + struct ATS_Network *cur = sh->net_head; + enum GNUNET_ATS_NetworkType type = GNUNET_ATS_NET_UNSPECIFIED; - if (addr->sa_family == AF_UNIX) - { - type = GNUNET_ATS_NET_LOOPBACK; - } + switch (addr->sa_family) + { + case AF_UNIX: + type = GNUNET_ATS_NET_LOOPBACK; + break; + case AF_INET: + { + const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr; - /* 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; + break; + } + case AF_INET6: + { + const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) 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; - } + if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr)) + type = GNUNET_ATS_NET_LOOPBACK; + break; + } + default: + GNUNET_break (0); + break; + } /* Check local networks */ while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED)) @@ -953,7 +981,6 @@ GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const stru cur = cur->next; continue; } - if (addr->sa_family == AF_INET) { struct sockaddr_in * a4 = (struct sockaddr_in *) addr; @@ -987,14 +1014,13 @@ GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const stru /* no local network found for this address, default: WAN */ if (type == GNUNET_ATS_NET_UNSPECIFIED) type = GNUNET_ATS_NET_WAN; - ats.type = htonl (GNUNET_ATS_NETWORK_TYPE); - ats.value = htonl (type); - - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api", + GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, + "ats-scheduling-api", "`%s' is in network `%s'\n", - GNUNET_a2s ((const struct sockaddr *) addr, addrlen), - GNUNET_ATS_print_network_type(type)); - return ats; + GNUNET_a2s (addr, + addrlen), + GNUNET_ATS_print_network_type (type)); + return type; } @@ -1003,7 +1029,7 @@ GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const stru * * @param cfg configuration to use * @param suggest_cb notification to call whenever the suggestation changed - * @param suggest_cb_cls closure for 'suggest_cb' + * @param suggest_cb_cls closure for @a suggest_cb * @return ats context */ struct GNUNET_ATS_SchedulingHandle * @@ -1017,11 +1043,14 @@ GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg, sh->cfg = cfg; 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); + 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); + &get_addresses, + sh); reconnect (sh); return sh; } @@ -1037,10 +1066,12 @@ GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh) { struct PendingMessage *p; struct GNUNET_ATS_SuggestHandle *cur; - struct GNUNET_ATS_SuggestHandle *next; + while (NULL != (p = sh->pending_head)) { - GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p); + GNUNET_CONTAINER_DLL_remove (sh->pending_head, + sh->pending_tail, + p); GNUNET_free (p); } if (NULL != sh->client) @@ -1053,26 +1084,26 @@ GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh) GNUNET_SCHEDULER_cancel (sh->task); sh->task = NULL; } - - next = sh->sug_head; - while (NULL != (cur = next)) + while (NULL != (cur = sh->sug_head)) { - next = cur->next; - GNUNET_CONTAINER_DLL_remove (sh->sug_head, sh->sug_tail, cur); - GNUNET_free (cur); + GNUNET_CONTAINER_DLL_remove (sh->sug_head, + sh->sug_tail, + cur); + GNUNET_free (cur); } - delete_networks (sh); - if (sh->interface_task != NULL) + if (NULL != sh->interface_task) { - GNUNET_SCHEDULER_cancel(sh->interface_task); + GNUNET_SCHEDULER_cancel (sh->interface_task); sh->interface_task = NULL; } - GNUNET_array_grow (sh->session_array, sh->session_array_size, 0); + GNUNET_array_grow (sh->session_array, + sh->session_array_size, + 0); GNUNET_free (sh); - sh = NULL; } + /** * We would like to reset the address suggestion block time for this * peer @@ -1096,7 +1127,9 @@ GNUNET_ATS_reset_backoff (struct GNUNET_ATS_SchedulingHandle *sh, m->header.size = htons (sizeof (struct ResetBackoffMessage)); m->reserved = htonl (0); m->peer = *peer; - GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p); + GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, + sh->pending_tail, + p); do_transmit (sh); } @@ -1157,18 +1190,17 @@ GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh, struct GNUNET_ATS_SuggestHandle *s; for (s = sh->sug_head; NULL != s; s = s->next) - if (0 == memcmp(peer, &s->id, sizeof (s->id))) - break; + if (0 == memcmp (peer, &s->id, sizeof (s->id))) + break; if (NULL == s) { - GNUNET_break (0); - return; - } - else - { - GNUNET_CONTAINER_DLL_remove (sh->sug_head, sh->sug_tail, s); - GNUNET_free (s); + GNUNET_break (0); + return; } + GNUNET_CONTAINER_DLL_remove (sh->sug_head, + sh->sug_tail, + s); + GNUNET_free (s); p = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct RequestAddressMessage)); @@ -1179,7 +1211,9 @@ GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh, m->header.size = htons (sizeof (struct RequestAddressMessage)); m->reserved = htonl (0); m->peer = *peer; - GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p); + GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, + sh->pending_tail, + p); do_transmit (sh); } @@ -1190,26 +1224,23 @@ GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh, * @param sh the scheduling handle * @param address the address * @param session the session - * @return GNUNET_YES or GNUNET_NO + * @return #GNUNET_YES or #GNUNET_NO */ int GNUNET_ATS_session_known (struct GNUNET_ATS_SchedulingHandle *sh, - const struct GNUNET_HELLO_Address *address, - struct Session *session) + const struct GNUNET_HELLO_Address *address, + struct Session *session) { - int s; - if (NULL != session) - { - if (NOT_FOUND != (s = find_session_id (sh, session, &address->peer))) - { - /* Existing */ - return GNUNET_YES; - } + if (NULL == session) return GNUNET_NO; - } + if (NOT_FOUND != find_session_id (sh, + session, + &address->peer)) + return GNUNET_YES; /* Exists */ return GNUNET_NO; } + /** * We have a new address ATS should know. Addresses have to be added with this * function before they can be: updated, set in use and destroyed @@ -1218,8 +1249,8 @@ GNUNET_ATS_session_known (struct GNUNET_ATS_SchedulingHandle *sh, * @param address the address * @param session session handle, can be NULL * @param ats performance data for the address - * @param ats_count number of performance records in 'ats' - * @return GNUNET_OK on success, GNUNET_SYSERR on error + * @param ats_count number of performance records in @a ats + * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ int GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh, @@ -1237,16 +1268,18 @@ GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh, size_t msize; uint32_t s = 0; - if (address == NULL) + if (NULL == address) { GNUNET_break (0); return GNUNET_SYSERR; } - namelen = (address->transport_name == NULL) ? 0 : strlen (address->transport_name) + 1; + namelen = (NULL == address->transport_name) + ? 0 + : strlen (address->transport_name) + 1; msize = sizeof (struct AddressUpdateMessage) + address->address_length + - ats_count * sizeof (struct GNUNET_ATS_Information) + namelen; + ats_count * sizeof (struct GNUNET_ATS_Information) + namelen; if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) || (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) || (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) || @@ -1264,7 +1297,9 @@ GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh, /* Already existing, nothing todo */ return GNUNET_SYSERR; } - s = find_empty_session_slot (sh, session, &address->peer); + s = find_empty_session_slot (sh, + session, + &address->peer); GNUNET_break (NOT_FOUND != s); } @@ -1284,18 +1319,23 @@ GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s', plugin `%s', session %p id %u\n", GNUNET_i2s (&address->peer), - address->transport_name, session, s); + address->transport_name, + session, + s); am = (struct GNUNET_ATS_Information *) &m[1]; memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information)); pm = (char *) &am[ats_count]; memcpy (pm, address->address, address->address_length); if (NULL != address->transport_name) - memcpy (&pm[address->address_length], address->transport_name, namelen); - GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p); + memcpy (&pm[address->address_length], + address->transport_name, + namelen); + GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, + sh->pending_tail, + p); do_transmit (sh); return GNUNET_OK; - } @@ -1380,10 +1420,14 @@ GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s', plugin `%s', session %p id %u\n", GNUNET_i2s (&address->peer), - address->transport_name, session, s); + address->transport_name, + session, + s); am = (struct GNUNET_ATS_Information *) &m[1]; - memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information)); + memcpy (am, + ats, + ats_count * sizeof (struct GNUNET_ATS_Information)); pm = (char *) &am[ats_count]; memcpy (pm, address->address, address->address_length); memcpy (&pm[address->address_length], address->transport_name, namelen); @@ -1405,7 +1449,8 @@ GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh, 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; @@ -1470,7 +1515,6 @@ GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh, memcpy (&pm[address->address_length], address->transport_name, namelen); GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p); do_transmit (sh); - return; } @@ -1494,9 +1538,9 @@ GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh, char *pm; size_t namelen; size_t msize; - uint32_t s = 0; + uint32_t s; - if (address == NULL) + if (NULL == address) { GNUNET_break (0); return; diff --git a/src/include/gnunet_ats_service.h b/src/include/gnunet_ats_service.h index 7be2e4758..e09dc5d75 100644 --- a/src/include/gnunet_ats_service.h +++ b/src/include/gnunet_ats_service.h @@ -35,11 +35,6 @@ */ #define GNUNET_ATS_NetworkTypeCount 6 -/** - * ATS network types as array initializer - */ -#define GNUNET_ATS_NetworkType { GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN, GNUNET_ATS_NET_BT } - /** * Types of networks (with separate quotas) we support. @@ -78,6 +73,13 @@ enum GNUNET_ATS_Network_Type }; +/** + * ATS network types as array initializer + */ +#define GNUNET_ATS_NetworkType { GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN, GNUNET_ATS_NET_BT } + + + /** * Default bandwidth assigned to a network : 64 KB/s */ @@ -430,9 +432,9 @@ GNUNET_ATS_print_network_type (enum GNUNET_ATS_Network_Type net); * @param sh the `struct GNUNET_ATS_SchedulingHandle` handle * @param addr address * @param addrlen address length - * @return location as `struct GNUNET_ATS_Information` + * @return type of the network the address belongs to */ -struct GNUNET_ATS_Information +enum GNUNET_ATS_Network_Type GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle *sh, const struct sockaddr *addr, socklen_t addrlen); diff --git a/src/include/gnunet_transport_plugin.h b/src/include/gnunet_transport_plugin.h index 17333551c..90d6ed2c6 100644 --- a/src/include/gnunet_transport_plugin.h +++ b/src/include/gnunet_transport_plugin.h @@ -120,15 +120,15 @@ typedef struct GNUNET_TIME_Relative /** - * Function that will be called to figure if an address is an loopback, - * LAN, WAN etc. address + * Function that will be called to figure if an address is an + * loopback, LAN, WAN etc. address * * @param cls closure * @param addr binary address * @param addrlen length of the @a addr - * @return ATS Information containing the network type + * @return type of the network the address belongs to */ -typedef struct GNUNET_ATS_Information +typedef enum GNUNET_ATS_Network_Type (*GNUNET_TRANSPORT_AddressToType) (void *cls, const struct sockaddr *addr, size_t addrlen); diff --git a/src/transport/gnunet-service-transport.c b/src/transport/gnunet-service-transport.c index 03fbc95db..2d2c0db46 100644 --- a/src/transport/gnunet-service-transport.c +++ b/src/transport/gnunet-service-transport.c @@ -633,36 +633,33 @@ plugin_env_session_end (void *cls, const struct GNUNET_HELLO_Address *address, * * @param cls closure * @param addr binary address - * @param addrlen length of the address - * @return ATS Information containing the network type + * @param addrlen length of the @a addr + * @return type of the network @a addr belongs to */ -static struct GNUNET_ATS_Information -plugin_env_address_to_type (void *cls, const struct sockaddr *addr, - size_t addrlen) +static enum GNUNET_ATS_Network_Type +plugin_env_address_to_type (void *cls, + const struct sockaddr *addr, + size_t addrlen) { - struct GNUNET_ATS_Information ats; - - ats.type = htonl (GNUNET_ATS_NETWORK_TYPE); - ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED); if (NULL == GST_ats) { GNUNET_break(0); - return ats; + return GNUNET_ATS_NET_UNSPECIFIED; } if (((addr->sa_family != AF_INET) && (addrlen != sizeof(struct sockaddr_in))) && ((addr->sa_family != AF_INET6) && (addrlen != sizeof(struct sockaddr_in6))) && (addr->sa_family != AF_UNIX)) { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Malformed address with length %u `%s'\n", addrlen, - GNUNET_a2s (addr, addrlen)); GNUNET_break(0); - return ats; + return GNUNET_ATS_NET_UNSPECIFIED; } - return GNUNET_ATS_address_get_type (GST_ats, addr, addrlen); + return GNUNET_ATS_address_get_type (GST_ats, + addr, + addrlen); } + /** * Notify ATS about the new address including the network this address is * located in. diff --git a/src/transport/plugin_transport_http_client.c b/src/transport/plugin_transport_http_client.c index cc1f39e9a..9c9af57ad 100644 --- a/src/transport/plugin_transport_http_client.c +++ b/src/transport/plugin_transport_http_client.c @@ -262,9 +262,9 @@ struct Session unsigned int msgs_in_queue; /** - * ATS network type in NBO + * ATS network type. */ - uint32_t ats_address_network_type; + enum GNUNET_ATS_Network_Type ats_address_network_type; }; @@ -1173,9 +1173,9 @@ client_receive_mst_cb (void *cls, char *stat_txt; plugin = s->plugin; + GNUNET_break (s->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED); atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE); - atsi.value = s->ats_address_network_type; - GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED)); + atsi.value = htonl (s->ats_address_network_type); delay = s->plugin->env->receive (plugin->env->cls, s->address, @@ -1941,7 +1941,7 @@ static enum GNUNET_ATS_Network_Type http_client_plugin_get_network (void *cls, struct Session *session) { - return ntohl (session->ats_address_network_type); + return session->ats_address_network_type; } @@ -1998,7 +1998,7 @@ http_client_plugin_get_session (void *cls, struct HTTP_Client_Plugin *plugin = cls; struct Session *s; struct sockaddr *sa; - struct GNUNET_ATS_Information ats; + enum GNUNET_ATS_Network_Type net_type; size_t salen = 0; int res; @@ -2021,8 +2021,7 @@ http_client_plugin_get_session (void *cls, } /* Determine network location */ - ats.type = htonl (GNUNET_ATS_NETWORK_TYPE); - ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED); + net_type = GNUNET_ATS_NET_UNSPECIFIED; sa = http_common_socket_from_address (address->address, address->address_length, &res); @@ -2039,15 +2038,15 @@ http_client_plugin_get_session (void *cls, { salen = sizeof (struct sockaddr_in6); } - ats = plugin->env->get_address_type (plugin->env->cls, sa, salen); + net_type = plugin->env->get_address_type (plugin->env->cls, sa, salen); GNUNET_free (sa); } else if (GNUNET_NO == res) { /* Cannot convert to sockaddr -> is external hostname */ - ats.value = htonl (GNUNET_ATS_NET_WAN); + net_type = GNUNET_ATS_NET_WAN; } - if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value)) + if (GNUNET_ATS_NET_UNSPECIFIED == net_type) { GNUNET_break (0); return NULL; @@ -2056,7 +2055,7 @@ http_client_plugin_get_session (void *cls, s = GNUNET_new (struct Session); s->plugin = plugin; s->address = GNUNET_HELLO_address_copy (address); - s->ats_address_network_type = ats.value; + s->ats_address_network_type = net_type; s->put.state = H_NOT_CONNECTED; s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_CLIENT_SESSION_TIMEOUT); diff --git a/src/transport/plugin_transport_http_server.c b/src/transport/plugin_transport_http_server.c index aaa17b2cf..c7c145328 100644 --- a/src/transport/plugin_transport_http_server.c +++ b/src/transport/plugin_transport_http_server.c @@ -1423,23 +1423,25 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin, conn_info->client_addr, sizeof (struct sockaddr_in)); addr_len = http_common_address_get_size (addr); - ats = plugin->env->get_address_type (plugin->env->cls, - conn_info->client_addr, - sizeof (struct sockaddr_in)); + ats.type = htonl (GNUNET_ATS_NETWORK_TYPE); + ats.value = htonl (plugin->env->get_address_type (plugin->env->cls, + conn_info->client_addr, + sizeof (struct sockaddr_in))); break; case (AF_INET6): addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6)); addr_len = http_common_address_get_size (addr); - ats = plugin->env->get_address_type (plugin->env->cls, - conn_info->client_addr, - sizeof (struct sockaddr_in6)); + ats.type = htonl (GNUNET_ATS_NETWORK_TYPE); + ats.value = htonl (plugin->env->get_address_type (plugin->env->cls, + conn_info->client_addr, + sizeof (struct sockaddr_in6))); break; default: - /* external host name */ + /* external host name */ ats.type = htonl (GNUNET_ATS_NETWORK_TYPE); - ats.type = htonl (GNUNET_ATS_NET_WAN); + ats.value = htonl (GNUNET_ATS_NET_WAN); return NULL; } s = GNUNET_new (struct Session); diff --git a/src/transport/plugin_transport_tcp.c b/src/transport/plugin_transport_tcp.c index 4f7b57641..01a4eade8 100644 --- a/src/transport/plugin_transport_tcp.c +++ b/src/transport/plugin_transport_tcp.c @@ -1521,7 +1521,7 @@ tcp_plugin_get_session (void *cls, const struct IPv4TcpAddress *t4; const struct IPv6TcpAddress *t6; unsigned int options; - struct GNUNET_ATS_Information ats; + enum GNUNET_ATS_Network_Type net_type; unsigned int is_natd = GNUNET_NO; size_t addrlen; #ifdef TCP_STEALTH @@ -1612,7 +1612,8 @@ tcp_plugin_get_session (void *cls, return NULL; } - ats = plugin->env->get_address_type (plugin->env->cls, sb, sbs); + net_type = plugin->env->get_address_type (plugin->env->cls, sb, sbs); + if ((is_natd == GNUNET_YES) && (addrlen == sizeof(struct IPv6TcpAddress))) { @@ -1645,7 +1646,7 @@ tcp_plugin_get_session (void *cls, address, NULL, GNUNET_YES); - session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (ats.value); + session->ats_address_network_type = net_type; GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED); session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT, &nat_connect_timeout, @@ -1742,7 +1743,7 @@ tcp_plugin_get_session (void *cls, address, GNUNET_SERVER_connect_socket (plugin->server, sa), GNUNET_NO); - session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (ats.value); + session->ats_address_network_type = net_type; GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED); GNUNET_SERVER_client_set_user_context(session->client, session); GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, @@ -2333,8 +2334,9 @@ handle_tcp_welcome (void *cls, } session = create_session (plugin, address, client, GNUNET_NO); GNUNET_HELLO_address_free (address); - ats = plugin->env->get_address_type (plugin->env->cls, vaddr, alen); - session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (ats.value); + session->ats_address_network_type = plugin->env->get_address_type (plugin->env->cls, vaddr, alen); + ats.type = htonl (GNUNET_ATS_NETWORK_TYPE); + ats.value = htonl (session->ats_address_network_type); LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new%s session %p for peer `%s' client %p \n", GNUNET_HELLO_address_check_option (session->address, diff --git a/src/transport/plugin_transport_udp.c b/src/transport/plugin_transport_udp.c index 48535f28b..76c9dfdec 100644 --- a/src/transport/plugin_transport_udp.c +++ b/src/transport/plugin_transport_udp.c @@ -1799,6 +1799,7 @@ udp_plugin_create_session (void *cls, if (sizeof (struct IPv4UdpAddress) == address->address_length) { struct sockaddr_in v4; + udp_v4 = (struct IPv4UdpAddress *) address->address; memset (&v4, '\0', sizeof (v4)); v4.sin_family = AF_INET; @@ -1807,9 +1808,10 @@ udp_plugin_create_session (void *cls, #endif v4.sin_port = udp_v4->u4_port; v4.sin_addr.s_addr = udp_v4->ipv4_addr; - s->ats = plugin->env->get_address_type (plugin->env->cls, - (const struct sockaddr *) &v4, - sizeof (v4)); + s->ats.type = htonl (GNUNET_ATS_NETWORK_TYPE); + s->ats.value = htonl (plugin->env->get_address_type (plugin->env->cls, + (const struct sockaddr *) &v4, + sizeof (v4))); } else if (sizeof (struct IPv6UdpAddress) == address->address_length) { @@ -1822,8 +1824,10 @@ udp_plugin_create_session (void *cls, #endif v6.sin6_port = udp_v6->u6_port; v6.sin6_addr = udp_v6->ipv6_addr; - s->ats = plugin->env->get_address_type (plugin->env->cls, - (const struct sockaddr *) &v6, sizeof (v6)); + s->ats.type = htonl (GNUNET_ATS_NETWORK_TYPE); + s->ats.value = htonl (plugin->env->get_address_type (plugin->env->cls, + (const struct sockaddr *) &v6, + sizeof (v6))); } if (NULL == s) @@ -2871,11 +2875,11 @@ analyze_send_error (struct Plugin *plugin, const struct sockaddr *sa, socklen_t slen, int error) { - struct GNUNET_ATS_Information type; + enum GNUNET_ATS_Network_Type type; type = plugin->env->get_address_type (plugin->env->cls, sa, slen); - if (((GNUNET_ATS_NET_LAN == ntohl (type.value)) - || (GNUNET_ATS_NET_WAN == ntohl (type.value))) + if (((GNUNET_ATS_NET_LAN == type) + || (GNUNET_ATS_NET_WAN == type)) && ((ENETUNREACH == errno)|| (ENETDOWN == errno))) { if (slen == sizeof (struct sockaddr_in)) diff --git a/src/transport/plugin_transport_udp_broadcasting.c b/src/transport/plugin_transport_udp_broadcasting.c index 6566245cd..3ef9ecaaf 100644 --- a/src/transport/plugin_transport_udp_broadcasting.c +++ b/src/transport/plugin_transport_udp_broadcasting.c @@ -111,21 +111,24 @@ struct Mstv4Context struct Plugin *plugin; struct IPv4UdpAddress addr; + /** - * ATS network type in NBO + * ATS network type. */ - uint32_t ats_address_network_type; + enum GNUNET_ATS_Network_Type ats_address_network_type; }; + struct Mstv6Context { struct Plugin *plugin; struct IPv6UdpAddress addr; + /** - * ATS network type in NBO + * ATS network type. */ - uint32_t ats_address_network_type; + enum GNUNET_ATS_Network_Type ats_address_network_type; }; @@ -152,7 +155,7 @@ broadcast_ipv6_mst_cb (void *cls, void *client, /* setup ATS */ atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE); - atsi.value = mc->ats_address_network_type; + atsi.value = htonl (mc->ats_address_network_type); GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED); hello = (struct GNUNET_MessageHeader *) &msg[1]; @@ -195,7 +198,7 @@ broadcast_ipv4_mst_cb (void *cls, void *client, /* setup ATS */ atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE); - atsi.value = mc->ats_address_network_type; + atsi.value = htonl (mc->ats_address_network_type); GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED); hello = (struct GNUNET_MessageHeader *) &msg[1]; @@ -221,8 +224,6 @@ udp_broadcast_receive (struct Plugin *plugin, const struct sockaddr *addr, size_t addrlen) { - struct GNUNET_ATS_Information ats; - if (addrlen == sizeof (struct sockaddr_in)) { LOG (GNUNET_ERROR_TYPE_DEBUG, @@ -235,8 +236,9 @@ udp_broadcast_receive (struct Plugin *plugin, mc->addr.ipv4_addr = av4->sin_addr.s_addr; mc->addr.u4_port = av4->sin_port; - ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen); - mc->ats_address_network_type = ats.value; + mc->ats_address_network_type = plugin->env->get_address_type (plugin->env->cls, + (const struct sockaddr *) addr, + addrlen); GNUNET_assert (NULL != plugin->broadcast_ipv4_mst); if (GNUNET_OK != @@ -256,8 +258,7 @@ udp_broadcast_receive (struct Plugin *plugin, mc->addr.ipv6_addr = av6->sin6_addr; mc->addr.u6_port = av6->sin6_port; - ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen); - mc->ats_address_network_type = ats.value; + mc->ats_address_network_type = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen); GNUNET_assert (NULL != plugin->broadcast_ipv4_mst); if (GNUNET_OK != GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size, @@ -473,7 +474,7 @@ iface_proc (void *cls, { struct Plugin *plugin = cls; struct BroadcastAddress *ba; - struct GNUNET_ATS_Information network; + enum GNUNET_ATS_Network_Type network; if (NULL == addr) return GNUNET_OK; @@ -489,7 +490,7 @@ iface_proc (void *cls, GNUNET_a2s (netmask, addrlen), name, netmask); network = plugin->env->get_address_type (plugin->env->cls, broadcast_addr, addrlen); - if (GNUNET_ATS_NET_LOOPBACK == ntohl(network.value)) + if (GNUNET_ATS_NET_LOOPBACK == network) { /* Broadcasting on loopback does not make sense */ return GNUNET_YES; diff --git a/src/transport/plugin_transport_unix.c b/src/transport/plugin_transport_unix.c index 460869181..112f15d09 100644 --- a/src/transport/plugin_transport_unix.c +++ b/src/transport/plugin_transport_unix.c @@ -1387,7 +1387,10 @@ unix_transport_server_start (void *cls) plugin->unix_socket_path[0] = '@'; un->sun_path[0] = '\0'; } - plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) un, un_len); + plugin->ats_network.type = htonl (GNUNET_ATS_NETWORK_TYPE); + plugin->ats_network.value = htonl (plugin->env->get_address_type (plugin->env->cls, + (const struct sockaddr *) un, + un_len)); plugin->unix_sock.desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0); if (NULL == plugin->unix_sock.desc) -- cgit v1.2.3