diff options
Diffstat (limited to 'src/transport/transport_api_address_to_string.c')
-rw-r--r-- | src/transport/transport_api_address_to_string.c | 128 |
1 files changed, 75 insertions, 53 deletions
diff --git a/src/transport/transport_api_address_to_string.c b/src/transport/transport_api_address_to_string.c index d881f9a08..afd0c7deb 100644 --- a/src/transport/transport_api_address_to_string.c +++ b/src/transport/transport_api_address_to_string.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | 3 | (C) 2009-2014 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -17,6 +17,11 @@ | |||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | /** | ||
21 | * @file transport/transport_api_address_to_string.c | ||
22 | * @author Christian Grothoff | ||
23 | * @brief enable clients to convert addresses to human readable strings | ||
24 | */ | ||
20 | #include "platform.h" | 25 | #include "platform.h" |
21 | #include "gnunet_util_lib.h" | 26 | #include "gnunet_util_lib.h" |
22 | #include "gnunet_arm_service.h" | 27 | #include "gnunet_arm_service.h" |
@@ -36,7 +41,7 @@ struct GNUNET_TRANSPORT_AddressToStringContext | |||
36 | GNUNET_TRANSPORT_AddressToStringCallback cb; | 41 | GNUNET_TRANSPORT_AddressToStringCallback cb; |
37 | 42 | ||
38 | /** | 43 | /** |
39 | * Closure for cb. | 44 | * Closure for @e cb. |
40 | */ | 45 | */ |
41 | void *cb_cls; | 46 | void *cb_cls; |
42 | 47 | ||
@@ -45,10 +50,6 @@ struct GNUNET_TRANSPORT_AddressToStringContext | |||
45 | */ | 50 | */ |
46 | struct GNUNET_CLIENT_Connection *client; | 51 | struct GNUNET_CLIENT_Connection *client; |
47 | 52 | ||
48 | /** | ||
49 | * When should this operation time out? | ||
50 | */ | ||
51 | struct GNUNET_TIME_Absolute timeout; | ||
52 | }; | 53 | }; |
53 | 54 | ||
54 | 55 | ||
@@ -64,18 +65,17 @@ address_response_processor (void *cls, | |||
64 | const struct GNUNET_MessageHeader *msg) | 65 | const struct GNUNET_MessageHeader *msg) |
65 | { | 66 | { |
66 | struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls; | 67 | struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls; |
67 | struct AddressToStringResultMessage *atsm; | 68 | const struct AddressToStringResultMessage *atsm; |
68 | const char *address; | 69 | const char *address; |
69 | uint16_t size; | 70 | uint16_t size; |
70 | uint32_t result; | 71 | int result; |
71 | uint32_t addr_len; | 72 | uint32_t addr_len; |
72 | char *empty_str = ""; | ||
73 | 73 | ||
74 | if (NULL == msg) | 74 | if (NULL == msg) |
75 | { | 75 | { |
76 | alucb->cb (alucb->cb_cls, | 76 | alucb->cb (alucb->cb_cls, |
77 | NULL, | 77 | NULL, |
78 | GNUNET_OK); | 78 | GNUNET_SYSERR); |
79 | GNUNET_CLIENT_disconnect (alucb->client); | 79 | GNUNET_CLIENT_disconnect (alucb->client); |
80 | GNUNET_free (alucb); | 80 | GNUNET_free (alucb); |
81 | return; | 81 | return; |
@@ -86,54 +86,72 @@ address_response_processor (void *cls, | |||
86 | size = ntohs (msg->size); | 86 | size = ntohs (msg->size); |
87 | if (size < sizeof (struct AddressToStringResultMessage)) | 87 | if (size < sizeof (struct AddressToStringResultMessage)) |
88 | { | 88 | { |
89 | alucb->cb (alucb->cb_cls, NULL, GNUNET_OK); | 89 | GNUNET_break (0); |
90 | alucb->cb (alucb->cb_cls, | ||
91 | NULL, | ||
92 | GNUNET_SYSERR); | ||
90 | GNUNET_CLIENT_disconnect (alucb->client); | 93 | GNUNET_CLIENT_disconnect (alucb->client); |
91 | GNUNET_free (alucb); | 94 | GNUNET_free (alucb); |
92 | return; | 95 | return; |
93 | } | 96 | } |
94 | atsm = (struct AddressToStringResultMessage *) msg; | 97 | atsm = (const struct AddressToStringResultMessage *) msg; |
95 | 98 | result = (int) ntohl (atsm->res); | |
96 | result = ntohl (atsm->res); | ||
97 | addr_len = ntohl (atsm->addr_len); | 99 | addr_len = ntohl (atsm->addr_len); |
98 | 100 | if (GNUNET_SYSERR == result) | |
99 | if (size == (sizeof (struct AddressToStringResultMessage))) | ||
100 | { | 101 | { |
101 | /* done, success depends on result */ | 102 | /* expect more replies; as this is not the last |
102 | alucb->cb (alucb->cb_cls, NULL, result); | 103 | call, we must pass the empty string for the address */ |
103 | GNUNET_CLIENT_disconnect (alucb->client); | 104 | alucb->cb (alucb->cb_cls, |
104 | GNUNET_free (alucb); | 105 | "", |
106 | GNUNET_NO); | ||
107 | GNUNET_CLIENT_receive (alucb->client, | ||
108 | &address_response_processor, | ||
109 | alucb, | ||
110 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
105 | return; | 111 | return; |
106 | } | 112 | } |
107 | 113 | if (size == (sizeof (struct AddressToStringResultMessage))) | |
108 | if (GNUNET_NO == result) | ||
109 | { | 114 | { |
110 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Client %p failed to resolve address \n", | 115 | if (GNUNET_OK != result) |
111 | alucb->client); | 116 | { |
112 | GNUNET_break (0); | 117 | GNUNET_break (0); |
113 | alucb->cb (alucb->cb_cls, empty_str, GNUNET_SYSERR); | 118 | alucb->cb (alucb->cb_cls, |
114 | 119 | NULL, | |
115 | /* expect more replies */ | 120 | GNUNET_SYSERR); |
116 | GNUNET_CLIENT_receive (alucb->client, &address_response_processor, alucb, | 121 | GNUNET_CLIENT_disconnect (alucb->client); |
117 | GNUNET_TIME_absolute_get_remaining (alucb->timeout)); | 122 | GNUNET_free (alucb); |
123 | return; | ||
124 | } | ||
125 | /* we are done (successfully, without communication errors) */ | ||
126 | alucb->cb (alucb->cb_cls, | ||
127 | NULL, | ||
128 | GNUNET_OK); | ||
129 | GNUNET_CLIENT_disconnect (alucb->client); | ||
130 | GNUNET_free (alucb); | ||
118 | return; | 131 | return; |
119 | } | 132 | } |
120 | |||
121 | address = (const char *) &atsm[1]; | 133 | address = (const char *) &atsm[1]; |
122 | if ( (addr_len > (size - (sizeof (struct AddressToStringResultMessage)))) || | 134 | if ( (addr_len > (size - (sizeof (struct AddressToStringResultMessage)))) || |
123 | (address[addr_len -1] != '\0') ) | 135 | (address[addr_len -1] != '\0') ) |
124 | { | 136 | { |
125 | /* invalid reply */ | 137 | /* invalid reply */ |
126 | GNUNET_break (0); | 138 | GNUNET_break (0); |
127 | alucb->cb (alucb->cb_cls, NULL, GNUNET_SYSERR); | 139 | alucb->cb (alucb->cb_cls, |
140 | NULL, | ||
141 | GNUNET_SYSERR); | ||
128 | GNUNET_CLIENT_disconnect (alucb->client); | 142 | GNUNET_CLIENT_disconnect (alucb->client); |
129 | GNUNET_free (alucb); | 143 | GNUNET_free (alucb); |
130 | return; | 144 | return; |
131 | } | 145 | } |
132 | |||
133 | /* expect more replies */ | 146 | /* expect more replies */ |
134 | GNUNET_CLIENT_receive (alucb->client, &address_response_processor, alucb, | 147 | GNUNET_CLIENT_receive (alucb->client, |
135 | GNUNET_TIME_absolute_get_remaining (alucb->timeout)); | 148 | &address_response_processor, |
136 | alucb->cb (alucb->cb_cls, address, GNUNET_OK); | 149 | alucb, |
150 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
151 | /* return normal reply to caller */ | ||
152 | alucb->cb (alucb->cb_cls, | ||
153 | address, | ||
154 | GNUNET_OK); | ||
137 | } | 155 | } |
138 | 156 | ||
139 | 157 | ||
@@ -146,17 +164,16 @@ address_response_processor (void *cls, | |||
146 | * (otherwise do reverse DNS lookup) | 164 | * (otherwise do reverse DNS lookup) |
147 | * @param timeout how long is the lookup allowed to take at most | 165 | * @param timeout how long is the lookup allowed to take at most |
148 | * @param aluc function to call with the results | 166 | * @param aluc function to call with the results |
149 | * @param aluc_cls closure for aluc | 167 | * @param aluc_cls closure for @a aluc |
150 | * @return handle to cancel the operation, NULL on error | 168 | * @return handle to cancel the operation, NULL on error |
151 | */ | 169 | */ |
152 | struct GNUNET_TRANSPORT_AddressToStringContext * | 170 | struct GNUNET_TRANSPORT_AddressToStringContext * |
153 | GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle | 171 | GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cfg, |
154 | *cfg, | ||
155 | const struct GNUNET_HELLO_Address *address, | 172 | const struct GNUNET_HELLO_Address *address, |
156 | int numeric, | 173 | int numeric, |
157 | struct GNUNET_TIME_Relative timeout, | 174 | struct GNUNET_TIME_Relative timeout, |
158 | GNUNET_TRANSPORT_AddressToStringCallback | 175 | GNUNET_TRANSPORT_AddressToStringCallback aluc, |
159 | aluc, void *aluc_cls) | 176 | void *aluc_cls) |
160 | { | 177 | { |
161 | size_t len; | 178 | size_t len; |
162 | size_t alen; | 179 | size_t alen; |
@@ -166,7 +183,7 @@ GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle | |||
166 | struct GNUNET_CLIENT_Connection *client; | 183 | struct GNUNET_CLIENT_Connection *client; |
167 | char *addrbuf; | 184 | char *addrbuf; |
168 | 185 | ||
169 | GNUNET_assert (address != NULL); | 186 | GNUNET_assert (NULL != address); |
170 | alen = address->address_length; | 187 | alen = address->address_length; |
171 | slen = strlen (address->transport_name) + 1; | 188 | slen = strlen (address->transport_name) + 1; |
172 | len = sizeof (struct AddressLookupMessage) + alen + slen; | 189 | len = sizeof (struct AddressLookupMessage) + alen + slen; |
@@ -178,8 +195,11 @@ GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle | |||
178 | client = GNUNET_CLIENT_connect ("transport", cfg); | 195 | client = GNUNET_CLIENT_connect ("transport", cfg); |
179 | if (NULL == client) | 196 | if (NULL == client) |
180 | return NULL; | 197 | return NULL; |
181 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Client %p tries to resolve for peer `%s'address len %u \n", | 198 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
182 | client, GNUNET_i2s (&address->peer), address->address_length); | 199 | "Client %p tries to resolve for peer `%s'address len %u \n", |
200 | client, | ||
201 | GNUNET_i2s (&address->peer), | ||
202 | address->address_length); | ||
183 | 203 | ||
184 | msg = GNUNET_malloc (len); | 204 | msg = GNUNET_malloc (len); |
185 | msg->header.size = htons (len); | 205 | msg->header.size = htons (len); |
@@ -188,17 +208,21 @@ GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle | |||
188 | msg->addrlen = htons ((uint16_t) alen); | 208 | msg->addrlen = htons ((uint16_t) alen); |
189 | msg->timeout = GNUNET_TIME_relative_hton (timeout); | 209 | msg->timeout = GNUNET_TIME_relative_hton (timeout); |
190 | addrbuf = (char *) &msg[1]; | 210 | addrbuf = (char *) &msg[1]; |
191 | memcpy (addrbuf, address->address, alen); | 211 | memcpy (addrbuf, |
192 | memcpy (&addrbuf[alen], address->transport_name, slen); | 212 | address->address, |
193 | 213 | alen); | |
214 | memcpy (&addrbuf[alen], | ||
215 | address->transport_name, | ||
216 | slen); | ||
194 | alc = GNUNET_new (struct GNUNET_TRANSPORT_AddressToStringContext); | 217 | alc = GNUNET_new (struct GNUNET_TRANSPORT_AddressToStringContext); |
195 | alc->cb = aluc; | 218 | alc->cb = aluc; |
196 | alc->cb_cls = aluc_cls; | 219 | alc->cb_cls = aluc_cls; |
197 | alc->timeout = GNUNET_TIME_relative_to_absolute (timeout); | ||
198 | alc->client = client; | 220 | alc->client = client; |
199 | GNUNET_assert (GNUNET_OK == | 221 | GNUNET_assert (GNUNET_OK == |
200 | GNUNET_CLIENT_transmit_and_get_response (client, &msg->header, | 222 | GNUNET_CLIENT_transmit_and_get_response (client, |
201 | timeout, GNUNET_YES, | 223 | &msg->header, |
224 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
225 | GNUNET_YES, | ||
202 | &address_response_processor, | 226 | &address_response_processor, |
203 | alc)); | 227 | alc)); |
204 | GNUNET_free (msg); | 228 | GNUNET_free (msg); |
@@ -212,9 +236,7 @@ GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle | |||
212 | * @param pic the context handle | 236 | * @param pic the context handle |
213 | */ | 237 | */ |
214 | void | 238 | void |
215 | GNUNET_TRANSPORT_address_to_string_cancel (struct | 239 | GNUNET_TRANSPORT_address_to_string_cancel (struct GNUNET_TRANSPORT_AddressToStringContext *pic) |
216 | GNUNET_TRANSPORT_AddressToStringContext | ||
217 | *pic) | ||
218 | { | 240 | { |
219 | GNUNET_CLIENT_disconnect (pic->client); | 241 | GNUNET_CLIENT_disconnect (pic->client); |
220 | GNUNET_free (pic); | 242 | GNUNET_free (pic); |