aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api_address_lookup.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-03-18 14:46:42 +0000
committerChristian Grothoff <christian@grothoff.org>2010-03-18 14:46:42 +0000
commitafa1480d98bca6d9a6d388e464080c231b4cfc1f (patch)
tree6803b7961145fb73cc84e5a2df14d7d5d718ff2b /src/transport/transport_api_address_lookup.c
parent2fc9115f16dbc9aeebd54b55038d67209cd8d566 (diff)
downloadgnunet-afa1480d98bca6d9a6d388e464080c231b4cfc1f.tar.gz
gnunet-afa1480d98bca6d9a6d388e464080c231b4cfc1f.zip
address resolution API implementation
Diffstat (limited to 'src/transport/transport_api_address_lookup.c')
-rw-r--r--src/transport/transport_api_address_lookup.c135
1 files changed, 88 insertions, 47 deletions
diff --git a/src/transport/transport_api_address_lookup.c b/src/transport/transport_api_address_lookup.c
index 4293b18f8..cd2e73584 100644
--- a/src/transport/transport_api_address_lookup.c
+++ b/src/transport/transport_api_address_lookup.c
@@ -1,3 +1,22 @@
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*/
1#include "platform.h" 20#include "platform.h"
2#include "gnunet_client_lib.h" 21#include "gnunet_client_lib.h"
3#include "gnunet_arm_service.h" 22#include "gnunet_arm_service.h"
@@ -8,68 +27,84 @@
8#include "gnunet_transport_service.h" 27#include "gnunet_transport_service.h"
9#include "transport.h" 28#include "transport.h"
10 29
11// FIXME: document 30/**
12struct AddressLookUpCB 31 * Context for the address lookup.
32 */
33struct AddressLookupCtx
13{ 34{
35 /**
36 * Function to call with the human-readable address.
37 */
14 GNUNET_TRANSPORT_AddressLookUpCallback cb; 38 GNUNET_TRANSPORT_AddressLookUpCallback cb;
15 void *cls; 39
16 struct GNUNET_TIME_Absolute timeout; 40 /**
41 * Closure for cb.
42 */
43 void *cb_cls;
44
45 /**
46 * Connection to the service.
47 */
17 struct GNUNET_CLIENT_Connection *client; 48 struct GNUNET_CLIENT_Connection *client;
49
50 /**
51 * When should this operation time out?
52 */
53 struct GNUNET_TIME_Absolute timeout;
18}; 54};
19 55
20 56
21// FIXME: document 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 */
22static void 64static void
23address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) 65address_response_processor (void *cls,
66 const struct GNUNET_MessageHeader *msg)
24{ 67{
25 struct AddressLookUpCB *alucb = cls; 68 struct AddressLookupCtx *alucb = cls;
26 const char *address; 69 const char *address;
27 uint16_t size; 70 uint16_t size;
28 71
29 if (msg == NULL) 72 if (msg == NULL)
30 { 73 {
31 /* timeout */ 74 alucb->cb (alucb->cb_cls, NULL);
32 alucb->cb (alucb->cls, NULL); 75 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
33 GNUNET_CLIENT_disconnect (alucb->client. GNUNET_NO);
34 GNUNET_free (alucb); 76 GNUNET_free (alucb);
35 return; 77 return;
36 } 78 }
79 GNUNET_break (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
37 size = ntohs (msg->size); 80 size = ntohs (msg->size);
38 if (size == sizeof (struct GNUNET_MessageHeader)) 81 if (size == sizeof (struct GNUNET_MessageHeader))
39 { 82 {
40 /* last reply */ 83 /* done! */
41 address = NULL; 84 alucb->cb (alucb->cb_cls, NULL);
42 } 85 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
43 else 86 GNUNET_free (alucb);
44 { 87 return;
45 address = (const char *) &msg[1];
46 if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
47 {
48 /* invalid reply */
49 GNUNET_break_op (0);
50 alucb->cb (alucb->cls, NULL);
51 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
52 GNUNET_free (alucb);
53 return;
54 }
55 else
56 {
57 /* expect more replies */
58 GNUNET_CLIENT_receive (alucb->client, &address_response_processor,
59 alucb,
60 GNUNET_TIME_absolute_get_remaining
61 (alucb->timeout));
62 }
63 } 88 }
64 alucb->cb (alucb->cls, address); 89 address = (const char *) &msg[1];
65 if (address == NULL) 90 if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
66 { 91 {
67 /* done! */ 92 /* invalid reply */
93 GNUNET_break (0);
94 alucb->cb (alucb->cb_cls, NULL);
68 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); 95 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
69 GNUNET_free (alucb); 96 GNUNET_free (alucb);
97 return;
70 } 98 }
99 /* expect more replies */
100 GNUNET_CLIENT_receive (alucb->client,
101 &address_response_processor, alucb,
102 GNUNET_TIME_absolute_get_remaining
103 (alucb->timeout));
104 alucb->cb (alucb->cb_cls, address);
71} 105}
72 106
107
73/** 108/**
74 * Convert a binary address into a human readable address. 109 * Convert a binary address into a human readable address.
75 * 110 *
@@ -95,14 +130,16 @@ GNUNET_TRANSPORT_address_lookup (struct GNUNET_SCHEDULER_Handle *sched,
95 GNUNET_TRANSPORT_AddressLookUpCallback aluc, 130 GNUNET_TRANSPORT_AddressLookUpCallback aluc,
96 void *aluc_cls) 131 void *aluc_cls)
97{ 132{
98 size_t len = 133 size_t slen;
99 sizeof (struct AddressLookupMessage) + addressLen + strlen (nameTrans) + 134 size_t len;
100 1;
101 struct AddressLookupMessage *msg; 135 struct AddressLookupMessage *msg;
102 struct GNUNET_TIME_Absolute abs_timeout; 136 struct GNUNET_TIME_Absolute abs_timeout;
103 struct AddressLookUpCB *aluCB; 137 struct AddressLookupCtx *aluCB;
104 struct GNUNET_CLIENT_Connection *client; 138 struct GNUNET_CLIENT_Connection *client;
139 char *addrbuf;
105 140
141 slen = strlen (nameTrans) + 1;
142 len = sizeof (struct AddressLookupMessage) + addressLen + slen;
106 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) 143 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
107 { 144 {
108 GNUNET_break (0); 145 GNUNET_break (0);
@@ -117,22 +154,26 @@ GNUNET_TRANSPORT_address_lookup (struct GNUNET_SCHEDULER_Handle *sched,
117 } 154 }
118 abs_timeout = GNUNET_TIME_relative_to_absolute (timeout); 155 abs_timeout = GNUNET_TIME_relative_to_absolute (timeout);
119 msg = GNUNET_malloc (len); 156 msg = GNUNET_malloc (len);
120 msg->header->size = htons (len); 157 msg->header.size = htons (len);
121 msg->header->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP); 158 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP);
159 msg->numeric_only = htonl (numeric);
122 msg->timeout = GNUNET_TIME_absolute_hton (abs_timeout); 160 msg->timeout = GNUNET_TIME_absolute_hton (abs_timeout);
123 msg->addrlen = htonl (addressLen); 161 msg->addrlen = htonl (addressLen);
124 char *addrbuf = (char *) &msg[1]; 162 addrbuf = (char *) &msg[1];
125 memcpy (addrbuf, address, addressLen); 163 memcpy (addrbuf, address, addressLen);
126 char *tbuf = &addrbuf[addressLen]; 164 memcpy (&addrbuf[addressLen], nameTrans, slen);
127 memcpy (tbuf, nameTrans, strlen (nameTrans) + 1); 165 aluCB = GNUNET_malloc (sizeof (struct AddressLookupCtx));
128 aluCB = GNUNET_malloc (sizeof (struct AddressLookUpCB));
129 aluCB->cb = aluc; 166 aluCB->cb = aluc;
130 aluCB->cb_cls = aluc_cls; 167 aluCB->cb_cls = aluc_cls;
131 aluCB->timeout = abs_timeout; 168 aluCB->timeout = abs_timeout;
132 aluCB->client = client; 169 aluCB->client = client;
133 GNUNET_CLIENT_transmit_and_get_response (client, msg->header, timeout, 170 GNUNET_CLIENT_transmit_and_get_response (client,
171 &msg->header,
172 timeout,
134 GNUNET_YES, 173 GNUNET_YES,
135 &address_response_processor, 174 &address_response_processor,
136 aluCB); 175 aluCB);
137 GNUNET_free (msg); 176 GNUNET_free (msg);
138} 177}
178
179/* end of transport_api_address_lookup.c */