/** * Client asked to resolve an address. Process the request. * * @param cls unused * @param client the client * @param message the resolution request */ static void clients_handle_address_to_string (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message) { const struct AddressLookupMessage *alum; struct GNUNET_TRANSPORT_PluginFunctions *papi; const char *plugin_name; const char *address; uint32_t address_len; uint16_t size; struct GNUNET_SERVER_TransmitContext *tc; struct AddressToStringContext *actx; struct GNUNET_TIME_Relative rtimeout; int32_t numeric; size = ntohs (message->size); if (size < sizeof (struct AddressLookupMessage)) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } alum = (const struct AddressLookupMessage *) message; address_len = ntohs (alum->addrlen); if (size <= sizeof (struct AddressLookupMessage) + address_len) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } address = (const char *) &alum[1]; plugin_name = (const char *) &address[address_len]; if ('\0' != plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1]) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout); numeric = ntohs (alum->numeric_only); tc = GNUNET_SERVER_transmit_context_create (client); papi = GST_plugins_printer_find (plugin_name); if (NULL == papi) { GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY); GNUNET_SERVER_transmit_context_run (tc, rtimeout); return; } actx = GNUNET_new (struct AddressToStringContext); actx->tc = tc; GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx); GNUNET_SERVER_disable_receive_done_warning (client); papi->address_pretty_printer (papi->cls, plugin_name, address, address_len, numeric, rtimeout, &transmit_address_to_client, actx); }
/** * Convert ATS properties from network to host byte order. * * @param hbo[OUT] value written * @param nbo value read */ void GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo, const struct GNUNET_ATS_PropertiesNBO *nbo) { hbo->utilization_out = ntohl (nbo->utilization_out); hbo->utilization_in = ntohl (nbo->utilization_in); hbo->scope = ntohl ((uint32_t) nbo->scope); hbo->distance = ntohl (nbo->distance); hbo->delay = GNUNET_TIME_relative_ntoh (nbo->delay); }
/** * Handle ANNOUNCE message. * * @param cls closure * @param client identification of the client * @param message the actual message */ static void handle_announce (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message) { const struct AnnounceMessage *am; const char *regex; struct ClientEntry *ce; uint16_t size; size = ntohs (message->size); am = (const struct AnnounceMessage *) message; regex = (const char *) &am[1]; if ( (size <= sizeof (struct AnnounceMessage)) || ('\0' != regex[size - sizeof (struct AnnounceMessage) - 1]) ) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } ce = GNUNET_new (struct ClientEntry); ce->client = client; ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay); ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency, &reannounce, ce); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to announce regex `%s' every %s\n", regex, GNUNET_STRINGS_relative_time_to_string (ce->frequency, GNUNET_NO)); ce->ah = REGEX_INTERNAL_announce (dht, my_private_key, regex, ntohs (am->compression), stats); if (NULL == ce->ah) { GNUNET_break (0); GNUNET_SCHEDULER_cancel (ce->refresh_task); GNUNET_free (ce); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce); GNUNET_SERVER_receive_done (client, GNUNET_OK); }
int main (int argc, char *argv[]) { struct GNUNET_TIME_Absolute now; struct GNUNET_TIME_AbsoluteNBO nown; struct GNUNET_TIME_Absolute future; struct GNUNET_TIME_Absolute past; struct GNUNET_TIME_Absolute last; struct GNUNET_TIME_Absolute forever; struct GNUNET_TIME_Absolute zero; struct GNUNET_TIME_Relative rel; struct GNUNET_TIME_Relative relForever; struct GNUNET_TIME_Relative relUnit; struct GNUNET_TIME_RelativeNBO reln; unsigned int i; GNUNET_log_setup ("test-time", "WARNING", NULL); forever = GNUNET_TIME_UNIT_FOREVER_ABS; relForever = GNUNET_TIME_UNIT_FOREVER_REL; relUnit = GNUNET_TIME_UNIT_MILLISECONDS; zero.abs_value_us = 0; last = now = GNUNET_TIME_absolute_get (); while (now.abs_value_us == last.abs_value_us) now = GNUNET_TIME_absolute_get (); GNUNET_assert (now.abs_value_us > last.abs_value_us); /* test overflow checking in multiply */ rel = GNUNET_TIME_UNIT_MILLISECONDS; GNUNET_log_skip (1, GNUNET_NO); for (i = 0; i < 55; i++) rel = GNUNET_TIME_relative_multiply (rel, 2); GNUNET_log_skip (0, GNUNET_NO); GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us); /*check zero */ rel.rel_value_us = (UINT64_MAX) - 1024; GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us == GNUNET_TIME_relative_multiply (rel, 0).rel_value_us); /* test infinity-check for relative to absolute */ GNUNET_log_skip (1, GNUNET_NO); last = GNUNET_TIME_relative_to_absolute (rel); GNUNET_assert (last.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us); GNUNET_log_skip (0, GNUNET_YES); /* check relative to absolute */ rel.rel_value_us = 1000000; GNUNET_assert (GNUNET_TIME_absolute_get ().abs_value_us < GNUNET_TIME_relative_to_absolute (rel).abs_value_us); /*check forever */ rel.rel_value_us = UINT64_MAX; GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == GNUNET_TIME_relative_to_absolute (rel).abs_value_us); /* check overflow for r2a */ rel.rel_value_us = (UINT64_MAX) - 1024; GNUNET_log_skip (1, GNUNET_NO); last = GNUNET_TIME_relative_to_absolute (rel); GNUNET_log_skip (0, GNUNET_NO); GNUNET_assert (last.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us); /* check overflow for relative add */ GNUNET_log_skip (1, GNUNET_NO); rel = GNUNET_TIME_relative_add (rel, rel); GNUNET_log_skip (0, GNUNET_NO); GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us); GNUNET_log_skip (1, GNUNET_NO); rel = GNUNET_TIME_relative_add (relForever, relForever); GNUNET_log_skip (0, GNUNET_NO); GNUNET_assert (rel.rel_value_us == relForever.rel_value_us); GNUNET_log_skip (1, GNUNET_NO); rel = GNUNET_TIME_relative_add (relUnit, relUnit); GNUNET_assert (rel.rel_value_us == 2 * relUnit.rel_value_us); /* check relation check in get_duration */ future.abs_value_us = now.abs_value_us + 1000000; GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).rel_value_us == 1000000); GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).rel_value_us == 0); GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero, forever).rel_value_us == forever.abs_value_us); past.abs_value_us = now.abs_value_us - 1000000; rel = GNUNET_TIME_absolute_get_duration (future); GNUNET_assert (rel.rel_value_us == 0); rel = GNUNET_TIME_absolute_get_duration (past); GNUNET_assert (rel.rel_value_us >= 1000000); /* check get remaining */ rel = GNUNET_TIME_absolute_get_remaining (now); GNUNET_assert (rel.rel_value_us == 0); rel = GNUNET_TIME_absolute_get_remaining (past); GNUNET_assert (rel.rel_value_us == 0); rel = GNUNET_TIME_absolute_get_remaining (future); GNUNET_assert (rel.rel_value_us > 0); GNUNET_assert (rel.rel_value_us <= 1000000); forever = GNUNET_TIME_UNIT_FOREVER_ABS; GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == GNUNET_TIME_absolute_get_remaining (forever).rel_value_us); /* check endianess */ reln = GNUNET_TIME_relative_hton (rel); GNUNET_assert (rel.rel_value_us == GNUNET_TIME_relative_ntoh (reln).rel_value_us); nown = GNUNET_TIME_absolute_hton (now); GNUNET_assert (now.abs_value_us == GNUNET_TIME_absolute_ntoh (nown).abs_value_us); /* check absolute addition */ future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS); GNUNET_assert (future.abs_value_us == now.abs_value_us + 1000 * 1000LL); future = GNUNET_TIME_absolute_add (forever, GNUNET_TIME_UNIT_ZERO); GNUNET_assert (future.abs_value_us == forever.abs_value_us); rel.rel_value_us = (UINT64_MAX) - 1024; now.abs_value_us = rel.rel_value_us; future = GNUNET_TIME_absolute_add (now, rel); GNUNET_assert (future.abs_value_us == forever.abs_value_us); /* check zero */ future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO); GNUNET_assert (future.abs_value_us == now.abs_value_us); GNUNET_assert (forever.abs_value_us == GNUNET_TIME_absolute_subtract (forever, GNUNET_TIME_UNIT_MINUTES).abs_value_us); /*check absolute subtract */ now.abs_value_us = 50000; rel.rel_value_us = 100000; GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us); rel.rel_value_us = 10000; GNUNET_assert (40000 == (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us); /*check relative divide */ GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == (GNUNET_TIME_relative_divide (rel, 0)).rel_value_us); rel = GNUNET_TIME_UNIT_FOREVER_REL; GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == (GNUNET_TIME_relative_divide (rel, 2)).rel_value_us); rel = GNUNET_TIME_relative_divide (relUnit, 2); GNUNET_assert (rel.rel_value_us == relUnit.rel_value_us / 2); /* check Return absolute time of 0ms */ zero = GNUNET_TIME_UNIT_ZERO_ABS; /* check GNUNET_TIME_calculate_eta */ last.abs_value_us = GNUNET_TIME_absolute_get ().abs_value_us - 1024; forever = GNUNET_TIME_UNIT_FOREVER_ABS; forever.abs_value_us = forever.abs_value_us - 1024; GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == GNUNET_TIME_calculate_eta (forever, 50000, 100000).rel_value_us); /* check zero */ GNUNET_log_skip (1, GNUNET_NO); GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us == (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value_us); GNUNET_log_skip (0, GNUNET_YES); /*check forever */ GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value_us); /*check relative subtract */ now = GNUNET_TIME_absolute_get (); rel.rel_value_us = now.abs_value_us; relForever.rel_value_us = rel.rel_value_us + 1024; GNUNET_assert (1024 == GNUNET_TIME_relative_subtract (relForever, rel).rel_value_us); /*check zero */ GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us == GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us); /*check forever */ rel.rel_value_us = UINT64_MAX; GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us); /*check GNUNET_TIME_relative_min */ now = GNUNET_TIME_absolute_get (); rel.rel_value_us = now.abs_value_us; relForever.rel_value_us = rel.rel_value_us - 1024; GNUNET_assert (relForever.rel_value_us == GNUNET_TIME_relative_min (rel, relForever).rel_value_us); /*check GNUNET_TIME_relative_max */ GNUNET_assert (rel.rel_value_us == GNUNET_TIME_relative_max (rel, relForever).rel_value_us); /*check GNUNET_TIME_absolute_min */ now = GNUNET_TIME_absolute_get (); last.abs_value_us = now.abs_value_us - 1024; GNUNET_assert (last.abs_value_us == GNUNET_TIME_absolute_min (now, last).abs_value_us); /*check GNUNET_TIME_absolute_max */ GNUNET_assert (now.abs_value_us == GNUNET_TIME_absolute_max (now, last).abs_value_us); return 0; }
/** * Client asked for transmission to a peer. Process the request. * * @param cls unused * @param client the client * @param message the send message that was sent */ static void clients_handle_send (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message) { const struct OutboundMessage *obm; const struct GNUNET_MessageHeader *obmm; struct SendTransmitContinuationContext *stcc; uint16_t size; uint16_t msize; struct TransportClient *tc; tc = lookup_client (client); if (NULL == tc) { /* client asked for transmission before 'START' */ GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } size = ntohs (message->size); if (size < sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader)) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } obm = (const struct OutboundMessage *) message; obmm = (const struct GNUNET_MessageHeader *) &obm[1]; msize = size - sizeof (struct OutboundMessage); if (msize < sizeof (struct GNUNET_MessageHeader)) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' request from client with target `%4s' and first message of type %u and total size %u\n", "SEND", GNUNET_i2s (&obm->peer), ntohs (obmm->type), msize); if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer)) { /* not connected, not allowed to send; can happen due to asynchronous operations */ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not send message to peer `%s': not connected\n", GNUNET_i2s (&obm->peer)); GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# bytes payload dropped (other peer was not connected)"), msize, GNUNET_NO); GNUNET_SERVER_receive_done (client, GNUNET_OK); return; } GNUNET_SERVER_receive_done (client, GNUNET_OK); stcc = GNUNET_new (struct SendTransmitContinuationContext); stcc->target = obm->peer; stcc->client = client; GNUNET_SERVER_client_keep (client); GST_manipulation_send (&obm->peer, obmm, msize, GNUNET_TIME_relative_ntoh (obm->timeout), &handle_send_transmit_continuation, stcc); }