- * @param service_name name of service to connect to
- * @param cfg configuration to use
- * @param attempt counter used to alternate between IP and UNIX domain sockets
- * @return NULL on error
- */
-static struct GNUNET_CONNECTION_Handle *
-do_connect (const char *service_name,
- const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt)
-{
- struct GNUNET_CONNECTION_Handle *connection;
- char *hostname;
- unsigned long long port;
-
- connection = NULL;
- if (0 == (attempt % 2))
- {
- /* on even rounds, try UNIX first */
- connection = try_unixpath (service_name, cfg);
- if (NULL != connection)
- return connection;
- }
- if (GNUNET_YES ==
- GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
- {
- if ((GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port))
- || (port > 65535) ||
- (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
- &hostname)))
- {
- LOG (GNUNET_ERROR_TYPE_WARNING,
- _
- ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
- service_name);
- return NULL;
- }
- if (0 == strlen (hostname))
- {
- GNUNET_free (hostname);
- LOG (GNUNET_ERROR_TYPE_WARNING,
- _("Need a non-empty hostname for service `%s'.\n"), service_name);
- return NULL;
- }
- }
- else
- {
- /* unspecified means 0 (disabled) */
- port = 0;
- hostname = NULL;
- }
- if (0 == port)
- {
- /* if port is 0, try UNIX */
- connection = try_unixpath (service_name, cfg);
- if (NULL != connection)
- return connection;
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
- service_name);
- GNUNET_free_non_null (hostname);
- return NULL;
- }
- connection = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
- GNUNET_free (hostname);
- return connection;
-}
-
-
-/**
- * Get a connection with a service.
- *
- * @param service_name name of the service
- * @param cfg configuration to use
- * @return NULL on error (service unknown to configuration)
- */
-struct GNUNET_CLIENT_Connection *
-GNUNET_CLIENT_connect (const char *service_name,
- const struct GNUNET_CONFIGURATION_Handle *cfg)
-{
- struct GNUNET_CLIENT_Connection *client;
- struct GNUNET_CONNECTION_Handle *connection;
-
- if (GNUNET_OK !=
- test_service_configuration (service_name,
- cfg))
- return NULL;
- connection = do_connect (service_name, cfg, 0);
- client = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
- client->first_message = GNUNET_YES;
- client->attempts = 1;
- client->connection = connection;
- client->service_name = GNUNET_strdup (service_name);
- client->cfg = cfg;
- client->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
- return client;
-}
-
-
-/**
- * Destroy connection with the service. This will automatically
- * cancel any pending "receive" request (however, the handler will
- * *NOT* be called, not even with a NULL message). Any pending
- * transmission request will also be cancelled UNLESS the callback for
- * the transmission request has already been called, in which case the
- * transmission 'finish_pending_write' argument determines whether or
- * not the write is guaranteed to complete before the socket is fully
- * destroyed (unless, of course, there is an error with the server in
- * which case the message may still be lost).
- *
- * @param client handle to the service connection