aboutsummaryrefslogtreecommitdiff
path: root/src/core/core_api_peer_request.c
blob: fa5764af6e31b1d7a98426f24ceec2d305525788 (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
176
177
178
179
180
181
182
183
184
/*
     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_request.c
 * @brief implementation of the peer_request functions 
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_core_service.h"
#include "core.h"


/**
 * Handle for a request to the core to connect to
 * a particular peer.  Can be used to cancel the request
 * (before the 'cont'inuation is called).
 */
struct GNUNET_CORE_PeerRequestHandle
{

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

  /**
   * Scheduler.
   */
  struct GNUNET_SCHEDULER_Handle *sched;

  /**
   * Function to call once done.
   */
  GNUNET_SCHEDULER_Task cont;
  
  /**
   * Closure for 'cont'.
   */
  void *cont_cls;

  /**
   * When to time out.
   */
  struct GNUNET_TIME_Absolute timeout;

  /**
   * Identity of the peer to connect to.
   */
  struct GNUNET_PeerIdentity peer;
	
  /**
   * Message type to use.
   */
  uint16_t type;
};


/**
 * Transmit the request to the core service.
 *
 * @param cls our 'struct GNUNET_CORE_PeerRequestHandle'
 * @param size number of bytes available in buf
 * @param buf where the callee should write the message
 * @return number of bytes written to buf
 */ 
static size_t
send_request (void *cls,
	      size_t size,
	      void *buf)
{
  struct GNUNET_CORE_PeerRequestHandle * prh = cls;
  struct ConnectMessage msg;

  if (buf == NULL)
    {
      GNUNET_SCHEDULER_add_continuation (prh->sched,
					 prh->cont,
					 prh->cont_cls,
					 GNUNET_SCHEDULER_REASON_TIMEOUT);
      GNUNET_CLIENT_disconnect (prh->client, GNUNET_NO);
      GNUNET_free (prh);
      return 0;
    }
  GNUNET_assert (size >= sizeof (struct ConnectMessage));
  msg.header.type = htons (prh->type);
  msg.header.size = htons (sizeof (struct ConnectMessage));
  msg.reserved = htonl (0);
  msg.timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (prh->timeout));
  msg.peer = prh->peer;
  memcpy (buf, &msg, sizeof (msg));
  GNUNET_SCHEDULER_add_continuation (prh->sched,
				     prh->cont,
				     prh->cont_cls,
				     GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  GNUNET_CLIENT_disconnect (prh->client, GNUNET_YES);
  GNUNET_free (prh);
  return sizeof (msg);
}


/**
 * Request that the core should try to connect to a particular peer.
 * Once the request has been transmitted to the core, the continuation
 * function will be called.  Note that this does NOT mean that a
 * connection was successfully established -- it only means that the
 * core will now try.  Successful establishment of the connection
 * will be signalled to the 'connects' callback argument of
 * 'GNUNET_CORE_connect' only.  If the core service does not respond
 * to our connection attempt within the given time frame, 'cont' will
 * be called with the TIMEOUT reason code.
 *
 * @param sched scheduler to use
 * @param cfg configuration to use
 * @param timeout how long to try to talk to core
 * @param peer who should we connect to
 * @param cont function to call once the request has been completed (or timed out)
 * @param cont_cls closure for cont
 * @return NULL on error (cont will not be called), otherwise handle for cancellation
 */
struct GNUNET_CORE_PeerRequestHandle *
GNUNET_CORE_peer_request_connect (struct GNUNET_SCHEDULER_Handle *sched,
				  const struct GNUNET_CONFIGURATION_Handle *cfg,
				  struct GNUNET_TIME_Relative timeout,
				  const struct GNUNET_PeerIdentity * peer,
				  GNUNET_SCHEDULER_Task cont,
				  void *cont_cls)
{
  struct GNUNET_CORE_PeerRequestHandle *ret;
  struct GNUNET_CLIENT_Connection *client;
  
  client = GNUNET_CLIENT_connect (sched, "core", cfg);
  if (client == NULL)
    return NULL;
  ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
  ret->client = client;
  ret->sched = sched;
  ret->cont = cont;
  ret->cont_cls = cont_cls;
  ret->peer = *peer;
  ret->type = GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT;
  ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  GNUNET_CLIENT_notify_transmit_ready (client,
				       sizeof (struct ConnectMessage),
				       timeout,
				       GNUNET_YES,
				       &send_request,
				       ret);
  return ret;
}


/**
 * Cancel a pending request to connect to a particular peer.  Must not
 * be called after the 'cont' function was invoked.
 *
 * @param req request handle that was returned for the original request
 */
void
GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
{
  GNUNET_CLIENT_disconnect (req->client, GNUNET_NO);
  GNUNET_free (req);
}


/* end of core_api_peer_request.c */