Esempio n. 1
0
static void
task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct sockaddr_in sa;
  struct sockaddr *sap[2];
  socklen_t slens[2];

  sap[0] = (struct sockaddr *) &sa;
  slens[0] = sizeof (sa);
  sap[1] = NULL;
  slens[1] = 0;
  memset (&sa, 0, sizeof (sa));
#if HAVE_SOCKADDR_IN_SIN_LEN
  sa.sin_len = sizeof (sa);
#endif
  sa.sin_family = AF_INET;
  sa.sin_port = htons (PORT);
  server =
      GNUNET_SERVER_create (NULL, NULL, sap, slens,
                            GNUNET_TIME_relative_multiply
                            (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
  GNUNET_assert (server != NULL);
  handlers[0].callback_cls = cls;
  GNUNET_SERVER_add_handlers (server, handlers);
  GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
  cfg = GNUNET_CONFIGURATION_create ();
  GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
  GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", "localhost");
  GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
                                         "localhost");
  client = GNUNET_CLIENT_connect ("test", cfg);
  GNUNET_assert (client != NULL);

  test_mq (client);
}
Esempio n. 2
0
/**
 * First task run by the scheduler.  Initializes the server and
 * a client and asks for a transmission from the client to the
 * server.
 *
 * @param cls NULL
 */
static void
task (void *cls)
{
  struct sockaddr_in sa;
  struct sockaddr *sap[2];
  socklen_t slens[2];
  struct GNUNET_MQ_Envelope *env;
  struct GNUNET_MessageHeader *msg;
  struct GNUNET_MQ_MessageHandler chandlers[] = {
    GNUNET_MQ_hd_fixed_size (reply,
                             MY_TYPE,
                             struct GNUNET_MessageHeader,
                             cls),
    GNUNET_MQ_handler_end ()
  };

  sap[0] = (struct sockaddr *) &sa;
  slens[0] = sizeof (sa);
  sap[1] = NULL;
  slens[1] = 0;
  memset (&sa, 0, sizeof (sa));
#if HAVE_SOCKADDR_IN_SIN_LEN
  sa.sin_len = sizeof (sa);
#endif
  sa.sin_family = AF_INET;
  sa.sin_port = htons (PORT);
  server = GNUNET_SERVER_create (NULL, NULL,
                                 sap, slens,
                                 TIMEOUT, GNUNET_NO);
  GNUNET_assert (server != NULL);
  GNUNET_SERVER_add_handlers (server, handlers);
  cfg = GNUNET_CONFIGURATION_create ();
  GNUNET_CONFIGURATION_set_value_number (cfg,
                                         "test-server",
                                         "PORT",
                                         PORT);
  GNUNET_CONFIGURATION_set_value_string (cfg,
                                         "test-server",
                                         "HOSTNAME",
                                         "localhost");
  GNUNET_CONFIGURATION_set_value_string (cfg,
                                         "resolver",
                                         "HOSTNAME",
                                         "localhost");
  mq = GNUNET_CLIENT_connect (cfg,
                              "test-server",
                              chandlers,
                              &mq_error_handler,
                              NULL);
  GNUNET_assert (NULL != mq);
  ok = 2;
  env = GNUNET_MQ_msg (msg,
                       MY_TYPE);
  GNUNET_MQ_send (mq,
                  env);
}
Esempio n. 3
0
/**
 * Run a service startup sequence within an existing
 * initialized system.
 *
 * @param service_name our service name
 * @param cfg configuration to use
 * @param options service options
 * @return NULL on error, service handle
 */
struct GNUNET_SERVICE_Context *
GNUNET_SERVICE_start (const char *service_name,
                      const struct GNUNET_CONFIGURATION_Handle *cfg,
		      enum GNUNET_SERVICE_Options options)
{
  int i;
  struct GNUNET_SERVICE_Context *sctx;

  sctx = GNUNET_new (struct GNUNET_SERVICE_Context);
  sctx->ready_confirm_fd = -1;  /* no daemonizing */
  sctx->ret = GNUNET_OK;
  sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
  sctx->service_name = service_name;
  sctx->cfg = cfg;
  sctx->options = options;

  /* setup subsystems */
  if (GNUNET_OK != setup_service (sctx))
  {
    GNUNET_SERVICE_stop (sctx);
    return NULL;
  }
  if (NULL != sctx->lsocks)
    sctx->server =
        GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
                                           sctx->timeout, sctx->require_found);
  else
    sctx->server =
        GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
                              sctx->timeout, sctx->require_found);

  if (NULL == sctx->server)
  {
    GNUNET_SERVICE_stop (sctx);
    return NULL;
  }
#ifndef WINDOWS
  if (NULL != sctx->addrs)
    for (i = 0; NULL != sctx->addrs[i]; i++)
      if ((AF_UNIX == sctx->addrs[i]->sa_family)
          && ('\0' != ((const struct sockaddr_un *)sctx->addrs[i])->sun_path[0]))
        GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sctx->addrs[i])->sun_path,
                                     sctx->match_uid,
                                     sctx->match_gid);
#endif
  sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
  memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
  i = 0;
  while ((sctx->my_handlers[i].callback != NULL))
    sctx->my_handlers[i++].callback_cls = sctx;
  GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
  return sctx;
}
/**
 * Main function that will be run.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param c configuration
 */
static void
run (void *cls, char *const *args, const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *c)
{
  static const struct GNUNET_SERVER_MessageHandler handlers[] = {
    {&test, NULL, GNUNET_MESSAGE_TYPE_NAT_TEST,
     sizeof (struct GNUNET_NAT_TestMessage)},
    {NULL, NULL, 0, 0}
  };
  unsigned int port;
  struct sockaddr_in in4;
  struct sockaddr_in6 in6;

  socklen_t slen[] = {
    sizeof (in4),
    sizeof (in6),
    0
  };
  struct sockaddr *sa[] = {
    (struct sockaddr *) &in4,
    (struct sockaddr *) &in6,
    NULL
  };

  cfg = c;
  if ((args[0] == NULL) || (1 != SSCANF (args[0], "%u", &port)) || (0 == port)
      || (65536 <= port))
  {
    FPRINTF (stderr,
             _
             ("Please pass valid port number as the first argument! (got `%s')\n"),
             args[0]);
    return;
  }
  memset (&in4, 0, sizeof (in4));
  memset (&in6, 0, sizeof (in6));
  in4.sin_family = AF_INET;
  in4.sin_port = htons ((uint16_t) port);
  in6.sin6_family = AF_INET6;
  in6.sin6_port = htons ((uint16_t) port);
#if HAVE_SOCKADDR_IN_SIN_LEN
  in4.sin_len = sizeof (in4);
  in6.sin6_len = sizeof (in6);
#endif
  server =
      GNUNET_SERVER_create (NULL, NULL, (struct sockaddr * const *) sa, slen,
                            GNUNET_TIME_UNIT_SECONDS, GNUNET_YES);
  GNUNET_SERVER_add_handlers (server, handlers);
  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
                                NULL);
}
static void
task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
    struct sockaddr_un un;
    const char *unixpath = "/tmp/testsock";
    size_t slen = strlen (unixpath);
    struct sockaddr *sap[2];
    socklen_t slens[2];

    memset (&un, 0, sizeof (un));
    un.sun_family = AF_UNIX;
    memcpy (un.sun_path, unixpath, slen);
    un.sun_path[slen] = '\0';
#if HAVE_SOCKADDR_IN_SIN_LEN
    un.sun_len = (u_char) sizeof (un);
#endif
#if LINUX
    un.sun_path[0] = '\0';
#endif

    sap[0] = (struct sockaddr *) &un;
    slens[0] = sizeof (un);
    sap[1] = NULL;
    slens[1] = 0;
    server =
        GNUNET_SERVER_create (NULL, NULL, sap, slens,
                              GNUNET_TIME_relative_multiply
                              (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
    GNUNET_assert (server != NULL);
    handlers[0].callback_cls = cls;
    GNUNET_SERVER_add_handlers (server, handlers);
    GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
    cfg = GNUNET_CONFIGURATION_create ();

    GNUNET_CONFIGURATION_set_value_string (cfg, "test", "UNIXPATH", unixpath);
    GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
                                           "localhost");

    client = GNUNET_CLIENT_connect ("test", cfg);
    GNUNET_assert (client != NULL);
    GNUNET_CLIENT_notify_transmit_ready (client, 256,
                                         GNUNET_TIME_relative_multiply
                                         (GNUNET_TIME_UNIT_MILLISECONDS, 250),
                                         GNUNET_NO, &notify_ready, NULL);
}
Esempio n. 6
0
static void
task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct sockaddr_in sa;
  struct sockaddr *sap[2];
  socklen_t slens[2];

  /* test that ill-configured client fails instantly */
  GNUNET_assert (NULL == GNUNET_CLIENT_connect ("invalid-service", cfg));

  /* test IPC between client and server */
  sap[0] = (struct sockaddr *) &sa;
  slens[0] = sizeof (sa);
  sap[1] = NULL;
  slens[1] = 0;
  memset (&sa, 0, sizeof (sa));
#if HAVE_SOCKADDR_IN_SIN_LEN
  sa.sin_len = sizeof (sa);
#endif
  sa.sin_family = AF_INET;
  sa.sin_port = htons (PORT);
  server =
      GNUNET_SERVER_create (NULL, NULL, sap, slens,
                            GNUNET_TIME_relative_multiply
                            (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO);
  GNUNET_assert (server != NULL);
  handlers[0].callback_cls = cls;
  handlers[1].callback_cls = cls;
  GNUNET_SERVER_add_handlers (server, handlers);
  client = GNUNET_CLIENT_connect (MYNAME, cfg);
  GNUNET_assert (client != NULL);
  GNUNET_assert (NULL !=
                 GNUNET_CLIENT_notify_transmit_ready (client,
                                                      sizeof (struct
                                                              GNUNET_MessageHeader),
                                                      GNUNET_TIME_relative_multiply
                                                      (GNUNET_TIME_UNIT_SECONDS,5),
                                                      GNUNET_NO, &make_msg,
                                                      NULL));
  GNUNET_CLIENT_receive (client, &recv_bounce, cls,
                         GNUNET_TIME_relative_multiply
                         (GNUNET_TIME_UNIT_MILLISECONDS, 10000));
}
static void
task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
    struct sockaddr_in sa;
    struct sockaddr *sap[2];
    socklen_t slens[2];

    sap[0] = (struct sockaddr *) &sa;
    slens[0] = sizeof (sa);
    sap[1] = NULL;
    slens[1] = 0;
    memset (&sa, 0, sizeof (sa));
#if HAVE_SOCKADDR_IN_SIN_LEN
    sa.sin_len = sizeof (sa);
#endif
    sa.sin_family = AF_INET;
    sa.sin_port = htons (PORT);
    server = GNUNET_SERVER_create (NULL, NULL, sap, slens, TIMEOUT, GNUNET_NO);
    GNUNET_assert (server != NULL);
    GNUNET_SERVER_add_handlers (server, handlers);
    GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, NULL);
    cfg = GNUNET_CONFIGURATION_create ();
    GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
    GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME",
                                           "localhost");
    GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
                                           "localhost");
    cc = GNUNET_CLIENT_connect ("test-server", cfg);
    GNUNET_assert (cc != NULL);
    GNUNET_assert (NULL !=
                   GNUNET_CLIENT_notify_transmit_ready (cc,
                           sizeof (struct
                                   GNUNET_MessageHeader),
                           TIMEOUT, GNUNET_YES,
                           &transmit_initial_message,
                           NULL));
}
Esempio n. 8
0
/**
 * Initial task for the service.
 *
 * @param cls service context
 * @param tc unused
 */
static void
service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_SERVICE_Context *sctx = cls;
  unsigned int i;

  if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
    return;
  (void) GNUNET_SPEEDUP_start_ (sctx->cfg);
  GNUNET_RESOLVER_connect (sctx->cfg);
  if (NULL != sctx->lsocks)
    sctx->server
      = GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
                                           sctx->timeout, sctx->require_found);
  else
    sctx->server
      = GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
                              sctx->timeout, sctx->require_found);
  if (NULL == sctx->server)
  {
    if (NULL != sctx->addrs)
      for (i = 0; NULL != sctx->addrs[i]; i++)
        LOG (GNUNET_ERROR_TYPE_INFO,
             _("Failed to start `%s' at `%s'\n"),
             sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
    sctx->ret = GNUNET_SYSERR;
    return;
  }
#ifndef WINDOWS
  if (NULL != sctx->addrs)
    for (i = 0; NULL != sctx->addrs[i]; i++)
      if ((AF_UNIX == sctx->addrs[i]->sa_family)
          && ('\0' != ((const struct sockaddr_un *)sctx->addrs[i])->sun_path[0]))
        GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sctx->addrs[i])->sun_path,
                                     sctx->match_uid,
                                     sctx->match_gid);
#endif


  if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
  {
    /* install a task that will kill the server
     * process if the scheduler ever gets a shutdown signal */
    sctx->shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                                        &shutdown_task,
							sctx);
  }
  sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
  memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
  i = 0;
  while (NULL != sctx->my_handlers[i].callback)
    sctx->my_handlers[i++].callback_cls = sctx;
  GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
  if (-1 != sctx->ready_confirm_fd)
  {
    GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
    GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
    sctx->ready_confirm_fd = -1;
    write_pid_file (sctx, getpid ());
  }
  if (NULL != sctx->addrs)
  {
    i = 0;
    while (NULL != sctx->addrs[i])
    {
      LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
           sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
      i++;
    }
  }
  sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
}