aboutsummaryrefslogtreecommitdiff
path: root/src/core/core_api_peer_get_info.c
blob: 1ca8d82afa4251c75f7be26f6b274a72f75c278b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
     This file is part of GNUnet.
     (C) 2009, 2010 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
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file core/core_api_peer_get_info.c
 * @brief implementation of the peer_change_preference functions 
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_core_service.h"
#include "core.h"


struct GNUNET_CORE_InformationRequestContext 
{
  
  /**
   * Our connection to the service.
   */
  struct GNUNET_CLIENT_Connection *client;

  /**
   * Function to call with the information.
   */
  GNUNET_CORE_PeerConfigurationInfoCallback info;

  /**
   * Closure for info.
   */
  void *info_cls;

};


/**
 * Receive reply from core service with information about a peer.
 *
 * @param cls our 'struct  GNUNET_CORE_InformationRequestContext *'
 * @param msg NULL on error (i.e. timeout)
 */
static void
receive_info (void *cls,
	      const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_CORE_InformationRequestContext *irc = cls;
  const struct ConfigurationInfoMessage *cim;
  static struct GNUNET_BANDWIDTH_Value32NBO zbw; /* zero bandwidth */

  if (msg == NULL)
    {
      if (irc->info != NULL)
	irc->info (irc->info_cls, 
		   NULL, zbw, zbw, 0, 0);     
      GNUNET_CLIENT_disconnect (irc->client, GNUNET_NO);
      GNUNET_free (irc);
      return;
    }
  if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO) ||
       (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage)) )
    {
      GNUNET_break (0);
      if (irc->info != NULL)
	irc->info (irc->info_cls, 
		   NULL, zbw, zbw, 0, 0);     
      GNUNET_CLIENT_disconnect (irc->client, GNUNET_NO);
      GNUNET_free (irc);
      return;
    }
  cim = (const struct ConfigurationInfoMessage*) msg;
  if (irc->info != NULL)
    irc->info (irc->info_cls,
	       &cim->peer,
	       cim->bw_in,
	       cim->bw_out,
	       ntohl (cim->reserved_amount),
	       GNUNET_ntohll (cim->preference));  
  GNUNET_CLIENT_disconnect (irc->client, GNUNET_NO);
  GNUNET_free (irc);
}


/**
 * Obtain statistics and/or change preferences for the given peer.
 *
 * @param cfg configuration to use
 * @param peer identifies the peer
 * @param timeout after how long should we give up (and call "info" with NULL
 *                for "peer" to signal an error)?
 * @param bw_out set to the current bandwidth limit (sending) for this peer,
 *                caller should set "bw_out" to "-1" to avoid changing
 *                the current value; otherwise "bw_out" will be lowered to
 *                the specified value; passing a pointer to "0" can be used to force
 *                us to disconnect from the peer; "bw_out" might not increase
 *                as specified since the upper bound is generally
 *                determined by the other peer!
 * @param amount reserve N bytes for receiving, negative
 *                amounts can be used to undo a (recent) reservation;
 * @param preference increase incoming traffic share preference by this amount;
 *                in the absence of "amount" reservations, we use this
 *                preference value to assign proportional bandwidth shares
 *                to all connected peers
 * @param info function to call with the resulting configuration information
 * @param info_cls closure for info
 * @return NULL on error
 */
struct GNUNET_CORE_InformationRequestContext *
GNUNET_CORE_peer_change_preference (const struct GNUNET_CONFIGURATION_Handle *cfg,
				    const struct GNUNET_PeerIdentity *peer,
				    struct GNUNET_TIME_Relative timeout,
				    struct GNUNET_BANDWIDTH_Value32NBO bw_out,
				    int32_t amount,
				    uint64_t preference,
				    GNUNET_CORE_PeerConfigurationInfoCallback info,
				    void *info_cls)
{
  struct GNUNET_CORE_InformationRequestContext *irc;
  struct RequestInfoMessage rim;
  struct GNUNET_CLIENT_Connection *client;
  int retry;

  client = GNUNET_CLIENT_connect ("core", cfg);
  if (client == NULL)
    return NULL;
  irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
  irc->client = client;
  irc->info = info;
  irc->info_cls = info_cls;
  rim.header.size = htons (sizeof (struct RequestInfoMessage));
  rim.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
  rim.reserved = htonl (0);
  rim.limit_outbound = bw_out;
  rim.reserve_inbound = htonl (amount);
  rim.preference_change = GNUNET_htonll(preference);
  rim.peer = *peer;
  retry = ( (amount == 0) && (preference == 0) ) ? GNUNET_YES : GNUNET_NO;
  GNUNET_assert (GNUNET_OK == GNUNET_CLIENT_transmit_and_get_response (client,
								       &rim.header,
								       timeout,
								       retry,
								       &receive_info,
								       irc));  
  return irc;
}


/**
 * Cancel request for getting information about a peer.
 *
 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
 */
void
GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
{
  GNUNET_CLIENT_disconnect (irc->client, GNUNET_NO);
  GNUNET_free (irc);
}

/* end of core_api_peer_get_info.c */