style fixes, minor bugfixes
[oweals/gnunet.git] / src / core / core_api_peer_request.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 3, 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_request.c
23  * @brief implementation of the peer_request functions 
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_core_service.h"
28 #include "core.h"
29
30
31 /**
32  * Handle for a request to the core to connect to
33  * a particular peer.  Can be used to cancel the request
34  * (before the 'cont'inuation is called).
35  */
36 struct GNUNET_CORE_PeerRequestHandle
37 {
38
39   /**
40    * Our connection to the service.
41    */
42   struct GNUNET_CLIENT_Connection *client;
43
44   /**
45    * Scheduler.
46    */
47   struct GNUNET_SCHEDULER_Handle *sched;
48
49   /**
50    * Function to call once done.
51    */
52   GNUNET_SCHEDULER_Task cont;
53   
54   /**
55    * Closure for 'cont'.
56    */
57   void *cont_cls;
58
59   /**
60    * When to time out.
61    */
62   struct GNUNET_TIME_Absolute timeout;
63
64   /**
65    * Identity of the peer to connect to.
66    */
67   struct GNUNET_PeerIdentity peer;
68         
69   /**
70    * Message type to use.
71    */
72   uint16_t type;
73 };
74
75
76 /**
77  * Transmit the request to the core service.
78  *
79  * @param cls our 'struct GNUNET_CORE_PeerRequestHandle'
80  * @param size number of bytes available in buf
81  * @param buf where the callee should write the message
82  * @return number of bytes written to buf
83  */ 
84 static size_t
85 send_request (void *cls,
86               size_t size,
87               void *buf)
88 {
89   struct GNUNET_CORE_PeerRequestHandle * prh = cls;
90   struct ConnectMessage msg;
91
92   if (buf == NULL)
93     {
94       if (prh->cont != NULL)
95         GNUNET_SCHEDULER_add_continuation (prh->sched,
96                                            prh->cont,
97                                            prh->cont_cls,
98                                            GNUNET_SCHEDULER_REASON_TIMEOUT);
99       GNUNET_CLIENT_disconnect (prh->client, GNUNET_NO);
100       GNUNET_free (prh);
101       return 0;
102     }
103   GNUNET_assert (size >= sizeof (struct ConnectMessage));
104   msg.header.type = htons (prh->type);
105   msg.header.size = htons (sizeof (struct ConnectMessage));
106   msg.reserved = htonl (0);
107   msg.timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (prh->timeout));
108   msg.peer = prh->peer;
109   memcpy (buf, &msg, sizeof (msg));
110   if (prh->cont != NULL)
111     {
112       GNUNET_SCHEDULER_add_continuation (prh->sched,
113                                          prh->cont,
114                                          prh->cont_cls,
115                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
116     }
117   GNUNET_CLIENT_disconnect (prh->client, GNUNET_YES);
118   GNUNET_free (prh);
119   return sizeof (msg);
120 }
121
122
123 /**
124  * Request that the core should try to connect to a particular peer.
125  * Once the request has been transmitted to the core, the continuation
126  * function will be called.  Note that this does NOT mean that a
127  * connection was successfully established -- it only means that the
128  * core will now try.  Successful establishment of the connection
129  * will be signalled to the 'connects' callback argument of
130  * 'GNUNET_CORE_connect' only.  If the core service does not respond
131  * to our connection attempt within the given time frame, 'cont' will
132  * be called with the TIMEOUT reason code.
133  *
134  * @param sched scheduler to use
135  * @param cfg configuration to use
136  * @param timeout how long to try to talk to core
137  * @param peer who should we connect to
138  * @param cont function to call once the request has been completed (or timed out)
139  * @param cont_cls closure for cont
140  * @return NULL on error (cont will not be called), otherwise handle for cancellation
141  */
142 struct GNUNET_CORE_PeerRequestHandle *
143 GNUNET_CORE_peer_request_connect (struct GNUNET_SCHEDULER_Handle *sched,
144                                   const struct GNUNET_CONFIGURATION_Handle *cfg,
145                                   struct GNUNET_TIME_Relative timeout,
146                                   const struct GNUNET_PeerIdentity * peer,
147                                   GNUNET_SCHEDULER_Task cont,
148                                   void *cont_cls)
149 {
150   struct GNUNET_CORE_PeerRequestHandle *ret;
151   struct GNUNET_CLIENT_Connection *client;
152   
153   client = GNUNET_CLIENT_connect (sched, "core", cfg);
154   if (client == NULL)
155     return NULL;
156   ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
157   ret->client = client;
158   ret->sched = sched;
159   ret->cont = cont;
160   ret->cont_cls = cont_cls;
161   ret->peer = *peer;
162   ret->type = GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT;
163   ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
164   GNUNET_CLIENT_notify_transmit_ready (client,
165                                        sizeof (struct ConnectMessage),
166                                        timeout,
167                                        GNUNET_YES,
168                                        &send_request,
169                                        ret);
170   return ret;
171 }
172
173
174 /**
175  * Cancel a pending request to connect to a particular peer.  Must not
176  * be called after the 'cont' function was invoked.
177  *
178  * @param req request handle that was returned for the original request
179  */
180 void
181 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
182 {
183   GNUNET_CLIENT_disconnect (req->client, GNUNET_NO);
184   GNUNET_free (req);
185 }
186
187
188 /* end of core_api_peer_request.c */