/* Dispatch client dynamic, it's normally called after a select
 * Based on dispatch_server function
 *
 * It's responsible for disconnecting the client
 * in case of protocol error or network error.
 */
void dispatch_dynamic(s_client *client, fd_set *set_read, fd_set *set_write)
{
	int k = 0;
	/* Dispatch server socket */
	if (client->soc.soc != -1 && FD_ISSET (client->soc.soc, set_read))
		k = dispatch_server_read(&client->soc, &client->soc_stream, &client->soc_bind,
				&client->socks, &client->buf, &client->stream_buf, client->conf);

	else if (client->soc.soc != -1 &&
			FD_ISSET (client->soc.soc, set_write))
		k = dispatch_server_write(&client->soc, &client->soc_stream, &client->socks, &client->buf, client->conf);
	if (k < 0){ disconnection(client);	return;}

	/* Dispatch stream socket */
	if (client->soc_stream.soc != -1 && FD_ISSET (client->soc_stream.soc, set_read))
		k = dispatch_client_read(&client->soc_stream, &client->soc,
				&client->socks_stream, &client->stream_buf, &client->buf, client->conf);
	else if (client->soc_stream.soc != -1 &&
			FD_ISSET (client->soc_stream.soc, set_write))
		k = dispatch_client_write(&client->soc_stream, &client->socks_stream, &client->stream_buf, client->conf);
	if (k < 0){ disconnection(client);	return;}

	/* Dispatch bind socket (it's server socket)*/
	if (client->soc_bind.soc != -1 &&
			FD_ISSET (client->soc_bind.soc, set_read)){
		if ( build_request_accept_bind(&client->socks, client->conf,
				&client->soc, &client->soc_bind, &client->stream_buf) == 0 ){
			client->socks.state = S_W_REQ_ACK;
		}
	}

}
/**
* Traitement d'une demande d'envoie de rapport d'un employé
**/
void* th_employee_management(void* param) 
{
  client_t employee = (client_t)param;
  int request(-1),continu(-1);
  
  cout << endl<< "Employe management : " << employee->name << endl;
  
  while(continu != 0)
  {
    cout <<  employee->des_client << endl;
    if(recv(employee->des_client,&request,sizeof(int),0)==-1) 
    {
      perror("Erreur reception employee management");
      continu = 0;
    }
      
    switch(request) {
      case 1 :  cout << "L'employé va saisir un rapport..."<<endl;
        employee_report_to_pdf(employee);
        break;
      case 2 :  cout << "L'employé va télécharger son rapport" << endl;
        download_PDF(employee,-1);
        break;
      case 3 : continu = 0; 
        cout << "L'employé quitte" << endl;
        break;
      default : cerr << "Erreur switch employee management" << endl;
    }
    
  }
  update_employee_report_status(employee,NULL);
  disconnection(employee);

pthread_exit(NULL);
}
Example #3
0
void sstp_disconnect(void) {

	if (!disconnection()) {
		if (sstpproc) {
			sstpproc(sstphwnd, sstprcv);
		}
	}
}
void HttpServerConnection::onError(err_t err)
{
	if (disconnection)
	{
		disconnection(*this);
		disconnection = nullptr;
	}
	TcpConnection::onError(err);
}
void HttpServerConnection::close()
{
	if (disconnection)
	{
		disconnection(*this);
		disconnection = nullptr;
	}
	TcpConnection::close();
}
Example #6
0
void SeekWidget::setPlaylistPlayer(PlaylistPlayer *player)
{

    disconnection();

    _playlistPlayer = player;
    _mediaPlayer = _playlistPlayer->mediaPlayer();
    _seek->setPlaylistPlayer(_playlistPlayer);

    connection();
}
/* Dispatch client, it's normally called after a select
 * Search client with socket in set_read and set_write and call
 * the right dispatcher.
 *
 * It's responsible for disconnecting the client
 * in case of protocol error or network error.
 */
void dispatch_client(s_client *client, fd_set *set_read, fd_set *set_write)
{
	int k = 0;

	/* Dispatch server socket */
	if (client->soc.soc != -1 && FD_ISSET (client->soc.soc, set_read))
		k = dispatch_client_read(&client->soc, &client->soc_stream,
				&client->socks, &client->buf, &client->stream_buf, client->conf);
	else if (client->soc.soc != -1 &&
			FD_ISSET (client->soc.soc, set_write))
		k = dispatch_client_write(&client->soc, &client->socks, &client->buf, client->conf);

	/* Error client disconnection */
	if (k < 0){ disconnection(client); }
}
Example #8
0
fileclient::~fileclient()
{
	disconnection();
}
Example #9
0
int fileclient::send_tmout(int sockfd, const char *buffer, int buffer_len, int timeout)
{
    int len = 0;

	fd_set write_fds;
    int offset = 0;
	struct timeval tval;

    if(sockfd < 0 || buffer == NULL || buffer_len < 0)
    	return -1;

	tval.tv_sec = timeout / 1000;
	tval.tv_usec = (timeout % 1000)*1000;

	int tryconn = 3;

    while(offset < buffer_len)
    {
		len = write(sockfd, buffer + offset, buffer_len - offset);
        if(len > 0)
        {
        	offset += len;
        	continue;
        }
        else
        {
            if(errno == EAGAIN)
            {
				FD_ZERO(&write_fds);
				FD_SET(sockfd, &write_fds);
				int res = select(sockfd + 1,  NULL, &write_fds, NULL, &tval);
                if(res > 0)
                {
                	continue;
                }
                else if(res < 0)
                {
                	printf("send_tmout select error %d %s \n", errno, strerror(errno));
                }
                else
                {
                	printf("send_tmout select timeout %u : %u \n", (unsigned int)tval.tv_sec, (unsigned int)tval.tv_usec);
                	return SOCK_ERROR;
                }
        	}
            else //在其他情况下说明遇到写错误EPIPE。对端主动发起关闭,和socket收到reset消息。
            {
                printf("write error disconnection : %s \n", strerror(errno));
            	disconnection();
                if(tryconn-- > 0)
                {
                	if(connection())
                	{
                		printf("reconnection success \n");
                	    continue;
                	}
                }
            }
            return SOCK_ERROR;
        }
    }

    return offset;
}
Example #10
0
//精确的表示接收多大的长度,如果读不到这个长度是会超时退出来的。
int fileclient::recv_tmout(int sockfd, char *buffer, int buffer_len, int timeout)
{
	fd_set read_fds;
    int offset = 0;

    if(sockfd < 0 || buffer == NULL || buffer_len < 0)
    	return -1;

	struct timeval tval;
	tval.tv_sec = timeout / 1000;
	tval.tv_usec = (timeout % 1000)*1000;
    int tryconn = 3;

	while (offset < buffer_len)
	{
		int len = read(sockfd, buffer, buffer_len - offset);

		if(len > 0)
		{
			offset += len;
            continue;
		}
		else if(len == 0)
		{
            printf("read = 0, remote close, disconnection \n");
        	disconnection();
//            if(tryconn-- > 0)
//            {
//            	if(connection())
//            	{
//            		printf("reconnection success \n");
//            	    continue;
//            	}
//            }
            return SOCK_REMOTE_CLOSE;
		}
		else
		{
            if(errno == EAGAIN)
            {
				FD_ZERO(&read_fds);
				FD_SET(sockfd, &read_fds);
				int res = select(sockfd + 1, &read_fds, NULL, NULL, &tval);
				if (res == 1)
				{
					if (FD_ISSET(sockfd, &read_fds))
						continue;
				}
				else if (res <= 0)
				{
                    printf("recv_tmout fileclient select timeout \n");
                    return SOCK_ERROR;
				}
            }
            else
            {
            	printf("read error disconnection : %s \n", strerror(errno));
            	disconnection();
                if(tryconn-- > 0)
                {
                	if(connection())
                	{
                		printf("reconnection success \n");
                	    continue;
                	}
                }
                return SOCK_ERROR;
            }
		}
	}

	return offset;
}
Example #11
0
void sstp_destruct(void) {

	disconnection();
}
Example #12
0
void server_relay(int port, int listen, int ssl) {
    int soc_ec_cli = -1, soc_ec = -1, maxfd, res, nc;
    fd_set set_read;
    fd_set set_write;
    struct sockaddr_in addrS;


#ifdef _WIN32
    WSADATA wsaData;
    int wsaInit = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (wsaInit != 0) {
        ERROR(L_NOTICE, "WSAStartup failed: %d\n", wsaInit);
        exit(1);
    }
#endif

    /* Init client tab */
    for (nc = 0; nc < MAXCLI; nc++)
        init_socket(&socks_pool[nc]);

    for (nc = 0; nc < MAXCLI; nc++)
        init_client (&tc[nc], nc, 0, NULL);

    TRACE(L_NOTICE, "server: set listening client socks relay ...");
    soc_ec = new_listen_socket (NULL, port, MAXCLI, &addrS);
    if (soc_ec < 0) goto fin_serveur;

    TRACE(L_NOTICE, "server: set server relay ...");
    soc_ec_cli = new_listen_socket (NULL, listen, MAXCLI, &addrS);
    if (soc_ec_cli < 0) goto fin_serveur;


#ifndef _WIN32
    if ( globalArgs.background == 1 ) {
        TRACE(L_NOTICE, "server: background ...");
        if ( daemon(0, 0) != 0 ) {
            perror("daemon");
            exit(1);
        }
    }

    bor_signal (SIGINT, capte_fin, SA_RESTART);

    /* TODO: Find a better way to exit the select and recall the init_select
     * SIGUSR1 is send by a thread to unblock the select */
    bor_signal (SIGUSR1, capte_usr1, SA_RESTART);
#endif


    while (boucle_princ) {
        init_select_reverse(soc_ec, soc_ec_cli, tc, &maxfd, &set_read, &set_write);

        res = select (maxfd+1, &set_read, &set_write, NULL,NULL);

        if (res > 0) {  /* Search eligible sockets */

            if (FD_ISSET (soc_ec, &set_read))
                new_connection_socket (soc_ec, socks_pool, ssl);

            if (FD_ISSET (soc_ec_cli, &set_read))
                new_connection_reverse (soc_ec_cli, tc, socks_pool);

            for (nc = 0; nc < MAXCLI; nc++) {
                dispatch_server(&tc[nc], &set_read, &set_write);
            }
        } else if ( res == 0) {
            /* If timeout was set in select and expired */
        } else if (res < 0) {
            if (errno == EINTR) ;  /* Received signal, it does nothing */
            else {
                perror ("select");
                goto fin_serveur;
            }
        }
    }

fin_serveur:
#ifdef HAVE_LIBSSL
    if (ssl == 1)
        ssl_cleaning();
#endif
    printf ("Server: closing sockets ...\n");
    if (soc_ec != -1) CLOSE_SOCKET(soc_ec);
    for (nc = 0; nc < MAXCLI; nc++) close_socket(&socks_pool[nc]);
    for (nc = 0; nc < MAXCLI; nc++) disconnection(&tc[nc]);

#ifdef _WIN32
    WSACleanup();
#endif
}
Example #13
0
void server(const char *bindAddr, int port, int ssl) {
    int soc_ec = -1, maxfd, res, nc;
    fd_set set_read;
    fd_set set_write;
    struct sockaddr_in addrS;
    char methods[2];

#ifdef _WIN32
    WSADATA wsaData;
    int wsaInit = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (wsaInit != 0) {
        ERROR(L_NOTICE, "WSAStartup failed: %d\n", wsaInit);
        exit(1);
    }
#endif

    s_socks_conf conf;
    s_socks_server_config config;
    conf.config.srv = &config;

    char versions[] = { SOCKS5_V,
                        SOCKS4_V
                      };

    config.allowed_version = versions;
    config.n_allowed_version = sizeof(versions);

    if ( globalArgsServer.fileauth[0] != 0 ) {
        methods[0] = 0x02;
        --config.n_allowed_version; /* Disable socks4 don't support auth */
    } else {
        methods[0] = 0x00;
    }


    config.allowed_method = methods;
    config.n_allowed_method = 1;
    config.check_auth = check_auth;

    /* Init client tab */
    for (nc = 0; nc < MAXCLI; nc++)
        init_client (&tc[nc], nc, M_SERVER, &conf);

    if(bindAddr[0] == 0)
        soc_ec = new_listen_socket (NULL, port, 0, &addrS);
    else
        soc_ec = new_listen_socket (bindAddr, port, 0, &addrS);

    if (soc_ec < 0) goto fin_serveur;


#ifndef _WIN32
    if ( globalArgsServer.daemon == 1 ) {
        TRACE(L_NOTICE, "server: mode daemon ...");
        if ( daemon(0, 0) != 0 ) {
            perror("daemon");
            exit(1);
        }
        writePID(PID_FILE);
    }

    bor_signal (SIGINT, capte_fin, SA_RESTART);

    /* Need in daemon to remove the PID file properly */
    bor_signal (SIGTERM, capte_fin, SA_RESTART);
    bor_signal (SIGPIPE, capte_sigpipe, SA_RESTART);
    /* TODO: Find a better way to exit the select and recall the init_select
     * SIGUSR1 is send by a thread to unblock the select */
    bor_signal (SIGUSR1, capte_usr1, SA_RESTART);
#endif

    while (boucle_princ) {
        init_select_server (soc_ec, tc, &maxfd, &set_read, &set_write);

        res = select (maxfd+1, &set_read, &set_write, NULL, NULL);

        if (res > 0) { /* Search eligible sockets */

            if (FD_ISSET (soc_ec, &set_read))
                new_connection (soc_ec, tc, ssl);

            for (nc = 0; nc < MAXCLI; nc++) {
                dispatch_server(&tc[nc], &set_read, &set_write);
            }

        } else if ( res == 0) {

        } else if (res < 0) {
            if (errno == EINTR) ; /* Received signal, it does nothing */
            else {
                perror ("select");
                goto fin_serveur;
            }
        }
    }

fin_serveur:
#ifdef HAVE_LIBSSL
    if (globalArgsServer.ssl == 1)
        ssl_cleaning();
#endif
    TRACE(L_NOTICE, "server: closing sockets ...");
    close_log();
    for (nc = 0; nc < MAXCLI; nc++) disconnection(&tc[nc]);
    if (soc_ec != -1) CLOSE_SOCKET(soc_ec);
    if ( globalArgsServer.daemon == 1 )	removePID(PID_FILE);

#ifdef _WIN32
        WSACleanup();
#endif
}
Example #14
0
/* Create a connection to a host trough a socks5
 * If success s contain a socket to server
 *
 * If listen port != 0, it means bind mode,
 * so host and port will be ignored
 *
 * Return:
 *  -1, connection error, unavailable socks server
 *   0, connection error, socks5 negociation
 *   1, connection establish, socket is set in struct s
 */
int new_socket_with_socks(s_socket *s,
		char *sockshost, int socksport,
		char *username, char *password,
		char *host, int port, int listen,
		int version, int ssl, int cmd)
{
	int maxfd = 0, res;
	fd_set set_read, set_write;

	s_socks_conf conf;
	s_socks_client_config config;
	conf.config.cli = &config;

	char method[] =  { 0x00, 0x02 };
	conf.config.cli->n_allowed_method = 2;
	conf.config.cli->allowed_method = method;

	/* If no username or password  we don't use auth */
	if ( username == NULL || password == NULL )
		--conf.config.cli->n_allowed_method;

	conf.config.cli->loop = 1;
	conf.config.cli->cmd = cmd;
	conf.config.cli->host = host;
	conf.config.cli->port = port;
	conf.config.cli->listen = listen;
	conf.config.cli->version = version;
	conf.config.cli->username = username;
	conf.config.cli->password = password;

	//memcpy(&conf.config, &config, sizeof(s_socks_serv_cli_config));

    s_client c;
	init_client (&c, 0, M_CLIENT, &conf);

	c.soc.soc = new_client_socket(sockshost, socksport,
		&c.soc.adrC, &c.soc.adrS);
	if ( c.soc.soc < 0 ){
		return -1;
	}

#ifdef HAVE_LIBSSL
	/* Init SSL here
	 */
	if (ssl == 1){
		TRACE(L_DEBUG, "client: socks5 enable ssl ...");
		c.soc.ssl = ssl_neogiciate_client(c.soc.soc);
		if ( c.soc.ssl == NULL ){
			ERROR(L_VERBOSE, "client: ssl error");
			return -3;
		}
		TRACE(L_DEBUG, "client: ssl ok.");
	}
#endif /* HAVE_LIBSSL */

    /* Select loop */
    while (conf.config.cli->loop  == 1 && c.soc.soc != -1) {

		FD_ZERO (&set_read);
		FD_ZERO (&set_write);
		maxfd = 0;

		/* Adds the socket in read fds */
		init_select_client(&c.soc, &c.socks, &c.buf, &maxfd, &set_read, &set_write);

		res = select (maxfd+1, &set_read, &set_write, NULL, NULL);
        if (res > 0) {
            dispatch_client(&c, &set_read, &set_write);
        } else if ( res == 0){

        }else if (res < 0) {
            if (errno == EINTR) ; /* Received signal, it does nothing */
            else { perror ("select"); disconnection(&c); return -1; }
        }
	}

	memcpy(s, &c.soc, sizeof(s_socket));
	return (c.soc.soc >= 0);
}
Example #15
0
int ClientNetwork::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QObject::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        switch (_id) {
        case 0: packetReceived(); break;
        case 1: packetCorrupted(); break;
        case 2: pingUpdated((*reinterpret_cast< quint32(*)>(_a[1]))); break;
        case 3: chatReceived((*reinterpret_cast< CLID(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2])),(*reinterpret_cast< QString(*)>(_a[3])),(*reinterpret_cast< ENUM_TYPE(*)>(_a[4]))); break;
        case 4: serverInformationsChanged((*reinterpret_cast< ServerInformations(*)>(_a[1]))); break;
        case 5: clientIDChanged((*reinterpret_cast< CLID(*)>(_a[1]))); break;
        case 6: nicknameChanged((*reinterpret_cast< CLID(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); break;
        case 7: error((*reinterpret_cast< ENUM_TYPE(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); break;
        case 8: clientVoted((*reinterpret_cast< CLID(*)>(_a[1])),(*reinterpret_cast< CLID(*)>(_a[2]))); break;
        case 9: newGameMaster((*reinterpret_cast< CLID(*)>(_a[1]))); break;
        case 10: serverName((*reinterpret_cast< QString(*)>(_a[1]))); break;
        case 11: motdChanged((*reinterpret_cast< QString(*)>(_a[1]))); break;
        case 12: gameLaunched(); break;
        case 13: narrationChanged((*reinterpret_cast< QString(*)>(_a[1]))); break;
        case 14: mapChanged((*reinterpret_cast< MapPtr(*)>(_a[1]))); break;
        case 15: mapFlare((*reinterpret_cast< QPoint(*)>(_a[1])),(*reinterpret_cast< CLID(*)>(_a[2]))); break;
        case 16: scriptToGMMsg((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); break;
        case 17: scriptToOwnerMsg((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); break;
        case 18: scriptActionMsg((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); break;
        case 19: scriptToPlayerMsg((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); break;
        case 20: scriptMsg((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); break;
        case 21: scriptError((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); break;
        case 22: updateEntities((*reinterpret_cast< const QList<EntityInformations>(*)>(_a[1]))); break;
        case 23: updateEntity((*reinterpret_cast< const EntityInformations(*)>(_a[1]))); break;
        case 24: entityDeleted((*reinterpret_cast< const QString(*)>(_a[1]))); break;
        case 25: scriptReceived((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); break;
        case 26: ressourcesUpdated((*reinterpret_cast< const QMap<QString,RSID>(*)>(_a[1]))); break;
        case 27: connectionEtablished(); break;
        case 28: connectionLost(); break;
        case 29: diceRolled((*reinterpret_cast< CLID(*)>(_a[1])),(*reinterpret_cast< quint16(*)>(_a[2])),(*reinterpret_cast< quint16(*)>(_a[3]))); break;
        case 30: sanctionned((*reinterpret_cast< CLID(*)>(_a[1])),(*reinterpret_cast< ENUM_TYPE(*)>(_a[2])),(*reinterpret_cast< QString(*)>(_a[3]))); break;
        case 31: clientJoined((*reinterpret_cast< CLID(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); break;
        case 32: clientLeft((*reinterpret_cast< CLID(*)>(_a[1]))); break;
        case 33: playSound((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); break;
        case 34: syncLibs((*reinterpret_cast< QList<SoundLibInformations>(*)>(_a[1]))); break;
        case 35: syncLanguagesList((*reinterpret_cast< QList<QPair<QString,QString> >(*)>(_a[1]))); break;
        case 36: syncDictionariesList((*reinterpret_cast< QStringList(*)>(_a[1]))); break;
        case 37: syncScriptList((*reinterpret_cast< QStringList(*)>(_a[1]))); break;
        case 38: updateCharacterList((*reinterpret_cast< const QStringList(*)>(_a[1]))); break;
        case 39: send((*reinterpret_cast< Packet*(*)>(_a[1])),(*reinterpret_cast< bool(*)>(_a[2]))); break;
        case 40: send((*reinterpret_cast< Packet*(*)>(_a[1]))); break;
        case 41: send((*reinterpret_cast< Packet(*)>(_a[1]))); break;
        case 42: send((*reinterpret_cast< qint32(*)>(_a[1])),(*reinterpret_cast< const QByteArray(*)>(_a[2]))); break;
        case 43: ping(); break;
        case 44: { qint32 _r = getPing();
            if (_a[0]) *reinterpret_cast< qint32*>(_a[0]) = _r; }  break;
        case 45: { QString _r = serverIP();
            if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; }  break;
        case 46: { quint16 _r = serverPort();
            if (_a[0]) *reinterpret_cast< quint16*>(_a[0]) = _r; }  break;
        case 47: flush(); break;
        case 48: connection(); break;
        case 49: disconnection(); break;
        case 50: { bool _r = setServer((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< quint16(*)>(_a[2])));
            if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; }  break;
        case 51: connected(); break;
        case 52: disconnected(); break;
        case 53: dataReceived(); break;
        case 54: socketError((*reinterpret_cast< QAbstractSocket::SocketError(*)>(_a[1]))); break;
        case 55: operatePacket((*reinterpret_cast< Packet*(*)>(_a[1]))); break;
        default: ;
        }
        _id -= 56;
    }
    return _id;
}
Example #16
0
void Player::disconnected()
{
    emit disconnection(id());
}