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 892 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_free_non_null, 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().

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

1062 {
1063  struct ClientState *cstate;
1064 
1065  if (GNUNET_OK !=
1067  cfg))
1068  return NULL;
1069  cstate = GNUNET_new (struct ClientState);
1071  cstate->cfg = cfg;
1073  cstate);
1074  cstate->mst = GNUNET_MST_create (&recv_message,
1075  cstate);
1076  if (GNUNET_YES ==
1078  service_name,
1079  "PORT"))
1080  {
1081  if (! ((GNUNET_OK !=
1083  service_name,
1084  "PORT",
1085  &cstate->port)) ||
1086  (cstate->port > 65535) ||
1087  (GNUNET_OK !=
1089  service_name,
1090  "HOSTNAME",
1091  &cstate->hostname))) &&
1092  (0 == strlen (cstate->hostname)))
1093  {
1094  GNUNET_free (cstate->hostname);
1095  cstate->hostname = NULL;
1097  _ ("Need a non-empty hostname for service `%s'.\n"),
1098  service_name);
1099  }
1100  }
1104  cstate,
1105  handlers,
1106  error_handler,
1107  error_handler_cls);
1108  return cstate->mq;
1109 }
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:862
static void connection_client_destroy_impl(struct GNUNET_MQ_Handle *mq, void *impl_state)
Implement the destruction of a message queue.
Definition: client.c:375
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: client.c:115
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#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:181
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:552
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:324
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:1280
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:774
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:714
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
#define GNUNET_YES
Definition: gnunet_common.h:77
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:828
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function: