static void
nm_client_state_changed (NMClient *client,
                         GParamSpec *gobject,
                         gpointer user_data)
{
    offline_mode_toggled_internal (NMN_MODEL (user_data), nm_client_get_state (client) == NM_STATE_ASLEEP);
}
static void
connectivity_nm_state_change_cb (NMClient *client,
    const GParamSpec *pspec,
    EmpathyConnectivity *connectivity)
{
  EmpathyConnectivityPriv *priv;
  gboolean new_nm_connected;
  NMState state;

  priv = GET_PRIV (connectivity);

  if (!priv->use_conn)
    return;

  state = nm_client_get_state (priv->nm_client);
  new_nm_connected = !(state == NM_STATE_CONNECTING
#if NM_CHECK_VERSION(0,8,992)
      || state == NM_STATE_DISCONNECTING
#endif
      || state == NM_STATE_ASLEEP
      || state == NM_STATE_DISCONNECTED);

  DEBUG ("New NetworkManager network state %d (connected: %s)", state,
      new_nm_connected ? "true" : "false");

  connectivity_change_state (connectivity, new_nm_connected);
}
static gboolean
test_get_state (NMClient *client)
{
	guint state;

	state = nm_client_get_state (client);
	g_print ("Current state: %d\n", state);

	return TRUE;
}
Exemple #4
0
static gboolean
is_online (void)
{
  NMClient *client;
  gboolean online;
  g_autoptr(GError) error = NULL;

  /* Don’t connect to NetworkManager when we are supposed to use the session
   * bus, as NM is on the system bus, and we don’t want to mock it up. */
  if (should_listen_on_session_bus ())
    {
      g_message ("Not using NetworkManager: assuming network is online.");
      return TRUE;
    }

  client = nm_client_new (NULL, &error);
  if (!client)
    {
      g_message ("Failed to get the NetworkManager client: %s", error->message);
      return FALSE;
    }

  /* Assume that the ostree server is remote and only consider to be
   * online for ostree updates if we have global connectivity.
   * For Avahi updates, local or site connectivity is adequate.
   */
  switch (nm_client_get_state (client)) {
  case NM_STATE_CONNECTED_LOCAL:
  case NM_STATE_CONNECTED_SITE:
  case NM_STATE_CONNECTED_GLOBAL:
    online = TRUE;
    break;
  case NM_STATE_UNKNOWN:
  case NM_STATE_ASLEEP:
  case NM_STATE_DISCONNECTED:
  case NM_STATE_DISCONNECTING:
  case NM_STATE_CONNECTING:
  default:
    online = FALSE;
    break;
  }
  g_object_unref (client);

  if (!online)
    info (EOS_UPDATER_NOT_ONLINE_MSGID,
          "Not currently online. Not updating");
  return online;
}
Exemple #5
0
/* Check if NM has an active connection */
gboolean is_nm_connected(void) {
    NMState state;
    NMClient *client = NULL;

#if !defined(GLIB_VERSION_2_36)
    g_type_init();
#endif

    client = nm_client_new();
    if (!client)
        return FALSE;

    state = nm_client_get_state(client);
    g_object_unref(client);

    if (state == NM_STATE_CONNECTED)
        return TRUE;
    else
        return FALSE;
}
Exemple #6
0
static gboolean
get_nm_state (NMClient *client)
{
	NMState state;
	char *state_string;
	gboolean success = TRUE;

	state = nm_client_get_state (client);

	switch (state) {
	case NM_STATE_ASLEEP:
		state_string = "asleep";
		break;
	case NM_STATE_CONNECTING:
		state_string = "connecting";
		break;
	case NM_STATE_CONNECTED_LOCAL:
		state_string = "connected (local only)";
		break;
	case NM_STATE_CONNECTED_SITE:
		state_string = "connected (site only)";
		break;
	case NM_STATE_CONNECTED_GLOBAL:
		state_string = "connected (global)";
		break;
	case NM_STATE_DISCONNECTED:
		state_string = "disconnected";
		break;
	case NM_STATE_UNKNOWN:
	default:
		state_string = "unknown";
		success = FALSE;
		break;
	}

	printf ("State: %s\n\n", state_string);

	return success;
}
static void
refresh_without_device (GisNetworkPage *page)
{
  GisNetworkPagePrivate *priv = page->priv;
  GtkWidget *label;
  GtkWidget *spinner;
  GtkWidget *swin;

  swin = WID("network-scrolledwindow");
  label = WID("no-network-label");
  spinner = WID("no-network-spinner");

  if (nm_client_get_state (priv->nm_client) == NM_STATE_CONNECTED_GLOBAL)
    ;
  else if (priv->nm_device != NULL)
    gtk_label_set_text (GTK_LABEL (label), _("Network is not available."));
  else
    gtk_label_set_text (GTK_LABEL (label), _("No network devices found."));

  gtk_widget_hide (swin);
  gtk_widget_hide (spinner);
  gtk_widget_show (label);
}
static void
client_properties_changed (GObject *object,
                           GParamSpec *pspec,
                           gpointer loop)
{
	NMClient *client = NM_CLIENT (object);
	NMState state;
	gboolean wait_startup = GPOINTER_TO_UINT (g_object_get_data (object, WAIT_STARTUP_TAG));

	if (!nm_client_get_nm_running (client))
		return;

	if (wait_startup) {
		if (!nm_client_get_startup (client))
			g_main_loop_quit (loop);
	} else {
		state = nm_client_get_state (client);
		if (   state == NM_STATE_CONNECTED_LOCAL
		    || state == NM_STATE_CONNECTED_SITE
		    || state == NM_STATE_CONNECTED_GLOBAL)
			g_main_loop_quit (loop);
	}
}
Exemple #9
0
static gboolean
show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_flds)
{
	gboolean startup = FALSE;
	NMState state = NM_STATE_UNKNOWN;
	NMConnectivityState connectivity = NM_CONNECTIVITY_UNKNOWN;
	gboolean net_enabled;
	gboolean wireless_hw_enabled, wireless_enabled;
	gboolean wwan_hw_enabled, wwan_enabled;
	GError *error = NULL;
	const char *fields_str;
	const char *fields_all =    print_flds ? print_flds : NMC_FIELDS_NM_STATUS_ALL;
	const char *fields_common = print_flds ? print_flds : NMC_FIELDS_NM_STATUS_COMMON;
	NmcOutputField *tmpl, *arr;
	size_t tmpl_len;

	if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
		fields_str = fields_common;
	else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0)
		fields_str = fields_all;
	else
		fields_str = nmc->required_fields;

	tmpl = nmc_fields_nm_status;
	tmpl_len = sizeof (nmc_fields_nm_status);
	nmc->print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error);

	if (error) {
		g_string_printf (nmc->return_text, _("Error: only these fields are allowed: %s"), fields_all);
		g_error_free (error);
		nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
		return FALSE;
	}

	nmc->get_client (nmc); /* create NMClient */

	if (!nm_client_get_nm_running (nmc->client)) {
		g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
		nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
		return FALSE;
	}

	state = nm_client_get_state (nmc->client);
	startup = nm_client_get_startup (nmc->client);
	connectivity = nm_client_get_connectivity (nmc->client);
	net_enabled = nm_client_networking_get_enabled (nmc->client);
	wireless_hw_enabled = nm_client_wireless_hardware_get_enabled (nmc->client);
	wireless_enabled = nm_client_wireless_get_enabled (nmc->client);
	wwan_hw_enabled = nm_client_wwan_hardware_get_enabled (nmc->client);
	wwan_enabled = nm_client_wwan_get_enabled (nmc->client);

	nmc->print_fields.header_name = pretty_header_name ? (char *) pretty_header_name : _("NetworkManager status");
	arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
	g_ptr_array_add (nmc->output_data, arr);

	arr = nmc_dup_fields_array (tmpl, tmpl_len, 0);
	set_val_strc (arr, 0, _("running"));
	set_val_strc (arr, 1, nm_client_get_version (nmc->client));
	set_val_strc (arr, 2, nm_state_to_string (state));
	set_val_strc (arr, 3, startup ? _("starting") : _("started"));
	set_val_strc (arr, 4, nm_connectivity_to_string (connectivity));
	set_val_strc (arr, 5, net_enabled ? _("enabled") : _("disabled"));
	set_val_strc (arr, 6, wireless_hw_enabled ? _("enabled") : _("disabled"));
	set_val_strc (arr, 7, wireless_enabled ? _("enabled") : _("disabled"));
	set_val_strc (arr, 8, wwan_hw_enabled ? _("enabled") : _("disabled"));
	set_val_strc (arr, 9, wwan_enabled ? _("enabled") : _("disabled"));

	/* Set colors */
	arr[2].color = state_to_color (state);
	arr[3].color = startup ? NMC_TERM_COLOR_YELLOW : NMC_TERM_COLOR_GREEN;
	arr[4].color = connectivity_to_color (connectivity);
	arr[5].color = net_enabled ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED;
	arr[6].color = wireless_hw_enabled ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED;
	arr[7].color = wireless_enabled ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED;
	arr[8].color = wwan_hw_enabled ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED;
	arr[9].color = wwan_enabled ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED;

	g_ptr_array_add (nmc->output_data, arr);

	print_data (nmc);  /* Print all data */

	return TRUE;
}
Exemple #10
0
int
main (int argc, char *argv[])
{
	int t_secs = 30;
	gboolean exit_no_nm = FALSE;
	gboolean wait_startup = FALSE;
	Timeout timeout;
	GOptionContext *opt_ctx = NULL;
	gboolean success;
	NMClient *client;
	NMState state = NM_STATE_UNKNOWN;
	GMainLoop *loop;
	gint64 remaining_ms;
	GError *error = NULL;

	GOptionEntry options[] = {
		{"timeout", 't', 0, G_OPTION_ARG_INT, &t_secs, N_("Time to wait for a connection, in seconds (without the option, default value is 30)"), "<timeout>"},
		{"exit", 'x', 0, G_OPTION_ARG_NONE, &exit_no_nm, N_("Exit immediately if NetworkManager is not running or connecting"), NULL},
		{"quiet", 'q', 0, G_OPTION_ARG_NONE, &timeout.quiet, N_("Don't print anything"), NULL},
		{"wait-for-startup", 's', 0, G_OPTION_ARG_NONE, &wait_startup, N_("Wait for NetworkManager startup instead of a connection"), NULL},
		{NULL}
	};

	timeout.start_timestamp_ms = g_get_monotonic_time () / (G_USEC_PER_SEC / 1000);
	timeout.quiet = FALSE;

	/* Set locale to be able to use environment variables */
	setlocale (LC_ALL, "");

	bindtextdomain (GETTEXT_PACKAGE, NMLOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

	opt_ctx = g_option_context_new (NULL);
	g_option_context_set_translation_domain (opt_ctx, GETTEXT_PACKAGE);
	g_option_context_set_ignore_unknown_options (opt_ctx, FALSE);
	g_option_context_set_help_enabled (opt_ctx, TRUE);
	g_option_context_add_main_entries (opt_ctx, options, NULL);

	g_option_context_set_summary (opt_ctx,
	                              _("Waits for NetworkManager to finish activating startup network connections."));

	success = g_option_context_parse (opt_ctx, &argc, &argv, NULL);
	g_option_context_free (opt_ctx);

	if (!success) {
		g_printerr ("%s: %s\n", argv[0],
		            _("Invalid option.  Please use --help to see a list of valid options."));
		return 2;
	}

	if (t_secs < 0 || t_secs > 3600)  {
		g_printerr ("%s: %s\n", argv[0],
		            _("Invalid option.  Please use --help to see a list of valid options."));
		return 2;
	}
	remaining_ms = t_secs * 1000;

	nm_g_type_init ();

	client = nm_client_new (NULL, &error);
	if (!client) {
		g_printerr (_("Error: Could not create NMClient object: %s."), error->message);
		g_error_free (error);
		return 2;
	}

	loop = g_main_loop_new (NULL, FALSE);

	g_object_set_data (G_OBJECT (client), WAIT_STARTUP_TAG, GUINT_TO_POINTER (wait_startup));
	state = nm_client_get_state (client);
	if (!nm_client_get_nm_running (client)) {
		if (exit_no_nm) {
			g_object_unref (client);
			return 1;
		}
	} else if (wait_startup) {
		if (!nm_client_get_startup (client)) {
			g_object_unref (client);
			return 0;
		}
	} else {
		if (   state == NM_STATE_CONNECTED_LOCAL
		    || state == NM_STATE_CONNECTED_SITE
		    || state == NM_STATE_CONNECTED_GLOBAL) {
			g_object_unref (client);
			return 0;
		}
	}
	if (exit_no_nm && (state != NM_STATE_CONNECTING)) {
		g_object_unref (client);
		return 1;
	}

	if (remaining_ms == 0) {
		g_object_unref (client);
		return 1;
	}

	g_signal_connect (client, "notify",
	                  G_CALLBACK (client_properties_changed), loop);

	timeout.end_timestamp_ms = timeout.start_timestamp_ms + remaining_ms;
	timeout.progress_step_duration = (timeout.end_timestamp_ms - timeout.start_timestamp_ms + PROGRESS_STEPS/2) / PROGRESS_STEPS;

	g_timeout_add (timeout.quiet ? remaining_ms : 0,
	               handle_timeout, &timeout);

	g_main_loop_run (loop);
	g_main_loop_unref (loop);

	g_object_unref (client);

	return 0;
}
Exemple #11
0
/*
 * Given an interface name (e.g., eth0) and address family (e.g., AF_INET),
 * return the IP address in human readable format (i.e., the output from
 * inet_ntop()).  Return NULL for no match or error.
 */
char *iface_ip2str(char *ifname, int family) {
    int i;
    NMClient *client = NULL;
    NMIP4Config *ip4config = NULL;
    NMIP4Address *ipaddr = NULL;
    NMDevice *candidate = NULL;
    struct in_addr tmp_addr;
    const GPtrArray *devices;
    const char *iface;
    char ipstr[INET_ADDRSTRLEN+1];

    if (ifname == NULL) {
        return NULL;
    }

    /* DCFIXME: add IPv6 once NM gains support */
    if (family != AF_INET) {
        return NULL;
    }

#if !defined(GLIB_VERSION_2_36)
    g_type_init();
#endif

    client = nm_client_new();
    if (!client) {
        return NULL;
    }

    if (nm_client_get_state(client) != NM_STATE_CONNECTED) {
        g_object_unref(client);
        return NULL;
    }

    devices = nm_client_get_devices(client);
    for (i=0; i < devices->len; i++) {
        candidate = g_ptr_array_index(devices, i);
        iface = nm_device_get_iface(candidate);

        if (nm_device_get_state(candidate) != NM_DEVICE_STATE_ACTIVATED)
            continue;

        if (!iface || strcmp(iface, ifname))
            continue;

        if (!(ip4config = nm_device_get_ip4_config(candidate)))
            continue;

        if (!(ipaddr = nm_ip4_config_get_addresses(ip4config)->data))
            continue;

        memset(&ipstr, '\0', sizeof(ipstr));
        tmp_addr.s_addr = nm_ip4_address_get_address(ipaddr);

        if (inet_ntop(AF_INET, &tmp_addr, ipstr, INET_ADDRSTRLEN) == NULL) {
            g_object_unref(client);
            return NULL;
        }

        g_object_unref(client);
        return g_strdup(ipstr);
    }

    g_object_unref(client);
    return NULL;
}