aboutsummaryrefslogtreecommitdiff
path: root/src/ats/gnunet-service-ats_addresses.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-02-05 16:09:26 +0000
committerChristian Grothoff <christian@grothoff.org>2015-02-05 16:09:26 +0000
commitb2a4dcbce4f56046fbdcd4b114ec7a3feb4793fd (patch)
tree80877f12acf494cafd50d574bdc1a2b2a218bacb /src/ats/gnunet-service-ats_addresses.c
parent5d063af93ad3de2223ff3078acac13761ab308fb (diff)
downloadgnunet-b2a4dcbce4f56046fbdcd4b114ec7a3feb4793fd.tar.gz
gnunet-b2a4dcbce4f56046fbdcd4b114ec7a3feb4793fd.zip
-cleaning up gnunet-service-ats_performance-*
Diffstat (limited to 'src/ats/gnunet-service-ats_addresses.c')
-rw-r--r--src/ats/gnunet-service-ats_addresses.c232
1 files changed, 231 insertions, 1 deletions
diff --git a/src/ats/gnunet-service-ats_addresses.c b/src/ats/gnunet-service-ats_addresses.c
index d0028f9b0..e87bf487b 100644
--- a/src/ats/gnunet-service-ats_addresses.c
+++ b/src/ats/gnunet-service-ats_addresses.c
@@ -220,6 +220,11 @@
220 */ 220 */
221struct GNUNET_CONTAINER_MultiPeerMap *GSA_addresses; 221struct GNUNET_CONTAINER_MultiPeerMap *GSA_addresses;
222 222
223/**
224 * Context for sending messages to performance clients without PIC.
225 */
226static struct GNUNET_SERVER_NotificationContext *nc;
227
223 228
224/** 229/**
225 * Update statistic on number of addresses. 230 * Update statistic on number of addresses.
@@ -701,12 +706,15 @@ GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
701 * known and current performance information. It has a solver component 706 * known and current performance information. It has a solver component
702 * responsible for the resource allocation. It tells the solver about changes 707 * responsible for the resource allocation. It tells the solver about changes
703 * and receives updates when the solver changes the resource allocation. 708 * and receives updates when the solver changes the resource allocation.
709 *
710 * @param server handle to our server
704 */ 711 */
705void 712void
706GAS_addresses_init () 713GAS_addresses_init (struct GNUNET_SERVER_Handle *server)
707{ 714{
708 GSA_addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO); 715 GSA_addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
709 update_addresses_stat (); 716 update_addresses_stat ();
717 nc = GNUNET_SERVER_notification_context_create (server, 32);
710} 718}
711 719
712 720
@@ -757,6 +765,8 @@ GAS_addresses_done ()
757 GAS_addresses_destroy_all (); 765 GAS_addresses_destroy_all ();
758 GNUNET_CONTAINER_multipeermap_destroy (GSA_addresses); 766 GNUNET_CONTAINER_multipeermap_destroy (GSA_addresses);
759 GSA_addresses = NULL; 767 GSA_addresses = NULL;
768 GNUNET_SERVER_notification_context_destroy (nc);
769 nc = NULL;
760} 770}
761 771
762 772
@@ -850,4 +860,224 @@ GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer,
850 GNUNET_BANDWIDTH_ZERO); 860 GNUNET_BANDWIDTH_ZERO);
851} 861}
852 862
863
864/**
865 * Information we need for the callbacks to return a list of addresses
866 * back to the client.
867 */
868struct AddressIteration
869{
870 /**
871 * Actual handle to the client.
872 */
873 struct GNUNET_SERVER_Client *client;
874
875 /**
876 * Are we sending all addresses, or only those that are active?
877 */
878 int all;
879
880 /**
881 * Which ID should be included in the response?
882 */
883 uint32_t id;
884
885};
886
887
888/**
889 * Send a #GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE with the
890 * given address details to the client identified in @a ai.
891 *
892 * @param ai our address information context (identifies the client)
893 * @param id the peer id this address is for
894 * @param plugin_name name of the plugin that supports this address
895 * @param plugin_addr address
896 * @param plugin_addr_len length of @a plugin_addr
897 * @param active #GNUNET_YES if this address is actively used
898 * @param atsi ats performance information
899 * @param atsi_count number of ats performance elements in @a atsi
900 * @param bandwidth_out current outbound bandwidth assigned to address
901 * @param bandwidth_in current inbound bandwidth assigned to address
902 */
903static void
904transmit_req_addr (struct AddressIteration *ai,
905 const struct GNUNET_PeerIdentity *id,
906 const char *plugin_name,
907 const void *plugin_addr,
908 size_t plugin_addr_len,
909 int active,
910 const struct GNUNET_ATS_Information *atsi,
911 uint32_t atsi_count,
912 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
913 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
914
915{
916 struct GNUNET_ATS_Information *atsp;
917 struct PeerInformationMessage *msg;
918 char *addrp;
919 size_t plugin_name_length;
920 size_t msize;
921
922 if (NULL != plugin_name)
923 plugin_name_length = strlen (plugin_name) + 1;
924 else
925 plugin_name_length = 0;
926 msize = sizeof (struct PeerInformationMessage) +
927 atsi_count * sizeof (struct GNUNET_ATS_Information) +
928 plugin_addr_len + plugin_name_length;
929 char buf[msize] GNUNET_ALIGN;
930
931 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
932 GNUNET_assert (atsi_count <
933 GNUNET_SERVER_MAX_MESSAGE_SIZE /
934 sizeof (struct GNUNET_ATS_Information));
935 msg = (struct PeerInformationMessage *) buf;
936 msg->header.size = htons (msize);
937 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
938 msg->ats_count = htonl (atsi_count);
939 msg->id = htonl (ai->id);
940 if (NULL != id)
941 msg->peer = *id;
942 else
943 memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
944 msg->address_length = htons (plugin_addr_len);
945 msg->address_active = ntohl (active);
946 msg->plugin_name_length = htons (plugin_name_length);
947 msg->bandwidth_out = bandwidth_out;
948 msg->bandwidth_in = bandwidth_in;
949 atsp = (struct GNUNET_ATS_Information *) &msg[1];
950 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
951 addrp = (char *) &atsp[atsi_count];
952 if (NULL != plugin_addr)
953 memcpy (addrp, plugin_addr, plugin_addr_len);
954 if (NULL != plugin_name)
955 strcpy (&addrp[plugin_addr_len], plugin_name);
956 GNUNET_SERVER_notification_context_unicast (nc,
957 ai->client,
958 &msg->header,
959 GNUNET_NO);
960}
961
962
963/**
964 * Iterator for #GAS_addresses_get_peer_info(), called with peer-specific
965 * information to be passed back to the client.
966 *
967 * @param cls closure with our `struct AddressIteration *`
968 * @param id the peer id
969 * @param plugin_name plugin name
970 * @param plugin_addr address
971 * @param plugin_addr_len length of @a plugin_addr
972 * @param active is address actively used
973 * @param atsi ats performance information
974 * @param atsi_count number of ats performance elements in @a atsi
975 * @param bandwidth_out current outbound bandwidth assigned to address
976 * @param bandwidth_in current inbound bandwidth assigned to address
977 */
978static void
979req_addr_peerinfo_it (void *cls,
980 const struct GNUNET_PeerIdentity *id,
981 const char *plugin_name,
982 const void *plugin_addr,
983 size_t plugin_addr_len,
984 int active,
985 const struct GNUNET_ATS_Information *atsi,
986 uint32_t atsi_count,
987 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
988 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
989{
990 struct AddressIteration *ai = cls;
991
992 if ( (NULL == id) &&
993 (NULL == plugin_name) &&
994 (NULL == plugin_addr) )
995 {
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997 "Address iteration done for one peer\n");
998 return;
999 }
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1001 "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
1002 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
1003 GNUNET_i2s (id),
1004 plugin_name,
1005 (unsigned int) ntohl (bandwidth_out.value__),
1006 (unsigned int) ntohl (bandwidth_in.value__));
1007 /* Transmit result (either if address is active, or if
1008 client wanted all addresses) */
1009 if ( (GNUNET_YES != ai->all) &&
1010 (GNUNET_YES != active))
1011 return;
1012 transmit_req_addr (ai,
1013 id,
1014 plugin_name,
1015 plugin_addr, plugin_addr_len,
1016 active,
1017 atsi,
1018 atsi_count,
1019 bandwidth_out,
1020 bandwidth_in);
1021}
1022
1023
1024/**
1025 * Handle 'address list request' messages from clients.
1026 *
1027 * @param cls unused, NULL
1028 * @param client client that sent the request
1029 * @param message the request message
1030 */
1031void
1032GAS_handle_request_address_list (void *cls,
1033 struct GNUNET_SERVER_Client *client,
1034 const struct GNUNET_MessageHeader *message)
1035{
1036 struct AddressIteration ai;
1037 const struct AddressListRequestMessage *alrm;
1038 struct GNUNET_PeerIdentity allzeros;
1039
1040 GNUNET_SERVER_notification_context_add (nc,
1041 client);
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "Received ADDRESSLIST_REQUEST message\n");
1044 alrm = (const struct AddressListRequestMessage *) message;
1045 ai.all = ntohl (alrm->all);
1046 ai.id = ntohl (alrm->id);
1047 ai.client = client;
1048
1049 memset (&allzeros,
1050 '\0',
1051 sizeof (struct GNUNET_PeerIdentity));
1052 if (0 == memcmp (&alrm->peer,
1053 &allzeros,
1054 sizeof (struct GNUNET_PeerIdentity)))
1055 {
1056 /* Return addresses for all peers */
1057 GAS_addresses_get_peer_info (NULL,
1058 &req_addr_peerinfo_it,
1059 &ai);
1060 }
1061 else
1062 {
1063 /* Return addresses for a specific peer */
1064 GAS_addresses_get_peer_info (&alrm->peer,
1065 &req_addr_peerinfo_it,
1066 &ai);
1067 }
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "Finished handling `%s' message\n",
1070 "ADDRESSLIST_REQUEST");
1071 transmit_req_addr (&ai,
1072 NULL, NULL, NULL,
1073 0, GNUNET_NO,
1074 NULL, 0,
1075 GNUNET_BANDWIDTH_ZERO,
1076 GNUNET_BANDWIDTH_ZERO);
1077 GNUNET_SERVER_receive_done (client,
1078 GNUNET_OK);
1079}
1080
1081
1082
853/* end of gnunet-service-ats_addresses.c */ 1083/* end of gnunet-service-ats_addresses.c */