GNUnet  0.10.x
Data Structures | Macros | Enumerations | Functions
service.c File Reference

functions related to starting services More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_constants.h"
#include "gnunet_resolver_service.h"
#include "speedup.h"
Include dependency graph for service.c:

Go to the source code of this file.

Data Structures

struct  ServiceListenContext
 Information the service tracks per listen operation. More...
 
struct  GNUNET_SERVICE_Handle
 Handle to a service. More...
 
struct  GNUNET_SERVICE_Client
 Handle to a client that is connected to a service. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from(kind, "util-service", __VA_ARGS__)
 
#define LOG_STRERROR(kind, syscall)   GNUNET_log_from_strerror(kind, "util-service", syscall)
 
#define LOG_STRERROR_FILE(kind, syscall, filename)   GNUNET_log_from_strerror_file(kind, "util-service", syscall, filename)
 

Enumerations

enum  SuspendReason {
  SUSPEND_STATE_NONE = 0, SUSPEND_STATE_APP = 1, SUSPEND_STATE_EMFILE = 2, SUSPEND_STATE_APP_AND_EMFILE = 3,
  SUSPEND_STATE_SHUTDOWN = 4
}
 Reasons why we might be suspended. More...
 

Functions

static int have_non_monitor_clients (struct GNUNET_SERVICE_Handle *sh)
 Check if any of the clients we have left are unrelated to monitoring. More...
 
static void do_suspend (struct GNUNET_SERVICE_Handle *sh, enum SuspendReason sr)
 Suspend accepting connections from the listen socket temporarily. More...
 
static void service_shutdown (void *cls)
 Shutdown task triggered when a service should be terminated. More...
 
static int check_ipv4_listed (const struct GNUNET_STRINGS_IPv4NetworkPolicy *list, const struct in_addr *add)
 Check if the given IP address is in the list of IP addresses. More...
 
static int check_ipv6_listed (const struct GNUNET_STRINGS_IPv6NetworkPolicy *list, const struct in6_addr *ip)
 Check if the given IP address is in the list of IP addresses. More...
 
static void do_send (void *cls)
 Task run when we are ready to transmit data to the client. More...
 
static void service_mq_send (struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
 Signature of functions implementing the sending functionality of a message queue. More...
 
static void service_mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
 Implementation function that cancels the currently sent message. More...
 
static void service_mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
 Generic error handler, called with the appropriate error code and the same closure specified at the creation of the message queue. More...
 
static void warn_no_client_continue (void *cls)
 Task run to warn about missing calls to GNUNET_SERVICE_client_continue(). More...
 
static int service_client_mst_cb (void *cls, const struct GNUNET_MessageHeader *message)
 Functions with this signature are called whenever a complete message is received by the tokenizer for a client. More...
 
static void service_client_recv (void *cls)
 A client sent us data. More...
 
static void start_client (struct GNUNET_SERVICE_Handle *sh, struct GNUNET_NETWORK_Handle *csock)
 We have successfully accepted a connection from a client. More...
 
static void accept_client (void *cls)
 We have a client. More...
 
static void do_resume (struct GNUNET_SERVICE_Handle *sh, enum SuspendReason sr)
 Resume accepting connections from the listen socket. More...
 
static void service_main (void *cls)
 First task run by any service. More...
 
static int process_acl4 (struct GNUNET_STRINGS_IPv4NetworkPolicy **ret, struct GNUNET_SERVICE_Handle *sh, const char *option)
 Parse an IPv4 access control list. More...
 
static int process_acl6 (struct GNUNET_STRINGS_IPv6NetworkPolicy **ret, struct GNUNET_SERVICE_Handle *sh, const char *option)
 Parse an IPv6 access control list. More...
 
static void add_unixpath (struct sockaddr **saddrs, socklen_t *saddrlens, const char *unixpath, int abstract)
 Add the given UNIX domain path as an address to the list (as the first entry). More...
 
static int get_server_addresses (const char *service_name, const struct GNUNET_CONFIGURATION_Handle *cfg, struct sockaddr ***addrs, socklen_t **addr_lens)
 Get the list of addresses that a server for the given service should bind to. More...
 
static struct GNUNET_NETWORK_Handleopen_listen_socket (const struct sockaddr *server_addr, socklen_t socklen)
 Create and initialize a listen socket for the server. More...
 
static int setup_service (struct GNUNET_SERVICE_Handle *sh)
 Setup service handle. More...
 
static char * get_user_name (struct GNUNET_SERVICE_Handle *sh)
 Get the name of the user that'll be used to provide the service. More...
 
static int set_user_id (struct GNUNET_SERVICE_Handle *sh)
 Set user ID. More...
 
static char * get_pid_file_name (struct GNUNET_SERVICE_Handle *sh)
 Get the name of the file where we will write the PID of the service. More...
 
static void pid_file_delete (struct GNUNET_SERVICE_Handle *sh)
 Delete the PID file that was created by our parent. More...
 
static int detach_terminal (struct GNUNET_SERVICE_Handle *sh)
 Detach from terminal. More...
 
static void teardown_service (struct GNUNET_SERVICE_Handle *sh)
 Tear down the service, closing the listen sockets and freeing the ACLs. More...
 
static void return_agpl (void *cls, const struct GNUNET_MessageHeader *msg)
 Function to return link to AGPL source upon request. More...
 
struct GNUNET_SERVICE_HandleGNUNET_SERVICE_start (const char *service_name, const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_SERVICE_ConnectHandler connect_cb, GNUNET_SERVICE_DisconnectHandler disconnect_cb, void *cls, const struct GNUNET_MQ_MessageHandler *handlers)
 Low-level function to start a service if the scheduler is already running. More...
 
void GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Handle *srv)
 Stops a service that was started with GNUNET_SERVICE_start(). More...
 
int GNUNET_SERVICE_run_ (int argc, char *const *argv, const char *service_name, enum GNUNET_SERVICE_Options options, GNUNET_SERVICE_InitCallback service_init_cb, GNUNET_SERVICE_ConnectHandler connect_cb, GNUNET_SERVICE_DisconnectHandler disconnect_cb, void *cls, const struct GNUNET_MQ_MessageHandler *handlers)
 Creates the "main" function for a GNUnet service. More...
 
void GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh)
 Suspend accepting connections from the listen socket temporarily. More...
 
void GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh)
 Resume accepting connections from the listen socket. More...
 
static void resume_client_receive (void *cls)
 Task run to resume receiving data from the client after the client called GNUNET_SERVICE_client_continue(). More...
 
void GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c)
 Continue receiving further messages from the given client. More...
 
void GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c)
 Disable the warning the server issues if a message is not acknowledged in a timely fashion. More...
 
static void finish_client_drop (void *cls)
 Asynchronously finish dropping the client. More...
 
void GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c)
 Ask the server to disconnect from the given client. More...
 
void GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh)
 Explicitly stops the service. More...
 
void GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c)
 Set the 'monitor' flag on this client. More...
 
void GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c)
 Set the persist option on this client. More...
 
struct GNUNET_MQ_HandleGNUNET_SERVICE_client_get_mq (struct GNUNET_SERVICE_Client *c)
 Obtain the message queue of c. More...
 

Detailed Description

functions related to starting services

functions related to starting services (redesign)

Author
Christian Grothoff
Christian Grothoff
Florian Dold

Definition in file service.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from(kind, "util-service", __VA_ARGS__)

◆ LOG_STRERROR

#define LOG_STRERROR (   kind,
  syscall 
)    GNUNET_log_from_strerror(kind, "util-service", syscall)

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  syscall,
  filename 
)    GNUNET_log_from_strerror_file(kind, "util-service", syscall, filename)

Definition at line 45 of file service.c.

Referenced by pid_file_delete().

Enumeration Type Documentation

◆ SuspendReason

Reasons why we might be suspended.

Enumerator
SUSPEND_STATE_NONE 

We are running normally.

SUSPEND_STATE_APP 

Application requested it.

SUSPEND_STATE_EMFILE 

OS ran out of file descriptors.

SUSPEND_STATE_APP_AND_EMFILE 

Both reasons, APP and EMFILE apply.

SUSPEND_STATE_SHUTDOWN 

Suspension because service was permanently shutdown.

Definition at line 83 of file service.c.

83  {
88 
93 
98 
103 
108 };
Both reasons, APP and EMFILE apply.
Definition: service.c:102
We are running normally.
Definition: service.c:87
Suspension because service was permanently shutdown.
Definition: service.c:107
OS ran out of file descriptors.
Definition: service.c:97
Application requested it.
Definition: service.c:92

Function Documentation

◆ have_non_monitor_clients()

static int have_non_monitor_clients ( struct GNUNET_SERVICE_Handle sh)
static

Check if any of the clients we have left are unrelated to monitoring.

Parameters
shservice to check clients for
Returns
GNUNET_YES if we have non-monitoring clients left

Definition at line 356 of file service.c.

References GNUNET_SERVICE_Handle::clients_head, GNUNET_NO, GNUNET_YES, and GNUNET_SERVICE_Client::next.

Referenced by finish_client_drop(), GNUNET_SERVICE_client_mark_monitor(), and service_shutdown().

357 {
358  for (struct GNUNET_SERVICE_Client *client = sh->clients_head; NULL != client;
359  client = client->next)
360  {
361  if (client->is_monitor)
362  continue;
363  return GNUNET_YES;
364  }
365  return GNUNET_NO;
366 }
struct GNUNET_SERVICE_Client * clients_head
Our clients, kept in a DLL.
Definition: service.c:158
#define GNUNET_NO
Definition: gnunet_common.h:78
Handle to a client that is connected to a service.
Definition: service.c:246
struct GNUNET_SERVICE_Client * next
Kept in a DLL.
Definition: service.c:250
#define GNUNET_YES
Definition: gnunet_common.h:77
Here is the caller graph for this function:

◆ do_suspend()

static void do_suspend ( struct GNUNET_SERVICE_Handle sh,
enum SuspendReason  sr 
)
static

Suspend accepting connections from the listen socket temporarily.

Resume activity using do_resume.

Parameters
shservice to stop accepting connections.
srreason for suspending accepting connections

Definition at line 377 of file service.c.

References GNUNET_assert, GNUNET_SCHEDULER_cancel(), ServiceListenContext::listen_task, ServiceListenContext::next, GNUNET_SERVICE_Handle::slc_head, and GNUNET_SERVICE_Handle::suspend_state.

Referenced by accept_client(), GNUNET_SERVICE_shutdown(), GNUNET_SERVICE_suspend(), and service_shutdown().

378 {
379  struct ServiceListenContext *slc;
380 
381  GNUNET_assert(0 == (sh->suspend_state & sr));
382  sh->suspend_state |= sr;
383  for (slc = sh->slc_head; NULL != slc; slc = slc->next)
384  {
385  if (NULL != slc->listen_task)
386  {
388  slc->listen_task = NULL;
389  }
390  }
391 }
struct ServiceListenContext * slc_head
DLL of listen sockets used to accept new connections.
Definition: service.c:148
enum SuspendReason suspend_state
Are we suspended, and if so, why?
Definition: service.c:216
struct ServiceListenContext * next
Kept in a DLL.
Definition: service.c:56
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Information the service tracks per listen operation.
Definition: service.c:52
struct GNUNET_SCHEDULER_Task * listen_task
Task scheduled to do the listening.
Definition: service.c:76
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
Here is the call graph for this function:
Here is the caller graph for this function:

◆ service_shutdown()

static void service_shutdown ( void *  cls)
static

Shutdown task triggered when a service should be terminated.

This considers active clients and the service options to see how this specific service is to be terminated, and depending on this proceeds with the shutdown logic.

Parameters
clsour struct GNUNET_SERVICE_Handle

Definition at line 403 of file service.c.

References do_suspend(), GNUNET_assert, GNUNET_NO, GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, GNUNET_SERVICE_OPTION_NONE, GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, GNUNET_SERVICE_shutdown(), have_non_monitor_clients(), GNUNET_SERVICE_Handle::options, ServiceListenContext::sh, GNUNET_SERVICE_Handle::suspend_state, and SUSPEND_STATE_SHUTDOWN.

Referenced by service_main().

404 {
405  struct GNUNET_SERVICE_Handle *sh = cls;
406 
407  switch (sh->options)
408  {
411  break;
412 
414  /* This task should never be run if we are using
415  the manual shutdown. */
416  GNUNET_assert(0);
417  break;
418 
420  if (0 == (sh->suspend_state & SUSPEND_STATE_SHUTDOWN))
424  break;
425  }
426 }
enum SuspendReason suspend_state
Are we suspended, and if so, why?
Definition: service.c:216
Handle to a service.
Definition: service.c:114
Do not trigger server shutdown on signal at all; instead, allow for the user to terminate the server ...
static void do_suspend(struct GNUNET_SERVICE_Handle *sh, enum SuspendReason sr)
Suspend accepting connections from the listen socket temporarily.
Definition: service.c:377
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_NO
Definition: gnunet_common.h:78
enum GNUNET_SERVICE_Options options
Our options.
Definition: service.c:221
Trigger a SOFT server shutdown on signals, allowing active non-monitor clients to complete their tran...
static struct SolverHandle * sh
Suspension because service was permanently shutdown.
Definition: service.c:107
static int have_non_monitor_clients(struct GNUNET_SERVICE_Handle *sh)
Check if any of the clients we have left are unrelated to monitoring.
Definition: service.c:356
void GNUNET_SERVICE_shutdown(struct GNUNET_SERVICE_Handle *sh)
Explicitly stops the service.
Definition: service.c:2370
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_ipv4_listed()

static int check_ipv4_listed ( const struct GNUNET_STRINGS_IPv4NetworkPolicy list,
const struct in_addr *  add 
)
static

Check if the given IP address is in the list of IP addresses.

Parameters
lista list of networks
addthe IP to check (in network byte order)
Returns
GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is

Definition at line 437 of file service.c.

References GNUNET_NO, GNUNET_YES, GNUNET_STRINGS_IPv4NetworkPolicy::netmask, and GNUNET_STRINGS_IPv4NetworkPolicy::network.

Referenced by accept_client().

439 {
440  unsigned int i;
441 
442  if (NULL == list)
443  return GNUNET_NO;
444  i = 0;
445  while ((0 != list[i].network.s_addr) || (0 != list[i].netmask.s_addr))
446  {
447  if ((add->s_addr & list[i].netmask.s_addr) ==
448  (list[i].network.s_addr & list[i].netmask.s_addr))
449  return GNUNET_YES;
450  i++;
451  }
452  return GNUNET_NO;
453 }
struct in_addr netmask
IPv4 netmask.
#define GNUNET_NO
Definition: gnunet_common.h:78
struct in_addr network
IPv4 address.
static int add
Desired action is to add a record.
#define GNUNET_YES
Definition: gnunet_common.h:77
Here is the caller graph for this function:

◆ check_ipv6_listed()

static int check_ipv6_listed ( const struct GNUNET_STRINGS_IPv6NetworkPolicy list,
const struct in6_addr *  ip 
)
static

Check if the given IP address is in the list of IP addresses.

Parameters
lista list of networks
ipthe IP to check (in network byte order)
Returns
GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is

Definition at line 464 of file service.c.

References GNUNET_is_zero, GNUNET_NO, and GNUNET_YES.

Referenced by accept_client().

466 {
467  unsigned int i;
468  unsigned int j;
469 
470  if (NULL == list)
471  return GNUNET_NO;
472  i = 0;
473 NEXT:
474  while (0 != GNUNET_is_zero(&list[i].network))
475  {
476  for (j = 0; j < sizeof(struct in6_addr) / sizeof(int); j++)
477  if (((((int *)ip)[j] & ((int *)&list[i].netmask)[j])) !=
478  (((int *)&list[i].network)[j] & ((int *)&list[i].netmask)[j]))
479  {
480  i++;
481  goto NEXT;
482  }
483  return GNUNET_YES;
484  }
485  return GNUNET_NO;
486 }
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_is_zero(a)
Check that memory in a is all zeros.
#define GNUNET_YES
Definition: gnunet_common.h:77
Here is the caller graph for this function:

◆ do_send()

static void do_send ( void *  cls)
static

Task run when we are ready to transmit data to the client.

Parameters
clsthe struct GNUNET_SERVICE_Client * to send to

Definition at line 496 of file service.c.

References buf, GNUNET_SERVICE_Client::drop_task, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_log_strerror, GNUNET_MQ_ERROR_WRITE, GNUNET_MQ_impl_send_continue(), GNUNET_MQ_impl_send_in_flight(), GNUNET_MQ_inject_error(), GNUNET_NETWORK_socket_send(), GNUNET_SCHEDULER_add_write_net(), GNUNET_TIME_UNIT_FOREVER_REL, LOG, GNUNET_SERVICE_Client::mq, GNUNET_SERVICE_Client::msg, GNUNET_SERVICE_Client::msg_pos, ret, GNUNET_SERVICE_Client::send_task, GNUNET_MessageHeader::size, GNUNET_SERVICE_Client::sock, and GNUNET_MessageHeader::type.

Referenced by service_mq_send().

497 {
498  struct GNUNET_SERVICE_Client *client = cls;
499  ssize_t ret;
500  size_t left;
501  const char *buf;
502 
504  "service: sending message with type %u\n",
505  ntohs(client->msg->type));
506 
507 
508  client->send_task = NULL;
509  buf = (const char *)client->msg;
510  left = ntohs(client->msg->size) - client->msg_pos;
511  ret = GNUNET_NETWORK_socket_send(client->sock, &buf[client->msg_pos], left);
512  GNUNET_assert(ret <= (ssize_t)left);
513  if (0 == ret)
514  {
515  LOG(GNUNET_ERROR_TYPE_DEBUG, "no data send");
517  return;
518  }
519  if (-1 == ret)
520  {
521  if ((EAGAIN == errno) || (EINTR == errno))
522  {
523  /* ignore */
524  ret = 0;
525  }
526  else
527  {
528  if (EPIPE != errno)
531  "socket send returned with error code %i",
532  errno);
534  return;
535  }
536  }
537  if (0 == client->msg_pos)
538  {
540  }
541  client->msg_pos += ret;
542  if (left > (size_t)ret)
543  {
544  GNUNET_assert(NULL == client->drop_task);
545  client->send_task =
547  client->sock,
548  &do_send,
549  client);
550  return;
551  }
553 }
void GNUNET_MQ_inject_error(struct GNUNET_MQ_Handle *mq, enum GNUNET_MQ_Error error)
Call the error handler of a message queue with the given error code.
Definition: mq.c:298
ssize_t GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, const void *buffer, size_t length)
Send data (always non-blocking).
Definition: network.c:804
struct GNUNET_SCHEDULER_Task * send_task
Task that transmit data to the client.
Definition: service.c:298
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_write_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *wfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1537
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
static int ret
Final status code.
Definition: gnunet-arm.c:89
struct GNUNET_MQ_Handle * mq
Message queue for the client.
Definition: service.c:270
static void do_send(void *cls)
Task run when we are ready to transmit data to the client.
Definition: service.c:496
Handle to a client that is connected to a service.
Definition: service.c:246
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
void GNUNET_MQ_impl_send_in_flight(struct GNUNET_MQ_Handle *mq)
Call the send notification for the current message, but do not try to send the next message until #gn...
Definition: mq.c:517
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
size_t msg_pos
Current position in msg at which we are transmitting.
Definition: service.c:320
FIXME: document!
static char buf[2048]
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define LOG(kind,...)
Definition: service.c:40
const struct GNUNET_MessageHeader * msg
Pointer to the message to be transmitted by send_task.
Definition: service.c:303
struct GNUNET_NETWORK_Handle * sock
Socket of this client.
Definition: service.c:265
void GNUNET_MQ_impl_send_continue(struct GNUNET_MQ_Handle *mq)
Call the send implementation for the next queued message, if any.
Definition: mq.c:484
struct GNUNET_SCHEDULER_Task * drop_task
Task run to finish dropping the client after the stack has properly unwound.
Definition: service.c:287
Here is the call graph for this function:
Here is the caller graph for this function:

◆ service_mq_send()

static void service_mq_send ( struct GNUNET_MQ_Handle mq,
const struct GNUNET_MessageHeader msg,
void *  impl_state 
)
static

Signature of functions implementing the sending functionality of a message queue.

Parameters
mqthe message queue
msgthe message to send
impl_stateour struct GNUNET_SERVICE_Client *

Definition at line 565 of file service.c.

References do_send(), GNUNET_SERVICE_Client::drop_task, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_SCHEDULER_add_write_net(), GNUNET_TIME_UNIT_FOREVER_REL, LOG, msg, GNUNET_SERVICE_Client::msg, GNUNET_SERVICE_Client::msg_pos, GNUNET_SERVICE_Client::send_task, GNUNET_MessageHeader::size, GNUNET_SERVICE_Client::sock, and GNUNET_MessageHeader::type.

Referenced by start_client().

568 {
569  struct GNUNET_SERVICE_Client *client = impl_state;
570 
571  (void)mq;
572  if (NULL != client->drop_task)
573  return; /* we're going down right now, do not try to send */
574  GNUNET_assert(NULL == client->send_task);
576  "Sending message of type %u and size %u to client\n",
577  ntohs(msg->type),
578  ntohs(msg->size));
579  client->msg = msg;
580  client->msg_pos = 0;
581  client->send_task =
583  client->sock,
584  &do_send,
585  client);
586 }
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_SCHEDULER_Task * send_task
Task that transmit data to the client.
Definition: service.c:298
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_write_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *wfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1537
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
static void do_send(void *cls)
Task run when we are ready to transmit data to the client.
Definition: service.c:496
Handle to a client that is connected to a service.
Definition: service.c:246
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
size_t msg_pos
Current position in msg at which we are transmitting.
Definition: service.c:320
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define LOG(kind,...)
Definition: service.c:40
const struct GNUNET_MessageHeader * msg
Pointer to the message to be transmitted by send_task.
Definition: service.c:303
struct GNUNET_NETWORK_Handle * sock
Socket of this client.
Definition: service.c:265
struct GNUNET_SCHEDULER_Task * drop_task
Task run to finish dropping the client after the stack has properly unwound.
Definition: service.c:287
Here is the call graph for this function:
Here is the caller graph for this function:

◆ service_mq_cancel()

static void service_mq_cancel ( struct GNUNET_MQ_Handle mq,
void *  impl_state 
)
static

Implementation function that cancels the currently sent message.

Parameters
mqmessage queue
impl_statestate specific to the implementation

Definition at line 596 of file service.c.

References GNUNET_assert, GNUNET_SCHEDULER_cancel(), GNUNET_SERVICE_Client::msg, GNUNET_SERVICE_Client::msg_pos, and GNUNET_SERVICE_Client::send_task.

Referenced by start_client().

597 {
598  struct GNUNET_SERVICE_Client *client = impl_state;
599 
600  (void)mq;
601  GNUNET_assert(0 == client->msg_pos);
602  client->msg = NULL;
604  client->send_task = NULL;
605 }
struct GNUNET_SCHEDULER_Task * send_task
Task that transmit data to the client.
Definition: service.c:298
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Handle to a client that is connected to a service.
Definition: service.c:246
size_t msg_pos
Current position in msg at which we are transmitting.
Definition: service.c:320
const struct GNUNET_MessageHeader * msg
Pointer to the message to be transmitted by send_task.
Definition: service.c:303
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
Here is the call graph for this function:
Here is the caller graph for this function:

◆ service_mq_error_handler()

static void service_mq_error_handler ( void *  cls,
enum GNUNET_MQ_Error  error 
)
static

Generic error handler, called with the appropriate error code and the same closure specified at the creation of the message queue.

Not every message queue implementation supports an error handler.

Parameters
clsclosure with our struct GNUNET_SERVICE_Client
errorerror code

Definition at line 618 of file service.c.

References GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_MQ_ERROR_NO_MATCH, GNUNET_NO, GNUNET_SERVICE_client_continue(), GNUNET_SERVICE_client_drop(), GNUNET_SERVICE_Handle::require_found, ServiceListenContext::sh, GNUNET_SERVICE_Client::sh, and GNUNET_SERVICE_Client::warn_type.

Referenced by start_client().

619 {
620  struct GNUNET_SERVICE_Client *client = cls;
621  struct GNUNET_SERVICE_Handle *sh = client->sh;
622 
623  if ((GNUNET_MQ_ERROR_NO_MATCH == error) && (GNUNET_NO == sh->require_found))
624  {
626  "No handler for message of type %u found\n",
627  (unsigned int)client->warn_type);
629  return; /* ignore error */
630  }
632 }
Handle to a service.
Definition: service.c:114
We received a message for which we have no matching handler.
int require_found
If GNUNET_YES, consider unknown message types an error where the client is disconnected.
Definition: service.c:239
#define GNUNET_NO
Definition: gnunet_common.h:78
Handle to a client that is connected to a service.
Definition: service.c:246
static struct SolverHandle * sh
uint16_t warn_type
Type of last message processed (for warn_no_receive_done).
Definition: service.c:344
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2315
#define GNUNET_log(kind,...)
struct GNUNET_SERVICE_Handle * sh
Service that this client belongs to.
Definition: service.c:260
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2234
Here is the call graph for this function:
Here is the caller graph for this function:

◆ warn_no_client_continue()

static void warn_no_client_continue ( void *  cls)
static

Task run to warn about missing calls to GNUNET_SERVICE_client_continue().

Parameters
clsour struct GNUNET_SERVICE_Client * to process more requests from

Definition at line 641 of file service.c.

References _, GNUNET_break, GNUNET_ERROR_TYPE_WARNING, GNUNET_SCHEDULER_add_delayed(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_absolute_get_duration(), GNUNET_TIME_UNIT_MINUTES, GNUNET_YES, LOG, GNUNET_SERVICE_Client::warn_start, GNUNET_SERVICE_Client::warn_task, and GNUNET_SERVICE_Client::warn_type.

Referenced by service_client_mst_cb().

642 {
643  struct GNUNET_SERVICE_Client *client = cls;
644 
645  GNUNET_break(
646  0 !=
647  client->warn_type); /* type should never be 0 here, as we don't use 0 */
650  client);
651  LOG(
653  _(
654  "Processing code for message of type %u did not call `GNUNET_SERVICE_client_continue' after %s\n"),
655  (unsigned int)client->warn_type,
657  client->warn_start),
658  GNUNET_YES));
659 }
#define GNUNET_TIME_UNIT_MINUTES
One minute.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
Handle to a client that is connected to a service.
Definition: service.c:246
struct GNUNET_TIME_Absolute warn_start
Time when we last gave a message from this client to the application.
Definition: service.c:315
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1237
struct GNUNET_SCHEDULER_Task * warn_task
Task that warns about missing calls to GNUNET_SERVICE_client_continue().
Definition: service.c:281
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:686
uint16_t warn_type
Type of last message processed (for warn_no_receive_done).
Definition: service.c:344
static void warn_no_client_continue(void *cls)
Task run to warn about missing calls to GNUNET_SERVICE_client_continue().
Definition: service.c:641
#define LOG(kind,...)
Definition: service.c:40
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
Definition: time.c:373
#define GNUNET_YES
Definition: gnunet_common.h:77
Here is the call graph for this function:
Here is the caller graph for this function:

◆ service_client_mst_cb()

static int service_client_mst_cb ( void *  cls,
const struct GNUNET_MessageHeader message 
)
static

Functions with this signature are called whenever a complete message is received by the tokenizer for a client.

Do not call GNUNET_MST_destroy() from within the scope of this callback.

Parameters
clsclosure with the struct GNUNET_SERVICE_Client *
messagethe actual message
Returns
GNUNET_OK on success, GNUNET_SYSERR if the client was dropped

Definition at line 674 of file service.c.

References GNUNET_SERVICE_Client::drop_task, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_MQ_inject_message(), GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_delayed(), GNUNET_SYSERR, GNUNET_TIME_absolute_get(), GNUNET_TIME_UNIT_MINUTES, GNUNET_YES, LOG, GNUNET_SERVICE_Client::mq, GNUNET_SERVICE_Client::needs_continue, GNUNET_MessageHeader::size, GNUNET_MessageHeader::type, warn_no_client_continue(), GNUNET_SERVICE_Client::warn_start, GNUNET_SERVICE_Client::warn_task, and GNUNET_SERVICE_Client::warn_type.

Referenced by start_client().

675 {
676  struct GNUNET_SERVICE_Client *client = cls;
677 
679  "Received message of type %u and size %u from client\n",
680  ntohs(message->type),
681  ntohs(message->size));
683  client->needs_continue = GNUNET_YES;
684  client->warn_type = ntohs(message->type);
686  GNUNET_assert(NULL == client->warn_task);
689  client);
690  GNUNET_MQ_inject_message(client->mq, message);
691  if (NULL != client->drop_task)
692  return GNUNET_SYSERR;
693  return GNUNET_OK;
694 }
#define GNUNET_TIME_UNIT_MINUTES
One minute.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
struct GNUNET_MQ_Handle * mq
Message queue for the client.
Definition: service.c:270
Handle to a client that is connected to a service.
Definition: service.c:246
struct GNUNET_TIME_Absolute warn_start
Time when we last gave a message from this client to the application.
Definition: service.c:315
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1237
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
struct GNUNET_SCHEDULER_Task * warn_task
Task that warns about missing calls to GNUNET_SERVICE_client_continue().
Definition: service.c:281
uint16_t warn_type
Type of last message processed (for warn_no_receive_done).
Definition: service.c:344
void GNUNET_MQ_inject_message(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *mh)
Call the message message handler that was registered for the type of the given message in the given m...
Definition: mq.c:200
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static void warn_no_client_continue(void *cls)
Task run to warn about missing calls to GNUNET_SERVICE_client_continue().
Definition: service.c:641
#define LOG(kind,...)
Definition: service.c:40
int needs_continue
Are we waiting for the application to call GNUNET_SERVICE_client_continue()?
Definition: service.c:339
#define GNUNET_YES
Definition: gnunet_common.h:77
struct GNUNET_SCHEDULER_Task * drop_task
Task run to finish dropping the client after the stack has properly unwound.
Definition: service.c:287
Here is the call graph for this function:
Here is the caller graph for this function:

◆ service_client_recv()

static void service_client_recv ( void *  cls)
static

A client sent us data.

Receive and process it. If we are done, reschedule this task.

Parameters
clsthe struct GNUNET_SERVICE_Client that sent us data.

Definition at line 704 of file service.c.

References GNUNET_SERVICE_Client::drop_task, GNUNET_assert, GNUNET_MST_read(), GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_read_net(), GNUNET_SERVICE_client_drop(), GNUNET_SYSERR, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, GNUNET_SERVICE_Client::mst, GNUNET_SERVICE_Client::needs_continue, GNUNET_SERVICE_Client::recv_task, ret, and GNUNET_SERVICE_Client::sock.

Referenced by resume_client_receive(), and start_client().

705 {
706  struct GNUNET_SERVICE_Client *client = cls;
707  int ret;
708 
709  client->recv_task = NULL;
710  ret = GNUNET_MST_read(client->mst, client->sock, GNUNET_NO, GNUNET_YES);
711  if (GNUNET_SYSERR == ret)
712  {
713  /* client closed connection (or IO error) */
714  if (NULL == client->drop_task)
715  {
718  }
719  return;
720  }
721  if (GNUNET_NO == ret)
722  return; /* more messages in buffer, wait for application
723  to be done processing */
724  GNUNET_assert(GNUNET_OK == ret);
725  if (GNUNET_YES == client->needs_continue)
726  return;
727  if (NULL != client->recv_task)
728  return;
729  /* MST needs more data, re-schedule read job */
730  client->recv_task =
732  client->sock,
734  client);
735 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static int ret
Final status code.
Definition: gnunet-arm.c:89
struct GNUNET_MessageStreamTokenizer * mst
Tokenizer we use for processing incoming data.
Definition: service.c:275
Handle to a client that is connected to a service.
Definition: service.c:246
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2315
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static void service_client_recv(void *cls)
A client sent us data.
Definition: service.c:704
int needs_continue
Are we waiting for the application to call GNUNET_SERVICE_client_continue()?
Definition: service.c:339
int GNUNET_MST_read(struct GNUNET_MessageStreamTokenizer *mst, struct GNUNET_NETWORK_Handle *sock, int purge, int one_shot)
Add incoming data to the receive buffer and call the callback for all complete messages.
Definition: mst.c:344
#define GNUNET_YES
Definition: gnunet_common.h:77
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1467
struct GNUNET_NETWORK_Handle * sock
Socket of this client.
Definition: service.c:265
struct GNUNET_SCHEDULER_Task * recv_task
Task that receives data from the client to pass it to the handlers.
Definition: service.c:293
struct GNUNET_SCHEDULER_Task * drop_task
Task run to finish dropping the client after the stack has properly unwound.
Definition: service.c:287
Here is the call graph for this function:
Here is the caller graph for this function:

◆ start_client()

static void start_client ( struct GNUNET_SERVICE_Handle sh,
struct GNUNET_NETWORK_Handle csock 
)
static

We have successfully accepted a connection from a client.

Now setup the client (with the scheduler) and tell the application.

Parameters
shservice that accepted the client
socksocket associated with the client

Definition at line 746 of file service.c.

References GNUNET_SERVICE_Handle::cb_cls, GNUNET_SERVICE_Handle::clients_head, GNUNET_SERVICE_Handle::clients_tail, GNUNET_SERVICE_Handle::connect_cb, GNUNET_CONTAINER_DLL_insert, GNUNET_MQ_queue_for_callbacks(), GNUNET_MQ_set_handlers_closure(), GNUNET_MST_create(), GNUNET_new, GNUNET_SCHEDULER_add_read_net(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_SERVICE_Handle::handlers, GNUNET_SERVICE_Client::mq, GNUNET_SERVICE_Client::mst, GNUNET_SERVICE_Client::recv_task, service_client_mst_cb(), service_client_recv(), service_mq_cancel(), service_mq_error_handler(), service_mq_send(), ServiceListenContext::sh, GNUNET_SERVICE_Client::sh, GNUNET_SERVICE_Client::sock, and GNUNET_SERVICE_Client::user_context.

Referenced by accept_client().

748 {
749  struct GNUNET_SERVICE_Client *client;
750 
751  client = GNUNET_new(struct GNUNET_SERVICE_Client);
753  client->sh = sh;
754  client->sock = csock;
756  NULL,
758  client,
759  sh->handlers,
761  client);
762  client->mst = GNUNET_MST_create(&service_client_mst_cb, client);
763  if (NULL != sh->connect_cb)
764  client->user_context = sh->connect_cb(sh->cb_cls, client, client->mq);
766  client->recv_task =
768  client->sock,
770  client);
771 }
static void service_mq_cancel(struct GNUNET_MQ_Handle *mq, void *impl_state)
Implementation function that cancels the currently sent message.
Definition: service.c:596
struct GNUNET_MQ_MessageHandler * handlers
Message handlers to use for all clients.
Definition: service.c:168
struct GNUNET_SERVICE_Client * clients_head
Our clients, kept in a DLL.
Definition: service.c:158
struct GNUNET_SERVICE_Client * clients_tail
Our clients, kept in a DLL.
Definition: service.c:163
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_MQ_Handle * mq
Message queue for the client.
Definition: service.c:270
static int service_client_mst_cb(void *cls, const struct GNUNET_MessageHeader *message)
Functions with this signature are called whenever a complete message is received by the tokenizer for...
Definition: service.c:674
struct GNUNET_MessageStreamTokenizer * mst
Tokenizer we use for processing incoming data.
Definition: service.c:275
Handle to a client that is connected to a service.
Definition: service.c:246
static void service_mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
Definition: service.c:618
void GNUNET_MQ_set_handlers_closure(struct GNUNET_MQ_Handle *mq, void *handlers_cls)
Change the closure argument in all of the handlers of the mq.
Definition: mq.c:581
void * user_context
User context value, value returned from the connect callback.
Definition: service.c:309
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:550
static struct SolverHandle * sh
struct GNUNET_MessageStreamTokenizer * GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
Definition: mst.c:84
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
GNUNET_SERVICE_ConnectHandler connect_cb
Function to call when clients connect.
Definition: service.c:133
static void service_mq_send(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
Signature of functions implementing the sending functionality of a message queue. ...
Definition: service.c:565
static void service_client_recv(void *cls)
A client sent us data.
Definition: service.c:704
struct GNUNET_SERVICE_Handle * sh
Service that this client belongs to.
Definition: service.c:260
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1467
struct GNUNET_NETWORK_Handle * sock
Socket of this client.
Definition: service.c:265
struct GNUNET_SCHEDULER_Task * recv_task
Task that receives data from the client to pass it to the handlers.
Definition: service.c:293
void * cb_cls
Closure for service_init_cb, connect_cb, disconnect_cb.
Definition: service.c:143
Here is the call graph for this function:
Here is the caller graph for this function:

◆ accept_client()

static void accept_client ( void *  cls)
static

We have a client.

Accept the incoming socket(s) (and reschedule the listen task).

Parameters
clsthe struct ServiceListenContext of the ready listen socket

Definition at line 781 of file service.c.

References _, check_ipv4_listed(), check_ipv6_listed(), do_suspend(), GNUNET_a2s(), GNUNET_assert, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_log_strerror, GNUNET_NETWORK_socket_accept(), GNUNET_NETWORK_socket_close(), GNUNET_OK, GNUNET_SCHEDULER_add_read_net(), GNUNET_TIME_UNIT_FOREVER_REL, ServiceListenContext::listen_socket, ServiceListenContext::listen_task, LOG, ok, ServiceListenContext::sh, start_client(), GNUNET_SERVICE_Handle::suspend_state, SUSPEND_STATE_EMFILE, GNUNET_SERVICE_Handle::v4_allowed, GNUNET_SERVICE_Handle::v4_denied, GNUNET_SERVICE_Handle::v6_allowed, and GNUNET_SERVICE_Handle::v6_denied.

Referenced by do_resume().

782 {
783  struct ServiceListenContext *slc = cls;
784  struct GNUNET_SERVICE_Handle *sh = slc->sh;
785 
786  slc->listen_task = NULL;
787  while (1)
788  {
789  struct GNUNET_NETWORK_Handle *sock;
790  const struct sockaddr_in *v4;
791  const struct sockaddr_in6 *v6;
792  struct sockaddr_storage sa;
793  socklen_t addrlen;
794  int ok;
795 
796  addrlen = sizeof(sa);
798  (struct sockaddr *)&sa,
799  &addrlen);
800  if (NULL == sock)
801  {
802  if (EMFILE == errno)
804  else if (EAGAIN != errno)
806  break;
807  }
808  switch (sa.ss_family)
809  {
810  case AF_INET:
811  GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
812  v4 = (const struct sockaddr_in *)&sa;
813  ok = (((NULL == sh->v4_allowed) ||
814  (check_ipv4_listed(sh->v4_allowed, &v4->sin_addr))) &&
815  ((NULL == sh->v4_denied) ||
816  (!check_ipv4_listed(sh->v4_denied, &v4->sin_addr))));
817  break;
818 
819  case AF_INET6:
820  GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
821  v6 = (const struct sockaddr_in6 *)&sa;
822  ok = (((NULL == sh->v6_allowed) ||
823  (check_ipv6_listed(sh->v6_allowed, &v6->sin6_addr))) &&
824  ((NULL == sh->v6_denied) ||
825  (!check_ipv6_listed(sh->v6_denied, &v6->sin6_addr))));
826  break;
827 
828  case AF_UNIX:
829  ok = GNUNET_OK; /* controlled using file-system ACL now */
830  break;
831 
832  default:
834  _("Unknown address family %d\n"),
835  sa.ss_family);
836  return;
837  }
838  if (!ok)
839  {
841  "Service rejected incoming connection from %s due to policy.\n",
842  GNUNET_a2s((const struct sockaddr *)&sa, addrlen));
844  continue;
845  }
847  "Service accepted incoming connection from %s.\n",
848  GNUNET_a2s((const struct sockaddr *)&sa, addrlen));
849  start_client(slc->sh, sock);
850  }
851  if (0 != sh->suspend_state)
852  return;
853  slc->listen_task =
855  slc->listen_socket,
856  &accept_client,
857  slc);
858 }
enum SuspendReason suspend_state
Are we suspended, and if so, why?
Definition: service.c:216
Handle to a service.
Definition: service.c:114
static int check_ipv4_listed(const struct GNUNET_STRINGS_IPv4NetworkPolicy *list, const struct in_addr *add)
Check if the given IP address is in the list of IP addresses.
Definition: service.c:437
static void accept_client(void *cls)
We have a client.
Definition: service.c:781
static void do_suspend(struct GNUNET_SERVICE_Handle *sh, enum SuspendReason sr)
Suspend accepting connections from the listen socket temporarily.
Definition: service.c:377
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_NETWORK_Handle * listen_socket
Socket we are listening on.
Definition: service.c:71
static int ok
Return value from &#39;main&#39; (0 == success)
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
struct GNUNET_STRINGS_IPv6NetworkPolicy * v6_denied
IPv6 addresses that are not allowed to connect.
Definition: service.c:183
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t *address_len)
Accept a new connection on a socket.
Definition: network.c:420
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
struct GNUNET_SERVICE_Handle * sh
Service this listen context belongs to.
Definition: service.c:66
static struct SolverHandle * sh
static int check_ipv6_listed(const struct GNUNET_STRINGS_IPv6NetworkPolicy *list, const struct in6_addr *ip)
Check if the given IP address is in the list of IP addresses.
Definition: service.c:464
Information the service tracks per listen operation.
Definition: service.c:52
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages)...
struct GNUNET_STRINGS_IPv4NetworkPolicy * v4_denied
IPv4 addresses that are not allowed to connect.
Definition: service.c:178
struct GNUNET_SCHEDULER_Task * listen_task
Task scheduled to do the listening.
Definition: service.c:76
struct GNUNET_STRINGS_IPv6NetworkPolicy * v6_allowed
IPv6 addresses that are allowed to connect (if not set, all are allowed).
Definition: service.c:195
#define LOG(kind,...)
Definition: service.c:40
static void start_client(struct GNUNET_SERVICE_Handle *sh, struct GNUNET_NETWORK_Handle *csock)
We have successfully accepted a connection from a client.
Definition: service.c:746
handle to a socket
Definition: network.c:46
struct GNUNET_STRINGS_IPv4NetworkPolicy * v4_allowed
IPv4 addresses that are allowed to connect (if not set, all are allowed).
Definition: service.c:189
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1467
OS ran out of file descriptors.
Definition: service.c:97
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:548
Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_resume()

static void do_resume ( struct GNUNET_SERVICE_Handle sh,
enum SuspendReason  sr 
)
static

Resume accepting connections from the listen socket.

Parameters
shservice to resume accepting connections.
srreason that is no longer causing the suspension, or SUSPEND_STATE_NONE on first startup

Definition at line 869 of file service.c.

References accept_client(), GNUNET_assert, GNUNET_SCHEDULER_add_read_net(), GNUNET_TIME_UNIT_FOREVER_REL, ServiceListenContext::listen_socket, ServiceListenContext::listen_task, ServiceListenContext::next, GNUNET_SERVICE_Handle::slc_head, GNUNET_SERVICE_Handle::suspend_state, and SUSPEND_STATE_NONE.

Referenced by finish_client_drop(), GNUNET_SERVICE_resume(), GNUNET_SERVICE_start(), and service_main().

870 {
871  struct ServiceListenContext *slc;
872 
873  GNUNET_assert((SUSPEND_STATE_NONE == sr) || (0 != (sh->suspend_state & sr)));
874  sh->suspend_state -= sr;
876  return;
877  for (slc = sh->slc_head; NULL != slc; slc = slc->next)
878  {
879  GNUNET_assert(NULL == slc->listen_task);
880  slc->listen_task =
882  slc->listen_socket,
883  &accept_client,
884  slc);
885  }
886 }
struct ServiceListenContext * slc_head
DLL of listen sockets used to accept new connections.
Definition: service.c:148
enum SuspendReason suspend_state
Are we suspended, and if so, why?
Definition: service.c:216
struct ServiceListenContext * next
Kept in a DLL.
Definition: service.c:56
static void accept_client(void *cls)
We have a client.
Definition: service.c:781
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_NETWORK_Handle * listen_socket
Socket we are listening on.
Definition: service.c:71
We are running normally.
Definition: service.c:87
Information the service tracks per listen operation.
Definition: service.c:52
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
struct GNUNET_SCHEDULER_Task * listen_task
Task scheduled to do the listening.
Definition: service.c:76
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1467
Here is the call graph for this function:
Here is the caller graph for this function:

◆ service_main()

static void service_main ( void *  cls)
static

First task run by any service.

Initializes our shutdown task, starts the listening operation on our listen sockets and launches the custom logic of the application service.

Parameters
clsour struct GNUNET_SERVICE_Handle

Definition at line 897 of file service.c.

References GNUNET_SERVICE_Handle::cb_cls, GNUNET_SERVICE_Handle::cfg, do_resume(), GNUNET_break, GNUNET_SCHEDULER_add_shutdown(), GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, GNUNET_SERVICE_Handle::options, GNUNET_SERVICE_Handle::ready_confirm_fd, GNUNET_SERVICE_Handle::service_init_cb, service_shutdown(), ServiceListenContext::sh, and SUSPEND_STATE_NONE.

Referenced by GNUNET_SERVICE_run_().

898 {
899  struct GNUNET_SERVICE_Handle *sh = cls;
900 
904 
905  if (-1 != sh->ready_confirm_fd)
906  {
907  GNUNET_break(1 == write(sh->ready_confirm_fd, ".", 1));
908  GNUNET_break(0 == close(sh->ready_confirm_fd));
909  sh->ready_confirm_fd = -1;
910  }
911 
912  if (NULL != sh->service_init_cb)
913  sh->service_init_cb(sh->cb_cls, sh->cfg, sh);
914 }
GNUNET_SERVICE_InitCallback service_init_cb
Main service-specific task to run.
Definition: service.c:128
Handle to a service.
Definition: service.c:114
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
Do not trigger server shutdown on signal at all; instead, allow for the user to terminate the server ...
enum GNUNET_SERVICE_Options options
Our options.
Definition: service.c:221
We are running normally.
Definition: service.c:87
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
int ready_confirm_fd
If we are daemonizing, this FD is set to the pipe to the parent.
Definition: service.c:228
static struct SolverHandle * sh
static void service_shutdown(void *cls)
Shutdown task triggered when a service should be terminated.
Definition: service.c:403
static void do_resume(struct GNUNET_SERVICE_Handle *sh, enum SuspendReason sr)
Resume accepting connections from the listen socket.
Definition: service.c:869
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: service.c:118
void * cb_cls
Closure for service_init_cb, connect_cb, disconnect_cb.
Definition: service.c:143
Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_acl4()

static int process_acl4 ( struct GNUNET_STRINGS_IPv4NetworkPolicy **  ret,
struct GNUNET_SERVICE_Handle sh,
const char *  option 
)
static

Parse an IPv4 access control list.

Parameters
retlocation where to write the ACL (set)
shservice context to use to get the configuration
optionname of the ACL option to parse
Returns
GNUNET_SYSERR on parse error, GNUNET_OK on success (including no ACL configured)

Definition at line 927 of file service.c.

References _, GNUNET_SERVICE_Handle::cfg, GNUNET_break, GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_have_value(), GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_OK, GNUNET_STRINGS_parse_ipv4_policy(), GNUNET_SYSERR, LOG, and GNUNET_SERVICE_Handle::service_name.

Referenced by setup_service().

930 {
931  char *opt;
932 
934  {
935  *ret = NULL;
936  return GNUNET_OK;
937  }
940  sh->service_name,
941  option,
942  &opt));
943  if (NULL == (*ret = GNUNET_STRINGS_parse_ipv4_policy(opt)))
944  {
946  _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
947  opt,
948  sh->service_name,
949  option);
950  GNUNET_free(opt);
951  return GNUNET_SYSERR;
952  }
953  GNUNET_free(opt);
954  return GNUNET_OK;
955 }
struct GNUNET_STRINGS_IPv4NetworkPolicy * GNUNET_STRINGS_parse_ipv4_policy(const char *routeListX)
Parse an IPv4 network policy.
Definition: strings.c:1519
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
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 * option
Name of the option.
Definition: gnunet-config.c:38
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
#define LOG(kind,...)
Definition: service.c:40
const char * service_name
Name of our service.
Definition: service.c:123
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: service.c:118
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_acl6()

static int process_acl6 ( struct GNUNET_STRINGS_IPv6NetworkPolicy **  ret,
struct GNUNET_SERVICE_Handle sh,
const char *  option 
)
static

Parse an IPv6 access control list.

Parameters
retlocation where to write the ACL (set)
shservice context to use to get the configuration
optionname of the ACL option to parse
Returns
GNUNET_SYSERR on parse error, GNUNET_OK on success (including no ACL configured)

Definition at line 968 of file service.c.

References _, GNUNET_SERVICE_Handle::cfg, GNUNET_break, GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_have_value(), GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_OK, GNUNET_STRINGS_parse_ipv6_policy(), GNUNET_SYSERR, LOG, and GNUNET_SERVICE_Handle::service_name.

Referenced by setup_service().

971 {
972  char *opt;
973 
975  {
976  *ret = NULL;
977  return GNUNET_OK;
978  }
981  sh->service_name,
982  option,
983  &opt));
984  if (NULL == (*ret = GNUNET_STRINGS_parse_ipv6_policy(opt)))
985  {
987  _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
988  opt,
989  sh->service_name,
990  option);
991  GNUNET_free(opt);
992  return GNUNET_SYSERR;
993  }
994  GNUNET_free(opt);
995  return GNUNET_OK;
996 }
struct GNUNET_STRINGS_IPv6NetworkPolicy * GNUNET_STRINGS_parse_ipv6_policy(const char *routeListX)
Parse an IPv6 network policy.
Definition: strings.c:1711
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
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 * option
Name of the option.
Definition: gnunet-config.c:38
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
#define LOG(kind,...)
Definition: service.c:40
const char * service_name
Name of our service.
Definition: service.c:123
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: service.c:118
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_unixpath()

static void add_unixpath ( struct sockaddr **  saddrs,
socklen_t *  saddrlens,
const char *  unixpath,
int  abstract 
)
static

Add the given UNIX domain path as an address to the list (as the first entry).

Parameters
saddrsarray to update
saddrlenswhere to store the address length
unixpathpath to add
abstractGNUNET_YES to add an abstract UNIX domain socket. This parameter is ignore on systems other than LINUX

Definition at line 1010 of file service.c.

References GNUNET_assert, GNUNET_new, GNUNET_strlcpy(), and GNUNET_YES.

Referenced by get_server_addresses().

1014 {
1015 #ifdef AF_UNIX
1016  struct sockaddr_un *un;
1017 
1018  un = GNUNET_new(struct sockaddr_un);
1019  un->sun_family = AF_UNIX;
1020  GNUNET_strlcpy(un->sun_path, unixpath, sizeof(un->sun_path));
1021 #ifdef LINUX
1022  if (GNUNET_YES == abstract)
1023  un->sun_path[0] = '\0';
1024 #endif
1025 #if HAVE_SOCKADDR_UN_SUN_LEN
1026  un->sun_len = (u_char)sizeof(struct sockaddr_un);
1027 #endif
1028  *saddrs = (struct sockaddr *)un;
1029  *saddrlens = sizeof(struct sockaddr_un);
1030 #else
1031  /* this function should never be called
1032  * unless AF_UNIX is defined! */
1033  GNUNET_assert(0);
1034 #endif
1035 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_YES
Definition: gnunet_common.h:77
size_t GNUNET_strlcpy(char *dst, const char *src, size_t n)
Like strlcpy but portable.
Definition: strings.c:219
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_server_addresses()

static int get_server_addresses ( const char *  service_name,
const struct GNUNET_CONFIGURATION_Handle cfg,
struct sockaddr ***  addrs,
socklen_t **  addr_lens 
)
static

Get the list of addresses that a server for the given service should bind to.

Parameters
service_namename of the service
cfgconfiguration (which specifies the addresses)
addrsset (call by reference) to an array of pointers to the addresses the server should bind to and listen on; the array will be NULL-terminated (on success)
addr_lensset (call by reference) to an array of the lengths of the respective struct sockaddr struct in the addrs array (on success)
Returns
number of addresses found on success, GNUNET_SYSERR if the configuration did not specify reasonable finding information or if it specified a hostname that could not be resolved; GNUNET_NO if the number of addresses configured is zero (in this case, *addrs and *addr_lens will be set to NULL).

Definition at line 1059 of file service.c.

References _, add_unixpath(), GNUNET_a2s(), GNUNET_assert, GNUNET_break, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_number(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_get_value_yesno(), GNUNET_CONFIGURATION_have_value(), GNUNET_DISK_directory_create_for_file(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_free_non_null, GNUNET_log_strerror_file, GNUNET_malloc, GNUNET_memcpy, GNUNET_NETWORK_shorten_unixpath(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_create(), GNUNET_NETWORK_test_pf(), GNUNET_new_array, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, hostname, LOG, LOG_STRERROR, ServiceListenContext::next, port, res, and ret.

Referenced by setup_service().

1063 {
1064  int disablev6;
1065  struct GNUNET_NETWORK_Handle *desc;
1066  unsigned long long port;
1067  char *unixpath;
1068  struct addrinfo hints;
1069  struct addrinfo *res;
1070  struct addrinfo *pos;
1071  struct addrinfo *next;
1072  unsigned int i;
1073  int resi;
1074  int ret;
1075  int abstract;
1076  struct sockaddr **saddrs;
1077  socklen_t *saddrlens;
1078  char *hostname;
1079 
1080  *addrs = NULL;
1081  *addr_lens = NULL;
1082  desc = NULL;
1083  disablev6 = GNUNET_NO;
1084  if ((GNUNET_NO == GNUNET_NETWORK_test_pf(PF_INET6)) ||
1085  (GNUNET_YES ==
1087  disablev6 = GNUNET_YES;
1088 
1089  port = 0;
1091  {
1093  service_name,
1094  "PORT",
1095  &port))
1096  {
1098  _("Require valid port number for service `%s' in configuration!\n"),
1099  service_name);
1100  }
1101  if (port > 65535)
1102  {
1104  _("Require valid port number for service `%s' in configuration!\n"),
1105  service_name);
1106  return GNUNET_SYSERR;
1107  }
1108  }
1109 
1110  if (GNUNET_CONFIGURATION_have_value(cfg, service_name, "BINDTO"))
1111  {
1114  service_name,
1115  "BINDTO",
1116  &hostname));
1117  }
1118  else
1119  hostname = NULL;
1120 
1121  unixpath = NULL;
1122  abstract = GNUNET_NO;
1123 #ifdef AF_UNIX
1124  if ((GNUNET_YES ==
1125  GNUNET_CONFIGURATION_have_value(cfg, service_name, "UNIXPATH")) &&
1127  service_name,
1128  "UNIXPATH",
1129  &unixpath)) &&
1130  (0 < strlen(unixpath)))
1131  {
1132  /* probe UNIX support */
1133  struct sockaddr_un s_un;
1134 
1135  if (strlen(unixpath) >= sizeof(s_un.sun_path))
1136  {
1138  _("UNIXPATH `%s' too long, maximum length is %llu\n"),
1139  unixpath,
1140  (unsigned long long)sizeof(s_un.sun_path));
1141  unixpath = GNUNET_NETWORK_shorten_unixpath(unixpath);
1142  LOG(GNUNET_ERROR_TYPE_INFO, _("Using `%s' instead\n"), unixpath);
1143  }
1144 #ifdef LINUX
1145  abstract = GNUNET_CONFIGURATION_get_value_yesno(cfg,
1146  "TESTING",
1147  "USE_ABSTRACT_SOCKETS");
1148  if (GNUNET_SYSERR == abstract)
1149  abstract = GNUNET_NO;
1150 #endif
1151  if ((GNUNET_YES != abstract) &&
1154  }
1155  if (NULL != unixpath)
1156  {
1157  desc = GNUNET_NETWORK_socket_create(AF_UNIX, SOCK_STREAM, 0);
1158  if (NULL == desc)
1159  {
1160  if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
1161  (EACCES == errno))
1162  {
1164  GNUNET_free_non_null(hostname);
1165  GNUNET_free(unixpath);
1166  return GNUNET_SYSERR;
1167  }
1169  _(
1170  "Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
1171  service_name,
1172  strerror(errno));
1173  GNUNET_free(unixpath);
1174  unixpath = NULL;
1175  }
1176  else
1177  {
1179  desc = NULL;
1180  }
1181  }
1182 #endif
1183 
1184  if ((0 == port) && (NULL == unixpath))
1185  {
1187  _(
1188  "Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
1189  service_name);
1190  GNUNET_free_non_null(hostname);
1191  return GNUNET_SYSERR;
1192  }
1193  if (0 == port)
1194  {
1195  saddrs = GNUNET_new_array(2, struct sockaddr *);
1196  saddrlens = GNUNET_new_array(2, socklen_t);
1197  add_unixpath(saddrs, saddrlens, unixpath, abstract);
1198  GNUNET_free_non_null(unixpath);
1199  GNUNET_free_non_null(hostname);
1200  *addrs = saddrs;
1201  *addr_lens = saddrlens;
1202  return 1;
1203  }
1204 
1205  if (NULL != hostname)
1206  {
1208  "Resolving `%s' since that is where `%s' will bind to.\n",
1209  hostname,
1210  service_name);
1211  memset(&hints, 0, sizeof(struct addrinfo));
1212  if (disablev6)
1213  hints.ai_family = AF_INET;
1214  hints.ai_protocol = IPPROTO_TCP;
1215  if ((0 != (ret = getaddrinfo(hostname, NULL, &hints, &res))) ||
1216  (NULL == res))
1217  {
1219  _("Failed to resolve `%s': %s\n"),
1220  hostname,
1221  gai_strerror(ret));
1222  GNUNET_free(hostname);
1223  GNUNET_free_non_null(unixpath);
1224  return GNUNET_SYSERR;
1225  }
1226  next = res;
1227  i = 0;
1228  while (NULL != (pos = next))
1229  {
1230  next = pos->ai_next;
1231  if ((disablev6) && (pos->ai_family == AF_INET6))
1232  continue;
1233  i++;
1234  }
1235  if (0 == i)
1236  {
1238  _("Failed to find %saddress for `%s'.\n"),
1239  disablev6 ? "IPv4 " : "",
1240  hostname);
1241  freeaddrinfo(res);
1242  GNUNET_free(hostname);
1243  GNUNET_free_non_null(unixpath);
1244  return GNUNET_SYSERR;
1245  }
1246  resi = i;
1247  if (NULL != unixpath)
1248  resi++;
1249  saddrs = GNUNET_new_array(resi + 1, struct sockaddr *);
1250  saddrlens = GNUNET_new_array(resi + 1, socklen_t);
1251  i = 0;
1252  if (NULL != unixpath)
1253  {
1254  add_unixpath(saddrs, saddrlens, unixpath, abstract);
1255  i++;
1256  }
1257  next = res;
1258  while (NULL != (pos = next))
1259  {
1260  next = pos->ai_next;
1261  if ((disablev6) && (AF_INET6 == pos->ai_family))
1262  continue;
1263  if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
1264  continue; /* not TCP */
1265  if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
1266  continue; /* huh? */
1268  "Service `%s' will bind to `%s'\n",
1269  service_name,
1270  GNUNET_a2s(pos->ai_addr, pos->ai_addrlen));
1271  if (AF_INET == pos->ai_family)
1272  {
1273  GNUNET_assert(sizeof(struct sockaddr_in) == pos->ai_addrlen);
1274  saddrlens[i] = pos->ai_addrlen;
1275  saddrs[i] = GNUNET_malloc(saddrlens[i]);
1276  GNUNET_memcpy(saddrs[i], pos->ai_addr, saddrlens[i]);
1277  ((struct sockaddr_in *)saddrs[i])->sin_port = htons(port);
1278  }
1279  else
1280  {
1281  GNUNET_assert(AF_INET6 == pos->ai_family);
1282  GNUNET_assert(sizeof(struct sockaddr_in6) == pos->ai_addrlen);
1283  saddrlens[i] = pos->ai_addrlen;
1284  saddrs[i] = GNUNET_malloc(saddrlens[i]);
1285  GNUNET_memcpy(saddrs[i], pos->ai_addr, saddrlens[i]);
1286  ((struct sockaddr_in6 *)saddrs[i])->sin6_port = htons(port);
1287  }
1288  i++;
1289  }
1290  GNUNET_free(hostname);
1291  freeaddrinfo(res);
1292  resi = i;
1293  }
1294  else
1295  {
1296  /* will bind against everything, just set port */
1297  if (disablev6)
1298  {
1299  /* V4-only */
1300  resi = 1;
1301  if (NULL != unixpath)
1302  resi++;
1303  i = 0;
1304  saddrs = GNUNET_new_array(resi + 1, struct sockaddr *);
1305  saddrlens = GNUNET_new_array(resi + 1, socklen_t);
1306  if (NULL != unixpath)
1307  {
1308  add_unixpath(saddrs, saddrlens, unixpath, abstract);
1309  i++;
1310  }
1311  saddrlens[i] = sizeof(struct sockaddr_in);
1312  saddrs[i] = GNUNET_malloc(saddrlens[i]);
1313 #if HAVE_SOCKADDR_IN_SIN_LEN
1314  ((struct sockaddr_in *)saddrs[i])->sin_len = saddrlens[i];
1315 #endif
1316  ((struct sockaddr_in *)saddrs[i])->sin_family = AF_INET;
1317  ((struct sockaddr_in *)saddrs[i])->sin_port = htons(port);
1318  }
1319  else
1320  {
1321  /* dual stack */
1322  resi = 2;
1323  if (NULL != unixpath)
1324  resi++;
1325  saddrs = GNUNET_new_array(resi + 1, struct sockaddr *);
1326  saddrlens = GNUNET_new_array(resi + 1, socklen_t);
1327  i = 0;
1328  if (NULL != unixpath)
1329  {
1330  add_unixpath(saddrs, saddrlens, unixpath, abstract);
1331  i++;
1332  }
1333  saddrlens[i] = sizeof(struct sockaddr_in6);
1334  saddrs[i] = GNUNET_malloc(saddrlens[i]);
1335 #if HAVE_SOCKADDR_IN_SIN_LEN
1336  ((struct sockaddr_in6 *)saddrs[i])->sin6_len = saddrlens[0];
1337 #endif
1338  ((struct sockaddr_in6 *)saddrs[i])->sin6_family = AF_INET6;
1339  ((struct sockaddr_in6 *)saddrs[i])->sin6_port = htons(port);
1340  i++;
1341  saddrlens[i] = sizeof(struct sockaddr_in);
1342  saddrs[i] = GNUNET_malloc(saddrlens[i]);
1343 #if HAVE_SOCKADDR_IN_SIN_LEN
1344  ((struct sockaddr_in *)saddrs[i])->sin_len = saddrlens[1];
1345 #endif
1346  ((struct sockaddr_in *)saddrs[i])->sin_family = AF_INET;
1347  ((struct sockaddr_in *)saddrs[i])->sin_port = htons(port);
1348  }
1349  }
1350  GNUNET_free_non_null(unixpath);
1351  *addrs = saddrs;
1352  *addr_lens = saddrlens;
1353  return resi;
1354 }
static void add_unixpath(struct sockaddr **saddrs, socklen_t *saddrlens, const char *unixpath, int abstract)
Add the given UNIX domain path as an address to the list (as the first entry).
Definition: service.c:1010
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.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
int GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:681
#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.
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; 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
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages)...
static int res
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.
#define LOG(kind,...)
Definition: service.c:40
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.
handle to a socket
Definition: network.c:46
char * GNUNET_NETWORK_shorten_unixpath(char *unixpath)
Given a unixpath that is too long (larger than UNIX_PATH_MAX), shorten it to an acceptable length whi...
Definition: network.c:151
#define GNUNET_YES
Definition: gnunet_common.h:77
int GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
#define LOG_STRERROR(kind, syscall)
Definition: service.c:42
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:548
#define GNUNET_malloc(size)
Wrapper around malloc.
int GNUNET_NETWORK_test_pf(int pf)
Test if the given protocol family is supported by this system.
Definition: network.c:78
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:900
Here is the call graph for this function:
Here is the caller graph for this function:

◆ open_listen_socket()

static struct GNUNET_NETWORK_Handle* open_listen_socket ( const struct sockaddr *  server_addr,
socklen_t  socklen 
)
static

Create and initialize a listen socket for the server.

Parameters
server_addraddress to listen on
socklenlength of server_addr
Returns
NULL on error, otherwise the listen socket

Definition at line 1365 of file service.c.

References _, GNUNET_a2s(), GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_NETWORK_socket_bind(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_create(), GNUNET_NETWORK_socket_listen(), GNUNET_OK, LOG, LOG_STRERROR, and port.

Referenced by setup_service().

1366 {
1367  struct GNUNET_NETWORK_Handle *sock;
1368  uint16_t port;
1369  int eno;
1370 
1371  switch (server_addr->sa_family)
1372  {
1373  case AF_INET:
1374  port = ntohs(((const struct sockaddr_in *)server_addr)->sin_port);
1375  break;
1376 
1377  case AF_INET6:
1378  port = ntohs(((const struct sockaddr_in6 *)server_addr)->sin6_port);
1379  break;
1380 
1381  case AF_UNIX:
1382  port = 0;
1383  break;
1384 
1385  default:
1386  GNUNET_break(0);
1387  port = 0;
1388  break;
1389  }
1390  sock = GNUNET_NETWORK_socket_create(server_addr->sa_family, SOCK_STREAM, 0);
1391  if (NULL == sock)
1392  {
1394  errno = 0;
1395  return NULL;
1396  }
1397  /* bind the socket */
1398  if (GNUNET_OK != GNUNET_NETWORK_socket_bind(sock, server_addr, socklen))
1399  {
1400  eno = errno;
1401  if (EADDRINUSE != errno)
1402  {
1403  /* we don't log 'EADDRINUSE' here since an IPv4 bind may
1404  * fail if we already took the port on IPv6; if both IPv4 and
1405  * IPv6 binds fail, then our caller will log using the
1406  * errno preserved in 'eno' */
1407  if (0 != port)
1409  _("`%s' failed for port %d (%s).\n"),
1410  "bind",
1411  port,
1412  (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
1413  else
1415  eno = 0;
1416  }
1417  else
1418  {
1419  if (0 != port)
1421  _("`%s' failed for port %d (%s): address already in use\n"),
1422  "bind",
1423  port,
1424  (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
1425  else if (AF_UNIX == server_addr->sa_family)
1426  {
1428  _("`%s' failed for `%s': address already in use\n"),
1429  "bind",
1430  GNUNET_a2s(server_addr, socklen));
1431  }
1432  }
1434  errno = eno;
1435  return NULL;
1436  }
1437  if (GNUNET_OK != GNUNET_NETWORK_socket_listen(sock, 5))
1438  {
1441  errno = 0;
1442  return NULL;
1443  }
1444  if (0 != port)
1446  "Server starts to listen on port %u.\n",
1447  port);
1448  return sock;
1449 }
int GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
Definition: network.c:696
int GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:474
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages)...
#define LOG(kind,...)
Definition: service.c:40
static uint16_t port
Port number.
Definition: gnunet-bcd.c:81
handle to a socket
Definition: network.c:46
#define LOG_STRERROR(kind, syscall)
Definition: service.c:42
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:548
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:900
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setup_service()

static int setup_service ( struct GNUNET_SERVICE_Handle sh)
static

Setup service handle.

Configuration may specify:

  • PORT (where to bind to for TCP)
  • UNIXPATH (where to bind to for UNIX domain sockets)
  • DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
  • BINDTO (hostname or IP address to bind to, otherwise we take everything)
  • ACCEPT_FROM (only allow connections from specified IPv4 subnets)
  • ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
  • REJECT_FROM (disallow allow connections from specified IPv4 subnets)
  • REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
Parameters
shservice context to initialize
Returns
GNUNET_OK if configuration succeeded

Definition at line 1469 of file service.c.

References _, GNUNET_SERVICE_Handle::cfg, dummy, get_server_addresses(), getenv(), GNUNET_break, GNUNET_CONFIGURATION_get_value_yesno(), GNUNET_CONFIGURATION_have_value(), GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_free_non_null, GNUNET_log, GNUNET_log_strerror, GNUNET_NETWORK_socket_box_native(), GNUNET_NETWORK_socket_close(), GNUNET_new, GNUNET_new_array, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, ServiceListenContext::listen_socket, LOG, ls, GNUNET_SERVICE_Handle::match_gid, GNUNET_SERVICE_Handle::match_uid, open_listen_socket(), process_acl4(), process_acl6(), GNUNET_SERVICE_Handle::require_found, GNUNET_SERVICE_Handle::service_name, ServiceListenContext::sh, GNUNET_SERVICE_Handle::slc_head, GNUNET_SERVICE_Handle::slc_tail, GNUNET_SERVICE_Handle::v4_allowed, GNUNET_SERVICE_Handle::v4_denied, GNUNET_SERVICE_Handle::v6_allowed, and GNUNET_SERVICE_Handle::v6_denied.

Referenced by GNUNET_SERVICE_run_(), and GNUNET_SERVICE_start().

1470 {
1471  int tolerant;
1472  struct GNUNET_NETWORK_Handle **lsocks;
1473  const char *nfds;
1474  unsigned int cnt;
1475  int flags;
1476  char dummy[2];
1477 
1478  if (GNUNET_CONFIGURATION_have_value(sh->cfg, sh->service_name, "TOLERANT"))
1479  {
1480  if (GNUNET_SYSERR ==
1481  (tolerant = GNUNET_CONFIGURATION_get_value_yesno(sh->cfg,
1482  sh->service_name,
1483  "TOLERANT")))
1484  {
1486  _("Specified value for `%s' of service `%s' is invalid\n"),
1487  "TOLERANT",
1488  sh->service_name);
1489  return GNUNET_SYSERR;
1490  }
1491  }
1492  else
1493  tolerant = GNUNET_NO;
1494 
1495  lsocks = NULL;
1496 
1497  errno = 0;
1498  if ((NULL != (nfds = getenv("LISTEN_FDS"))) &&
1499  (1 == sscanf(nfds, "%u%1s", &cnt, dummy)) && (cnt > 0) &&
1500  (cnt < FD_SETSIZE) && (cnt + 4 < FD_SETSIZE))
1501  {
1502  lsocks = GNUNET_new_array(cnt + 1, struct GNUNET_NETWORK_Handle *);
1503  while (0 < cnt--)
1504  {
1505  flags = fcntl(3 + cnt, F_GETFD);
1506  if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1507  (NULL == (lsocks[cnt] = GNUNET_NETWORK_socket_box_native(3 + cnt))))
1508  {
1510  _(
1511  "Could not access pre-bound socket %u, will try to bind myself\n"),
1512  (unsigned int)3 + cnt);
1513  cnt++;
1514  while (NULL != lsocks[cnt])
1516  GNUNET_NETWORK_socket_close(lsocks[cnt++]));
1517  GNUNET_free(lsocks);
1518  lsocks = NULL;
1519  break;
1520  }
1521  }
1522  unsetenv("LISTEN_FDS");
1523  }
1524 
1525  if (NULL != lsocks)
1526  {
1527  /* listen only on inherited sockets if we have any */
1528  struct GNUNET_NETWORK_Handle **ls;
1529 
1530  for (ls = lsocks; NULL != *ls; ls++)
1531  {
1532  struct ServiceListenContext *slc;
1533 
1534  slc = GNUNET_new(struct ServiceListenContext);
1535  slc->sh = sh;
1536  slc->listen_socket = *ls;
1538  }
1539  GNUNET_free(lsocks);
1540  }
1541  else
1542  {
1543  struct sockaddr **addrs;
1544  socklen_t *addrlens;
1545  int num;
1546 
1547  num = get_server_addresses(sh->service_name, sh->cfg, &addrs, &addrlens);
1548  if (GNUNET_SYSERR == num)
1549  return GNUNET_SYSERR;
1550 
1551  for (int i = 0; i < num; i++)
1552  {
1553  struct ServiceListenContext *slc;
1554 
1555  slc = GNUNET_new(struct ServiceListenContext);
1556  slc->sh = sh;
1557  slc->listen_socket = open_listen_socket(addrs[i], addrlens[i]);
1558  GNUNET_free(addrs[i]);
1559  if (NULL == slc->listen_socket)
1560  {
1562  GNUNET_free(slc);
1563  continue;
1564  }
1566  }
1567  GNUNET_free_non_null(addrlens);
1568  GNUNET_free_non_null(addrs);
1569  if ((0 != num) && (NULL == sh->slc_head))
1570  {
1571  /* All attempts to bind failed, hard failure */
1572  GNUNET_log(
1574  _(
1575  "Could not bind to any of the ports I was supposed to, refusing to run!\n"));
1576  return GNUNET_SYSERR;
1577  }
1578  }
1579 
1580  sh->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1582  sh->service_name,
1583  "UNIX_MATCH_UID");
1585  sh->service_name,
1586  "UNIX_MATCH_GID");
1587  process_acl4(&sh->v4_denied, sh, "REJECT_FROM");
1588  process_acl4(&sh->v4_allowed, sh, "ACCEPT_FROM");
1589  process_acl6(&sh->v6_denied, sh, "REJECT_FROM6");
1590  process_acl6(&sh->v6_allowed, sh, "ACCEPT_FROM6");
1591  return GNUNET_OK;
1592 }
struct ServiceListenContext * slc_head
DLL of listen sockets used to accept new connections.
Definition: service.c:148
static int get_server_addresses(const char *service_name, const struct GNUNET_CONFIGURATION_Handle *cfg, struct sockaddr ***addrs, socklen_t **addr_lens)
Get the list of addresses that a server for the given service should bind to.
Definition: service.c:1059
int match_uid
Do we require a matching UID for UNIX domain socket connections? GNUNET_NO means that the UID does no...
Definition: service.c:202
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
int match_gid
Do we require a matching GID for UNIX domain socket connections? Ignored if match_uid is GNUNET_YES...
Definition: service.c:211
int require_found
If GNUNET_YES, consider unknown message types an error where the client is disconnected.
Definition: service.c:239
struct GNUNET_NETWORK_Handle * listen_socket
Socket we are listening on.
Definition: service.c:71
#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.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static int process_acl6(struct GNUNET_STRINGS_IPv6NetworkPolicy **ret, struct GNUNET_SERVICE_Handle *sh, const char *option)
Parse an IPv6 access control list.
Definition: service.c:968
struct GNUNET_STRINGS_IPv6NetworkPolicy * v6_denied
IPv6 addresses that are not allowed to connect.
Definition: service.c:183
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
struct GNUNET_SERVICE_Handle * sh
Service this listen context belongs to.
Definition: service.c:66
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_box_native(SOCKTYPE fd)
Box a native socket (and check that it is a socket).
Definition: network.c:624
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 struct SolverHandle * sh
struct ServiceListenContext * slc_tail
DLL of listen sockets used to accept new connections.
Definition: service.c:153
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
Information the service tracks per listen operation.
Definition: service.c:52
struct GNUNET_STRINGS_IPv4NetworkPolicy * v4_denied
IPv4 addresses that are not allowed to connect.
Definition: service.c:178
static struct GNUNET_NETWORK_Handle * open_listen_socket(const struct sockaddr *server_addr, socklen_t socklen)
Create and initialize a listen socket for the server.
Definition: service.c:1365
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
char * getenv()
static int process_acl4(struct GNUNET_STRINGS_IPv4NetworkPolicy **ret, struct GNUNET_SERVICE_Handle *sh, const char *option)
Parse an IPv4 access control list.
Definition: service.c:927
static struct GNUNET_NETWORK_Handle * ls
Listen socket for STUN processing.
Definition: gnunet-nat.c:85
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
struct GNUNET_STRINGS_IPv6NetworkPolicy * v6_allowed
IPv6 addresses that are allowed to connect (if not set, all are allowed).
Definition: service.c:195
#define LOG(kind,...)
Definition: service.c:40
#define GNUNET_log(kind,...)
const char * service_name
Name of our service.
Definition: service.c:123
handle to a socket
Definition: network.c:46
struct GNUNET_STRINGS_IPv4NetworkPolicy * v4_allowed
IPv4 addresses that are allowed to connect (if not set, all are allowed).
Definition: service.c:189
#define GNUNET_YES
Definition: gnunet_common.h:77
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: service.c:118
int GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:548
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_user_name()

static char* get_user_name ( struct GNUNET_SERVICE_Handle sh)
static

Get the name of the user that'll be used to provide the service.

Parameters
shservice context
Returns
value of the 'USERNAME' option

Definition at line 1603 of file service.c.

References GNUNET_SERVICE_Handle::cfg, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_OK, and GNUNET_SERVICE_Handle::service_name.

Referenced by set_user_id().

1604 {
1605  char *un;
1606 
1608  sh->service_name,
1609  "USERNAME",
1610  &un))
1611  return NULL;
1612  return un;
1613 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
const char * service_name
Name of our service.
Definition: service.c:123
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.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: service.c:118
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_user_id()

static int set_user_id ( struct GNUNET_SERVICE_Handle sh)
static

Set user ID.

Parameters
shservice context
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 1623 of file service.c.

References _, get_user_name(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_OK, GNUNET_SYSERR, and LOG.

Referenced by GNUNET_SERVICE_run_().

1624 {
1625  char *user;
1626 
1627  if (NULL == (user = get_user_name(sh)))
1628  return GNUNET_OK; /* keep */
1629 
1630  struct passwd *pws;
1631 
1632  errno = 0;
1633  pws = getpwnam(user);
1634  if (NULL == pws)
1635  {
1637  _("Cannot obtain information about user `%s': %s\n"),
1638  user,
1639  errno == 0 ? _("No such user") : strerror(errno));
1640  GNUNET_free(user);
1641  return GNUNET_SYSERR;
1642  }
1643  if ((0 != setgid(pws->pw_gid)) || (0 != setegid(pws->pw_gid)) ||
1644 #if HAVE_INITGROUPS
1645  (0 != initgroups(user, pws->pw_gid)) ||
1646 #endif
1647  (0 != setuid(pws->pw_uid)) || (0 != seteuid(pws->pw_uid)))
1648  {
1649  if ((0 != setregid(pws->pw_gid, pws->pw_gid)) ||
1650  (0 != setreuid(pws->pw_uid, pws->pw_uid)))
1651  {
1653  _("Cannot change user/group to `%s': %s\n"),
1654  user,
1655  strerror(errno));
1656  GNUNET_free(user);
1657  return GNUNET_SYSERR;
1658  }
1659  }
1660 
1661  GNUNET_free(user);
1662  return GNUNET_OK;
1663 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static char * get_user_name(struct GNUNET_SERVICE_Handle *sh)
Get the name of the user that&#39;ll be used to provide the service.
Definition: service.c:1603
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#define LOG(kind,...)
Definition: service.c:40
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_pid_file_name()

static char* get_pid_file_name ( struct GNUNET_SERVICE_Handle sh)
static

Get the name of the file where we will write the PID of the service.

Parameters
shservice context
Returns
name of the file for the process ID

Definition at line 1674 of file service.c.

References GNUNET_SERVICE_Handle::cfg, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_OK, and GNUNET_SERVICE_Handle::service_name.

Referenced by pid_file_delete().

1675 {
1676  char *pif;
1677 
1679  sh->service_name,
1680  "PIDFILE",
1681  &pif))
1682  return NULL;
1683  return pif;
1684 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
const char * service_name
Name of our service.
Definition: service.c:123
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.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: service.c:118
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pid_file_delete()

static void pid_file_delete ( struct GNUNET_SERVICE_Handle sh)
static

Delete the PID file that was created by our parent.

Parameters
shservice context

Definition at line 1693 of file service.c.

References get_pid_file_name(), GNUNET_ERROR_TYPE_WARNING, GNUNET_free, and LOG_STRERROR_FILE.

Referenced by GNUNET_SERVICE_run_().

1694 {
1695  char *pif = get_pid_file_name(sh);
1696 
1697  if (NULL == pif)
1698  return; /* no PID file */
1699  if (0 != unlink(pif))
1701  GNUNET_free(pif);
1702 }
static char * get_pid_file_name(struct GNUNET_SERVICE_Handle *sh)
Get the name of the file where we will write the PID of the service.
Definition: service.c:1674
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: service.c:45
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ detach_terminal()

static int detach_terminal ( struct GNUNET_SERVICE_Handle sh)
static

Detach from terminal.

Parameters
shservice context
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 1712 of file service.c.

References _, GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_OK, GNUNET_SYSERR, LOG, LOG_STRERROR, pid, and GNUNET_SERVICE_Handle::ready_confirm_fd.

Referenced by GNUNET_SERVICE_run_().

1713 {
1714  pid_t pid;
1715  int nullfd;
1716  int filedes[2];
1717 
1718  if (0 != pipe(filedes))
1719  {
1721  return GNUNET_SYSERR;
1722  }
1723  pid = fork();
1724  if (pid < 0)
1725  {
1727  return GNUNET_SYSERR;
1728  }
1729  if (0 != pid)
1730  {
1731  /* Parent */
1732  char c;
1733 
1734  GNUNET_break(0 == close(filedes[1]));
1735  c = 'X';
1736  if (1 != read(filedes[0], &c, sizeof(char)))
1738  fflush(stdout);
1739  switch (c)
1740  {
1741  case '.':
1742  exit(0);
1743 
1744  case 'I':
1746  _("Service process failed to initialize\n"));
1747  break;
1748 
1749  case 'S':
1751  _("Service process could not initialize server function\n"));
1752  break;
1753 
1754  case 'X':
1756  _("Service process failed to report status\n"));
1757  break;
1758  }
1759  exit(1); /* child reported error */
1760  }
1761  GNUNET_break(0 == close(0));
1762  GNUNET_break(0 == close(1));
1763  GNUNET_break(0 == close(filedes[0]));
1764  nullfd = open("/dev/null", O_RDWR | O_APPEND);
1765  if (nullfd < 0)
1766  return GNUNET_SYSERR;
1767  /* set stdin/stdout to /dev/null */
1768  if ((dup2(nullfd, 0) < 0) || (dup2(nullfd, 1) < 0))
1769  {
1771  (void)close(nullfd);
1772  return GNUNET_SYSERR;
1773  }
1774  (void)close(nullfd);
1775  /* Detach from controlling terminal */
1776  pid = setsid();
1777  if (-1 == pid)
1779  sh->ready_confirm_fd = filedes[1];
1780 
1781  return GNUNET_OK;
1782 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
int ready_confirm_fd
If we are daemonizing, this FD is set to the pipe to the parent.
Definition: service.c:228
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#define LOG(kind,...)
Definition: service.c:40
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
#define LOG_STRERROR(kind, syscall)
Definition: service.c:42
Here is the caller graph for this function:

◆ teardown_service()

static void teardown_service ( struct GNUNET_SERVICE_Handle sh)
static

Tear down the service, closing the listen sockets and freeing the ACLs.

Parameters
shhandle to the service to tear down.

Definition at line 1792 of file service.c.

References GNUNET_break, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_free_non_null, GNUNET_NETWORK_socket_close(), GNUNET_OK, GNUNET_SCHEDULER_cancel(), ServiceListenContext::listen_socket, ServiceListenContext::listen_task, GNUNET_SERVICE_Handle::slc_head, GNUNET_SERVICE_Handle::slc_tail, GNUNET_SERVICE_Handle::v4_allowed, GNUNET_SERVICE_Handle::v4_denied, GNUNET_SERVICE_Handle::v6_allowed, and GNUNET_SERVICE_Handle::v6_denied.

Referenced by GNUNET_SERVICE_run_(), and GNUNET_SERVICE_stop().

1793 {
1794  struct ServiceListenContext *slc;
1795 
1800  while (NULL != (slc = sh->slc_head))
1801  {
1803  if (NULL != slc->listen_task)
1807  GNUNET_free(slc);
1808  }
1809 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct ServiceListenContext * slc_head
DLL of listen sockets used to accept new connections.
Definition: service.c:148
struct GNUNET_NETWORK_Handle * listen_socket
Socket we are listening on.
Definition: service.c:71
#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.
struct GNUNET_STRINGS_IPv6NetworkPolicy * v6_denied
IPv6 addresses that are not allowed to connect.
Definition: service.c:183
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct ServiceListenContext * slc_tail
DLL of listen sockets used to accept new connections.
Definition: service.c:153
Information the service tracks per listen operation.
Definition: service.c:52
struct GNUNET_STRINGS_IPv4NetworkPolicy * v4_denied
IPv4 addresses that are not allowed to connect.
Definition: service.c:178
struct GNUNET_SCHEDULER_Task * listen_task
Task scheduled to do the listening.
Definition: service.c:76
struct GNUNET_STRINGS_IPv6NetworkPolicy * v6_allowed
IPv6 addresses that are allowed to connect (if not set, all are allowed).
Definition: service.c:195
struct GNUNET_STRINGS_IPv4NetworkPolicy * v4_allowed
IPv4 addresses that are allowed to connect (if not set, all are allowed).
Definition: service.c:189
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:548
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
Here is the call graph for this function:
Here is the caller graph for this function:

◆ return_agpl()

static void return_agpl ( void *  cls,
const struct GNUNET_MessageHeader msg 
)
static

Function to return link to AGPL source upon request.

Parameters
clsclosure with the identification of the client
msgAGPL request

Definition at line 1819 of file service.c.

References env, GNUNET_AGPL_URL, GNUNET_MESSAGE_TYPE_RESPONSE_AGPL, GNUNET_MQ_msg_extra, GNUNET_MQ_send(), GNUNET_SERVICE_client_continue(), GNUNET_SERVICE_client_get_mq(), mq, and res.

Referenced by GNUNET_SERVICE_start().

1820 {
1821  struct GNUNET_SERVICE_Client *client = cls;
1822  struct GNUNET_MQ_Handle *mq;
1823  struct GNUNET_MQ_Envelope *env;
1824  struct GNUNET_MessageHeader *res;
1825  size_t slen;
1826 
1827  (void)msg;
1828  slen = strlen(GNUNET_AGPL_URL) + 1;
1830  memcpy(&res[1], GNUNET_AGPL_URL, slen);
1831  mq = GNUNET_SERVICE_client_get_mq(client);
1832  GNUNET_MQ_send(mq, env);
1834 }
struct GNUNET_MQ_Handle * GNUNET_SERVICE_client_get_mq(struct GNUNET_SERVICE_Client *c)
Obtain the message queue of c.
Definition: service.c:2424
Handle to a client that is connected to a service.
Definition: service.c:246
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
static int res
#define GNUNET_MESSAGE_TYPE_RESPONSE_AGPL
Source code link.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
Handle to a message queue.
Definition: mq.c:84
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
#define GNUNET_AGPL_URL
NOTE: You MUST adjust this URL to point to the location of a publicly accessible repository (or TGZ) ...
Header for all communications.
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:351
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2234
Here is the call graph for this function:
Here is the caller graph for this function:

◆ resume_client_receive()

static void resume_client_receive ( void *  cls)
static

Task run to resume receiving data from the client after the client called GNUNET_SERVICE_client_continue().

Parameters
clsour struct GNUNET_SERVICE_Client

Definition at line 2198 of file service.c.

References GNUNET_SERVICE_Client::drop_task, GNUNET_assert, GNUNET_MST_next(), GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_read_net(), GNUNET_SERVICE_client_drop(), GNUNET_SYSERR, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, GNUNET_SERVICE_Client::mst, GNUNET_SERVICE_Client::needs_continue, GNUNET_SERVICE_Client::recv_task, ret, service_client_recv(), and GNUNET_SERVICE_Client::sock.

Referenced by GNUNET_SERVICE_client_continue().

2199 {
2200  struct GNUNET_SERVICE_Client *c = cls;
2201  int ret;
2202 
2203  c->recv_task = NULL;
2204  /* first, check if there is still something in the buffer */
2205  ret = GNUNET_MST_next(c->mst, GNUNET_YES);
2206  if (GNUNET_SYSERR == ret)
2207  {
2208  if (NULL == c->drop_task)
2210  return;
2211  }
2212  if (GNUNET_NO == ret)
2213  return; /* done processing, wait for more later */
2214  GNUNET_assert(GNUNET_OK == ret);
2215  if (GNUNET_YES == c->needs_continue)
2216  return; /* #GNUNET_MST_next() did give a message to the client */
2217  /* need to receive more data from the network first */
2218  if (NULL != c->recv_task)
2219  return;
2221  c->sock,
2223  c);
2224 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
int GNUNET_MST_next(struct GNUNET_MessageStreamTokenizer *mst, int one_shot)
Obtain the next message from the mst, assuming that there are more unprocessed messages in the intern...
Definition: mst.c:393
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static int ret
Final status code.
Definition: gnunet-arm.c:89
struct GNUNET_MessageStreamTokenizer * mst
Tokenizer we use for processing incoming data.
Definition: service.c:275
Handle to a client that is connected to a service.
Definition: service.c:246
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2315
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static void service_client_recv(void *cls)
A client sent us data.
Definition: service.c:704
int needs_continue
Are we waiting for the application to call GNUNET_SERVICE_client_continue()?
Definition: service.c:339
#define GNUNET_YES
Definition: gnunet_common.h:77
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1467
struct GNUNET_NETWORK_Handle * sock
Socket of this client.
Definition: service.c:265
struct GNUNET_SCHEDULER_Task * recv_task
Task that receives data from the client to pass it to the handlers.
Definition: service.c:293
struct GNUNET_SCHEDULER_Task * drop_task
Task run to finish dropping the client after the stack has properly unwound.
Definition: service.c:287
Here is the call graph for this function:
Here is the caller graph for this function:

◆ finish_client_drop()

static void finish_client_drop ( void *  cls)
static

Asynchronously finish dropping the client.

Parameters
clsthe struct GNUNET_SERVICE_Client.

Definition at line 2275 of file service.c.

References do_resume(), GNUNET_SERVICE_Client::drop_task, GNUNET_assert, GNUNET_break, GNUNET_free, GNUNET_MQ_destroy(), GNUNET_MST_destroy(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_free_memory_only_(), GNUNET_NO, GNUNET_OK, GNUNET_SERVICE_shutdown(), have_non_monitor_clients(), GNUNET_SERVICE_Client::mq, GNUNET_SERVICE_Client::mst, GNUNET_SERVICE_Client::persist, GNUNET_SERVICE_Client::recv_task, GNUNET_SERVICE_Client::send_task, ServiceListenContext::sh, GNUNET_SERVICE_Client::sh, GNUNET_SERVICE_Client::sock, GNUNET_SERVICE_Handle::suspend_state, SUSPEND_STATE_EMFILE, SUSPEND_STATE_SHUTDOWN, and GNUNET_SERVICE_Client::warn_task.

Referenced by GNUNET_SERVICE_client_drop().

2276 {
2277  struct GNUNET_SERVICE_Client *c = cls;
2278  struct GNUNET_SERVICE_Handle *sh = c->sh;
2279 
2280  c->drop_task = NULL;
2281  GNUNET_assert(NULL == c->send_task);
2282  GNUNET_assert(NULL == c->recv_task);
2283  GNUNET_assert(NULL == c->warn_task);
2284  GNUNET_MST_destroy(c->mst);
2285  GNUNET_MQ_destroy(c->mq);
2286  if (GNUNET_NO == c->persist)
2287  {
2289  if ((0 != (SUSPEND_STATE_EMFILE & sh->suspend_state)) &&
2290  (0 == (SUSPEND_STATE_SHUTDOWN & sh->suspend_state)))
2292  }
2293  else
2294  {
2296  }
2297  GNUNET_free(c);
2298  if ((0 != (SUSPEND_STATE_SHUTDOWN & sh->suspend_state)) &&
2301 }
enum SuspendReason suspend_state
Are we suspended, and if so, why?
Definition: service.c:216
struct GNUNET_SCHEDULER_Task * send_task
Task that transmit data to the client.
Definition: service.c:298
Handle to a service.
Definition: service.c:114
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
struct GNUNET_MQ_Handle * mq
Message queue for the client.
Definition: service.c:270
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_MessageStreamTokenizer * mst
Tokenizer we use for processing incoming data.
Definition: service.c:275
Handle to a client that is connected to a service.
Definition: service.c:246
void GNUNET_NETWORK_socket_free_memory_only_(struct GNUNET_NETWORK_Handle *desc)
Only free memory of a socket, keep the file descriptor untouched.
Definition: network.c:610
void GNUNET_MST_destroy(struct GNUNET_MessageStreamTokenizer *mst)
Destroys a tokenizer.
Definition: mst.c:410
struct GNUNET_SCHEDULER_Task * warn_task
Task that warns about missing calls to GNUNET_SERVICE_client_continue().
Definition: service.c:281
static struct SolverHandle * sh
int persist
Persist the file handle for this client no matter what happens, force the OS to close once the proces...
Definition: service.c:327
static void do_resume(struct GNUNET_SERVICE_Handle *sh, enum SuspendReason sr)
Resume accepting connections from the listen socket.
Definition: service.c:869
struct GNUNET_SERVICE_Handle * sh
Service that this client belongs to.
Definition: service.c:260
Suspension because service was permanently shutdown.
Definition: service.c:107
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
OS ran out of file descriptors.
Definition: service.c:97
struct GNUNET_NETWORK_Handle * sock
Socket of this client.
Definition: service.c:265
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:548
struct GNUNET_SCHEDULER_Task * recv_task
Task that receives data from the client to pass it to the handlers.
Definition: service.c:293
struct GNUNET_SCHEDULER_Task * drop_task
Task run to finish dropping the client after the stack has properly unwound.
Definition: service.c:287
static int have_non_monitor_clients(struct GNUNET_SERVICE_Handle *sh)
Check if any of the clients we have left are unrelated to monitoring.
Definition: service.c:356
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_SERVICE_shutdown(struct GNUNET_SERVICE_Handle *sh)
Explicitly stops the service.
Definition: service.c:2370
Here is the call graph for this function:
Here is the caller graph for this function: