missing function reference
[oweals/gnunet.git] / src / core / core_api_peer_get_info.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file core/core_api_peer_get_info.c
23  * @brief implementation of the peer_change_preference functions 
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_core_service.h"
28 #include "core.h"
29
30
31 struct GNUNET_CORE_InformationRequestContext 
32 {
33   
34   /**
35    * Our connection to the service.
36    */
37   struct GNUNET_CLIENT_Connection *client;
38
39   /**
40    * Function to call with the information.
41    */
42   GNUNET_CORE_PeerConfigurationInfoCallback info;
43
44   /**
45    * Closure for info.
46    */
47   void *info_cls;
48
49 };
50
51
52 /**
53  * Receive reply from core service with information about a peer.
54  *
55  * @param cls our 'struct  GNUNET_CORE_InformationRequestContext *'
56  * @param msg NULL on error (i.e. timeout)
57  */
58 static void
59 receive_info (void *cls,
60               const struct GNUNET_MessageHeader *msg)
61 {
62   struct GNUNET_CORE_InformationRequestContext *irc = cls;
63   const struct ConfigurationInfoMessage *cim;
64
65   if (msg == NULL)
66     {
67       if (irc->info != NULL)
68         irc->info (irc->info_cls, 
69                    NULL, 0, 0, 0, 0);     
70       GNUNET_CLIENT_disconnect (irc->client);
71       GNUNET_free (irc);
72       return;
73     }
74   if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO) ||
75        (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage)) )
76     {
77       GNUNET_break (0);
78       if (irc->info != NULL)
79         irc->info (irc->info_cls, 
80                    NULL, 0, 0, 0, 0);     
81       GNUNET_CLIENT_disconnect (irc->client);
82       GNUNET_free (irc);
83       return;
84     }
85   cim = (const struct ConfigurationInfoMessage*) msg;
86   if (irc->info != NULL)
87     irc->info (irc->info_cls,
88                &cim->peer,
89                ntohl (cim->bpm_in),
90                ntohl (cim->bpm_out),
91                ntohl (cim->reserved_amount),
92                GNUNET_ntohll (cim->preference));  
93   GNUNET_CLIENT_disconnect (irc->client);
94   GNUNET_free (irc);
95 }
96
97
98 /**
99  * Obtain statistics and/or change preferences for the given peer.
100  *
101  * @param sched scheduler to use
102  * @param cfg configuration to use
103  * @param peer identifies the peer
104  * @param timeout after how long should we give up (and call "info" with NULL
105  *                for "peer" to signal an error)?
106  * @param bpm_out set to the current bandwidth limit (sending) for this peer,
107  *                caller should set "bpm_out" to "-1" to avoid changing
108  *                the current value; otherwise "bpm_out" will be lowered to
109  *                the specified value; passing a pointer to "0" can be used to force
110  *                us to disconnect from the peer; "bpm_out" might not increase
111  *                as specified since the upper bound is generally
112  *                determined by the other peer!
113  * @param amount reserve N bytes for receiving, negative
114  *                amounts can be used to undo a (recent) reservation;
115  * @param preference increase incoming traffic share preference by this amount;
116  *                in the absence of "amount" reservations, we use this
117  *                preference value to assign proportional bandwidth shares
118  *                to all connected peers
119  * @param info function to call with the resulting configuration information
120  * @param info_cls closure for info
121  * @return NULL on error
122  */
123 struct GNUNET_CORE_InformationRequestContext *
124 GNUNET_CORE_peer_change_preference (struct GNUNET_SCHEDULER_Handle *sched,
125                                     const struct GNUNET_CONFIGURATION_Handle *cfg,
126                                     const struct GNUNET_PeerIdentity *peer,
127                                     struct GNUNET_TIME_Relative timeout,
128                                     uint32_t bpm_out,
129                                     int32_t amount,
130                                     uint64_t preference,
131                                     GNUNET_CORE_PeerConfigurationInfoCallback info,
132                                     void *info_cls)
133 {
134   struct GNUNET_CORE_InformationRequestContext *irc;
135   struct RequestInfoMessage rim;
136   struct GNUNET_CLIENT_Connection *client;
137   int retry;
138
139   client = GNUNET_CLIENT_connect (sched, "core", cfg);
140   if (client == NULL)
141     return NULL;
142   irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
143   irc->client = client;
144   irc->info = info;
145   irc->info_cls = info_cls;
146   rim.header.size = htons (sizeof (struct RequestInfoMessage));
147   rim.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
148   rim.reserved = htonl (0);
149   rim.limit_outbound_bpm = htonl (bpm_out);
150   rim.reserve_inbound = htonl (amount);
151   rim.preference_change = GNUNET_htonll(preference);
152   rim.peer = *peer;
153   retry = ( (amount == 0) && (preference == 0) ) ? GNUNET_YES : GNUNET_NO;
154   GNUNET_assert (GNUNET_OK == GNUNET_CLIENT_transmit_and_get_response (client,
155                                                                        &rim.header,
156                                                                        timeout,
157                                                                        retry,
158                                                                        &receive_info,
159                                                                        irc));  
160   return irc;
161 }
162
163
164 /**
165  * Cancel request for getting information about a peer.
166  *
167  * @param irc context returned by the original GNUNET_CORE_peer_get_info call
168  */
169 void
170 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
171 {
172   GNUNET_CLIENT_disconnect (irc->client);
173   GNUNET_free (irc);
174 }
175
176 /* end of core_api_peer_get_info.c */