GNUnet  0.11.x
Functions
Client library

Generic client-side communication with services. More...

Functions

int GNUNET_CLIENT_test (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name)
 Test if the port or UNIXPATH of the given service_name is in use and thus (most likely) the respective service is up. More...
 
struct GNUNET_MQ_HandleGNUNET_CLIENT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
 Create a message queue to connect to a GNUnet service. More...
 

Detailed Description

Generic client-side communication with services.

See also
Documentation

Function Documentation

◆ GNUNET_CLIENT_test()

int GNUNET_CLIENT_test ( const struct GNUNET_CONFIGURATION_Handle cfg,
const char *  service_name 
)

Test if the port or UNIXPATH of the given service_name is in use and thus (most likely) the respective service is up.

Parameters
cfgour configuration
service_namename of the service to connect to
Returns
GNUNET_YES if the service is (likely) up (or running remotely), GNUNET_NO if the service is (definitively) down, GNUNET_SYSERR if the configuration does not give us the necessary information about the service, or if we could not check (e.g. socket() failed)
Parameters
cfgour configuration
service_namename of the service to connect to
Returns
GNUNET_YES if the service is (likely) up, GNUNET_NO if the service is (definitively) down, GNUNET_SYSERR if the configuration does not give us the necessary information about the service, or if we could not check (e.g. socket() failed)

Definition at line 899 of file client.c.

901 {
902  char *hostname = NULL;
903  unsigned long long port;
904  int ret;
905 
906 #if AF_UNIX
907  {
908  char *unixpath = NULL;
909 
910  if (GNUNET_OK ==
912  service_name,
913  "UNIXPATH",
914  &unixpath))
915  {
916  if (0 == strlen (unixpath))
917  {
918  GNUNET_free (unixpath);
919  return GNUNET_SYSERR; /* empty string not OK */
920  }
921  if (0 == access (unixpath,
922  F_OK))
923  {
924  GNUNET_free (unixpath);
925  return GNUNET_OK; /* file exists, we assume service is running */
926  }
927  GNUNET_free (unixpath);
928  }
929  else if (GNUNET_OK ==
931  service_name,
932  "UNIXPATH"))
933  {
934  /* UNIXPATH specified but not a valid path! */
936  service_name,
937  "UNIXPATH",
938  _ ("not a valid filename"));
939  return GNUNET_SYSERR;
940  }
941  }
942 #endif
943 
944  if ( (GNUNET_OK !=
946  service_name,
947  "PORT",
948  &port)) ||
949  (port > 65535) ||
950  (0 == port) )
951  {
952  return GNUNET_SYSERR;
953  }
954  if (GNUNET_OK ==
956  service_name,
957  "HOSTNAME",
958  &hostname))
959  {
960  /* We always assume remotes are up */
961  ret = GNUNET_YES;
962  }
963  else
964  {
965  /* We look for evidence the service is up */
966  ret = GNUNET_NO;
967  }
968  if ( (NULL == hostname) ||
969  (0 == strcasecmp (hostname,
970  "localhost")) ||
971  (0 == strcasecmp (hostname,
972  "ip6-localnet")) )
973  {
974  /* service runs on loopback */
975  struct sockaddr_in v4;
976  struct sockaddr_in6 v6;
977  int sock;
978 
979  memset (&v4, 0, sizeof (v4));
980  memset (&v6, 0, sizeof (v6));
981  v4.sin_family = AF_INET;
982  v4.sin_port = htons ((uint16_t) port);
983 #if HAVE_SOCKADDR_IN_SUN_LEN
984  v4.sin_len = (u_char) sizeof(struct sockaddr_in);
985 #endif
986  inet_pton (AF_INET,
987  "127.0.0.1",
988  &v4.sin_addr);
989  ret = GNUNET_NO;
990  sock = socket (AF_INET,
991  SOCK_STREAM,
992  0);
993  if (-1 != sock)
994  {
995  if (0 != bind (sock,
996  (struct sockaddr *) &v4,
997  sizeof (v4)))
998  {
999  /* bind failed, so someone is listening! */
1000  ret = GNUNET_YES;
1001  }
1002  (void) close (sock);
1003  }
1004  else
1005  {
1007  "socket");
1008  if (GNUNET_NO == ret)
1009  ret = GNUNET_SYSERR;
1010  }
1011  v6.sin6_family = AF_INET6;
1012  v6.sin6_port = htons ((uint16_t) port);
1013 #if HAVE_SOCKADDR_IN_SUN_LEN
1014  v6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
1015 #endif
1016  inet_pton (AF_INET6,
1017  "::1",
1018  &v6.sin6_addr);
1019  sock = socket (AF_INET6,
1020  SOCK_STREAM,
1021  0);
1022  if (-1 != sock)
1023  {
1024  if (0 != bind (sock,
1025  (struct sockaddr *) &v6,
1026  sizeof (v6)))
1027  {
1028  /* bind failed, so someone is listening! */
1029  ret = GNUNET_YES;
1030  }
1031  (void) close (sock);
1032  }
1033  else
1034  {
1036  "socket");
1037  /* not changing 'ret' intentionally here, as
1038  v4 succeeding and v6 failing just means we
1039  should use v4 */
1040  }
1041  }
1042  else
1043  {
1044  /* service running remotely */
1045  ret = GNUNET_OK;
1046  }
1048  return ret;
1049 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static uint16_t port
Port number.
Definition: gnunet-bcd.c:83
static char * hostname
Our hostname; we give this to all the peers we start.
static char * service_name
Option -s: service name (hash to get service descriptor)
Definition: gnunet-vpn.c:51
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_have_value(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Test if we have a value for a particular option.
void GNUNET_log_config_invalid(enum GNUNET_ErrorType kind, const char *section, const char *option, const char *required)
Log error message about invalid configuration option value.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_free(ptr)
Wrapper around free.
#define _(String)
GNU gettext support macro.
Definition: platform.h:177

References _, cfg, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_number(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_have_value(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log_config_invalid(), GNUNET_log_strerror, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, hostname, port, ret, and service_name.

Referenced by action_loop(), and run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CLIENT_connect()

struct GNUNET_MQ_Handle* GNUNET_CLIENT_connect ( const struct GNUNET_CONFIGURATION_Handle cfg,
const char *  service_name,
const struct GNUNET_MQ_MessageHandler handlers,
GNUNET_MQ_ErrorHandler  error_handler,
void *  error_handler_cls 
)

Create a message queue to connect to a GNUnet service.

If handlers are specified, receive messages from the connection.

Parameters
connectionthe client connection
handlershandlers for receiving messages, can be NULL
error_handlererror handler
error_handler_clsclosure for the error_handler
Returns
the message queue, NULL on error

If handlers are specified, receive messages from the connection.

Parameters
cfgour configuration
service_namename of the service to connect to
handlershandlers for receiving messages, can be NULL
error_handlererror handler
error_handler_clsclosure for the error_handler
Returns
the message queue, NULL on error

Definition at line 1064 of file client.c.

1069 {
1070  struct ClientState *cstate;
1071 
1072  if (GNUNET_OK !=
1074  cfg))
1075  return NULL;
1076  cstate = GNUNET_new (struct ClientState);
1078  cstate->cfg = cfg;
1080  cstate);
1081  cstate->mst = GNUNET_MST_create (&recv_message,
1082  cstate);
1083  if (GNUNET_YES ==
1085  service_name,
1086  "PORT"))
1087  {
1088  if (! ((GNUNET_OK !=
1090  service_name,
1091  "PORT",
1092  &cstate->port)) ||
1093  (cstate->port > 65535) ||
1094  (GNUNET_OK !=
1096  service_name,
1097  "HOSTNAME",
1098  &cstate->hostname))) &&
1099  (0 == strlen (cstate->hostname)))
1100  {
1101  GNUNET_free (cstate->hostname);
1102  cstate->hostname = NULL;
1104  _ ("Need a non-empty hostname for service `%s'.\n"),
1105  service_name);
1106  }
1107  }
1111  cstate,
1112  handlers,
1113  error_handler,
1114  error_handler_cls);
1115  return cstate->mq;
1116 }
static void error_handler(void *cls, enum GNUNET_MQ_Error error)
We encountered an error handling the MQ to the ATS service.
static void connection_client_cancel_impl(struct GNUNET_MQ_Handle *mq, void *impl_state)
Cancel the currently sent message.
Definition: client.c:869
static void connection_client_send_impl(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
Implements the transmission functionality of a message queue.
Definition: client.c:837
static void start_connect(void *cls)
Try to connect to the service.
Definition: client.c:783
static int recv_message(void *cls, const struct GNUNET_MessageHeader *msg)
We have received a full message, pass to the MQ dispatcher.
Definition: client.c:332
static void connection_client_destroy_impl(struct GNUNET_MQ_Handle *mq, void *impl_state)
Implement the destruction of a message queue.
Definition: client.c:383
static int test_service_configuration(const char *service_name, const struct GNUNET_CONFIGURATION_Handle *cfg)
Test whether the configuration has proper values for connection (UNIXPATH || (PORT && HOSTNAME)).
Definition: client.c:722
#define LOG(kind,...)
Definition: client.c:36
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_MQ_Handle * GNUNET_MQ_queue_for_callbacks(GNUNET_MQ_SendImpl send, GNUNET_MQ_DestroyImpl destroy, GNUNET_MQ_CancelImpl cancel, void *impl_state, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *cls)
Create a message queue for the specified handlers.
Definition: mq.c:565
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1296
struct GNUNET_MessageStreamTokenizer * GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
Definition: mst.c:85
State we keep per client.
struct GNUNET_MQ_Handle * mq
MQ to talk to client.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: client.c:115
struct GNUNET_MessageStreamTokenizer * mst
Tokenizer for inbound messages.
Definition: client.c:162
struct GNUNET_SCHEDULER_Task * retry_task
Task for trying to connect to the service.
Definition: client.c:147
char * hostname
Hostname, if any.
Definition: client.c:137
unsigned long long port
TCP port (0 for disabled).
Definition: client.c:183
char * service_name
Name of the service we interact with.
Definition: client.c:132

References _, cfg, ClientState::cfg, connection_client_cancel_impl(), connection_client_destroy_impl(), connection_client_send_impl(), error_handler(), GNUNET_CONFIGURATION_get_value_number(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_have_value(), GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_MQ_queue_for_callbacks(), GNUNET_MST_create(), GNUNET_new, GNUNET_OK, GNUNET_SCHEDULER_add_now(), GNUNET_strdup, GNUNET_YES, handlers, ClientState::hostname, LOG, ClientState::mq, ClientState::mst, ClientState::port, recv_message(), ClientState::retry_task, ClientState::service_name, service_name, start_connect(), and test_service_configuration().

Referenced by addr_cb(), announce_reconnect(), connect_task(), create_internal(), create_loc_uri(), do_connect(), do_peer_connect(), do_plugin_connect(), do_reconnect(), GNUNET_CONSENSUS_create(), GNUNET_CONVERSATION_call_start(), GNUNET_DATASTORE_disconnect(), GNUNET_FS_get_indexed_files(), GNUNET_IDENTITY_ego_lookup(), GNUNET_IDENTITY_ego_lookup_by_suffix(), GNUNET_NAT_AUTO_autoconfig_start(), GNUNET_REVOCATION_query(), GNUNET_REVOCATION_revoke(), GNUNET_SCALARPRODUCT_accept_computation(), GNUNET_SCALARPRODUCT_start_computation(), GNUNET_SECRETSHARING_create_session(), GNUNET_SECRETSHARING_decrypt(), GNUNET_SETI_create(), GNUNET_SETU_create(), GNUNET_TESTBED_barrier_wait(), GNUNET_TESTBED_controller_connect(), GNUNET_TESTBED_LOGGER_connect(), GNUNET_TRANSPORT_address_to_string(), GNUNET_TRANSPORT_offer_hello(), hash_for_index_cb(), listen_connect(), reconnect(), reconnect_arm(), reconnect_arm_monitor(), reconnect_phone(), reconnect_task(), search_reconnect(), test_nat_punched(), try_connect(), try_reconnect(), and unindex_finish().

Here is the call graph for this function:
Here is the caller graph for this function: