Example #1
0
/**
 * Looking our own hostname.
 *
 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
 * @param callback function to call with addresses
 * @param cls closure for callback
 * @param timeout how long to try resolving
 * @return handle that can be used to cancel the request, NULL on error
 */
struct GNUNET_RESOLVER_RequestHandle *
GNUNET_RESOLVER_hostname_resolve (int af,
                                  struct GNUNET_TIME_Relative timeout,
                                  GNUNET_RESOLVER_AddressCallback callback,
                                  void *cls)
{
  char hostname[GNUNET_OS_get_hostname_max_length () + 1];

  if (0 != gethostname (hostname, sizeof (hostname) - 1))
  {
    LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                  "gethostname");
    return NULL;
  }
  LOG (GNUNET_ERROR_TYPE_DEBUG, "Resolving our hostname `%s'\n", hostname);
  return GNUNET_RESOLVER_ip_get (hostname, af, timeout, callback, cls);
}
Example #2
0
/**
 * Get local fully qualified af name
 *
 * @return fqdn
 */
char *
GNUNET_RESOLVER_local_fqdn_get ()
{
  struct hostent *host;
  char hostname[GNUNET_OS_get_hostname_max_length () + 1];

  if (0 != gethostname (hostname, sizeof (hostname) - 1))
  {
    LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                  "gethostname");
    return NULL;
  }
  LOG (GNUNET_ERROR_TYPE_DEBUG, "Resolving our FQDN `%s'\n", hostname);
  host = gethostbyname (hostname);
  if (NULL == host)
  {
    LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not resolve our FQDN : %s\n"),
         hstrerror (h_errno));
    return NULL;
  }
  return GNUNET_strdup (host->h_name);
}
int
main (int argc, char *av[])
{
  static char *const argv[] = {
    "test-gnunet-service-arm",
    "-c",
    "test_arm_api_data.conf",
    NULL
  };
  static struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };
  char hostname[GNUNET_OS_get_hostname_max_length () + 1];

  if (0 != gethostname (hostname, sizeof (hostname) - 1))
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                         "gethostname");
    FPRINTF (stderr,
             "%s",
             "Failed to determine my own hostname, testcase not run.\n");
    return 0;
  }
  if ( (0 == strcmp (hostname,
		     "localhost")) ||
       (0 == strcmp (hostname,
		     "ipv6-localnet")) )
  {
    /* we cannot use 'localhost' as this would not trigger the
       resolver service (see resolver_api.c); so in this case,
       we fall back to (ab)using gnu.org. */
    strcpy (hostname,
	    "www.gnu.org");
  }
  /* trigger DNS lookup */
#if HAVE_GETADDRINFO
  {
    struct addrinfo *ai;
    int ret;

    if (0 != (ret = getaddrinfo (hostname, NULL, NULL, &ai)))
    {
      FPRINTF (stderr,
               "Failed to resolve my hostname `%s', testcase not run.\n",
               hostname);
      return 0;
    }
    freeaddrinfo (ai);
  }
#elif HAVE_GETHOSTBYNAME2
  {
    struct hostent *host;

    host = gethostbyname2 (hostname, AF_INET);
    if (NULL == host)
      host = gethostbyname2 (hostname, AF_INET6);
    if (NULL == host)
      {
        FPRINTF (stderr,
                 "Failed to resolve my hostname `%s', testcase not run.\n",
                 hostname);
        return 0;
      }
  }
#elif HAVE_GETHOSTBYNAME
  {
    struct hostent *host;

    host = gethostbyname (hostname);
    if (NULL == host)
      {
        FPRINTF (stderr,
                 "Failed to resolve my hostname `%s', testcase not run.\n",
                 hostname);
        return 0;
      }
  }
#else
  FPRINTF (stderr,
           "libc fails to have resolver function, testcase not run.\n");
  return 0;
#endif
  GNUNET_log_setup ("test-gnunet-service-arm",
		    "WARNING",
		    NULL);
  GNUNET_break (GNUNET_OK ==
		GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
				    argv, "test-gnunet-service-arm",
				    "nohelp", options,
                                    &run, NULL));
  if (0 != ret)
  {
    fprintf (stderr,
             "Test failed with error code %d\n",
             ret);
  }
  return ret;
}
Example #4
0
/**
 * The main scheduler run task
 *
 * @param cls NULL
 * @param tc scheduler task context
 */
static void
run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_TESTBED_Host **hosts;
  const struct GNUNET_CONFIGURATION_Handle *null_cfg;
  char *tmpdir;
  char *hostname;
  size_t hostname_len;
  unsigned int nhosts;

  null_cfg = GNUNET_CONFIGURATION_create ();
  nhosts = GNUNET_TESTBED_hosts_load_from_loadleveler (null_cfg, &hosts);
  if (0 == nhosts)
  {
    GNUNET_break (0);
    ret = GNUNET_SYSERR;
    return;
  }
  hostname_len = GNUNET_OS_get_hostname_max_length ();
  hostname = GNUNET_malloc (hostname_len);
  if (0 != gethostname (hostname, hostname_len))
  {
    LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot get hostname.  Exiting\n");
    GNUNET_free (hostname);
    destroy_hosts (hosts, nhosts);
    ret = GNUNET_SYSERR;
    return;
  }
  if (NULL == strstr (GNUNET_TESTBED_host_get_hostname (hosts[0]), hostname))
  {
    LOG_DEBUG ("Exiting as `%s' is not the lowest host\n", hostname);
    GNUNET_free (hostname);
    ret = GNUNET_OK;
    return;
  }
  LOG_DEBUG ("Will be executing `%s' on host `%s'\n", argv2[0], hostname);
  GNUNET_free (hostname);
  destroy_hosts (hosts, nhosts);
  tmpdir = getenv ("TMPDIR");
  if (NULL == tmpdir)
    tmpdir = getenv ("TMP");
  if (NULL == tmpdir)
    tmpdir = getenv ("TEMP");
  if (NULL == tmpdir)
    tmpdir = "/tmp";
  (void) GNUNET_asprintf (&fn, "%s/gnunet-testbed-spawn.lock", tmpdir);
  /* Open the unique file; we can create it then we can spawn the child process
     else we exit */
  fh = open (fn, O_CREAT | O_EXCL | O_CLOEXEC,
             S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  if (-1 == fh)
  {
    if (EEXIST == errno)
    {
      LOG_DEBUG ("Lock file already created by other process.  Exiting\n");
      ret = GNUNET_OK;
      return;
    }
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "open");
    ret = GNUNET_SYSERR;
    return;
  }
  /* Spawn the new process here */
  LOG (GNUNET_ERROR_TYPE_INFO, _("Spawning process `%s'\n"), argv2[0]);
  child = GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
                                       NULL, NULL,
                                       argv2[0], argv2);
  if (NULL == child)
  {
    GNUNET_break (0);
    ret = GNUNET_SYSERR;
    shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
    return;
  }
  ret = GNUNET_OK;
  terminate_task_id =
      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                    &terminate_task, NULL);
  child_death_task_id =
    GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
				    GNUNET_DISK_pipe_handle (sigpipe,
							     GNUNET_DISK_PIPE_END_READ),
				    &child_death_task, NULL);
}
Example #5
0
/**
 * Get local fully qualified af name
 *
 * @return fqdn
 */
char *
GNUNET_RESOLVER_local_fqdn_get ()
{
    char hostname[GNUNET_OS_get_hostname_max_length () + 1];

    if (0 != gethostname (hostname, sizeof (hostname) - 1))
    {
        LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                      "gethostname");
        return NULL;
    }
    LOG (GNUNET_ERROR_TYPE_DEBUG,
         "Resolving our FQDN `%s'\n",
         hostname);
#if HAVE_GETADDRINFO
    {
        struct addrinfo *ai;
        int ret;
        char *rval;

        if (0 != (ret = getaddrinfo (hostname, NULL, NULL, &ai)))
        {
            LOG (GNUNET_ERROR_TYPE_ERROR,
                 _("Could not resolve our FQDN: %s\n"),
                 gai_strerror (ret));
            return NULL;
        }
        if (NULL != ai->ai_canonname)
            rval = GNUNET_strdup (ai->ai_canonname);
        else
            rval = GNUNET_strdup (hostname);
        freeaddrinfo (ai);
        return rval;
    }
#elif HAVE_GETHOSTBYNAME2
    {
        struct hostent *host;

        host = gethostbyname2 (hostname, AF_INET);
        if (NULL == host)
            host = gethostbyname2 (hostname, AF_INET6);
        if (NULL == host)
        {
            LOG (GNUNET_ERROR_TYPE_ERROR,
                 _("Could not resolve our FQDN: %s\n"),
                 hstrerror (h_errno));
            return NULL;
        }
        return GNUNET_strdup (host->h_name);
    }
#elif HAVE_GETHOSTBYNAME
    {
        struct hostent *host;

        host = gethostbyname (hostname);
        if (NULL == host)
        {
            LOG (GNUNET_ERROR_TYPE_ERROR,
                 _("Could not resolve our FQDN: %s\n"),
                 hstrerror (h_errno));
            return NULL;
        }
        return GNUNET_strdup (host->h_name);
    }
#else
    /* fallback: just hope name is already FQDN */
    return GNUNET_strdup (hostname);
#endif
}