Exemplo n.º 1
0
static void
test_disconnect_equal()
{
  boost::signals2::signal<void ()> s0;

  connections[0] = s0.connect(remove_connection(0));
  connections[1] = s0.connect(remove_connection(1));
  connections[2] = s0.connect(remove_connection(2));
  connections[3] = s0.connect(remove_connection(3));

  std::cout << "Deleting 2" << std::endl;

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "0123");

#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
  connections[2].disconnect();
#else
  s0.disconnect(remove_connection(2));
#endif

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "013");
}
Exemplo n.º 2
0
static void
ifcfg_dir_changed (GFileMonitor *monitor,
                   GFile *file,
                   GFile *other_file,
                   GFileMonitorEvent event_type,
                   gpointer user_data)
{
	SettingsPluginIfcfg *plugin = SETTINGS_PLUGIN_IFCFG (user_data);
	char *path, *ifcfg_path;
	NMIfcfgConnection *connection;

	path = g_file_get_path (file);

	ifcfg_path = utils_detect_ifcfg_path (path, FALSE);
	_LOGD ("ifcfg_dir_changed(%s) = %d // %s", path, event_type, ifcfg_path ? ifcfg_path : "(none)");
	if (ifcfg_path) {
		connection = find_by_path (plugin, ifcfg_path);
		switch (event_type) {
		case G_FILE_MONITOR_EVENT_DELETED:
			if (connection)
				remove_connection (plugin, connection);
			break;
		case G_FILE_MONITOR_EVENT_CREATED:
		case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
			/* Update or new */
			update_connection (plugin, NULL, ifcfg_path, connection, TRUE, NULL, NULL);
			break;
		default:
			break;
		}
		g_free (ifcfg_path);
	}
	g_free (path);
}
Exemplo n.º 3
0
void obs_encoder_stop(obs_encoder_t *encoder,
		void (*new_packet)(void *param, struct encoder_packet *packet),
		void *param)
{
	bool   last = false;
	size_t idx;

	if (!encoder) return;

	pthread_mutex_lock(&encoder->callbacks_mutex);

	idx = get_callback_idx(encoder, new_packet, param);
	if (idx != DARRAY_INVALID) {
		da_erase(encoder->callbacks, idx);
		last = (encoder->callbacks.num == 0);
	}

	pthread_mutex_unlock(&encoder->callbacks_mutex);

	if (last) {
		remove_connection(encoder);

		if (encoder->destroy_on_stop)
			obs_encoder_actually_destroy(encoder);
	}
}
Exemplo n.º 4
0
static inline bool obs_encoder_stop_internal(obs_encoder_t *encoder,
		void (*new_packet)(void *param, struct encoder_packet *packet),
		void *param)
{
	bool   last = false;
	size_t idx;

	pthread_mutex_lock(&encoder->callbacks_mutex);

	idx = get_callback_idx(encoder, new_packet, param);
	if (idx != DARRAY_INVALID) {
		da_erase(encoder->callbacks, idx);
		last = (encoder->callbacks.num == 0);
	}

	pthread_mutex_unlock(&encoder->callbacks_mutex);

	if (last) {
		remove_connection(encoder);
		encoder->initialized = false;

		if (encoder->destroy_on_stop) {
			pthread_mutex_unlock(&encoder->init_mutex);
			obs_encoder_actually_destroy(encoder);
			return true;
		}
	}

	return false;
}
Exemplo n.º 5
0
static void full_stop(struct obs_encoder *encoder)
{
	if (encoder) {
		pthread_mutex_lock(&encoder->callbacks_mutex);
		da_free(encoder->callbacks);
		remove_connection(encoder);
		pthread_mutex_unlock(&encoder->callbacks_mutex);
	}
}
Exemplo n.º 6
0
void server_manager::stop()
{
	if(free_) {
		SDLNet_TCP_Close(server_socket);
		remove_connection(connection_);
		server_socket = 0;
		free_ = false;
	}
}
Exemplo n.º 7
0
static void
test_bloodbath()
{
  boost::signals2::signal<void ()> s0;

  connections[0] = s0.connect(remove_connection(0, 1));
  connections[1] = s0.connect(remove_connection(1, 1));
  connections[2] = s0.connect(remove_connection(2, 0));
  connections[3] = s0.connect(remove_connection(3, 2));

  std::cout << "0 removes 1, 2 removes 0, 3 removes 2" << std::endl;

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "023");

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "3");
}
Exemplo n.º 8
0
bool disconnect(connection s)
{
	if(s == 0) {
		while(sockets.empty() == false) {
			assert(sockets.back() != 0);
			while(disconnect(sockets.back()) == false) {
				SDL_Delay(1);
			}
		}
		return true;
	}
	if (!is_server()) last_ping = 0;

	const connection_map::iterator info = connections.find(s);
	if(info != connections.end()) {
		if (info->second.sock == server_socket)
		{
			return true;
		}
		if (!network_worker_pool::close_socket(info->second.sock)) {
			return false;
		}
	}

	bad_sockets.erase(s);

	std::deque<network::connection>::iterator dqi = std::find(disconnection_queue.begin(),disconnection_queue.end(),s);
	if(dqi != disconnection_queue.end()) {
		disconnection_queue.erase(dqi);
	}

	const sockets_list::iterator i = std::find(sockets.begin(),sockets.end(),s);
	if(i != sockets.end()) {
		sockets.erase(i);

		const TCPsocket sock = get_socket(s);

		waiting_sockets.erase(s);
		SDLNet_TCP_DelSocket(socket_set,sock);
		SDLNet_TCP_Close(sock);

		remove_connection(s);
	} else {
		if(sockets.size() == 1) {
			DBG_NW << "valid socket: " << static_cast<int>(*sockets.begin()) << "\n";
		}
	}

	return true;
}
Exemplo n.º 9
0
bool test_connections(){
  web_context * ctx = alloc0(sizeof(web_context));
  for(int i = 0; i < 100; i++){
    char namebuf[100];
    sprintf(namebuf, "__test%i", i);
    add_connection(ctx, namebuf, "aa", "bb");
  }
  ASSERT(get_connection_by_name(ctx, "__test1"));
  remove_connection(ctx, "__test1");
  //ASSERT(get_connection_by_name(ctx, "__test1") == NULL);
  //ASSERT(get_connection_by_name(ctx, "__test14")->connection == (udpc_connection *) 14);
  dealloc(ctx->active_connections);
  dealloc(ctx);
  return true;
}
Exemplo n.º 10
0
 /// Destroy an endpoint
 ~endpoint() {
     m_alog->at(log::alevel::DEVEL) << "Endpoint destructor called" << log::endl;
     // Tell the memory pool we don't want to be notified about newly freed
     // messages any more (because we wont be here)
     m_pool->set_callback(NULL);
     
     // Detach any connections that are still alive at this point
     boost::lock_guard<boost::recursive_mutex> lock(m_lock);
     
     typename std::set<connection_ptr>::iterator it;
     
     while (!m_connections.empty()) {
         remove_connection(*m_connections.begin());
     }
     m_alog->at(log::alevel::DEVEL) << "Endpoint destructor done" << log::endl;
 }
static void
ifcfg_dir_changed (GFileMonitor *monitor,
                   GFile *file,
                   GFile *other_file,
                   GFileMonitorEvent event_type,
                   gpointer user_data)
{
	SCPluginIfcfg *plugin = SC_PLUGIN_IFCFG (user_data);
	char *path, *base, *ifcfg_path;
	NMIfcfgConnection *connection;

	path = g_file_get_path (file);
	if (utils_should_ignore_file (path, FALSE)) {
		g_free (path);
		return;
	}

	_LOGD ("ifcfg_dir_changed(%s) = %d", path, event_type);

	base = g_file_get_basename (file);
	if (utils_is_ifcfg_alias_file (base, NULL)) {
		/* Alias file changed. Get the base ifcfg file from it */
		ifcfg_path = utils_get_ifcfg_from_alias (path);
	} else {
		/* Given any ifcfg, keys, or routes file, get the ifcfg file path */
		ifcfg_path = utils_get_ifcfg_path (path);
	}
	if (ifcfg_path) {
		connection = find_by_path (plugin, ifcfg_path);
		switch (event_type) {
		case G_FILE_MONITOR_EVENT_DELETED:
			if (connection)
				remove_connection (plugin, connection);
			break;
		case G_FILE_MONITOR_EVENT_CREATED:
		case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
			/* Update or new */
			update_connection (plugin, NULL, ifcfg_path, connection, TRUE, NULL, NULL);
			break;
		default:
			break;
		}
		g_free (ifcfg_path);
	}
	g_free (path);
	g_free (base);
}
Exemplo n.º 12
0
static gboolean connection_removed(DBusConnection *conn, DBusMessage *message,
				void *user_data)
{
	const char *path;
	const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;

	if (dbus_message_has_signature(message, signature) == FALSE) {
		connman_error("vpn removed signature \"%s\" does not match "
							"expected \"%s\"",
			dbus_message_get_signature(message), signature);
		return TRUE;
	}

	dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
				DBUS_TYPE_INVALID);
	remove_connection(conn, path);
	return TRUE;
}
Exemplo n.º 13
0
static void
test_remove_after()
{
  boost::signals2::signal<void ()> s0;

  connections[0] = s0.connect(remove_connection(0, 1));
  connections[1] = s0.connect(remove_connection(1));
  connections[2] = s0.connect(remove_connection(2));
  connections[3] = s0.connect(remove_connection(3));

  std::cout << "0 removes 1" << std::endl;

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "023");

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "023");

  s0.disconnect_all_slots();
  BOOST_CHECK(s0.empty());

  connections[0] = s0.connect(remove_connection(0));
  connections[1] = s0.connect(remove_connection(1, 3));
  connections[2] = s0.connect(remove_connection(2));
  connections[3] = s0.connect(remove_connection(3));

  std::cout << "1 removes 3" << std::endl;

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "012");

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "012");
}
Exemplo n.º 14
0
static void
test_remove_prior()
{
  boost::signals2::signal<void ()> s0;

  connections[0] = s0.connect(remove_connection(0));
  connections[1] = s0.connect(remove_connection(1, 0));
  connections[2] = s0.connect(remove_connection(2));
  connections[3] = s0.connect(remove_connection(3));

  std::cout << "1 removes 0" << std::endl;

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "0123");

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "123");

  s0.disconnect_all_slots();
  BOOST_CHECK(s0.empty());

  connections[0] = s0.connect(remove_connection(0));
  connections[1] = s0.connect(remove_connection(1));
  connections[2] = s0.connect(remove_connection(2));
  connections[3] = s0.connect(remove_connection(3, 2));

  std::cout << "3 removes 2" << std::endl;

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "0123");

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "013");
}
Exemplo n.º 15
0
static void
dir_changed (GFileMonitor *monitor,
             GFile *file,
             GFile *other_file,
             GFileMonitorEvent event_type,
             gpointer user_data)
{
	NMSystemConfigInterface *config = NM_SYSTEM_CONFIG_INTERFACE (user_data);
	SCPluginKeyfile *self = SC_PLUGIN_KEYFILE (config);
	NMKeyfileConnection *connection;
	char *full_path;
	gboolean exists;

	full_path = g_file_get_path (file);
	if (nm_keyfile_plugin_utils_should_ignore_file (full_path)) {
		g_free (full_path);
		return;
	}
	exists = g_file_test (full_path, G_FILE_TEST_EXISTS);

	nm_log_dbg (LOGD_SETTINGS, "dir_changed(%s) = %d; file %s", full_path, event_type, exists ? "exists" : "does not exist");

	connection = find_by_path (self, full_path);

	switch (event_type) {
	case G_FILE_MONITOR_EVENT_DELETED:
		if (!exists && connection)
			remove_connection (SC_PLUGIN_KEYFILE (config), connection);
		break;
	case G_FILE_MONITOR_EVENT_CREATED:
	case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
		if (exists)
			update_connection (SC_PLUGIN_KEYFILE (config), NULL, full_path, connection, TRUE, NULL, NULL);
		break;
	default:
		break;
	}

	g_free (full_path);
}
Exemplo n.º 16
0
void* client_handler(void* arg) {

    struct sockaddr_storage clientaddr;
    socklen_t clientaddrsize = sizeof  clientaddr;
     // Accept client connections. Please look at accept() on how to use it.
   
    if(client_id >0 ) {
        /* Create another thread to wait for a future connection */
        
    } else {
        perror("accept()");
        exit(1);
    }
    printf("New client on socket descriptor %d\n", client_id);
    char host[256],port[256];
    // Using getnameinfo() get the host and port details about the client.
    
      
      printf("%s:%s has connected\n", host,port);

    add_connection(client_id);
   
    FILE* netin = fdopen(client_id,"r");
    while(1) {
    // Read the message from client and send it to all listeners.

     }

   // Send closing message to the client and close the connection.
      



    remove_connection(client_id);
    return NULL;
}
Exemplo n.º 17
0
void Slot::disconnect(Slot *slot) {
  remove_connection(slot);
  slot->remove_connection(this);
}
Exemplo n.º 18
0
void IPACM_LanToLan::handle_del_lan2lan_connection(ipacm_event_connection* data)
{
	IPACMDBG_H("Del lan2lan connection info: IP type: %d, src_v4_addr: 0x%08x, dst_v4_addr: 0x%08x\n", data->iptype, data->src_ipv4_addr, data->dst_ipv4_addr);
	IPACMDBG_H("src_v6_addr: 0x%08x%08x%08x%08x, dst_v6_addr: 0x%08x%08x%08x%08x", data->src_ipv6_addr[0], data->src_ipv6_addr[1], data->src_ipv6_addr[2],
				data->src_ipv6_addr[3], data->dst_ipv6_addr[0], data->dst_ipv6_addr[1], data->dst_ipv6_addr[2], data->dst_ipv6_addr[3]);

	bool res;
	uint64_t src_ipv6_addr, dst_ipv6_addr;
	client_info* src_client_ptr;
	client_info* dst_client_ptr;

	if(data->iptype == IPA_IP_v4)
	{
		if(client_info_v4_.count(data->src_ipv4_addr) == 0
			|| client_info_v4_.count(data->dst_ipv4_addr) == 0)	//if not found the client
		{
			IPACMDBG_H("Not found either source or dest client, return.\n");
			return;
		}
		client_info& src_client = client_info_v4_[data->src_ipv4_addr];
		client_info& dst_client = client_info_v4_[data->dst_ipv4_addr];
		src_client_ptr = &src_client;
		dst_client_ptr = &dst_client;
	}
	else
	{
		memcpy(&src_ipv6_addr, &(data->src_ipv6_addr[2]), sizeof(uint64_t));
		memcpy(&dst_ipv6_addr, &(data->dst_ipv6_addr[2]), sizeof(uint64_t));
		if(client_info_v6_.count(src_ipv6_addr) == 0
			|| client_info_v6_.count(dst_ipv6_addr) == 0)//if not found the client
		{
			IPACMDBG_H("Not found either source or dest client, return.\n");
			return;
		}
		client_info& src_client = client_info_v6_[src_ipv6_addr];
		client_info& dst_client = client_info_v6_[dst_ipv6_addr];
		src_client_ptr = &src_client;
		dst_client_ptr = &dst_client;
	}

	res = remove_connection(src_client_ptr, dst_client_ptr);

	if(res && src_client_ptr->is_active && dst_client_ptr->is_active)
	{
		IPACMDBG_H("Erase link info for both src and dst entries.\n");
		erase_offload_link(data->iptype, src_client_ptr, dst_client_ptr);
	}
	else
	{
		if(res && src_client_ptr->is_powersave && (dst_client_ptr->is_active || dst_client_ptr->is_powersave))
		{
			IPACMDBG_H("Erase link info for both src and dst entries due to src powersave.\n");
			erase_offload_link(data->iptype, src_client_ptr, dst_client_ptr);
		}
		if(res && dst_client_ptr->is_powersave && (src_client_ptr->is_active || src_client_ptr->is_powersave))
		{
			IPACMDBG_H("Erase link info for both src and dst entries due to dst powersave.\n");
			erase_offload_link(data->iptype, dst_client_ptr, src_client_ptr);
		}
	}

	//if the src client is not active and not powersave mode, if peer list is empty, remove client entry
	if(res && src_client_ptr->is_active == false && src_client_ptr->is_powersave == false && src_client_ptr->peer.size() == 0)
	{
		IPACMDBG_H("Peer list of src is empty, remove src entry.\n");
		if(data->iptype == IPA_IP_v4)
		{
			client_info_v4_.erase(data->src_ipv4_addr);
		}
		else
		{
			client_info_v6_.erase(src_ipv6_addr);
		}
	}

	//if the dst client is not active and not powersave mode, if peer list is empty, remove client entry
	if(res && dst_client_ptr->is_active == false && dst_client_ptr->is_powersave == false && dst_client_ptr->peer.size() == 0)
	{
		IPACMDBG_H("Peer list of dst is empty, remove dst entry.\n");
		if(data->iptype == IPA_IP_v4)
		{
			client_info_v4_.erase(data->dst_ipv4_addr);
		}
		else
		{
			client_info_v6_.erase(dst_ipv6_addr);
		}
	}
	return;
}
void ConnectionIOServer::InternalThreadEntry() {
    fd_set master_rfds, working_rfds;
    fd_set master_wfds, working_wfds;
    FD_ZERO(&master_rfds);
    FD_ZERO(&master_wfds);
    FD_SET(pipefd_[0], &master_rfds);       
    
    // Keep track of closed connections
    std::vector<int> remove_list;
    
    while (1) {
        working_rfds = master_rfds;
        working_wfds = master_wfds;
                    
        // Determine the maximum file descriptor.
        int max_fd = connection_map_.empty() ? pipefd_[0] : 
            std::max(pipefd_[0], (connection_map_.rbegin())->first);
        
        // Block until there is activity
        int activity = select(max_fd + 1, &working_rfds, &working_wfds, 
                              NULL, NULL);
        
        // Check to see if there was an error with select.
        if (activity < 0 && errno != EINTR) {
            return; // TODO: Improve cleanup on error. 
        }
        
        // See if there are new items to sockets to add
        if (FD_ISSET(pipefd_[0], &working_rfds)) {
            if (add_connection(&master_rfds) == -1) {
                return; // TODO: Should clean up connections                
            }
        }
        
        // See if any of the connections is ready for read/write
        for (std::map<int, Connection*>::iterator it = connection_map_.begin();
                it != connection_map_.end(); it++) {
            if (FD_ISSET(it->first, &working_wfds)) {
                int w_result = it->second->flush_writes();
                if (w_result == -1) {
                    remove_list.push_back(it->first);
                    continue; // Done with this socket
                } else if (w_result == 0) {
                    // Completely finished flushing the writes for this
                    // socket. Remove it from the fd_set
                    FD_CLR(it->first, &master_wfds);                        
                }
            }            
            if (FD_ISSET(it->first, &working_rfds)) {
                int r_result = it->second->handle_request();
                if (r_result == -1) {
                    remove_list.push_back(it->first);
                    continue; // Done with this socket                        
                }
                // Check if this socket needs to be added to the wfds.
                if (it->second->pending_writes_exist()) {
                    FD_SET(it->first, &master_wfds);    
                }                    
            }
        }
        
        // Check if there are any connections that need to be removed.
        if (!remove_list.empty()) {
            for (unsigned i = 0; i < remove_list.size(); ++i) {
                remove_connection(remove_list[i], &master_rfds, &master_wfds);
            }
            remove_list.clear();
        }
    }        
}
Exemplo n.º 20
0
void
check_socket(int num)
    /* check for new connection, command, connection closed */
{
    int fd = -1, avoid_fd = -1, addr_len = sizeof(struct sockaddr_un);
    struct sockaddr_un client_addr;
    long int buf_int[SOCKET_MSG_LEN];
    int read_len = 0;
    struct fcrondyn_cl *client = NULL, *prev_client = NULL;

    if ( num <= 0 )
	/* no socket to check : go directly to the end of that function */
	goto final_settings;

    debug("Checking socket ...");

    if ( FD_ISSET(listen_fd, &read_set) ) {
	debug("got new connection ...");
	if ((fd = accept(listen_fd, (struct sockaddr *)&client_addr, &addr_len)) == -1) {
	    error_e("could not accept new connection : isset(listen_fd = %d) = %d",
		    listen_fd, FD_ISSET(listen_fd, &read_set));
	}
	else {
	    fcntl(fd, F_SETFD, 1);
	    /* set fd to O_NONBLOCK : we do not want fcron to be stopped on error, etc */
	    if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) == -1) {
		error_e("Could not set fd attribute O_NONBLOCK : connection rejected.");
		shutdown(fd, SHUT_RDWR);
		close(fd);
	    }
	    else {
		Alloc(client, fcrondyn_cl);
		client->fcl_sock_fd = fd;
		/* means : not authenticated yet : */
		client->fcl_user = NULL;
		client->fcl_cmd = NULL;

		/* include new entry in client list */
		client->fcl_next = fcrondyn_cl_base;
		fcrondyn_cl_base = client;
		client->fcl_idle_since = now;
		/* to avoid trying to read from it in this call */
		avoid_fd = fd;
		
		FD_SET(fd, &master_set);
		if ( fd > set_max_fd )
		    set_max_fd = fd;
		fcrondyn_cl_num += 1;
		
		debug("Added connection fd : %d - %d connections", fd, fcrondyn_cl_num);
	    }
	}
    }

    client = fcrondyn_cl_base;
    while ( client != NULL ) {
	if (! FD_ISSET(client->fcl_sock_fd, &read_set) || client->fcl_sock_fd==avoid_fd){
	    /* check if the connection has not been idle for too long ... */
	    if (client->fcl_user==NULL && now - client->fcl_idle_since > MAX_AUTH_TIME ){
		warn("Connection with no auth for more than %ds : closing it.",
		     MAX_AUTH_TIME);
		remove_connection(&client, prev_client);
	    }
	    else if ( now - client->fcl_idle_since > MAX_IDLE_TIME ) {
		warn("Connection of %s is idle for more than %ds : closing it.",
		     client->fcl_user, MAX_IDLE_TIME);
		remove_connection(&client, prev_client);
	    }
	    else {
		/* nothing to do on this one ... check the next one */
		prev_client = client;
		client = client->fcl_next;
	    }
	    continue;
	}

	if ( (read_len = recv(client->fcl_sock_fd, buf_int, sizeof(buf_int), 0)) <= 0 ) {
	    if (read_len == 0) {
		/* connection closed by client */
		remove_connection(&client, prev_client);
	    }
	    else {
		error_e("error recv() from sock fd %d", client->fcl_sock_fd);
		prev_client = client;
		client = client->fcl_next;
	    }
	}
	else {
	    client->fcl_cmd_len = read_len;
	    client->fcl_cmd = buf_int;
	    if ( client->fcl_user == NULL )
		/* not authenticated yet */
		auth_client(client);
	    else {
		/* we've just read a command ... */
		client->fcl_idle_since = now;
		exe_cmd(client);
	    }
	    prev_client = client;
	    client = client->fcl_next;
	}
    }

  final_settings:
    /* copy master_set in read_set, because read_set is modified by select() */
    read_set = master_set;
}
Exemplo n.º 21
0
static void
test_remove_self()
{
  boost::signals2::signal<void ()> s0;

  connections[0] = s0.connect(remove_connection(0));
  connections[1] = s0.connect(remove_connection(1));
  connections[2] = s0.connect(remove_connection(2, 2));
  connections[3] = s0.connect(remove_connection(3));

  std::cout << "Deleting 2" << std::endl;

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "0123");

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "013");

  s0.disconnect_all_slots();
  BOOST_CHECK(s0.empty());

  connections[0] = s0.connect(remove_connection(0));
  connections[1] = s0.connect(remove_connection(1));
  connections[2] = s0.connect(remove_connection(2));
  connections[3] = s0.connect(remove_connection(3, 3));

  std::cout << "Deleting 3" << std::endl;

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "0123");

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "012");

  s0.disconnect_all_slots();
  BOOST_CHECK(s0.num_slots() == 0);

  connections[0] = s0.connect(remove_connection(0, 0));
  connections[1] = s0.connect(remove_connection(1));
  connections[2] = s0.connect(remove_connection(2));
  connections[3] = s0.connect(remove_connection(3));

  std::cout << "Deleting 0" << std::endl;

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "0123");

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "123");

  s0.disconnect_all_slots();
  BOOST_CHECK(s0.empty());

  connections[0] = s0.connect(remove_connection(0, 0));
  connections[1] = s0.connect(remove_connection(1, 1));
  connections[2] = s0.connect(remove_connection(2, 2));
  connections[3] = s0.connect(remove_connection(3, 3));

  std::cout << "Mass suicide" << std::endl;

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "0123");

  test_output = "";
  s0(); std::cout << std::endl;
  BOOST_CHECK(test_output == "");
}
/**
 * DTLS over SCTP specific listener function. This function is called by @c listenerThread()
 */
void IpfixReceiverDtlsSctpIpV4::run() {

    struct sockaddr_in clientAddress;
    socklen_t clientAddressLen = sizeof(struct sockaddr_in);

    int ret;
    int rfd;
    struct timespec timeOut;

    /* set a 400ms time-out on the pselect */
    timeOut.tv_sec = 0L;
    timeOut.tv_nsec = 400000000L;

    while(!exitFlag) {
	fd_set tmpreadfds = readfds;
	fd_set tmpwritefds = writefds;
	ret = pselect(maxfd + 1, &tmpreadfds, &tmpwritefds, NULL, &timeOut, NULL);
	if (ret == 0) {
	    /* Timeout */
	    continue;
	}
	if ((ret == -1) && (errno == EINTR)) {
	    DPRINTF("select() returned due to signal");
	    /* There was a signal... ignore */
	    continue;
	}
	if (ret < 0) {
	    msg(MSG_ERROR ,"select() returned with an error: %s",strerror(errno));
	    THROWEXCEPTION("IpfixReceiverDtlsSctpIpV4: terminating listener thread");
	    break;
	}
	DPRINTF("select() returned %d",ret);
	// looking for a new client to connect at listen_socket
	if (FD_ISSET(listen_socket, &tmpreadfds)){
	    rfd = accept(listen_socket, (struct sockaddr*)&clientAddress, &clientAddressLen);

	    if (rfd >= 0){
		if ( ! isHostAuthorized(&clientAddress.sin_addr,
			    sizeof(clientAddress.sin_addr))) {
		    /* Do not accept connections from unauthorized hosts. */
		    close(rfd);
		} else {
		    msg(MSG_DEBUG, "IpfixReceiverDtlsSctpIpV4: Client connected from %s:%d, FD=%d", inet_ntoa(clientAddress.sin_addr), ntohs(clientAddress.sin_port), rfd);
		    DtlsConnectionPtr conn = DtlsConnectionPtr( new DtlsConnection(*this,&clientAddress,rfd));
		    connections.insert(make_pair(rfd,conn));
		    update_maxfd();
		}
	    }else{
		msg(MSG_ERROR ,"accept() in ipfixReceiver failed");
		/* TODO: Don't throw an exception here. */
		THROWEXCEPTION("IpfixReceiverDtlsSctpIpV4: unable to accept new connection");
	    }
	}
	// check all connected sockets for new available data
	for (rfd = 0; rfd <= maxfd; ++rfd) {
	    if (rfd == listen_socket) continue;
	    if (FD_ISSET(rfd, &readfds) || FD_ISSET(rfd, &writefds)) {
		if (FD_ISSET(rfd, &readfds)) DPRINTF("descriptor %d is ready for reading",rfd);
		if (FD_ISSET(rfd, &writefds)) DPRINTF("descriptor %d is ready for writing",rfd);
		connections_map::iterator it = connections.find(rfd);
		if (it == connections.end()) {
		    /* This should not happend. */
		    msg(MSG_ERROR,"Can't find connection for file descriptor.");
		    FD_CLR(rfd,&readfds);
		    FD_CLR(rfd,&writefds);
		    continue;
		}
		ret = it->second->fdready();
		if (ret == 0) {
		    DPRINTF("fdready() returned 0. Deleting connection.");
		    remove_connection(rfd);
		    update_maxfd();
		}
	    }
	}
    }
    msg(MSG_DEBUG, "IpfixReceiverDtlsSctpIpV4: Exiting");
}
Exemplo n.º 23
0
/* update_connection:
 * @self: the plugin instance
 * @source: if %NULL, this re-reads the connection from @full_path
 *   and updates it. When passing @source, this adds a connection from
 *   memory.
 * @full_path: the filename of the keyfile to be loaded
 * @connection: an existing connection that might be updated.
 *   If given, @connection must be an existing connection that is currently
 *   owned by the plugin.
 * @protect_existing_connection: if %TRUE, and !@connection, we don't allow updating
 *   an existing connection with the same UUID.
 *   If %TRUE and @connection, allow updating only if the reload would modify
 *   @connection (without changing its UUID) or if we would create a new connection.
 *   In other words, if this paramter is %TRUE, we only allow creating a
 *   new connection (with an unseen UUID) or updating the passed in @connection
 *   (whereas the UUID cannot change).
 *   Note, that this allows for @connection to be replaced by a new connection.
 * @protected_connections: (allow-none): if given, we only update an
 *   existing connection if it is not contained in this hash.
 * @error: error in case of failure
 *
 * Loads a connection from file @full_path. This can both be used to
 * load a connection initially or to update an existing connection.
 *
 * If you pass in an existing connection and the reloaded file happens
 * to have a different UUID, the connection is deleted.
 * Beware, that means that after the function, you have a dangling pointer
 * if the returned connection is different from @connection.
 *
 * Returns: the updated connection.
 * */
static NMKeyfileConnection *
update_connection (SCPluginKeyfile *self,
                   NMConnection *source,
                   const char *full_path,
                   NMKeyfileConnection *connection,
                   gboolean protect_existing_connection,
                   GHashTable *protected_connections,
                   GError **error)
{
	SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (self);
	NMKeyfileConnection *connection_new;
	NMKeyfileConnection *connection_by_uuid;
	GError *local = NULL;
	const char *uuid;

	g_return_val_if_fail (!source || NM_IS_CONNECTION (source), NULL);
	g_return_val_if_fail (full_path || source, NULL);

	if (full_path)
		nm_log_dbg (LOGD_SETTINGS, "keyfile: loading from file \"%s\"...", full_path);

	connection_new = nm_keyfile_connection_new (source, full_path, &local);
	if (!connection_new) {
		/* Error; remove the connection */
		if (source)
			nm_log_warn (LOGD_SETTINGS, "keyfile: error creating connection %s: %s", nm_connection_get_uuid (source), local->message);
		else
			nm_log_warn (LOGD_SETTINGS, "keyfile: error loading connection from file %s: %s", full_path, local->message);
		if (   connection
		    && !protect_existing_connection
		    && (!protected_connections || !g_hash_table_contains (protected_connections, connection)))
			remove_connection (self, connection);
		g_propagate_error (error, local);
		return NULL;
	}

	uuid = nm_connection_get_uuid (NM_CONNECTION (connection_new));
	connection_by_uuid = g_hash_table_lookup (priv->connections, uuid);

	if (   connection
	    && connection != connection_by_uuid) {

		if (   (protect_existing_connection && connection_by_uuid != NULL)
		    || (protected_connections && g_hash_table_contains (protected_connections, connection))) {
			NMKeyfileConnection *conflicting = (protect_existing_connection && connection_by_uuid != NULL) ? connection_by_uuid : connection;

			if (source)
				nm_log_warn (LOGD_SETTINGS, "keyfile: cannot update protected "NM_KEYFILE_CONNECTION_LOG_FMT" connection due to conflicting UUID %s", NM_KEYFILE_CONNECTION_LOG_ARG (conflicting), uuid);
			else
				nm_log_warn (LOGD_SETTINGS, "keyfile: cannot load %s due to conflicting UUID for "NM_KEYFILE_CONNECTION_LOG_FMT, full_path, NM_KEYFILE_CONNECTION_LOG_ARG (conflicting));
			g_object_unref (connection_new);
			g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
			                      "Cannot update protected connection due to conflicting UUID");
			return NULL;
		}

		/* The new connection has a different UUID then the original one.
		 * Remove @connection. */
		remove_connection (self, connection);
	}

	if (   connection_by_uuid
	    && (   (!connection && protect_existing_connection)
	        || (protected_connections && g_hash_table_contains (protected_connections, connection_by_uuid)))) {
		if (source)
			nm_log_warn (LOGD_SETTINGS, "keyfile: cannot update connection due to conflicting UUID for "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_by_uuid));
		else
			nm_log_warn (LOGD_SETTINGS, "keyfile: cannot load %s due to conflicting UUID for "NM_KEYFILE_CONNECTION_LOG_FMT, full_path, NM_KEYFILE_CONNECTION_LOG_ARG (connection_by_uuid));
		g_object_unref (connection_new);
		g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
		                      "Skip updating protected connection during reload");
		return NULL;
	}

	if (connection_by_uuid) {
		const char *old_path;

		old_path = nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection_by_uuid));

		if (nm_connection_compare (NM_CONNECTION (connection_by_uuid),
		                           NM_CONNECTION (connection_new),
		                           NM_SETTING_COMPARE_FLAG_IGNORE_AGENT_OWNED_SECRETS |
		                           NM_SETTING_COMPARE_FLAG_IGNORE_NOT_SAVED_SECRETS)) {
			/* Nothing to do... except updating the path. */
			if (old_path && g_strcmp0 (old_path, full_path) != 0)
				nm_log_info (LOGD_SETTINGS, "keyfile: rename \"%s\" to "NM_KEYFILE_CONNECTION_LOG_FMT" without other changes", old_path, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new));
		} else {
			/* An existing connection changed. */
			if (source)
				nm_log_info (LOGD_SETTINGS, "keyfile: update "NM_KEYFILE_CONNECTION_LOG_FMT" from %s", NM_KEYFILE_CONNECTION_LOG_ARG (connection_new), NM_KEYFILE_CONNECTION_LOG_PATH (old_path));
			else if (!g_strcmp0 (old_path, nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection_new))))
				nm_log_info (LOGD_SETTINGS, "keyfile: update "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new));
			else if (old_path)
				nm_log_info (LOGD_SETTINGS, "keyfile: rename \"%s\" to "NM_KEYFILE_CONNECTION_LOG_FMT, old_path, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new));
			else
				nm_log_info (LOGD_SETTINGS, "keyfile: update and persist "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new));

			if (!nm_settings_connection_replace_settings (NM_SETTINGS_CONNECTION (connection_by_uuid),
			                                              NM_CONNECTION (connection_new),
			                                              FALSE,  /* don't set Unsaved */
			                                              "keyfile-update",
			                                              &local)) {
				/* Shouldn't ever get here as 'connection_new' was verified by the reader already
				 * and the UUID did not change. */
				g_assert_not_reached ();
			}
			g_assert_no_error (local);
		}
		nm_settings_connection_set_filename (NM_SETTINGS_CONNECTION (connection_by_uuid), full_path);
		g_object_unref (connection_new);
		return connection_by_uuid;
	} else {
		if (source)
			nm_log_info (LOGD_SETTINGS, "keyfile: add connection "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new));
		else
			nm_log_info (LOGD_SETTINGS, "keyfile: new connection "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new));
		g_hash_table_insert (priv->connections, g_strdup (uuid), connection_new);

		g_signal_connect (connection_new, NM_SETTINGS_CONNECTION_REMOVED,
		                  G_CALLBACK (connection_removed_cb),
		                  self);

		if (!source) {
			/* Only raise the signal if we were called without source, i.e. if we read the connection from file.
			 * Otherwise, we were called by add_connection() which does not expect the signal. */
			g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED, connection_new);
		}
		return connection_new;
	}
}
static NMIfcfgConnection *
update_connection (SCPluginIfcfg *self,
                   NMConnection *source,
                   const char *full_path,
                   NMIfcfgConnection *connection,
                   gboolean protect_existing_connection,
                   GHashTable *protected_connections,
                   GError **error)
{
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
	NMIfcfgConnection *connection_new;
	NMIfcfgConnection *connection_by_uuid;
	GError *local = NULL;
	const char *new_unmanaged = NULL, *old_unmanaged = NULL;
	const char *new_unrecognized = NULL, *old_unrecognized = NULL;
	gboolean unmanaged_changed = FALSE, unrecognized_changed = FALSE;
	const char *uuid;

	g_return_val_if_fail (!source || NM_IS_CONNECTION (source), NULL);
	g_return_val_if_fail (full_path || source, NULL);

	if (full_path)
		_LOGD ("loading from file \"%s\"...", full_path);

	/* Create a NMIfcfgConnection instance, either by reading from @full_path or
	 * based on @source. */
	connection_new = nm_ifcfg_connection_new (source, full_path, error);
	if (!connection_new) {
		/* Unexpected failure. Probably the file is invalid? */
		if (   connection
		    && !protect_existing_connection
		    && (!protected_connections || !g_hash_table_contains (protected_connections, connection)))
			remove_connection (self, connection);
		return NULL;
	}

	uuid = nm_connection_get_uuid (NM_CONNECTION (connection_new));
	connection_by_uuid = g_hash_table_lookup (priv->connections, uuid);

	if (   connection
	    && connection != connection_by_uuid) {

		if (   (protect_existing_connection && connection_by_uuid != NULL)
		    || (protected_connections && g_hash_table_contains (protected_connections, connection))) {
			NMIfcfgConnection *conflicting = (protect_existing_connection && connection_by_uuid != NULL) ? connection_by_uuid : connection;

			if (source)
				_LOGW ("cannot update protected connection "NM_IFCFG_CONNECTION_LOG_FMT" due to conflicting UUID %s", NM_IFCFG_CONNECTION_LOG_ARG (conflicting), uuid);
			else
				_LOGW ("cannot load %s due to conflicting UUID for "NM_IFCFG_CONNECTION_LOG_FMT, full_path, NM_IFCFG_CONNECTION_LOG_ARG (conflicting));
			g_object_unref (connection_new);
			g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
			                     "Cannot update protected connection due to conflicting UUID");
			return NULL;
		}

		/* The new connection has a different UUID then the original one that we
		 * are about to update. Remove @connection. */
		remove_connection (self, connection);
	}

	/* Check if the found connection with the same UUID is not protected from updating. */
	if (   connection_by_uuid
	    && (   (!connection && protect_existing_connection)
	        || (protected_connections && g_hash_table_contains (protected_connections, connection_by_uuid)))) {
		if (source)
			_LOGW ("cannot update connection due to conflicting UUID for "NM_IFCFG_CONNECTION_LOG_FMT, NM_IFCFG_CONNECTION_LOG_ARG (connection_by_uuid));
		else
			_LOGW ("cannot load %s due to conflicting UUID for "NM_IFCFG_CONNECTION_LOG_FMT, full_path, NM_IFCFG_CONNECTION_LOG_ARG (connection_by_uuid));
		g_object_unref (connection_new);
		g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
		                      "Skip updating protected connection during reload");
		return NULL;
	}

	/* Evaluate unmanaged/unrecognized flags. */
	if (connection_by_uuid)
		old_unmanaged = nm_ifcfg_connection_get_unmanaged_spec (connection_by_uuid);
	new_unmanaged = nm_ifcfg_connection_get_unmanaged_spec (connection_new);
	unmanaged_changed = g_strcmp0 (old_unmanaged, new_unmanaged);

	if (connection_by_uuid)
		old_unrecognized = nm_ifcfg_connection_get_unrecognized_spec (connection_by_uuid);
	new_unrecognized = nm_ifcfg_connection_get_unrecognized_spec (connection_new);
	unrecognized_changed = g_strcmp0 (old_unrecognized, new_unrecognized);

	if (connection_by_uuid) {
		const char *old_path;

		old_path = nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection_by_uuid));

		if (   !unmanaged_changed
		    && !unrecognized_changed
		    && nm_connection_compare (NM_CONNECTION (connection_by_uuid),
		                              NM_CONNECTION (connection_new),
		                              NM_SETTING_COMPARE_FLAG_IGNORE_AGENT_OWNED_SECRETS |
		                              NM_SETTING_COMPARE_FLAG_IGNORE_NOT_SAVED_SECRETS)) {
			if (old_path && g_strcmp0 (old_path, full_path) != 0)
				_LOGI ("rename \"%s\" to "NM_IFCFG_CONNECTION_LOG_FMT" without other changes", nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection_by_uuid)), NM_IFCFG_CONNECTION_LOG_ARG (connection_new));
		} else {

			/*******************************************************
			 * UPDATE
			 *******************************************************/

			if (source)
				_LOGI ("update "NM_IFCFG_CONNECTION_LOG_FMT" from %s", NM_IFCFG_CONNECTION_LOG_ARG (connection_new), NM_IFCFG_CONNECTION_LOG_PATH (old_path));
			else if (!g_strcmp0 (old_path, nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection_new))))
				_LOGI ("update "NM_IFCFG_CONNECTION_LOG_FMT, NM_IFCFG_CONNECTION_LOG_ARG (connection_new));
			else if (old_path)
				_LOGI ("rename \"%s\" to "NM_IFCFG_CONNECTION_LOG_FMT, old_path, NM_IFCFG_CONNECTION_LOG_ARG (connection_new));
			else
				_LOGI ("update and persist "NM_IFCFG_CONNECTION_LOG_FMT, NM_IFCFG_CONNECTION_LOG_ARG (connection_new));

			g_object_set (connection_by_uuid,
			              NM_IFCFG_CONNECTION_UNMANAGED_SPEC, new_unmanaged,
			              NM_IFCFG_CONNECTION_UNRECOGNIZED_SPEC, new_unrecognized,
			              NULL);

			if (!nm_settings_connection_replace_settings (NM_SETTINGS_CONNECTION (connection_by_uuid),
			                                              NM_CONNECTION (connection_new),
			                                              FALSE,  /* don't set Unsaved */
			                                              "ifcfg-update",
			                                              &local)) {
				/* Shouldn't ever get here as 'connection_new' was verified by the reader already
				 * and the UUID did not change. */
				g_assert_not_reached ();
			}
			g_assert_no_error (local);

			if (new_unmanaged || new_unrecognized) {
				if (!old_unmanaged && !old_unrecognized) {
					g_object_ref (connection_by_uuid);
					/* Unexport the connection by telling the settings service it's
					 * been removed.
					 */
					nm_settings_connection_signal_remove (NM_SETTINGS_CONNECTION (connection_by_uuid));
					/* Remove the path so that claim_connection() doesn't complain later when
					 * interface gets managed and connection is re-added. */
					nm_connection_set_path (NM_CONNECTION (connection_by_uuid), NULL);

					/* signal_remove() will end up removing the connection from our hash,
					 * so add it back now.
					 */
					g_hash_table_insert (priv->connections,
					                     g_strdup (nm_connection_get_uuid (NM_CONNECTION (connection_by_uuid))),
					                     connection_by_uuid);
				}
			} else {
				if (old_unmanaged /* && !new_unmanaged */) {
					_LOGI ("Managing connection "NM_IFCFG_CONNECTION_LOG_FMT" and its device because NM_CONTROLLED was true.",
					       NM_IFCFG_CONNECTION_LOG_ARG (connection_new));
					g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED, connection_by_uuid);
				} else if (old_unrecognized /* && !new_unrecognized */) {
					_LOGI ("Managing connection "NM_IFCFG_CONNECTION_LOG_FMT" because it is now a recognized type.",
					       NM_IFCFG_CONNECTION_LOG_ARG (connection_new));
					g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED, connection_by_uuid);
				}
			}

			if (unmanaged_changed)
				g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNMANAGED_SPECS_CHANGED);
			if (unrecognized_changed)
				g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNRECOGNIZED_SPECS_CHANGED);
		}
		nm_settings_connection_set_filename (NM_SETTINGS_CONNECTION (connection_by_uuid), full_path);
		g_object_unref (connection_new);
		return connection_by_uuid;
	} else {

		/*******************************************************
		 * ADD
		 *******************************************************/

		if (source)
			_LOGI ("add connection "NM_IFCFG_CONNECTION_LOG_FMT, NM_IFCFG_CONNECTION_LOG_ARG (connection_new));
		else
			_LOGI ("new connection "NM_IFCFG_CONNECTION_LOG_FMT, NM_IFCFG_CONNECTION_LOG_ARG (connection_new));
		g_hash_table_insert (priv->connections, g_strdup (uuid), connection_new);

		g_signal_connect (connection_new, NM_SETTINGS_CONNECTION_REMOVED,
		                  G_CALLBACK (connection_removed_cb),
		                  self);

		if (nm_ifcfg_connection_get_unmanaged_spec (connection_new)) {
			const char *spec;
			const char *device_id;

			spec = nm_ifcfg_connection_get_unmanaged_spec (connection_new);
			device_id = strchr (spec, ':');
			if (device_id)
				device_id++;
			else
				device_id = spec;
			_LOGW ("Ignoring connection "NM_IFCFG_CONNECTION_LOG_FMT" / device '%s' due to NM_CONTROLLED=no.",
			       NM_IFCFG_CONNECTION_LOG_ARG (connection_new), device_id);
		} else if (nm_ifcfg_connection_get_unrecognized_spec (connection_new))
			_LOGW ("Ignoring connection "NM_IFCFG_CONNECTION_LOG_FMT" of unrecognized type.", NM_IFCFG_CONNECTION_LOG_ARG (connection_new));

		/* watch changes of ifcfg hardlinks */
		g_signal_connect (G_OBJECT (connection_new), "ifcfg-changed",
		                  G_CALLBACK (connection_ifcfg_changed), self);

		if (!source) {
			/* Only raise the signal if we were called without source, i.e. if we read the connection from file.
			 * Otherwise, we were called by add_connection() which does not expect the signal. */
			if (nm_ifcfg_connection_get_unmanaged_spec (connection_new))
				g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNMANAGED_SPECS_CHANGED);
			else if (nm_ifcfg_connection_get_unrecognized_spec (connection_new))
				g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNRECOGNIZED_SPECS_CHANGED);
			else
				g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED, connection_new);
		}
		return connection_new;
	}
}
Exemplo n.º 25
0
static void
process_message(iscsid_request_t *req, iscsid_response_t **prsp, int *prsp_temp)
{
	iscsid_response_t *rsp;
	void *p = req->parameter;

	*prsp_temp = FALSE;
	*prsp = rsp = (iscsid_response_t *)(void *)rsp_buf;
	rsp->parameter_length = 0;
	rsp->status = ISCSID_STATUS_SUCCESS;

	switch (req->request) {
	case ISCSID_ADD_TARGET:
		if (req->parameter_length < sizeof(iscsid_add_target_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		add_target((iscsid_add_target_req_t *)p, prsp, prsp_temp);
		break;

	case ISCSID_ADD_PORTAL:
		if (req->parameter_length != sizeof(iscsid_add_portal_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		add_portal((iscsid_add_portal_req_t *)p, prsp, prsp_temp);
		break;

	case ISCSID_SET_TARGET_OPTIONS:
		if (req->parameter_length != sizeof(iscsid_get_set_target_options_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		rsp->status = set_target_options((iscsid_get_set_target_options_t *)p);
		break;

	case ISCSID_GET_TARGET_OPTIONS:
		if (req->parameter_length != sizeof(iscsid_sym_id_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		rsp->status = ISCSID_STATUS_NOTIMPL;
		break;

	case ISCSID_SET_TARGET_AUTHENTICATION:
		if (req->parameter_length !=
			sizeof(iscsid_set_target_authentication_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		rsp->status = set_target_auth((iscsid_set_target_authentication_req_t *)p);
		break;

	case ISCSID_SLP_FIND_TARGETS:
		rsp->status = ISCSID_STATUS_NOTIMPL;
		break;

	case ISCSID_REFRESH_TARGETS:
		if (req->parameter_length < sizeof(iscsid_refresh_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		rsp->status = refresh_targets((iscsid_refresh_req_t *)p);
		break;

	case ISCSID_REMOVE_TARGET:
		if (req->parameter_length != sizeof(iscsid_list_id_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		rsp->status = remove_target((iscsid_list_id_t *)p);
		break;

	case ISCSID_SEARCH_LIST:
		if (req->parameter_length != sizeof(iscsid_search_list_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		search_list((iscsid_search_list_req_t *)p, prsp, prsp_temp);
		break;

	case ISCSID_GET_LIST:
		if (req->parameter_length != sizeof(iscsid_get_list_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		get_list((iscsid_get_list_req_t *)p, prsp, prsp_temp);
		break;

	case ISCSID_GET_TARGET_INFO:
		if (req->parameter_length != sizeof(iscsid_list_id_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		get_target_info((iscsid_list_id_t *)p, prsp, prsp_temp);
		break;

	case ISCSID_GET_PORTAL_INFO:
		if (req->parameter_length != sizeof(iscsid_list_id_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		get_portal_info((iscsid_list_id_t *)p, prsp, prsp_temp);
		break;

#ifndef ISCSI_MINIMAL
	case ISCSID_ADD_ISNS_SERVER:
		if (req->parameter_length != sizeof(iscsid_add_isns_server_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		add_isns_server((iscsid_add_isns_server_req_t *)p,
						prsp, prsp_temp);
		break;

	case ISCSID_GET_ISNS_SERVER:
		if (req->parameter_length != sizeof(iscsid_sym_id_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		get_isns_server((iscsid_sym_id_t *)p, prsp, prsp_temp);
		break;

	case ISCSID_SLP_FIND_ISNS_SERVERS:
		rsp->status = ISCSID_STATUS_NOTIMPL;
		break;

	case ISCSID_REMOVE_ISNS_SERVER:
		if (req->parameter_length != sizeof(iscsid_sym_id_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		rsp->status = remove_isns_server((iscsid_sym_id_t *)p);
		break;
#endif

	case ISCSID_ADD_INITIATOR_PORTAL:
		if (req->parameter_length != sizeof(iscsid_add_initiator_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		add_initiator_portal((iscsid_add_initiator_req_t *)p,
							prsp, prsp_temp);
		break;

	case ISCSID_GET_INITIATOR_PORTAL:
		if (req->parameter_length != sizeof(iscsid_sym_id_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		get_initiator_portal((iscsid_sym_id_t *)p, prsp, prsp_temp);
		break;

	case ISCSID_REMOVE_INITIATOR_PORTAL:
		if (req->parameter_length != sizeof(iscsid_sym_id_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		rsp->status = remove_initiator_portal((iscsid_sym_id_t *)p);
		break;

	case ISCSID_LOGIN:
		if (req->parameter_length != sizeof(iscsid_login_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		login((iscsid_login_req_t *)p, rsp);
		break;

	case ISCSID_ADD_CONNECTION:
		if (req->parameter_length != sizeof(iscsid_login_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		add_connection((iscsid_login_req_t *)p, rsp);
		break;

	case ISCSID_LOGOUT:
		if (req->parameter_length != sizeof(iscsid_sym_id_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		rsp->status = logout((iscsid_sym_id_t *)p);
		break;

	case ISCSID_REMOVE_CONNECTION:
		if (req->parameter_length != sizeof(iscsid_remove_connection_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		rsp->status = remove_connection((iscsid_remove_connection_req_t *)p);
		break;

	case ISCSID_GET_SESSION_LIST:
		get_session_list(prsp, prsp_temp);
		break;

	case ISCSID_GET_CONNECTION_LIST:
		if (req->parameter_length != sizeof(iscsid_sym_id_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		get_connection_list((iscsid_sym_id_t *)p, prsp, prsp_temp);
		break;

	case ISCSID_GET_CONNECTION_INFO:
		if (req->parameter_length != sizeof(iscsid_get_connection_info_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		get_connection_info((iscsid_get_connection_info_req_t *)p,
							prsp, prsp_temp);
		break;

	case ISCSID_SET_NODE_NAME:
		if (req->parameter_length != sizeof(iscsid_set_node_name_req_t)) {
			rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
			break;
		}
		rsp->status = set_node_name((iscsid_set_node_name_req_t *)p);
		break;

	case ISCSID_GET_VERSION:
		get_version(prsp, prsp_temp);
		break;

	default:
		rsp->status = ISCSID_STATUS_INVALID_REQUEST;
		break;
	}
}
Exemplo n.º 26
0
static void
read_connections (NMSystemConfigInterface *config)
{
	SCPluginKeyfile *self = SC_PLUGIN_KEYFILE (config);
	SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (self);
	GDir *dir;
	GError *error = NULL;
	const char *item;
	GHashTable *alive_connections;
	GHashTableIter iter;
	NMKeyfileConnection *connection;
	GPtrArray *dead_connections = NULL;
	guint i;
	GPtrArray *filenames;
	GHashTable *paths;

	dir = g_dir_open (KEYFILE_DIR, 0, &error);
	if (!dir) {
		nm_log_warn (LOGD_SETTINGS, "keyfile: cannot read directory '%s': (%d) %s",
		             KEYFILE_DIR,
		             error ? error->code : -1,
		             error && error->message ? error->message : "(unknown)");
		g_clear_error (&error);
		return;
	}

	alive_connections = g_hash_table_new (NULL, NULL);

	filenames = g_ptr_array_new_with_free_func (g_free);
	while ((item = g_dir_read_name (dir))) {
		if (nm_keyfile_plugin_utils_should_ignore_file (item))
			continue;
		g_ptr_array_add (filenames, g_build_filename (KEYFILE_DIR, item, NULL));
	}
	g_dir_close (dir);

	/* While reloading, we don't replace connections that we already loaded while
	 * iterating over the files.
	 *
	 * To have sensible, reproducible behavior, sort the paths by last modification
	 * time prefering older files.
	 */
	paths = _paths_from_connections (priv->connections);
	g_ptr_array_sort_with_data (filenames, (GCompareDataFunc) _sort_paths, paths);
	g_hash_table_destroy (paths);

	for (i = 0; i < filenames->len; i++) {
		connection = update_connection (self, NULL, filenames->pdata[i], NULL, FALSE, alive_connections, NULL);
		if (connection)
			g_hash_table_add (alive_connections, connection);
	}
	g_ptr_array_free (filenames, TRUE);

	g_hash_table_iter_init (&iter, priv->connections);
	while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &connection)) {
		if (   !g_hash_table_contains (alive_connections, connection)
		    && nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection))) {
			if (!dead_connections)
				dead_connections = g_ptr_array_new ();
			g_ptr_array_add (dead_connections, connection);
		}
	}
	g_hash_table_destroy (alive_connections);

	if (dead_connections) {
		for (i = 0; i < dead_connections->len; i++)
			remove_connection (self, dead_connections->pdata[i]);
		g_ptr_array_free (dead_connections, TRUE);
	}
}
Exemplo n.º 27
0
int server_loop(struct command_context *command_context)
{
	struct service *service;

	bool poll_ok = true;

	/* used in select() */
	fd_set read_fds;
	int fd_max;

	/* used in accept() */
	int retval;

#ifndef _WIN32
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
		LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
#endif

	while (!shutdown_openocd) {
		/* monitor sockets for activity */
		fd_max = 0;
		FD_ZERO(&read_fds);

		/* add service and connection fds to read_fds */
		for (service = services; service; service = service->next) {
			if (service->fd != -1) {
				/* listen for new connections */
				FD_SET(service->fd, &read_fds);

				if (service->fd > fd_max)
					fd_max = service->fd;
			}

			if (service->connections) {
				struct connection *c;

				for (c = service->connections; c; c = c->next) {
					/* check for activity on the connection */
					FD_SET(c->fd, &read_fds);
					if (c->fd > fd_max)
						fd_max = c->fd;
				}
			}
		}

		struct timeval tv;
		tv.tv_sec = 0;
		if (poll_ok) {
			/* we're just polling this iteration, this is faster on embedded
			 * hosts */
			tv.tv_usec = 0;
			retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
		} else {
			/* Every 100ms */
			tv.tv_usec = 100000;
			/* Only while we're sleeping we'll let others run */
			openocd_sleep_prelude();
			kept_alive();
			retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
			openocd_sleep_postlude();
		}

		if (retval == -1) {
#ifdef _WIN32

			errno = WSAGetLastError();

			if (errno == WSAEINTR)
				FD_ZERO(&read_fds);
			else {
				LOG_ERROR("error during select: %s", strerror(errno));
				exit(-1);
			}
#else

			if (errno == EINTR)
				FD_ZERO(&read_fds);
			else {
				LOG_ERROR("error during select: %s", strerror(errno));
				exit(-1);
			}
#endif
		}

		if (retval == 0) {
			/* We only execute these callbacks when there was nothing to do or we timed
			 *out */
			target_call_timer_callbacks();
			process_jim_events(command_context);

			FD_ZERO(&read_fds);	/* eCos leaves read_fds unchanged in this case!  */

			/* We timed out/there was nothing to do, timeout rather than poll next time
			 **/
			poll_ok = false;
		} else {
			/* There was something to do, next time we'll just poll */
			poll_ok = true;
		}

		/* This is a simple back-off algorithm where we immediately
		 * re-poll if we did something this time around.
		 *
		 * This greatly improves performance of DCC.
		 */
		poll_ok = poll_ok || target_got_message();

		for (service = services; service; service = service->next) {
			/* handle new connections on listeners */
			if ((service->fd != -1)
			    && (FD_ISSET(service->fd, &read_fds))) {
				if (service->max_connections > 0)
					add_connection(service, command_context);
				else {
					if (service->type == CONNECTION_TCP) {
						struct sockaddr_in sin;
						socklen_t address_size = sizeof(sin);
						int tmp_fd;
						tmp_fd = accept(service->fd,
								(struct sockaddr *)&service->sin,
								&address_size);
						close_socket(tmp_fd);
					}
					LOG_INFO(
						"rejected '%s' connection, no more connections allowed",
						service->name);
				}
			}

			/* handle activity on connections */
			if (service->connections) {
				struct connection *c;

				for (c = service->connections; c; ) {
					if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending) {
						retval = service->input(c);
						if (retval != ERROR_OK) {
							struct connection *next = c->next;
							if (service->type == CONNECTION_PIPE) {
								/* if connection uses a pipe then
								 *shutdown openocd on error */
								shutdown_openocd = 1;
							}
							remove_connection(service, c);
							LOG_INFO("dropped '%s' connection",
								service->name);
							c = next;
							continue;
						}
					}
					c = c->next;
				}
			}
		}

#ifdef _WIN32
		MSG msg;
		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
			if (msg.message == WM_QUIT)
				shutdown_openocd = 1;
		}
#endif
	}

	return ERROR_OK;
}
static void
read_connections (SCPluginIfcfg *plugin)
{
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (plugin);
	GDir *dir;
	GError *err = NULL;
	const char *item;
	GHashTable *alive_connections;
	GHashTableIter iter;
	NMIfcfgConnection *connection;
	GPtrArray *dead_connections = NULL;
	guint i;
	GPtrArray *filenames;
	GHashTable *paths;

	dir = g_dir_open (IFCFG_DIR, 0, &err);
	if (!dir) {
		_LOGW ("Could not read directory '%s': %s", IFCFG_DIR, err->message);
		g_error_free (err);
		return;
	}

	alive_connections = g_hash_table_new (NULL, NULL);

	filenames = g_ptr_array_new_with_free_func (g_free);
	while ((item = g_dir_read_name (dir))) {
		char *full_path;

		if (utils_should_ignore_file (item, TRUE))
			continue;
		if (utils_is_ifcfg_alias_file (item, NULL))
			continue;

		full_path = g_build_filename (IFCFG_DIR, item, NULL);
		if (!utils_get_ifcfg_name (full_path, TRUE))
			g_free (full_path);
		else
			g_ptr_array_add (filenames, full_path);
	}
	g_dir_close (dir);

	/* While reloading, we don't replace connections that we already loaded while
	 * iterating over the files.
	 *
	 * To have sensible, reproducible behavior, sort the paths by last modification
	 * time prefering older files.
	 */
	paths = _paths_from_connections (priv->connections);
	g_ptr_array_sort_with_data (filenames, (GCompareDataFunc) _sort_paths, paths);
	g_hash_table_destroy (paths);

	for (i = 0; i < filenames->len; i++) {
		connection = update_connection (plugin, NULL, filenames->pdata[i], NULL, FALSE, alive_connections, NULL);
		if (connection)
			g_hash_table_add (alive_connections, connection);
	}
	g_ptr_array_free (filenames, TRUE);

	g_hash_table_iter_init (&iter, priv->connections);
	while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &connection)) {
		if (   !g_hash_table_contains (alive_connections, connection)
		    && nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection))) {
			if (!dead_connections)
				dead_connections = g_ptr_array_new ();
			g_ptr_array_add (dead_connections, connection);
		}
	}
	g_hash_table_destroy (alive_connections);

	if (dead_connections) {
		for (i = 0; i < dead_connections->len; i++)
			remove_connection (plugin, dead_connections->pdata[i]);
		g_ptr_array_free (dead_connections, TRUE);
	}
}
Exemplo n.º 29
0
// Point d'entree dans le programme
int main(int argc,char** argv)
{
    FILE* transaction_file = fopen(S_LEC,"a+");
    fclose(transaction_file);
    transaction_file = fopen(S_LEC,"r");

    if (transaction_file == NULL) {
        fprintf(stderr,"Impossible d'ouvrir le fichier %s\n",S_LEC);
        return -1;
    }
    char line_buffer[256], message[256];
    int i, flags;
    Connection* connection;
    PRIM_PACKET p;

    // Recuperation des references au tuyaux
    // (respectivement l'ecriture et l'ecoute)
    transToNet_pipe = atoi(argv[1]);
    netToTrans_pipe = atoi(argv[2]);

    // On s'assure que les file descriptors sont en mode 0_NONBLOCK
    flags = fcntl(transToNet_pipe,F_GETFL,0);
    fcntl(transToNet_pipe,F_SETFL, flags | O_NONBLOCK);
    flags = fcntl(netToTrans_pipe,F_GETFL,0);
    fcntl(netToTrans_pipe,F_SETFL, flags | O_NONBLOCK);

    if (DEBUG) printf("TRANSPORT\nMes fd sont:\n%i,%i\n",netToTrans_pipe,transToNet_pipe);

    //ALGO
    //--------
    //TANT QUE FICHIER_NON_VIDE
    //LIRE S_LEC

    //REGARDER_TABLE_DE_CONNEXIONS

    //SI CONNEXION_EXISTE_PAS
    //ENVOYER_CONNECTION_REQ
    //ECOUTER_REPONSE

    //SI REPONSE_POSITIVE
    //CHANGER_ETAT_CONNEXION_TABLE_CONNEXION
    //ENVOYER_DATA_REQ
    //SINON
    //SUPPRIMER_CONNEXION
    //FIN SI
    //SINON
    //ENVOYER_DATA_REQ
    //FIN SI
    //FIN TANT QUE

    //POUR TOUS LES CONNEXION
    //SUPPRIMMER_CONNEXION

    // Lecture du fichier S_LEC, envoie des requetes et ecoute
    // de la reponse.
    while(fgets(line_buffer, 256, transaction_file))
    {
        connection = (Connection*) findConnection(line_buffer[0]);

        // La connexion n'existe pas
        if (!connection/*==NULL*/) {

            // Engager une connexion
            connection = (Connection*) add_connection(line_buffer[0],NULL,NULL);

            // Creaction d'un paquet contenant la primitive N_CONNECT_req
            p.prim = N_CONNECT_req;

            // Genere un seed pour une valeur pseudo-aleatoire
            // differente par rand()
            srand(time(NULL));
            p.con_prim_packet.src_addr = (unsigned char) rand();  // Adresses aleatoires pour la source
            p.con_prim_packet.dest_addr = (unsigned char) rand(); // et la destination..
            p.con_prim_packet.con_number = connection->tcon.con_number;

            // Envoie d'un paquet et ecoute de la reponse de ER
            writePrimPacketToStdOut(&p,I_AM);
            if(sendPacketToInterface(&p,transToNet_pipe) == -1)
                return -1;
            if (getPacketFromInterface(&p,netToTrans_pipe) == -1)
                return -1;

            char result[256];
            // Action en consequence
            switch(p.prim)
            {
            // CON_PRIM_PACKET reçu => N_CONNECT_conf
            case N_CONNECT_conf:
                // Ecriture des resutlats dans S_ECR
                sprintf(result,"Reception de la primitive N_CONNECT.conf sur la connection %c\n", connection->tcon.con_number);
                writeResults(result,S_ECR);

                // Confirmation de la connexion
                connection->tcon.state = 0x01;

                // Construction du paquet de DATA
                p.prim = N_DATA_req;
                getMessageFromBuffer(line_buffer,message);

                strcpy(p.data_prim_packet.transaction,message);
                p.data_prim_packet.con_number = connection->tcon.con_number;

                // Envoie du paquet a l'ER
                writePrimPacketToStdOut(&p,I_AM);
                if(sendPacketToInterface(&p,transToNet_pipe) == -1)
                    return -1;
                break;
            // REL_PRIM_PACKET reçu => N_DISCONNECT_ind
            case N_DISCONNECT_ind:
                // Ecriture des resultats dans S_ECR
                sprintf(result,"Reception de la primitive N_DISCONNECT.ind pour la connexion %c\n",connection->tcon.con_number);
                writeResults(result,S_ECR);

                // Retrait de la connexion de la table de connexions
                remove_connection(connection->tcon.con_number);
                break;
            }
        }
        else {
            // Construction du paquet de DATA
            p.prim = N_DATA_req;
            getMessageFromBuffer(line_buffer,message);
            strcpy(p.data_prim_packet.transaction,message);
            p.data_prim_packet.con_number = connection->tcon.con_number;

            // Envoyer N_DATA.req
            writePrimPacketToStdOut(&p,I_AM);
            if(sendPacketToInterface(&p,transToNet_pipe) == -1)
                return -1;
        }
    }

    // Construction d'un paquet REL_PRIM_PACKET
    // et vidage de la table de connexions
    p.prim = N_DISCONNECT_req;
    connection = first_con_node;
    while (connection) {
        p.rel_prim_packet.con_number = connection->tcon.con_number;
        writePrimPacketToStdOut(&p,I_AM);
        if(sendPacketToInterface(&p,transToNet_pipe) == -1)
            return -1;
        connection = connection->tcon.next;
    }

    deleteAllConnections();

    // Fermeture du fichier S_LEC
    fclose(transaction_file);

    return 0;
}