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 (i.e. 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 (i.e. socket() failed)

Definition at line 898 of file client.c.

References _, 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 AddressProbe::sock.

Referenced by action_loop(), and run().

900 {
901  char *hostname = NULL;
902  unsigned long long port;
903  int ret;
904 
905 #if AF_UNIX
906  {
907  char *unixpath = NULL;
908 
909  if (GNUNET_OK ==
911  service_name,
912  "UNIXPATH",
913  &unixpath))
914  {
915  if (0 == strlen (unixpath))
916  {
917  GNUNET_free (unixpath);
918  return GNUNET_SYSERR; /* empty string not OK */
919  }
920  if (0 == access (unixpath,
921  F_OK))
922  {
923  GNUNET_free (unixpath);
924  return GNUNET_OK; /* file exists, we assume service is running */
925  }
926  GNUNET_free (unixpath);
927  }
928  else if (GNUNET_OK ==
930  service_name,
931  "UNIXPATH"))
932  {
933  /* UNIXPATH specified but not a valid path! */
935  service_name,
936  "UNIXPATH",
937  _ ("not a valid filename"));
938  return GNUNET_SYSERR;
939  }
940  }
941 #endif
942 
943  if ( (GNUNET_OK !=
945  service_name,
946  "PORT",
947  &port)) ||
948  (port > 65535) ||
949  (0 == port) )
950  {
951  return GNUNET_SYSERR;
952  }
953  if (GNUNET_OK ==
955  service_name,
956  "HOSTNAME",
957  &hostname))
958  {
959  /* We always assume remotes are up */
960  ret = GNUNET_YES;
961  }
962  else
963  {
964  /* We look for evidence the service is up */
965  ret = GNUNET_NO;
966  }
967  if ( (NULL == hostname) ||
968  (0 == strcasecmp (hostname,
969  "localhost")) ||
970  (0 == strcasecmp (hostname,
971  "ip6-localnet")) )
972  {
973  /* service runs on loopback */
974  struct sockaddr_in v4;
975  struct sockaddr_in6 v6;
976  int sock;
977 
978  memset (&v4, 0, sizeof (v4));
979  memset (&v6, 0, sizeof (v6));
980  v4.sin_family = AF_INET;
981  v4.sin_port = htons ((uint16_t) port);
982 #if HAVE_SOCKADDR_IN_SUN_LEN
983  v4.sin_len = (u_char) sizeof(struct sockaddr_in);
984 #endif
985  inet_pton (AF_INET,
986  "127.0.0.1",
987  &v4.sin_addr);
988  ret = GNUNET_NO;
989  sock = socket (AF_INET,
990  SOCK_STREAM,
991  0);
992  if (-1 != sock)
993  {
994  if (0 != bind (sock,
995  (struct sockaddr *) &v4,
996  sizeof (v4)))
997  {
998  /* bind failed, so someone is listening! */
999  ret = GNUNET_YES;
1000  }
1001  (void) close (sock);
1002  }
1003  else
1004  {
1006  "socket");
1007  if (GNUNET_NO == ret)
1008  ret = GNUNET_SYSERR;
1009  }
1010  v6.sin6_family = AF_INET6;
1011  v6.sin6_port = htons ((uint16_t) port);
1012 #if HAVE_SOCKADDR_IN_SUN_LEN
1013  v6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
1014 #endif
1015  inet_pton (AF_INET6,
1016  "::1",
1017  &v6.sin6_addr);
1018  sock = socket (AF_INET6,
1019  SOCK_STREAM,
1020  0);
1021  if (-1 != sock)
1022  {
1023  if (0 != bind (sock,
1024  (struct sockaddr *) &v6,
1025  sizeof (v6)))
1026  {
1027  /* bind failed, so someone is listening! */
1028  ret = GNUNET_YES;
1029  }
1030  (void) close (sock);
1031  }
1032  else
1033  {
1035  "socket");
1036  /* not changing 'ret' intentionally here, as
1037  v4 succeeding and v6 failing just means we
1038  should use v4 */
1039  }
1040  }
1041  else
1042  {
1043  /* service running remotely */
1044  ret = GNUNET_OK;
1045  }
1046  GNUNET_free (hostname);
1047  return ret;
1048 }
int 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.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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 _(String)
GNU gettext support macro.
Definition: platform.h:184
#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...
int 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.
static char * service_name
Option -s: service name (hash to get service descriptor)
Definition: gnunet-vpn.c:51
int 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.
static char * hostname
Our hostname; we give this to all the peers we start.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:82
int 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.
#define GNUNET_free(ptr)
Wrapper around free.
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 1063 of file client.c.

References _, cfg, ClientState::cfg, connection_client_cancel_impl(), connection_client_destroy_impl(), connection_client_send_impl(), AddressProbe::cstate, 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, ClientState::hostname, LOG, ClientState::mq, ClientState::mst, ClientState::port, recv_message(), ClientState::retry_task, ClientState::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().

1068 {
1069  struct ClientState *cstate;
1070 
1071  if (GNUNET_OK !=
1073  cfg))
1074  return NULL;
1075  cstate = GNUNET_new (struct ClientState);
1077  cstate->cfg = cfg;
1079  cstate);
1080  cstate->mst = GNUNET_MST_create (&recv_message,
1081  cstate);
1082  if (GNUNET_YES ==
1084  service_name,
1085  "PORT"))
1086  {
1087  if (! ((GNUNET_OK !=
1089  service_name,
1090  "PORT",
1091  &cstate->port)) ||
1092  (cstate->port > 65535) ||
1093  (GNUNET_OK !=
1095  service_name,
1096  "HOSTNAME",
1097  &cstate->hostname))) &&
1098  (0 == strlen (cstate->hostname)))
1099  {
1100  GNUNET_free (cstate->hostname);
1101  cstate->hostname = NULL;
1103  _ ("Need a non-empty hostname for service `%s'.\n"),
1104  service_name);
1105  }
1106  }
1110  cstate,
1111  handlers,
1112  error_handler,
1113  error_handler_cls);
1114  return cstate->mq;
1115 }
State we keep per client.
int 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.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
struct GNUNET_MQ_Handle * mq
MQ to talk to client.
#define LOG(kind,...)
Definition: client.c:36
static void connection_client_cancel_impl(struct GNUNET_MQ_Handle *mq, void *impl_state)
Cancel the currently sent message.
Definition: client.c:868
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
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: client.c:115
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
char * service_name
Name of the service we interact with.
Definition: client.c:132
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
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
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
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
int 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.
static void start_connect(void *cls)
Try to connect to the service.
Definition: client.c:782
static char * service_name
Option -s: service name (hash to get service descriptor)
Definition: gnunet-vpn.c:51
struct GNUNET_MessageStreamTokenizer * GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
Definition: mst.c:85
int 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.
static void error_handler(void *cls, enum GNUNET_MQ_Error error)
We encountered an error handling the MQ to the ATS service.
struct GNUNET_MessageStreamTokenizer * mst
Tokenizer for inbound messages.
Definition: client.c:162
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
unsigned long long port
TCP port (0 for disabled).
Definition: client.c:183
char * hostname
Hostname, if any.
Definition: client.c:137
struct GNUNET_SCHEDULER_Task * retry_task
Task for trying to connect to the service.
Definition: client.c:147
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:836
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function: