Exemplo n.º 1
0
void setup_socket()
{
    printf("%s\n","We are starting zzz...");
    int listen_fd = Open_listenfd(my_port);
    printf("Setting up listen_fd with number: %d\n", listen_fd);
    SA clientaddr;
    /*// Setup your read_set with FD_ZERO and the server socket descriptor
    FD_ZERO(&p.read_set);
    FD_SET(listen_fd, &p.read_set);
    p.maxfd = listen_fd + 1;*/
    init_pool(listen_fd, &p);
    while(1)
    {
        p.ready_set = p.read_set;
        p.nready = select(p.maxfd + 1, &p.ready_set, &p.write_set, NULL, NULL);
        if(FD_ISSET(listen_fd, &p.ready_set))
        {
            socklen_t clientlen = sizeof(clientaddr);
            int conn_fd = Accept(listen_fd, &clientaddr, &clientlen);//If there is a request then accept it
            printf("Accepting with conn_fd: %d\n", conn_fd);
            add_client(conn_fd, &p);
        }
        check_clients(&p);
        /*socklen_t clientlen = sizeof(clientaddr);
        int conn_fd = Accept(listen_fd, &clientaddr, &clientlen);//If there is a request then accept it
        printf("Accepting with conn_fd: %d\n", conn_fd);
        break;*/
    }
}
Exemplo n.º 2
0
void
cql::cql_client_pool_impl_t::add_client(const std::string& server,
                                        unsigned int       port)
{
    std::list<std::string> e;
    add_client(server, port, NULL, e);
}
Exemplo n.º 3
0
  static void	server_read(t_select *select, t_client *server)
  {
#ifdef DEBUG
    printf("*** select: server read\n");
#endif /* !DEBUG */
    add_client(select, server);
  }
Exemplo n.º 4
0
/* adds a new X11 client to the list, IF it does not already exist */
void set_client_window(Window win)
{
	int i, scr_count;
	struct client *cnode;

	/* When a magellan application exits, the SDK sets another window to avoid
	 * crashing the original proprietary daemon.  The new free SDK will set
	 * consistently the root window for that purpose, which we can ignore here
	 * easily.
	 */
	scr_count = ScreenCount(dpy);
	for(i=0; i<scr_count; i++) {
		if(win == RootWindow(dpy, i)) {
			return;
		}
	}

	cnode = client_list->next;
	while(cnode) {
		if(cnode->win == win) {
			return;
		}
		cnode = cnode->next;
	}

	add_client(CLIENT_X11, &win);
}
Exemplo n.º 5
0
void		server_read(t_info *info, t_client **client)
{
  t_client	*c;

  c = add_client(info, (*client)->socket, 0, 0);
  begin_session(info, &c);
}
Exemplo n.º 6
0
void test_add_client(struct login_message lm) {
    if(add_client(lm)){
        printf("client %s is in %s\n", lm.userid, lm.dir);
    } else {
        printf("client %s added to %s\n", lm.userid, lm.dir);
    }
}
Exemplo n.º 7
0
/**
:* @method		: SockServer::accept6
 * @return		:
 *	< Socket	: success, new client accepted.
 *	< NULL		: fail.
 * @desc		:
 *	This method is used by server socket for accepting a new client
 *	connection. This is a for IPv6 server socket.
 */
Socket* SockServer::accept6()
{
	socklen_t		client_addrlen;
	struct sockaddr_in6	client_addr;
	Socket*			client = NULL;

	client = new Socket();
	if (!client) {
		return NULL;
	}

	client_addrlen	= SockAddr::IN_SIZE;
	client->_d	= ::accept(_d, (struct sockaddr *) &client_addr
					, &client_addrlen);
	if (client->_d < 0) {
		delete client;
		return NULL;
	}

	inet_ntop(_family, &client_addr.sin6_addr, client->_name._v
		, client->_name._l);

	client->_status	= O_RDWR | O_SYNC;

	add_client(client);

	return client;
}
Exemplo n.º 8
0
int main(int argc, char **argv)
{
    int listenfd, connfd, port;
    socklen_t clientlen = sizeof(struct sockaddr_in);
    struct sockaddr_in clientaddr;
    static pool pool;

    if (argc != 2) {
        fprintf(stderr, "usage: %s <port>\n", argv[0]);
        exit(0);
    }
    port = atoi(argv[1]);

    listenfd = Open_listenfd(port);
    init_pool(listenfd, &pool);

    while (1) {
        /* Wait for listening/connected descriptor(s) to become ready */
        pool.ready_set = pool.read_set;
        pool.nready = Select(pool.maxfd+1, &pool.ready_set, NULL, NULL, NULL);

        /* If listening descriptor ready, add new client to pool */
        if (FD_ISSET(listenfd, &pool.ready_set)) {
            connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
            add_client(connfd, &pool);
        }

        /* Echo a text line from each ready connected descriptor */
        check_client(&pool);
    }
}
Exemplo n.º 9
0
void	server_read(t_env *e, int fd)
{
  int	cs;

  cs = add_client(e, fd);
  printf("New client : %d\n", cs);
}
Exemplo n.º 10
0
/* Start worker
 */
int start_worker(t_session *session) {
#ifndef ENABLE_THREAD_POOL
	int result = -1;
	pthread_attr_t child_attr;
	pthread_t      child_thread;

	if (pthread_attr_init(&child_attr) != 0) {
		log_system(session, "pthread init error");
	} else {
		if (pthread_attr_setdetachstate(&child_attr, PTHREAD_CREATE_DETACHED) != 0) {
			log_system(session, "pthread set detach state error");
		} else if (pthread_attr_setstacksize(&child_attr, PTHREAD_STACK_SIZE) != 0) {
			log_system(session, "pthread set stack size error");
		} else if (add_client(session) == 0) {
			if (pthread_create(&child_thread, &child_attr, (void*)connection_handler, (void*)session) == 0) {
				/* Worker thread started
				 */
				result = 0;
			} else {
				remove_client(session, false);
				log_system(session, "pthread create error");
			}
		}
		pthread_attr_destroy(&child_attr);
	}

	return result;
#else
	int result = 0;
	t_session_list *new_session;

	pthread_mutex_lock(&thread_pool_mutex);

	if (waiting_workers <= 0) {
		if (add_thread_to_pool(session) == -1) {
			result = -1;
		}
	} else if ((new_session = (t_session_list*)malloc(sizeof(t_session_list))) == NULL) {
		result = -1;
	} else {
		new_session->session = session;

		new_session->next = session_list;
		session_list = new_session;

		if (pthread_cond_signal(&thread_pool_cond) != 0) {
			result = -1;
			session_list = session_list->next;
			free(new_session);
		} else {
			waiting_workers--;
		}
	}

	pthread_mutex_unlock(&thread_pool_mutex);

	return result;
#endif
}
Exemplo n.º 11
0
struct client *
create_client(struct domain *domain, int sockfd,
#ifndef DISABLE_LOGGING
              struct sockaddr *addr, socklen_t addrlen,
#endif
              int randomfd)
{
    struct client *client;
    int ret;
#ifdef HAVE_DEV_RANDOM
    ssize_t nbytes;
#endif

    client = calloc(1, sizeof(*client));
    if (client == NULL)
        return NULL;

#ifndef DISABLE_LOGGING
    if ((size_t)addrlen > sizeof(client->address))
        addrlen = sizeof(client->address);
    memcpy(&client->address, addr, addrlen);
#endif /* DISABLE_LOGGING */

    /* a good random client id is vitally important for security,
       because secondary connections authorized themselves with it */
#ifdef HAVE_DEV_RANDOM
    nbytes = read(randomfd, &client->id, sizeof(client->id));
    if (nbytes < (ssize_t)sizeof(client->id)) {
        fprintf(stderr, "random number generation failed\n");
        free(client);
        return NULL;
    }
#else
    (void)randomfd;

    client->id = (random() << 24) + (random() << 16)
        + (random() << 8) + random();
#endif

    client->sockets[0] = sockfd;
    client->num_sockets = 1;
    client->timeout = time(NULL) + 60;

#ifndef DISABLE_LOGGING
    update_client_name(client);
#endif

    ret = add_client(domain, client);
    if (!ret) {
        log(1, "domain 0 is full, rejecting new client %s\n",
            client->name);
        free_client(client);
        return NULL;
    }

    log(2, "new client: %s\n", client->name);

    return client;
}
Exemplo n.º 12
0
void on_connect(SOCK s,void*ud,int err)
{
    if(s != INVALID_SOCK) {
        struct connection * con = new_conn(s,0);
        add_client(con);
        bind2engine((ENGINE)ud,con,on_process_packet,remove_client);
    }
}
Exemplo n.º 13
0
void process_new_connection(datasocket_t s)
{
	set_recv_timeout(s,10*1000);
	set_send_timeout(s,10*1000);
	add_client(s);
	++count;
	printf("%d\n",count);
}
Exemplo n.º 14
0
void new_client(int fd)
{
  fcntl(fd, F_SETFL, 0);
  if (init_sf_source(fd) < 0)
    close(fd);
  else
    add_client(fd);
}
Exemplo n.º 15
0
static void
try_connect (server_auth_t * auth)
{
    int     f;
    CONNECTION *cli;
    unsigned int ip;

    /* attempt a connection.  we do this nonblocking so that the server
       doesn't halt if it takes a long time to connect */
    f = make_tcp_connection (auth->name, auth->port, &ip);
    if (f == -1)
	return;

    cli = new_connection ();
    if (!cli)
	goto error;
    cli->fd = f;
    cli->host = STRDUP (auth->alias ? auth->alias : auth->name);
    if (!cli->host)
    {
	OUTOFMEMORY ("try_connect");
	goto error;
    }
    cli->server_login = 1;
    if ((cli->opt.auth = CALLOC (1, sizeof (AUTH))) == 0)
    {
	OUTOFMEMORY ("try_connect");
	goto error;
    }
    cli->opt.auth->nonce = generate_nonce ();
    if (!cli->opt.auth->nonce)
    {
	log_message ("try_connect: could not generate nonce, closing connection");
	goto error;
    }
    cli->ip = BSWAP32 (ip);
    cli->port = auth->port;

    if (add_client (cli, 1/* server connection */))
	goto error;

    return;
  error:
    log_message ("try_connect: closing connection");
    if (cli)
    {
	CLOSE (cli->fd);
	if (cli->host)
	    FREE (cli->host);
	if (cli->opt.auth)
	{
	    if (cli->opt.auth->nonce)
		FREE (cli->opt.auth->nonce);
	    FREE (cli->opt.auth);
	}
	FREE (cli);
    }
}
Exemplo n.º 16
0
void accept_client(SOCK s,void*ud)
{
	struct connection *c = new_conn(s,0);
	add_client(c);
	struct netservice *tcpserver = (struct netservice *)ud;
	tcpserver->bind(tcpserver,c,on_process_packet,remove_client
					,5000,c_recv_timeout,5000,c_send_timeout
					);
}
Exemplo n.º 17
0
 fc::shared_ptr<ServiceClientType> get_client( const fc::sha1& id ) {
   auto scp = get_client( id, ServiceClientType::static_name() );
   if( !scp ) {
      fc::shared_ptr<ServiceClientType> sc( new ServiceClientType( *this, id ) );
      add_client( id, sc );
      return sc;
   }
   return fc::dynamic_pointer_cast<ServiceClientType>(scp);
 }
Exemplo n.º 18
0
int handle_requests()
{
    struct sockaddr_in remote;
    struct pollfd pfds[MAX_CLIENTS];
    int cli_fd, i, n;
    
    size_t len = sizeof(remote);

// akceptujemy przychodzace polaczenia i dodajemy nowych klientow do tablicy
    while ((cli_fd = accept(sock_fd, (struct sockaddr *)&remote, &len))>0)
	if( add_client(cli_fd) < 0) 
	{
	    close(cli_fd);
	    return 0;
	}


// przygotowujemy liste deskryptorow dla poll()-a
    for(n=i=0;i < MAX_CLIENTS;i++)
	if(client_tab[i].fd)
	{    
	    pfds[n].fd = client_tab[i].fd;
	    pfds[n].events = POLLIN | POLLHUP; // reagujemy na przychodzace dane i zerwanie sesji
	    pfds[n].revents = 0;

	    n++;
	}    

    int ret;

// pollujemy klientow    
    if(( ret = poll(pfds, n, 10)) < 0) die("poll error: %s", strerror(errno));

// brak ruchu - koniec pracy funkcji
    if(!ret)  return 0; 

// sprawdzamy, co sie wydarzylo:
    for(i=0; i<n ;i++)
    {
	if(pfds[i].revents & POLLHUP)					// klient zerwal polaczenie?
	{
	    fprintf(stderr, "[lsrv] client %d closed connection.\n", pfds[i].fd);
	    close(pfds[i].fd);
	    remove_client(pfds[i].fd);
	} else if(pfds[i].revents & POLLIN)				// nadeszly dane od klienta?
	{
	    if(handle_client(find_client(pfds[i].fd)) < 0)
	    {
	        close(pfds[i].fd);
		remove_client(pfds[i].fd);
    	    }
	}  	
    }
    
    return 0;    

}
Exemplo n.º 19
0
Arquivo: serve.c Projeto: ivokub/kftp
pthread_t * thread_client_handle(client * this_client){
	pthread_t * current_thread = malloc(sizeof(pthread_t));
	if (pthread_create(current_thread, NULL, client_handle_middle, (void *) this_client)) {
		return NULL;
	} else {
		add_client(this_client);
		return current_thread;
	}
}
Exemplo n.º 20
0
void
cql::cql_client_pool_impl_t::add_client(const std::string&                      server,
                                        unsigned int                            port,
                                        cql::cql_client_t::cql_event_callback_t event_callback,
                                        const std::list<std::string>&           events)
{
    std::map<std::string, std::string> credentials;
    add_client(server, port, event_callback, events, credentials);
}
Exemplo n.º 21
0
TreelogServer::~TreelogServer ()
{
  // Handle case where no log has been attached yet.
  if (has_unhandled_events ())
    {
      boost::shared_ptr<Treelog> progress (new TreelogProgress ());
      add_client (progress);
    }
}  
Exemplo n.º 22
0
void accept_callback(SOCK s,void *ud)
{
	ENGINE *engine = (ENGINE*)ud;
	struct connection *c = connection_create(s,0,SINGLE_THREAD,on_process_packet,remove_client);
	add_client(c);
	setNonblock(s);
	connection_start_recv(c);
	Bind2Engine(*engine,s,RecvFinish,SendFinish,NULL);
}
Exemplo n.º 23
0
/* Main loop for thread in thread pool
 */
static void thread_wait_loop(t_thread_pool *self) {
	t_session *session;
	t_thread_pool *prev;

	do {
		if (self->session == NULL) {
			pthread_mutex_lock(&thread_pool_mutex);

			waiting_workers++;
			if (pthread_cond_wait(&thread_pool_cond, &thread_pool_mutex) != 0) {
				waiting_workers--;
				session = NULL;
			} else {
				session = last_session(session_list);
			}

			pthread_mutex_unlock(&thread_pool_mutex);
		} else {
			session = self->session;
			self->session = NULL;
		}

		if (session != NULL) {
			if (add_client(session) == 0) {
				connection_handler(session);
			} else {
				close(session->client_socket);
				free(session);
			}
		}
	} while (self->quit == false);

	/* Remove thread record from pool
	 */
	pthread_mutex_lock(&thread_pool_mutex);

	if (thread_pool == self) {
		thread_pool = thread_pool->next;
	} else {
		prev = thread_pool;
		while (prev != NULL) {
			if (prev->next == self) {
				prev->next = self->next;
				break;
			}
			prev = prev->next;
		}
	}
	thread_pool_size--;

	pthread_mutex_unlock(&thread_pool_mutex);

	free(self);

	pthread_exit(NULL);
}
Exemplo n.º 24
0
static NMDHCPClient *
client_start (NMDHCPManager *self,
              const char *iface,
              const char *uuid,
              gboolean ipv6,
              NMSettingIP4Config *s_ip4,
              NMSettingIP6Config *s_ip6,
              guint32 timeout,
              guint8 *dhcp_anycast_addr,
              const char *hostname,
              gboolean info_only)
{
	NMDHCPManagerPrivate *priv;
	NMDHCPClient *client;
	gboolean success = FALSE;

	g_return_val_if_fail (self, NULL);
	g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
	g_return_val_if_fail (iface != NULL, NULL);
	g_return_val_if_fail (uuid != NULL, NULL);

	priv = NM_DHCP_MANAGER_GET_PRIVATE (self);

	/* Ensure we have a usable DHCP client */
	g_return_val_if_fail (priv->client_type != 0, NULL);

	/* Kill any old client instance */
	client = get_client_for_iface (self, iface, ipv6);
	if (client) {
		nm_dhcp_client_stop (client);
		remove_client (self, client);
	}

	/* And make a new one */
	client = g_object_new (priv->client_type,
	                       NM_DHCP_CLIENT_INTERFACE, iface,
	                       NM_DHCP_CLIENT_IPV6, ipv6,
	                       NM_DHCP_CLIENT_UUID, uuid,
	                       NM_DHCP_CLIENT_TIMEOUT, timeout ? timeout : DHCP_TIMEOUT,
	                       NULL);
	g_return_val_if_fail (client != NULL, NULL);
	add_client (self, client);

	if (ipv6)
		success = nm_dhcp_client_start_ip6 (client, s_ip6, dhcp_anycast_addr, hostname, info_only);
	else
		success = nm_dhcp_client_start_ip4 (client, s_ip4, dhcp_anycast_addr, hostname);

	if (!success) {
		remove_client (self, client);
		g_object_unref (client);
		client = NULL;
	}

	return client;
}
Exemplo n.º 25
0
void Manage(int what)
{
	cout << "Would you like to:" << endl;
	cout << "1. Search existing objects" << endl;
	cout << "2. Add a new one" << endl;
	cout << "3. Go back" << endl;

	char ch;

	cin.clear();
	cin.ignore(numeric_limits<streamsize>::max(), '\n');

	ch = getchar();

	switch (ch)
	{
	case'1':
		switch (what)
		{
		case 1:
			bookSearch();
			break;
		case 2:
			clientSearch();
			break;
		case 3:
			catSearch();
			break;
		}
		break;
	case '2':
		switch (what)
		{
		case 1:
			add_book();
			break;
		case 2:
			add_client();
			break;
		case 3:
			add_cat();
			break;
		}
		break;
	case '3':
		break;
	default:
		cout << "Please provide a valid response." << endl;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		Manage(what);
		break;
	}
system("pause");
}
Exemplo n.º 26
0
void 
Network::accept_stream(void) 
{
   NetStream *s = wait_for_connect();
   cerr << "Network   accept_stream from ---->" << s->name() << endl;
   add_stream(s);
   add_client(s);

   // notify observers
   notify_net(Network_obs::accept_str, s);
}
Exemplo n.º 27
0
int                     Epoll::listen_new_client(int fd, __uint32_t flags)
{
  Client                *client;

  client = new Client();
  init_event_struct((void*)client, flags);
  client->set_socket(fd);
  add_client(client);
  this->new_events[Epoll::NEW_CONN].push_back(client);
  return (0);
}
Exemplo n.º 28
0
void accept_callback(HANDLE s,void *ud)
{
	HANDLE *engine = (HANDLE*)ud;	
	struct connection *c = connection_create(s,0,SINGLE_THREAD,on_process_packet,remove_client);
	add_client(c);
	printf("cli fd:%d\n",s);
	setNonblock(s);
	//发出第一个读请求
	connection_start_recv(c);
	Bind2Engine(*engine,s,RecvFinish,SendFinish);
}
Exemplo n.º 29
0
void accept_callback(SOCKET s,void *ud)
{
	DWORD err_code = 0;
	HANDLE *iocp = (HANDLE*)ud;
	struct connection *c = connection_create(s,1,on_process_packet,remove_client);
	add_client(c);
	//++clientcount;
	printf("cli fd:%d\n",s);
	Bind2Engine(*iocp,(Socket_t)c);
	//发出第一个读请求
	connection_recv(c);
}
Exemplo n.º 30
0
static gboolean accept_client(GIOChannel*  source,
                              GIOCondition condition,
                              gpointer     p)
{
  int                    opt;
  socklen_t              optlen;
  int                    newfd;
  dsmesock_connection_t* newconn     = 0;

  if( condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL) ) {
    dsme_log(LOG_CRIT, "disabling client connect watcher");
    return FALSE;
  }

  newfd = accept(g_io_channel_unix_get_fd(source), 0, 0);
  if(newfd == -1) return TRUE;

  newconn = dsmesock_init(newfd);
  if(newconn == 0) {
    close(newfd);
    return TRUE;
  }

  opt    = 1;
  optlen = sizeof(opt);

  setsockopt(newfd, SOL_SOCKET, SO_PASSCRED, &opt, optlen);
  /* If that fails it is not fatal */

  optlen = sizeof(newconn->ucred);
  if(getsockopt(newfd, SOL_SOCKET, SO_PEERCRED,
                &newconn->ucred, &optlen) == -1)
  {
    /* if that fails, fill some bogus values */
    newconn->ucred.pid =  0;
    newconn->ucred.uid = -1;
    newconn->ucred.gid = -1;
  }

  if (!(newconn->channel = g_io_channel_unix_new(newfd)) ||
      !g_io_add_watch(newconn->channel,
		      G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
                      handle_client,
                      newconn))
  {
    close_client(newconn);
  } else {
    add_client(newconn);
  }

  return TRUE; /* do not discard the listening channel */
}