aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api_address_to_string.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/transport_api_address_to_string.c')
-rw-r--r--src/transport/transport_api_address_to_string.c211
1 files changed, 108 insertions, 103 deletions
diff --git a/src/transport/transport_api_address_to_string.c b/src/transport/transport_api_address_to_string.c
index d5e383914..c7de39ea8 100644
--- a/src/transport/transport_api_address_to_string.c
+++ b/src/transport/transport_api_address_to_string.c
@@ -33,7 +33,8 @@
33/** 33/**
34 * Context for the address lookup. 34 * Context for the address lookup.
35 */ 35 */
36struct GNUNET_TRANSPORT_AddressToStringContext { 36struct GNUNET_TRANSPORT_AddressToStringContext
37{
37 /** 38 /**
38 * Function to call with the human-readable address. 39 * Function to call with the human-readable address.
39 */ 40 */
@@ -59,35 +60,35 @@ struct GNUNET_TRANSPORT_AddressToStringContext {
59 * @return #GNUNET_OK if message is well-formed 60 * @return #GNUNET_OK if message is well-formed
60 */ 61 */
61static int 62static int
62check_reply(void *cls, 63check_reply (void *cls,
63 const struct AddressToStringResultMessage *atsm) 64 const struct AddressToStringResultMessage *atsm)
64{ 65{
65 uint16_t size = ntohs(atsm->header.size) - sizeof(*atsm); 66 uint16_t size = ntohs (atsm->header.size) - sizeof(*atsm);
66 const char *address; 67 const char *address;
67 int result; 68 int result;
68 uint32_t addr_len; 69 uint32_t addr_len;
69 70
70 result = (int)ntohl(atsm->res); 71 result = (int) ntohl (atsm->res);
71 addr_len = ntohl(atsm->addr_len); 72 addr_len = ntohl (atsm->addr_len);
72 if (GNUNET_SYSERR == result) 73 if (GNUNET_SYSERR == result)
73 return GNUNET_OK; 74 return GNUNET_OK;
74 if (0 == size) 75 if (0 == size)
76 {
77 if (GNUNET_OK != result)
75 { 78 {
76 if (GNUNET_OK != result) 79 GNUNET_break (0);
77 { 80 return GNUNET_SYSERR;
78 GNUNET_break(0);
79 return GNUNET_SYSERR;
80 }
81 return GNUNET_OK;
82 } 81 }
83 address = (const char *)&atsm[1]; 82 return GNUNET_OK;
83 }
84 address = (const char *) &atsm[1];
84 if ((addr_len > size) || 85 if ((addr_len > size) ||
85 (address[addr_len - 1] != '\0')) 86 (address[addr_len - 1] != '\0'))
86 { 87 {
87 /* invalid reply */ 88 /* invalid reply */
88 GNUNET_break(0); 89 GNUNET_break (0);
89 return GNUNET_SYSERR; 90 return GNUNET_SYSERR;
90 } 91 }
91 return GNUNET_OK; 92 return GNUNET_OK;
92} 93}
93 94
@@ -99,40 +100,40 @@ check_reply(void *cls,
99 * @param msg message with the human-readable address 100 * @param msg message with the human-readable address
100 */ 101 */
101static void 102static void
102handle_reply(void *cls, 103handle_reply (void *cls,
103 const struct AddressToStringResultMessage *atsm) 104 const struct AddressToStringResultMessage *atsm)
104{ 105{
105 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls; 106 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls;
106 uint16_t size = ntohs(atsm->header.size) - sizeof(*atsm); 107 uint16_t size = ntohs (atsm->header.size) - sizeof(*atsm);
107 const char *address; 108 const char *address;
108 int result; 109 int result;
109 110
110 result = (int)ntohl(atsm->res); 111 result = (int) ntohl (atsm->res);
111 if (GNUNET_SYSERR == result) 112 if (GNUNET_SYSERR == result)
112 { 113 {
113 /* expect more replies; as this is not the last 114 /* expect more replies; as this is not the last
114 call, we must pass the empty string for the address */ 115 call, we must pass the empty string for the address */
115 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116 "Address resolution failed\n"); 117 "Address resolution failed\n");
117 alucb->cb(alucb->cb_cls, 118 alucb->cb (alucb->cb_cls,
118 "", 119 "",
119 GNUNET_NO); 120 GNUNET_NO);
120 return; 121 return;
121 } 122 }
122 if (0 == size) 123 if (0 == size)
123 { 124 {
124 /* we are done (successfully, without communication errors) */ 125 /* we are done (successfully, without communication errors) */
125 alucb->cb(alucb->cb_cls, 126 alucb->cb (alucb->cb_cls,
126 NULL, 127 NULL,
127 GNUNET_OK); 128 GNUNET_OK);
128 GNUNET_TRANSPORT_address_to_string_cancel(alucb); 129 GNUNET_TRANSPORT_address_to_string_cancel (alucb);
129 return; 130 return;
130 } 131 }
131 address = (const char *)&atsm[1]; 132 address = (const char *) &atsm[1];
132 /* return normal reply to caller, also expect more replies */ 133 /* return normal reply to caller, also expect more replies */
133 alucb->cb(alucb->cb_cls, 134 alucb->cb (alucb->cb_cls,
134 address, 135 address,
135 GNUNET_OK); 136 GNUNET_OK);
136} 137}
137 138
138 139
@@ -146,17 +147,17 @@ handle_reply(void *cls,
146 * @param error error code 147 * @param error error code
147 */ 148 */
148static void 149static void
149mq_error_handler(void *cls, 150mq_error_handler (void *cls,
150 enum GNUNET_MQ_Error error) 151 enum GNUNET_MQ_Error error)
151{ 152{
152 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls; 153 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls;
153 154
154 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Disconnected from transport, address resolution failed\n"); 156 "Disconnected from transport, address resolution failed\n");
156 alucb->cb(alucb->cb_cls, 157 alucb->cb (alucb->cb_cls,
157 NULL, 158 NULL,
158 GNUNET_SYSERR); 159 GNUNET_SYSERR);
159 GNUNET_TRANSPORT_address_to_string_cancel(alucb); 160 GNUNET_TRANSPORT_address_to_string_cancel (alucb);
160} 161}
161 162
162 163
@@ -173,21 +174,23 @@ mq_error_handler(void *cls,
173 * @return handle to cancel the operation, NULL on error 174 * @return handle to cancel the operation, NULL on error
174 */ 175 */
175struct GNUNET_TRANSPORT_AddressToStringContext * 176struct GNUNET_TRANSPORT_AddressToStringContext *
176GNUNET_TRANSPORT_address_to_string(const struct GNUNET_CONFIGURATION_Handle *cfg, 177GNUNET_TRANSPORT_address_to_string (const struct
177 const struct GNUNET_HELLO_Address *address, 178 GNUNET_CONFIGURATION_Handle *cfg,
178 int numeric, 179 const struct GNUNET_HELLO_Address *address,
179 struct GNUNET_TIME_Relative timeout, 180 int numeric,
180 GNUNET_TRANSPORT_AddressToStringCallback aluc, 181 struct GNUNET_TIME_Relative timeout,
181 void *aluc_cls) 182 GNUNET_TRANSPORT_AddressToStringCallback
183 aluc,
184 void *aluc_cls)
182{ 185{
183 struct GNUNET_TRANSPORT_AddressToStringContext *alc 186 struct GNUNET_TRANSPORT_AddressToStringContext *alc
184 = GNUNET_new(struct GNUNET_TRANSPORT_AddressToStringContext); 187 = GNUNET_new (struct GNUNET_TRANSPORT_AddressToStringContext);
185 struct GNUNET_MQ_MessageHandler handlers[] = { 188 struct GNUNET_MQ_MessageHandler handlers[] = {
186 GNUNET_MQ_hd_var_size(reply, 189 GNUNET_MQ_hd_var_size (reply,
187 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY, 190 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY,
188 struct AddressToStringResultMessage, 191 struct AddressToStringResultMessage,
189 alc), 192 alc),
190 GNUNET_MQ_handler_end() 193 GNUNET_MQ_handler_end ()
191 }; 194 };
192 size_t alen; 195 size_t alen;
193 size_t slen; 196 size_t slen;
@@ -196,49 +199,49 @@ GNUNET_TRANSPORT_address_to_string(const struct GNUNET_CONFIGURATION_Handle *cfg
196 char *addrbuf; 199 char *addrbuf;
197 200
198 alen = address->address_length; 201 alen = address->address_length;
199 slen = strlen(address->transport_name) + 1; 202 slen = strlen (address->transport_name) + 1;
200 if ((alen + slen >= GNUNET_MAX_MESSAGE_SIZE 203 if ((alen + slen >= GNUNET_MAX_MESSAGE_SIZE
201 - sizeof(struct AddressLookupMessage)) || 204 - sizeof(struct AddressLookupMessage)) ||
202 (alen >= GNUNET_MAX_MESSAGE_SIZE) || 205 (alen >= GNUNET_MAX_MESSAGE_SIZE) ||
203 (slen >= GNUNET_MAX_MESSAGE_SIZE)) 206 (slen >= GNUNET_MAX_MESSAGE_SIZE))
204 { 207 {
205 GNUNET_break(0); 208 GNUNET_break (0);
206 GNUNET_free(alc); 209 GNUNET_free (alc);
207 return NULL; 210 return NULL;
208 } 211 }
209 alc->cb = aluc; 212 alc->cb = aluc;
210 alc->cb_cls = aluc_cls; 213 alc->cb_cls = aluc_cls;
211 alc->mq = GNUNET_CLIENT_connect(cfg, 214 alc->mq = GNUNET_CLIENT_connect (cfg,
212 "transport", 215 "transport",
213 handlers, 216 handlers,
214 &mq_error_handler, 217 &mq_error_handler,
215 alc); 218 alc);
216 if (NULL == alc->mq) 219 if (NULL == alc->mq)
217 { 220 {
218 GNUNET_break(0); 221 GNUNET_break (0);
219 GNUNET_free(alc); 222 GNUNET_free (alc);
220 return NULL; 223 return NULL;
221 } 224 }
222 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 225 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
223 "Client tries to resolve for peer `%s' address plugin %s len %u\n", 226 "Client tries to resolve for peer `%s' address plugin %s len %u\n",
224 GNUNET_i2s(&address->peer), 227 GNUNET_i2s (&address->peer),
225 address->transport_name, 228 address->transport_name,
226 (unsigned int)address->address_length); 229 (unsigned int) address->address_length);
227 env = GNUNET_MQ_msg_extra(msg, 230 env = GNUNET_MQ_msg_extra (msg,
228 alen + slen, 231 alen + slen,
229 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING); 232 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING);
230 msg->numeric_only = htons((int16_t)numeric); 233 msg->numeric_only = htons ((int16_t) numeric);
231 msg->addrlen = htons((uint16_t)alen); 234 msg->addrlen = htons ((uint16_t) alen);
232 msg->timeout = GNUNET_TIME_relative_hton(timeout); 235 msg->timeout = GNUNET_TIME_relative_hton (timeout);
233 addrbuf = (char *)&msg[1]; 236 addrbuf = (char *) &msg[1];
234 GNUNET_memcpy(addrbuf, 237 GNUNET_memcpy (addrbuf,
235 address->address, 238 address->address,
236 alen); 239 alen);
237 GNUNET_memcpy(&addrbuf[alen], 240 GNUNET_memcpy (&addrbuf[alen],
238 address->transport_name, 241 address->transport_name,
239 slen); 242 slen);
240 GNUNET_MQ_send(alc->mq, 243 GNUNET_MQ_send (alc->mq,
241 env); 244 env);
242 return alc; 245 return alc;
243} 246}
244 247
@@ -249,10 +252,12 @@ GNUNET_TRANSPORT_address_to_string(const struct GNUNET_CONFIGURATION_Handle *cfg
249 * @param alc the context handle 252 * @param alc the context handle
250 */ 253 */
251void 254void
252GNUNET_TRANSPORT_address_to_string_cancel(struct GNUNET_TRANSPORT_AddressToStringContext *alc) 255GNUNET_TRANSPORT_address_to_string_cancel (struct
256 GNUNET_TRANSPORT_AddressToStringContext
257 *alc)
253{ 258{
254 GNUNET_MQ_destroy(alc->mq); 259 GNUNET_MQ_destroy (alc->mq);
255 GNUNET_free(alc); 260 GNUNET_free (alc);
256} 261}
257 262
258 263