Esempio n. 1
0
int supplicant_stop(struct connman_device *device)
{
	int index = connman_device_get_index(device);
	struct supplicant_task *task;

	_DBG_SUPPLICANT("device %p", device);

	task = find_task_by_index(index);
	if (task == NULL)
		return -ENODEV;

	g_free(task->range);

	task_list = g_slist_remove(task_list, task);

	if (task->scan_call != NULL) {
		dbus_pending_call_cancel(task->scan_call);
		task->scan_call = NULL;
	}

	if (task->result_call != NULL) {
		dbus_pending_call_cancel(task->result_call);
		task->result_call = NULL;
	}

	if (task->scanning == TRUE)
		connman_device_set_scanning(task->device, FALSE);

	remove_network(task);

	disconnect_network(task);

	return remove_interface(task);
}
Esempio n. 2
0
static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
{
    struct connman_device *device = user_data;
    struct ethernet_data *ethernet = connman_device_get_data(device);

    DBG("index %d flags %d change %d", ethernet->index, flags, change);

    if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
        if (flags & IFF_UP) {
            DBG("power on");
            connman_device_set_powered(device, TRUE);
        } else {
            DBG("power off");
            connman_device_set_powered(device, FALSE);
        }
    }

    if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
        if (flags & IFF_LOWER_UP) {
            DBG("carrier on");
            add_network(device, ethernet);
        } else {
            DBG("carrier off");
            remove_network(device, ethernet);
        }
    }

    ethernet->flags = flags;
}
Esempio n. 3
0
static int task_connect(struct supplicant_task *task)
{
	const char *address, *security, *passphrase;
	const void *ssid;
	unsigned int ssid_len;
	int err;

	address = connman_network_get_string(task->network, "Address");
	security = connman_network_get_string(task->network, "WiFi.Security");
	passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");

	ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);

	_DBG_SUPPLICANT("address %s security %s", address, security);

	if (security == NULL && passphrase == NULL)
		return -EINVAL;

	if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
		return -EINVAL;

	remove_network(task);

	set_ap_scan(task);

	add_network(task);

	set_network(task, ssid, ssid_len, address, security, passphrase);

	err = select_network(task);
	if (err < 0)
		return err;

	return -EINPROGRESS;
}
static void
dialog_response_cb (GtkDialog *dialog,
    gint response,
    EmpathyIrcNetworkChooserDialog *self)
{
  if (response == GTK_RESPONSE_OK)
    add_network (self);
  else if (response == GTK_RESPONSE_APPLY)
    edit_network (self);
  else if (response == GTK_RESPONSE_REJECT)
    remove_network (self);
}
Esempio n. 5
0
// ---- Function merge ----
// Merges the two town-network, and returns the new recno for the network
// 
//  - tn1, tn2: the record numbers of the the towns networks
//  - <returns>: the new town network recno for the combined network
//
int TownNetworkArray::merge(int tn1, int tn2)
{
	TownNetwork *pTN1 = network(tn1), *pTN2 = network(tn2), *pSwap;
	if (pTN2->size() > pTN1->size())
		pSwap = pTN1, pTN1 = pTN2, pTN2 = pSwap;

	// Merge 2 into 1
	pTN1->merge_in(pTN2);
	remove_network(pTN2->recno()); pTN2 = NULL;

	return pTN1->recno();
}
Esempio n. 6
0
static void ethernet_remove(struct connman_device *device)
{
    struct ethernet_data *ethernet = connman_device_get_data(device);

    DBG("device %p", device);

    connman_device_set_data(device, NULL);

    connman_rtnl_remove_watch(ethernet->watch);

    remove_network(device, ethernet);

    g_free(ethernet);
}
Esempio n. 7
0
void remove_datacenter(provider *pv){

    int i;

    for(i=0;i<pv->ndc;i++){
	remove_network(&pv->datacenters[i]);
	if(pv->datacenters[i].alloc_apps->num_alloc_apps == 0){
	    free(pv->datacenters[i].alloc_apps);
	}
	else{
	    //printf("datacenter: apps!!");
	}
    }
    free(pv->datacenters);
}
Esempio n. 8
0
int supplicant_disconnect(struct connman_network *network)
{
	struct supplicant_task *task;
	int index;

	_DBG_SUPPLICANT("network %p", network);

	index = connman_network_get_index(network);

	task = find_task_by_index(index);
	if (task == NULL)
		return -ENODEV;

	if (task->disconnecting == TRUE)
		return -EALREADY;

	remove_network(task);

	disconnect_network(task);

	task->disconnecting = TRUE;

	return 0;
}
static void
remove_clicked_cb (GtkToolButton *button,
    TpawIrcNetworkChooserDialog *self)
{
  remove_network (self);
}
static gboolean
read_netlink_messages (GSocket      *socket,
                       GIOCondition  condition,
                       gpointer      user_data)
{
  GNetworkMonitorNetlink *nl = user_data;
  GInputVector iv;
  gssize len;
  GSocketControlMessage **cmsgs = NULL;
  gint num_cmsgs = 0, i, flags;
  GError *error = NULL;
  GCredentials *creds;
  uid_t sender;
  struct nlmsghdr *msg;
  struct rtmsg *rtmsg;
  struct rtattr *attr;
  gsize attrlen;
  guint8 *dest, *gateway;
  gboolean retval = TRUE;

  iv.buffer = NULL;
  iv.size = 0;

  flags = MSG_PEEK | MSG_TRUNC;
  len = g_socket_receive_message (nl->priv->sock, NULL, &iv, 1,
                                  NULL, NULL, &flags, NULL, &error);
  if (len < 0)
    {
      g_warning ("Error on netlink socket: %s", error->message);
      g_error_free (error);
      if (nl->priv->dump_networks)
        finish_dump (nl);
      return FALSE;
    }

  iv.buffer = g_malloc (len);
  iv.size = len;
  len = g_socket_receive_message (nl->priv->sock, NULL, &iv, 1,
                                  &cmsgs, &num_cmsgs, NULL, NULL, &error);
  if (len < 0)
    {
      g_warning ("Error on netlink socket: %s", error->message);
      g_error_free (error);
      if (nl->priv->dump_networks)
        finish_dump (nl);
      return FALSE;
    }

  if (num_cmsgs != 1 || !G_IS_UNIX_CREDENTIALS_MESSAGE (cmsgs[0]))
    goto done;

  creds = g_unix_credentials_message_get_credentials (G_UNIX_CREDENTIALS_MESSAGE (cmsgs[0]));
  sender = g_credentials_get_unix_user (creds, NULL);
  if (sender != 0)
    goto done;

  msg = (struct nlmsghdr *) iv.buffer;
  for (; len > 0; msg = NLMSG_NEXT (msg, len))
    {
      if (!NLMSG_OK (msg, (size_t) len))
        {
          g_warning ("netlink message was truncated; shouldn't happen...");
          retval = FALSE;
          goto done;
        }

      switch (msg->nlmsg_type)
        {
        case RTM_NEWROUTE:
        case RTM_DELROUTE:
          rtmsg = NLMSG_DATA (msg);

          if (rtmsg->rtm_family != AF_INET && rtmsg->rtm_family != AF_INET6)
            continue;
          if (rtmsg->rtm_type == RTN_UNREACHABLE)
            continue;

          attrlen = NLMSG_PAYLOAD (msg, sizeof (struct rtmsg));
          attr = RTM_RTA (rtmsg);
          dest = gateway = NULL;
          while (RTA_OK (attr, attrlen))
            {
              if (attr->rta_type == RTA_DST)
                dest = RTA_DATA (attr);
              else if (attr->rta_type == RTA_GATEWAY)
                gateway = RTA_DATA (attr);
              attr = RTA_NEXT (attr, attrlen);
            }

          if (dest || gateway)
            {
              if (msg->nlmsg_type == RTM_NEWROUTE)
                add_network (nl, rtmsg->rtm_family, rtmsg->rtm_dst_len, dest, gateway);
              else
                remove_network (nl, rtmsg->rtm_family, rtmsg->rtm_dst_len, dest, gateway);
              queue_request_dump (nl);
            }
          break;

        case NLMSG_DONE:
          finish_dump (nl);
          goto done;

        case NLMSG_ERROR:
          {
            struct nlmsgerr *e = NLMSG_DATA (msg);

            g_warning ("netlink error: %s", g_strerror (-e->error));
          }
          retval = FALSE;
          goto done;

        default:
          g_warning ("unexpected netlink message %d", msg->nlmsg_type);
          retval = FALSE;
          goto done;
        }
    }

 done:
  for (i = 0; i < num_cmsgs; i++)
    g_object_unref (cmsgs[i]);
  g_free (cmsgs);

  g_free (iv.buffer);

  if (!retval && nl->priv->dump_networks)
    finish_dump (nl);
  return retval;
}
Esempio n. 11
0
// ---- Function town_destroyed ----
// Updates the town networks when a town is destroyed
// 
//  - townRecno: the record number of the town.
//
void TownNetworkArray::town_destroyed(int townRecno)
{
	// Logic: Set Pulsing = false, and pulsed for the source Town to true. Remove source Town from network.
	// Iterate over all linked towns of same nation:
	//   If not Pulsing, then Pulse link, which will recursively pulse all towns that are linked via any number of hops. Set Pulsing = true.
	//   Else if Pulsing, if linked town has been pulsed, continue. If it has not been pulsed, then split off all pulsed towns so far into a
	//   new town network; set Pulsing = false, and redo iteration for current linked town.
	//   Finally, reset pulsed for all towns in the original network to false.
	// NOTE: Do not cache town-network numbers in this function, use pNetwork->town_network_recno instead.

	if (townRecno == 0) {if (DEBUG_CHECK) throw "townRecno is 0"; else return;}

	Town *pTown = town_array[townRecno];
	if (pTown == NULL) {if (DEBUG_CHECK) throw "Town no longer exists in TownArray"; else return;}

	// Independent towns do not form town networks
	if (pTown->nation_recno == 0)
	{
		if (DEBUG_CHECK && pTown->town_network_recno != 0) throw "Found independent town with a non-trivial town network";
		return;
	}

	if (DEBUG_CHECK && pTown->town_network_pulsed) throw "Town has town_network_pulsed to true outside pulsing session";


	TownNetwork *pNetwork = network(pTown->town_network_recno); // The currently 'active' network (the one being pulsed), may change during the loop
	
	// Remove the town from the town-network, before starting the rebuild. If this was the only town in it, then remove the network and return
	pNetwork->remove_town(townRecno);
	if (pNetwork->size() == 0)
	{
		remove_network(pNetwork->recno());
		return;
	}

	// Keep track of all town networks involved, so the pulsed can be reset
	TownNetwork *allNetworks[1 + MAX_LINKED_TOWN_TOWN], // Can only have as much disconnected parts as there can be links to the town
				**pNextAllNetwork = allNetworks; // Fill pointer
	*(pNextAllNetwork++) = NULL; // Set first element to 0, so it can be used as a end-of-list delimiter
	*(pNextAllNetwork++) = pNetwork;


	// Initialise pulsing-variables
	int nationRecno = pTown->nation_recno;
	bool Pulsing = false;
	pTown->town_network_pulsed = true;
	int pulsedCount = 0;
	
	// Iterate over the linked towns
	for (int i = 0; i < pTown->linked_town_count; ++i)
	{
		Town *pLinked = town_array[pTown->linked_town_array[i]];
		if (pLinked->nation_recno != nationRecno) continue;

		if (!Pulsing)
		{
			Pulsing = true;
			pulsedCount = pulse(pLinked->town_recno, pLinked->nation_recno);
		}
		else if ( ! pLinked->town_network_pulsed)
		{
			// Found disconnected parts, so need to split off all pulsed. Note: this may cause the town-network-recno's of any of the towns
			// in the original network to change, so the town network recno should not be cached.
			TownNetwork *pNewNetwork = add_network(nationRecno);
			*(pNextAllNetwork++) = pNewNetwork;
			// Decide which part gets to stay in the original network by checking which is larger
			bool movePulsed = (pulsedCount <= (pNetwork->size() / 2 + 1));
			pNetwork = pNetwork->split_by_pulsed(pNewNetwork, movePulsed); // pNetwork is set to the network of unpulsed towns
			// Redo iteration for current, now with pulsing false
			Pulsing = false;
			--i;
		}		
	}

	// Reset pulsed for all towns
	while( *(--pNextAllNetwork) != NULL )
		(*pNextAllNetwork)->reset_pulsed();
	pTown->town_network_pulsed = false;
}
Esempio n. 12
0
int main(int argc, const char **argv) {
	/* Check uid */
	if (getuid() != 0) {
		fprintf(stderr,"Swifer must be run as root.\n");
		return 1;
	}
	/* Check config file for interface and dhcp */
	FILE *cfg;
	if ( (cfg=fopen(config,"r")) ) {
		char *line = calloc(MAX_LINE+1,sizeof(char));
		char *val = calloc(MAX_LINE+1,sizeof(char));
		while (fgets(line,MAX_LINE,cfg) != NULL) {
			if (line[0] == '#') continue;
			if (sscanf(line,"INTERFACE = %s",val))
				strncpy(ifname,val,IFNAMSIZ);
			else if (sscanf(line,"DHCP = %s",val))
				strncpy(dhcp,val,DHCPLEN);
			else if (sscanf(line,"PRE_UP = %s",val))
				hook_preup = strdup(val);
			else if (sscanf(line,"POST_UP = %s",val))
				hook_postup = strdup(val);
			else if (strncmp(line,"[NETWORKS]",10)==0)
				break;
		}
		free(line); free(val); fclose(cfg);
	}
	/* Get basic wifi info */
	we_ver = iw_get_kernel_we_version();
	skfd = iw_sockets_open();
	iw_get_basic_config(skfd,ifname,&cur);
	/* Bring up interface (eg "ip link set IFACE up") */
	struct ifreq req;
	int err;
	strncpy(req.ifr_name,ifname,IFNAMSIZ);
	if ( (err=ioctl(skfd,SIOCGIFFLAGS,&req)) ){
		int loop = 0;
		while ( (err=ioctl(skfd,SIOCGIFFLAGS,&req)) ) {
			usleep(100000);
			if (loop++ > 50) break;
		}
		if (err) {
			close(skfd);
			return 2;
		}
	}
	req.ifr_flags |= IFF_UP;
	if (ioctl(skfd,SIOCSIFFLAGS,&req)) {
		close(skfd); return 3;
	}
	/* Processes command line arguments */
	int i;
	for (i = 1; i < argc; i++) {
		if (strncmp(argv[i],"ad",2)==0) mode |= MODE_ADD;
		else if (strncmp(argv[i],"au",2)==0) mode |= MODE_AUTO;
		else if (strncmp(argv[i],"hi",2)==0) mode |= MODE_HIDDEN;
		else if (strncmp(argv[i],"an",2)==0) mode |= (MODE_ANY | MODE_AUTO);
		else if (strncmp(argv[i],"re",2)==0) mode |= (MODE_RECONNECT | MODE_AUTO);
		else if (strncmp(argv[i],"ve",2)==0) mode |= MODE_VERBOSE;
		else if (strncmp(argv[i],"wa",2)==0) mode |= MODE_WAIT;
		else if (strncmp(argv[i],"de",2)==0) {
			if (argc > i+1) remove_network(argv[i+1]);
		}
		else fprintf(stderr,"[%s] Ignoring unknown parameter: %s\n",
			argv[0],argv[i]);
	}
	if ( (mode & MODE_VERBOSE) && (mode & MODE_AUTO) ) mode &= ~MODE_VERBOSE;
	/* Scan and select network */
	iw_scan(skfd,ifname,we_ver,&context);
	wireless_scan *ws;
	if (mode & MODE_AUTO) {
		if (mode && MODE_WAIT) {
			int loops;
			/* keep trying for up to 5 minutes */
			for (loops = 0; loops < 30 && !(ws=get_best()); ++loops)
				sleep(TIMEOUT);
		}
		else ws = get_best();
	}
	else ws = show_menu();
	const char *arg[4];
	if (ws) { /* Stop any current processes then connect to "ws" */
		arg[0] = killall; arg[1] = dhcp; arg[2] = NULL;
		if (fork()==0) {
			fclose(stdout); fclose(stderr);
			execvp(arg[0],(char * const *) arg);
		}
		arg[1] = wpa_sup;
		if (fork()==0) {
			fclose(stdout); fclose(stderr);
			execvp(arg[0],(char * const *) arg);
		}
		sleep(1);
		if ( (mode & MODE_ADD) && is_known(ws) ) mode &= ~MODE_ADD;
		if (ws->b.key_flags == 2048) mode |= MODE_SECURE;
		mode_t pre = umask(S_IWGRP|S_IWOTH|S_IRGRP|S_IROTH);
		ws_connect(ws);
		umask(pre);
	}
	else if ( !(mode & MODE_RECONNECT) ) {
		fprintf(stderr,"[swifer] no suitable networks found.\n");
		return 5;
	}
	/* Keep alive to reconnect? */
	iw_sockets_close(skfd);
	if (mode & MODE_RECONNECT) {
		if (fork() == 0) {
			setsid();
			int level = THRESHOLD + 1, ret;
			char scanline[256];
			snprintf(scanline,255,"%%*[^\n]\n%%*[^\n]\n%s: %%*d %%d.",ifname);
			FILE *procw;
			while (level > THRESHOLD) {
				sleep(TIMEOUT);
				procw = fopen(PROC_NET_WIRELESS,"r");
				ret = fscanf(procw,scanline,&level);
				fclose(procw);
				if (ret != 1) level = 0;
			}
			arg[0] = argv[0]; arg[1] = re; arg[2] = an; arg[3] = NULL;
			if ( !(mode & MODE_ANY)) arg[2] = NULL;
			execvp(arg[0],(char * const *) arg);
		}
	}
	if (hook_preup) free(hook_preup);
	if (hook_postup) free(hook_postup);
	return 0;
}