api call for connected peer lookup by peer id
[oweals/gnunet.git] / src / transport / transport_api_peer_address_lookup.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 #include "platform.h"
21 #include "gnunet_client_lib.h"
22 #include "gnunet_arm_service.h"
23 #include "gnunet_hello_lib.h"
24 #include "gnunet_protocols.h"
25 #include "gnunet_server_lib.h"
26 #include "gnunet_time_lib.h"
27 #include "gnunet_transport_service.h"
28 #include "transport.h"
29
30 /**
31  * Context for the address lookup.
32  */
33 struct AddressLookupCtx
34 {
35   /**
36    * Function to call with the human-readable address.
37    */
38   GNUNET_TRANSPORT_AddressLookUpCallback cb;
39
40   /**
41    * Closure for cb.
42    */
43   void *cb_cls;
44
45   /**
46    * Connection to the service.
47    */
48   struct GNUNET_CLIENT_Connection *client;
49
50   /**
51    * When should this operation time out?
52    */
53   struct GNUNET_TIME_Absolute timeout;
54 };
55
56
57 /**
58  * Function called with responses from the service.
59  *
60  * @param cls our 'struct AddressLookupCtx*'
61  * @param msg NULL on timeout or error, otherwise presumably a
62  *        message with the human-readable address
63  */
64 static void
65 peer_address_response_processor (void *cls,
66                                  const struct GNUNET_MessageHeader *msg)
67 {
68   struct AddressLookupCtx *alucb = cls;
69   const char *address;
70   uint16_t size;
71
72   if (msg == NULL)
73     {
74       alucb->cb (alucb->cb_cls, NULL);
75       GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
76       GNUNET_free (alucb);
77       return;
78     }
79   GNUNET_break (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
80   size = ntohs (msg->size);
81   if (size == sizeof (struct GNUNET_MessageHeader))
82     {
83       /* done! */
84       alucb->cb (alucb->cb_cls, NULL);
85       GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
86       GNUNET_free (alucb);
87       return;
88     }
89   address = (const char *) &msg[1];
90   if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
91     {
92       /* invalid reply */
93       GNUNET_break (0);
94       alucb->cb (alucb->cb_cls, NULL);
95       GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
96       GNUNET_free (alucb);
97       return;
98     }
99   /* expect more replies */
100   GNUNET_CLIENT_receive (alucb->client,
101                          &peer_address_response_processor, alucb,
102                          GNUNET_TIME_absolute_get_remaining
103                          (alucb->timeout));
104   alucb->cb (alucb->cb_cls, address);
105 }
106
107
108 /**
109  * Return all the known addresses for a peer.
110  *
111  * @param cfg configuration to use
112  * @param peer peer identity to look up the addresses of
113  * @param timeout how long is the lookup allowed to take at most
114  * @param peer_address_callback function to call with the results
115  * @param peer_address_callback_cls closure for peer_address_callback
116  */
117 void
118 GNUNET_TRANSPORT_peer_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg,
119                                       const struct GNUNET_PeerIdentity *peer,
120                                       struct GNUNET_TIME_Relative timeout,
121                                       GNUNET_TRANSPORT_AddressLookUpCallback peer_address_callback,
122                                       void *peer_address_callback_cls)
123 {
124   struct PeerAddressLookupMessage msg;
125   struct GNUNET_TIME_Absolute abs_timeout;
126   struct AddressLookupCtx *peer_address_lookup_cb;
127   struct GNUNET_CLIENT_Connection *client;
128
129   client = GNUNET_CLIENT_connect ("transport", cfg);
130   if (client == NULL)
131     {
132       peer_address_callback (peer_address_callback_cls, NULL);
133       return;
134     }
135   abs_timeout = GNUNET_TIME_relative_to_absolute (timeout);
136
137   msg.header.size = htons (sizeof(struct PeerAddressLookupMessage));
138   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PEER_ADDRESS_LOOKUP);
139   msg.timeout = GNUNET_TIME_absolute_hton (abs_timeout);
140   memcpy(&msg.peer, peer, sizeof(struct GNUNET_PeerIdentity));
141   peer_address_lookup_cb = GNUNET_malloc (sizeof (struct AddressLookupCtx));
142   peer_address_lookup_cb->cb = peer_address_callback;
143   peer_address_lookup_cb->cb_cls = peer_address_callback_cls;
144   peer_address_lookup_cb->timeout = abs_timeout;
145   peer_address_lookup_cb->client = client;
146   GNUNET_assert (GNUNET_OK ==
147                  GNUNET_CLIENT_transmit_and_get_response (client,
148                                                           &msg.header,
149                                                           timeout,
150                                                           GNUNET_YES,
151                                                           &peer_address_response_processor,
152                                                           peer_address_lookup_cb));
153 }
154
155 /* end of transport_api_peer_address_lookup.c */