GNUnet 0.28.1-dev.5-1-gae1c02d74
 
Loading...
Searching...
No Matches
Client library

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

Collaboration diagram for Client library:

Functions

enum GNUNET_GenericReturnValue 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.
 
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.
 

Detailed Description

Generic client-side communication with services.

See also
Documentation

Function Documentation

◆ GNUNET_CLIENT_test()

enum GNUNET_GenericReturnValue 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 1006 of file client.c.

1008{
1009 char *hostname = NULL;
1010 unsigned long long port;
1011 int ret;
1012
1013#if AF_UNIX
1014 {
1015 char *unixpath = NULL;
1016
1017 if (GNUNET_OK ==
1020 "UNIXPATH",
1021 &unixpath))
1022 {
1023 if (0 == strlen (unixpath))
1024 {
1025 GNUNET_free (unixpath);
1026 return GNUNET_SYSERR; /* empty string not OK */
1027 }
1028 if (0 == access (unixpath,
1029 F_OK))
1030 {
1031 enum GNUNET_GenericReturnValue ures;
1032
1033 if (arm_bound (cfg,
1034 service_name))
1035 {
1036 GNUNET_free (unixpath);
1037 return GNUNET_YES; /* ARM is listening, do not wake the service */
1038 }
1039 ures = test_unixpath (unixpath);
1040 GNUNET_free (unixpath);
1041 if (GNUNET_NO != ures)
1042 return ures;
1043 /* socket file exists, but nobody is listening on it */
1044 }
1045 else
1046 {
1047 GNUNET_free (unixpath);
1048 }
1049 if (GNUNET_YES !=
1052 "PORT"))
1053 return GNUNET_NO; /* UNIXPATH is all we had to go by */
1054 }
1055 else if (GNUNET_OK ==
1058 "UNIXPATH"))
1059 {
1060 /* UNIXPATH specified but not a valid path! */
1063 "UNIXPATH",
1064 _ ("not a valid filename"));
1065 return GNUNET_SYSERR;
1066 }
1067 }
1068#endif
1069
1070 if ( (GNUNET_OK !=
1073 "PORT",
1074 &port)) ||
1075 (port > 65535) ||
1076 (0 == port) )
1077 {
1080 "PORT");
1081 return GNUNET_SYSERR;
1082 }
1083 if (GNUNET_OK ==
1086 "HOSTNAME",
1087 &hostname))
1088 {
1089 /* We always assume remotes are up */
1090 ret = GNUNET_YES;
1091 }
1092 else
1093 {
1094 /* We look for evidence the service is up */
1095 ret = GNUNET_NO;
1096 }
1097 if ( (NULL == hostname) ||
1098 (0 == strcasecmp (hostname,
1099 "localhost")) ||
1100 (0 == strcasecmp (hostname,
1101 "ip6-localnet")) )
1102 {
1103 /* service runs on loopback */
1104 struct sockaddr_in v4;
1105 struct sockaddr_in6 v6;
1106 int sock;
1107
1108 memset (&v4, 0, sizeof (v4));
1109 memset (&v6, 0, sizeof (v6));
1110 v4.sin_family = AF_INET;
1111 v4.sin_port = htons ((uint16_t) port);
1112#if HAVE_SOCKADDR_IN_SUN_LEN
1113 v4.sin_len = (u_char) sizeof(struct sockaddr_in);
1114#endif
1115 GNUNET_assert (1 == inet_pton (AF_INET,
1116 "127.0.0.1",
1117 &v4.sin_addr));
1118 ret = GNUNET_NO;
1119 sock = socket (AF_INET,
1120 SOCK_STREAM,
1121 0);
1122 if (-1 != sock)
1123 {
1124 if (0 != bind (sock,
1125 (struct sockaddr *) &v4,
1126 sizeof (v4)))
1127 {
1128 /* bind failed, so someone is listening! */
1129 ret = GNUNET_YES;
1130 }
1131 (void) close (sock);
1132 }
1133 else
1134 {
1136 "socket");
1137 if (GNUNET_NO == ret)
1139 }
1140 v6.sin6_family = AF_INET6;
1141 v6.sin6_port = htons ((uint16_t) port);
1142#if HAVE_SOCKADDR_IN_SUN_LEN
1143 v6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
1144#endif
1145 inet_pton (AF_INET6,
1146 "::1",
1147 &v6.sin6_addr);
1148 sock = socket (AF_INET6,
1149 SOCK_STREAM,
1150 0);
1151 if (-1 != sock)
1152 {
1153 if (0 != bind (sock,
1154 (struct sockaddr *) &v6,
1155 sizeof (v6)))
1156 {
1157 /* bind failed, so someone is listening! */
1158 ret = GNUNET_YES;
1159 }
1160 (void) close (sock);
1161 }
1162 else
1163 {
1165 "socket");
1166 /* not changing 'ret' intentionally here, as
1167 v4 succeeding and v6 failing just means we
1168 should use v4 */
1169 }
1170 }
1171 else
1172 {
1173 /* service running remotely */
1174 ret = GNUNET_OK;
1175 }
1176 GNUNET_free (hostname);
1177 return ret;
1178}
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
static uint16_t port
Port number.
Definition gnunet-bcd.c:146
static char * service_name
Option -s: service name (hash to get service descriptor)
Definition gnunet-vpn.c:50
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.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#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:179

References _, cfg, GNUNET_assert, 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_config_missing(), GNUNET_log_strerror, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, port, ret, and service_name.

Referenced by arm_is_running(), 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
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 1182 of file client.c.

1187{
1188 struct ClientState *cstate;
1189
1190 if (GNUNET_OK !=
1192 cfg))
1193 return NULL;
1194 cstate = GNUNET_new (struct ClientState);
1196 cstate->cfg = cfg;
1198 cstate);
1200 cstate);
1201 if (GNUNET_YES ==
1204 "PORT"))
1205 {
1206 if (! ((GNUNET_OK !=
1209 "PORT",
1210 &cstate->port)) ||
1211 (cstate->port > 65535) ||
1212 (GNUNET_SYSERR ==
1215 "HOSTNAME",
1216 &cstate->hostname))) &&
1217 (0 == strlen (cstate->hostname)))
1218 {
1219 GNUNET_free (cstate->hostname);
1220 cstate->hostname = NULL;
1222 _ ("Need a non-empty hostname for service `%s'.\n"),
1223 service_name);
1224 }
1225 }
1229 cstate,
1230 handlers,
1232 error_handler_cls);
1233 return cstate->mq;
1234}
struct GNUNET_MQ_MessageHandlers handlers[]
Definition 003.c:1
static void error_handler(void *cls, enum GNUNET_MQ_Error error)
Function called on connection trouble.
static void connection_client_cancel_impl(struct GNUNET_MQ_Handle *mq, void *impl_state)
Cancel the currently sent message.
Definition client.c:873
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:841
static void start_connect(void *cls)
Try to connect to the service.
Definition client.c:787
static enum GNUNET_GenericReturnValue recv_message(void *cls, const struct GNUNET_MessageHeader *msg)
We have received a full message, pass to the MQ dispatcher.
Definition client.c:335
static void connection_client_destroy_impl(struct GNUNET_MQ_Handle *mq, void *impl_state)
Implement the destruction of a message queue.
Definition client.c:386
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:726
#define LOG(kind,...)
Definition client.c:37
#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:514
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:1310
struct GNUNET_MessageStreamTokenizer * GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
Definition mst.c:86
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:116
struct GNUNET_MessageStreamTokenizer * mst
Tokenizer for inbound messages.
Definition client.c:163
struct GNUNET_SCHEDULER_Task * retry_task
Task for trying to connect to the service.
Definition client.c:148
char * hostname
Hostname, if any.
Definition client.c:138
unsigned long long port
TCP port (0 for disabled).
Definition client.c:184
char * service_name
Name of the service we interact with.
Definition client.c:133

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_SYSERR, GNUNET_YES, handlers, ClientState::hostname, LOG, ClientState::mq, ClientState::mst, ClientState::port, recv_message(), ClientState::retry_task, service_name, ClientState::service_name, start_connect(), and test_service_configuration().

Referenced by addr_cb(), addr_cb(), announce_reconnect(), connect_task(), create_internal(), create_loc_uri(), do_connect(), do_reconnect(), 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_TRANSPORT_links_list(), hash_for_index_cb(), listen_connect(), listen_connect(), listen_connect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect(), reconnect_arm(), reconnect_arm_monitor(), reconnect_phone(), reconnect_task(), search_reconnect(), test_nat_punched(), try_connect(), try_connect(), try_reconnect(), and unindex_finish().

Here is the call graph for this function: