aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api_address_lookup.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2011-11-29 15:43:11 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2011-11-29 15:43:11 +0000
commit0c3e3371bcc2dad9c4f14561068a1752f01b1756 (patch)
tree6aa926c5f1ad962594f6c9ad9b238500ad644702 /src/transport/transport_api_address_lookup.c
parentfd2601f3fce6eefbb7e0f9aaca61f9169b25d3f4 (diff)
downloadgnunet-0c3e3371bcc2dad9c4f14561068a1752f01b1756.tar.gz
gnunet-0c3e3371bcc2dad9c4f14561068a1752f01b1756.zip
- latest changes for refactoring: iterate sends disassembled hello-address
Diffstat (limited to 'src/transport/transport_api_address_lookup.c')
-rw-r--r--src/transport/transport_api_address_lookup.c203
1 files changed, 75 insertions, 128 deletions
diff --git a/src/transport/transport_api_address_lookup.c b/src/transport/transport_api_address_lookup.c
index 385abd423..5904de5e2 100644
--- a/src/transport/transport_api_address_lookup.c
+++ b/src/transport/transport_api_address_lookup.c
@@ -65,7 +65,6 @@ struct GNUNET_TRANSPORT_PeerAddressLookupContext
65 struct GNUNET_TIME_Absolute timeout; 65 struct GNUNET_TIME_Absolute timeout;
66}; 66};
67 67
68
69/** 68/**
70 * Function called with responses from the service. 69 * Function called with responses from the service.
71 * 70 *
@@ -77,46 +76,61 @@ static void
77peer_address_response_processor (void *cls, 76peer_address_response_processor (void *cls,
78 const struct GNUNET_MessageHeader *msg) 77 const struct GNUNET_MessageHeader *msg)
79{ 78{
80 struct GNUNET_TRANSPORT_PeerAddressLookupContext *alucb = cls; 79 struct GNUNET_TRANSPORT_PeerAddressLookupContext *pal_ctx = cls;
80 struct AddressIterateResponseMessage *air_msg = (struct AddressIterateResponseMessage *) &msg[1];
81 const struct GNUNET_HELLO_Address *address; 81 const struct GNUNET_HELLO_Address *address;
82 uint16_t size; 82 uint16_t size;
83 83 if (air_msg == NULL)
84 if (msg == NULL)
85 { 84 {
86 alucb->cb (alucb->cb_cls, NULL); 85 pal_ctx->cb (pal_ctx->cb_cls, NULL);
87 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); 86 GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
88 GNUNET_free (alucb); 87 GNUNET_free (pal_ctx);
89 return; 88 return;
90 } 89 }
91 GNUNET_break (ntohs (msg->type) ==
92 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
93 size = ntohs (msg->size); 90 size = ntohs (msg->size);
91 GNUNET_break (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
94 if (size == sizeof (struct GNUNET_MessageHeader)) 92 if (size == sizeof (struct GNUNET_MessageHeader))
95 { 93 {
96 /* done! */ 94 /* done! */
97 alucb->cb (alucb->cb_cls, NULL ); 95 pal_ctx->cb (pal_ctx->cb_cls, NULL );
98 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); 96 GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
99 GNUNET_free (alucb); 97 GNUNET_free (pal_ctx);
100 return; 98 return;
101 } 99 }
102 address = (const struct GNUNET_HELLO_Address *) &msg[1]; 100
103#if 0 101 size_t tlen = ntohl(air_msg->pluginlen);
104 if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0') 102 size_t alen = ntohl(air_msg->addrlen);
103
104 if (size != sizeof (struct GNUNET_MessageHeader) + sizeof (struct AddressIterateResponseMessage) + tlen + alen)
105 {
106 GNUNET_break_op (0);
107 pal_ctx->cb (pal_ctx->cb_cls, NULL );
108 GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
109 GNUNET_free (pal_ctx);
110 return;
111 }
112
113 char * addr = (char *) &air_msg[1];
114 char * transport_name = &addr[alen];
115
116 if (transport_name[tlen] != '\0')
105 { 117 {
106 /* invalid reply */ 118 GNUNET_break_op (0);
107 GNUNET_break (0); 119 pal_ctx->cb (pal_ctx->cb_cls, NULL );
108 alucb->cb (alucb->cb_cls, NULL ); 120 GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
109 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); 121 GNUNET_free (pal_ctx);
110 GNUNET_free (alucb);
111 return; 122 return;
112 } 123 }
113#endif 124
125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "peer %s transport_name: %s\n",GNUNET_i2s(&air_msg->peer), transport_name);
126
127 address = GNUNET_HELLO_address_allocate(&air_msg->peer, transport_name, addr, alen);
128
114 /* expect more replies */ 129 /* expect more replies */
115 GNUNET_CLIENT_receive (alucb->client, &peer_address_response_processor, alucb, 130 GNUNET_CLIENT_receive (pal_ctx->client, &peer_address_response_processor, pal_ctx,
116 GNUNET_TIME_absolute_get_remaining (alucb->timeout)); 131 GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
117 132
118 /* REFACTOR FIX THIS */ 133 pal_ctx->cb (pal_ctx->cb_cls, address);
119 alucb->cb (alucb->cb_cls, address );
120} 134}
121 135
122 136
@@ -132,35 +146,45 @@ peer_address_response_processor (void *cls,
132 */ 146 */
133struct GNUNET_TRANSPORT_PeerAddressLookupContext * 147struct GNUNET_TRANSPORT_PeerAddressLookupContext *
134GNUNET_TRANSPORT_peer_get_active_addresses (const struct GNUNET_CONFIGURATION_Handle *cfg, 148GNUNET_TRANSPORT_peer_get_active_addresses (const struct GNUNET_CONFIGURATION_Handle *cfg,
135 const struct GNUNET_PeerIdentity *peer, 149 const struct GNUNET_PeerIdentity *peer,
136 int one_shot, 150 int one_shot,
137 struct GNUNET_TIME_Relative timeout, 151 struct GNUNET_TIME_Relative timeout,
138 GNUNET_TRANSPORT_AddressLookUpCallback peer_address_callback, 152 GNUNET_TRANSPORT_AddressLookUpCallback peer_address_callback,
139 void *peer_address_callback_cls) 153 void *peer_address_callback_cls)
140{ 154{
141 struct PeerAddressLookupMessage msg; 155 struct GNUNET_TRANSPORT_PeerAddressLookupContext *pal_ctx;
142 struct GNUNET_TRANSPORT_PeerAddressLookupContext *alc; 156 struct AddressIterateMessage msg;
143 struct GNUNET_CLIENT_Connection *client; 157 struct GNUNET_CLIENT_Connection *client;
158 struct GNUNET_TIME_Absolute abs_timeout;
144 159
145 client = GNUNET_CLIENT_connect ("transport", cfg); 160 client = GNUNET_CLIENT_connect ("transport", cfg);
146 if (client == NULL) 161 if (client == NULL)
162 {
163 peer_address_callback (peer_address_callback_cls, NULL);
147 return NULL; 164 return NULL;
148 msg.header.size = htons (sizeof (struct PeerAddressLookupMessage)); 165 }
149 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PEER_ADDRESS_LOOKUP); 166
150 msg.reserved = htonl (0); 167 abs_timeout = GNUNET_TIME_relative_to_absolute (timeout);
151 msg.timeout = GNUNET_TIME_relative_hton (timeout); 168
152 memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity)); 169 msg.header.size = htons (sizeof (struct AddressIterateMessage));
153 alc = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerAddressLookupContext)); 170 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE);
154 alc->cb = peer_address_callback; 171 msg.timeout = GNUNET_TIME_absolute_hton (abs_timeout);
155 alc->cb_cls = peer_address_callback_cls; 172 if (peer == NULL)
156 alc->timeout = GNUNET_TIME_relative_to_absolute (timeout); 173 memset (&msg.peer, 0 , sizeof (struct GNUNET_PeerIdentity));
157 alc->client = client; 174 else
175 memcpy (&msg.peer, peer , sizeof (struct GNUNET_PeerIdentity));
176
177 pal_ctx = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerAddressLookupContext));
178 pal_ctx->cb = peer_address_callback;
179 pal_ctx->cb_cls = peer_address_callback_cls;
180 pal_ctx->timeout = abs_timeout;
181 pal_ctx->client = client;
158 GNUNET_assert (GNUNET_OK == 182 GNUNET_assert (GNUNET_OK ==
159 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header, 183 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
160 timeout, GNUNET_YES, 184 timeout, GNUNET_YES,
161 &peer_address_response_processor, 185 &peer_address_response_processor,
162 alc)); 186 pal_ctx));
163 return alc; 187 return pal_ctx;
164} 188}
165 189
166 190
@@ -179,64 +203,7 @@ GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct
179} 203}
180 204
181/** 205/**
182 * Function called with responses from the service. 206 * Return all the known addresses for all peers.
183 *
184 * @param cls our 'struct AddressLookupCtx*'
185 * @param msg NULL on timeout or error, otherwise presumably a
186 * message with the human-readable peer and address
187 */
188static void
189peer_address_iteration_response_processor (void *cls,
190 const struct GNUNET_MessageHeader *msg)
191{
192 struct GNUNET_TRANSPORT_PeerAddressLookupContext *alucb = cls;
193 struct AddressIterateResponseMessage *arm;
194 struct GNUNET_HELLO_Address * address;
195 uint16_t size;
196
197 if (msg == NULL)
198 {
199 alucb->cb (alucb->cb_cls, NULL);
200 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
201 GNUNET_free (alucb);
202 return;
203 }
204
205 GNUNET_break (ntohs (msg->type) ==
206 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
207 size = ntohs (msg->size);
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %u size %u\n",
209 ntohs (msg->type), size);
210 if (size == sizeof (struct GNUNET_MessageHeader))
211 {
212 /* done! */
213 alucb->cb (alucb->cb_cls, NULL);
214 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
215 GNUNET_free (alucb);
216 return;
217 }
218 if (size < sizeof (struct AddressIterateResponseMessage))
219 {
220 /* invalid reply */
221 GNUNET_break (0);
222 alucb->cb (alucb->cb_cls, NULL);
223 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
224 GNUNET_free (alucb);
225 return;
226 }
227
228 arm = (struct AddressIterateResponseMessage *) &msg[1];
229 address = (struct GNUNET_HELLO_Address *) &arm[1];
230
231 /* expect more replies */
232 GNUNET_CLIENT_receive (alucb->client, &peer_address_response_processor, alucb,
233 GNUNET_TIME_absolute_get_remaining (alucb->timeout));
234 alucb->cb (alucb->cb_cls, address);
235}
236
237
238/**
239 * Return all the known addresses for a peer.
240 * 207 *
241 * @param cfg configuration to use 208 * @param cfg configuration to use
242 * @param timeout how long is the lookup allowed to take at most 209 * @param timeout how long is the lookup allowed to take at most
@@ -250,32 +217,12 @@ GNUNET_TRANSPORT_address_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
250 peer_address_callback, 217 peer_address_callback,
251 void *peer_address_callback_cls) 218 void *peer_address_callback_cls)
252{ 219{
253 struct AddressIterateMessage msg; 220 GNUNET_TRANSPORT_peer_get_active_addresses (cfg,
254 struct GNUNET_TIME_Absolute abs_timeout; 221 NULL,
255 struct GNUNET_TRANSPORT_PeerAddressLookupContext *peer_address_lookup_cb; 222 GNUNET_YES,
256 struct GNUNET_CLIENT_Connection *client; 223 timeout,
257 224 peer_address_callback,
258 client = GNUNET_CLIENT_connect ("transport", cfg); 225 peer_address_callback_cls);
259 if (client == NULL)
260 {
261 peer_address_callback (peer_address_callback_cls, NULL);
262 return;
263 }
264 abs_timeout = GNUNET_TIME_relative_to_absolute (timeout);
265
266 msg.header.size = htons (sizeof (struct AddressIterateMessage));
267 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE);
268 msg.timeout = GNUNET_TIME_absolute_hton (abs_timeout);
269 peer_address_lookup_cb = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerAddressLookupContext));
270 peer_address_lookup_cb->cb = peer_address_callback;
271 peer_address_lookup_cb->cb_cls = peer_address_callback_cls;
272 peer_address_lookup_cb->timeout = abs_timeout;
273 peer_address_lookup_cb->client = client;
274 GNUNET_assert (GNUNET_OK ==
275 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
276 timeout, GNUNET_YES,
277 &peer_address_iteration_response_processor,
278 peer_address_lookup_cb));
279} 226}
280 227
281/* end of transport_api_peer_address_lookup.c */ 228/* end of transport_api_peer_address_lookup.c */