diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-05-01 14:51:28 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-05-01 14:51:28 +0000 |
commit | 9c22a5acbf2ae9eecc83fa616a2e80b5351908f0 (patch) | |
tree | e90f2d2b90363bf80972462816e99a6356823148 /src | |
parent | 624967dfd6e1bfdeae0052e6fb1d614893638f63 (diff) | |
download | gnunet-9c22a5acbf2ae9eecc83fa616a2e80b5351908f0.tar.gz gnunet-9c22a5acbf2ae9eecc83fa616a2e80b5351908f0.zip |
-fixing #2289
Diffstat (limited to 'src')
-rw-r--r-- | src/util/client.c | 127 | ||||
-rw-r--r-- | src/util/test_client.c | 4 |
2 files changed, 89 insertions, 42 deletions
diff --git a/src/util/client.c b/src/util/client.c index e098f2183..c29b48e6b 100644 --- a/src/util/client.c +++ b/src/util/client.c | |||
@@ -248,6 +248,79 @@ struct GNUNET_CLIENT_Connection | |||
248 | 248 | ||
249 | 249 | ||
250 | /** | 250 | /** |
251 | * Try connecting to the server using UNIX domain sockets. | ||
252 | * | ||
253 | * @param service_name name of service to connect to | ||
254 | * @param cfg configuration to use | ||
255 | * @return NULL on error, connection to UNIX otherwise | ||
256 | */ | ||
257 | static struct GNUNET_CONNECTION_Handle * | ||
258 | try_unixpath (const char *service_name, | ||
259 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
260 | { | ||
261 | #if AF_UNIX | ||
262 | struct GNUNET_CONNECTION_Handle *connection; | ||
263 | char *unixpath; | ||
264 | |||
265 | unixpath = NULL; | ||
266 | if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && | ||
267 | (0 < strlen (unixpath))) | ||
268 | { | ||
269 | /* We have a non-NULL unixpath, need to validate it */ | ||
270 | connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); | ||
271 | if (NULL != connection) | ||
272 | { | ||
273 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", | ||
274 | unixpath); | ||
275 | GNUNET_free (unixpath); | ||
276 | return connection; | ||
277 | } | ||
278 | } | ||
279 | GNUNET_free_non_null (unixpath); | ||
280 | #endif | ||
281 | return NULL; | ||
282 | } | ||
283 | |||
284 | |||
285 | /** | ||
286 | * Try connecting to the server using UNIX domain sockets. | ||
287 | * | ||
288 | * @param service_name name of service to connect to | ||
289 | * @param cfg configuration to use | ||
290 | * @return GNUNET_OK if the configuration is valid, GNUNET_SYSERR if not | ||
291 | */ | ||
292 | static int | ||
293 | test_service_configuration (const char *service_name, | ||
294 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
295 | { | ||
296 | int ret = GNUNET_SYSERR; | ||
297 | char *hostname = NULL; | ||
298 | unsigned long long port; | ||
299 | #if AF_UNIX | ||
300 | char *unixpath = NULL; | ||
301 | |||
302 | if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && | ||
303 | (0 < strlen (unixpath))) | ||
304 | ret = GNUNET_OK; | ||
305 | GNUNET_free_non_null (unixpath); | ||
306 | #endif | ||
307 | |||
308 | if ( (GNUNET_YES == | ||
309 | GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT")) && | ||
310 | (GNUNET_OK == | ||
311 | GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port)) && | ||
312 | (port <= 65535) && (0 != port) && | ||
313 | (GNUNET_OK == | ||
314 | GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME", | ||
315 | &hostname)) && | ||
316 | (0 != strlen (hostname)) ) | ||
317 | ret = GNUNET_OK; | ||
318 | GNUNET_free_non_null (hostname); | ||
319 | return ret; | ||
320 | } | ||
321 | |||
322 | |||
323 | /** | ||
251 | * Try to connect to the service. | 324 | * Try to connect to the service. |
252 | * | 325 | * |
253 | * @param service_name name of service to connect to | 326 | * @param service_name name of service to connect to |
@@ -261,32 +334,16 @@ do_connect (const char *service_name, | |||
261 | { | 334 | { |
262 | struct GNUNET_CONNECTION_Handle *connection; | 335 | struct GNUNET_CONNECTION_Handle *connection; |
263 | char *hostname; | 336 | char *hostname; |
264 | char *unixpath; | ||
265 | unsigned long long port; | 337 | unsigned long long port; |
266 | 338 | ||
267 | connection = NULL; | 339 | connection = NULL; |
268 | #if AF_UNIX | ||
269 | if (0 == (attempt % 2)) | 340 | if (0 == (attempt % 2)) |
270 | { | 341 | { |
271 | /* on even rounds, try UNIX */ | 342 | /* on even rounds, try UNIX first */ |
272 | unixpath = NULL; | 343 | connection = try_unixpath (service_name, cfg); |
273 | if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && | 344 | if (NULL != connection) |
274 | (0 < strlen (unixpath))) | 345 | return connection; |
275 | { | ||
276 | /* We have a non-NULL unixpath, need to validate it */ | ||
277 | connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); | ||
278 | if (NULL != connection) | ||
279 | { | ||
280 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", | ||
281 | unixpath); | ||
282 | GNUNET_free (unixpath); | ||
283 | return connection; | ||
284 | } | ||
285 | } | ||
286 | GNUNET_free_non_null (unixpath); | ||
287 | } | 346 | } |
288 | #endif | ||
289 | |||
290 | if (GNUNET_YES == | 347 | if (GNUNET_YES == |
291 | GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT")) | 348 | GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT")) |
292 | { | 349 | { |
@@ -319,28 +376,10 @@ do_connect (const char *service_name, | |||
319 | } | 376 | } |
320 | if (0 == port) | 377 | if (0 == port) |
321 | { | 378 | { |
322 | #if AF_UNIX | 379 | /* if port is 0, try UNIX */ |
323 | if (0 != (attempt % 2)) | 380 | connection = try_unixpath (service_name, cfg); |
324 | { | 381 | if (NULL != connection) |
325 | /* try UNIX */ | 382 | return connection; |
326 | unixpath = NULL; | ||
327 | if ((GNUNET_OK == | ||
328 | GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", | ||
329 | &unixpath)) && | ||
330 | (0 < strlen (unixpath))) | ||
331 | { | ||
332 | connection = | ||
333 | GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath); | ||
334 | if (NULL != connection) | ||
335 | { | ||
336 | GNUNET_free (unixpath); | ||
337 | GNUNET_free_non_null (hostname); | ||
338 | return connection; | ||
339 | } | ||
340 | } | ||
341 | GNUNET_free_non_null (unixpath); | ||
342 | } | ||
343 | #endif | ||
344 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 383 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
345 | "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n", | 384 | "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n", |
346 | service_name); | 385 | service_name); |
@@ -367,6 +406,10 @@ GNUNET_CLIENT_connect (const char *service_name, | |||
367 | struct GNUNET_CLIENT_Connection *client; | 406 | struct GNUNET_CLIENT_Connection *client; |
368 | struct GNUNET_CONNECTION_Handle *connection; | 407 | struct GNUNET_CONNECTION_Handle *connection; |
369 | 408 | ||
409 | if (GNUNET_OK != | ||
410 | test_service_configuration (service_name, | ||
411 | cfg)) | ||
412 | return NULL; | ||
370 | connection = do_connect (service_name, cfg, 0); | 413 | connection = do_connect (service_name, cfg, 0); |
371 | client = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection)); | 414 | client = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection)); |
372 | client->attempts = 1; | 415 | client->attempts = 1; |
diff --git a/src/util/test_client.c b/src/util/test_client.c index 92b9ed49f..54881b2dd 100644 --- a/src/util/test_client.c +++ b/src/util/test_client.c | |||
@@ -137,6 +137,10 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
137 | struct sockaddr *sap[2]; | 137 | struct sockaddr *sap[2]; |
138 | socklen_t slens[2]; | 138 | socklen_t slens[2]; |
139 | 139 | ||
140 | /* test that ill-configured client fails instantly */ | ||
141 | GNUNET_assert (NULL == GNUNET_CLIENT_connect ("invalid-service", cfg)); | ||
142 | |||
143 | /* test IPC between client and server */ | ||
140 | sap[0] = (struct sockaddr *) &sa; | 144 | sap[0] = (struct sockaddr *) &sa; |
141 | slens[0] = sizeof (sa); | 145 | slens[0] = sizeof (sa); |
142 | sap[1] = NULL; | 146 | sap[1] = NULL; |