Exemplo n.º 1
0
int FSSftp::CheckSession( int* err, FSCInfo* info )
{

	if ( sshSession ) { return 0; }

	try
	{

		unsigned ip;
		int e;

		if ( !GetHostIp( unicode_to_utf8( _operParam.server.Data() ).data(), &ip, &e ) )
		{
			throw int( e );
		}

		_sock.Create();
		_sock.Connect( ntohl( ip ), _operParam.port );

		sshSession = libssh2_session_init();

		if ( !sshSession ) { throw int( SSH_INTERROR_X3 ); }

		libssh2_session_set_blocking( sshSession, 0 );

		WHILE_EAGAIN_( e, libssh2_session_handshake( sshSession, _sock.Id() ) );

		if ( e ) { throw int( e - 1000 ); }

		FSString userName = "";

		if ( _operParam.user.Data()[0] )
		{
			userName = _operParam.user.Data();
		}
		else
		{
#ifndef _WIN32
			char* ret = getenv( "LOGNAME" );

			if ( ret )
			{
				userName = FSString( sys_charset_id, ret );
				_operParam.user = userName.GetUnicode();

				MutexLock infoLock( &infoMutex );
				_infoParam.user = userName.GetUnicode();
			}

#endif
		};

		char* authList = 0;

		char* charUserName = ( char* )userName.Get( _operParam.charset );

		while ( true )
		{
			authList = libssh2_userauth_list( sshSession, charUserName, strlen( charUserName ) );

			if ( authList ) { break; }

			CheckSessionEagain();
			WaitSocket( info );
		}


		//publickey,password,keyboard-interactive
		static const char passId[] = "password";
		static const char kInterId[] = "keyboard-interactive";


		static unicode_t userSymbol = '@';

		while ( true )
		{
			if ( !strncmp( authList, passId, strlen( passId ) ) )
			{
				FSPromptData data;
				data.visible = false;
				data.prompt = utf8_to_unicode( "Password:"******"SFTP_" ).data(),
				        carray_cat<unicode_t>( userName.GetUnicode(), &userSymbol, _operParam.server.Data() ).data(),
				        &data, 1 ) ) { throw int( SSH_INTERROR_STOPPED ); }

				int ret;
				WHILE_EAGAIN_( ret, libssh2_userauth_password( sshSession,
				                                               ( char* )FSString( _operParam.user.Data() ).Get( _operParam.charset ),
				                                               ( char* )FSString( data.prompt.Data() ).Get( _operParam.charset ) ) );

				if ( ret ) { throw int( ret - 1000 ); }

				break; //!!!
			}
			else if ( !strncmp( authList, kInterId, strlen( kInterId ) ) )
			{
				MutexLock lock( &kbdIntMutex );
				kbdIntInfo = info;
				kbdIntParam = &_operParam;

				int ret;
				WHILE_EAGAIN_( ret,
				               libssh2_userauth_keyboard_interactive( sshSession,
				                                                      ( char* )FSString( _operParam.user.Data() ).Get( _operParam.charset ),
				                                                      KbIntCallback )
				             );

				if ( ret ) { throw int( ret - 1000 ); }

				break; //!!!
			}

			char* s = authList;

			while ( *s && *s != ',' ) { s++; }

			if ( !*s ) { break; }

			authList = s + 1;
		};


		while ( true )
		{
			sftpSession = libssh2_sftp_init( sshSession );

			if ( sftpSession ) { break; }

			if ( !sftpSession )
			{
				int e = libssh2_session_last_errno( sshSession );

				if ( e != LIBSSH2_ERROR_EAGAIN ) { throw int( e - 1000 ); }
			}

			WaitSocket( info );
		}

		return 0;

	}
	catch ( int e )
	{
		if ( err ) { *err = e; }

		//if (sftpSession) ??? похоже закрытие сессии все решает
		if ( sshSession ) { libssh2_session_free( sshSession ); }

		sshSession = 0;
		sftpSession = 0;
		_sock.Close( false );
		return ( e == -2 ) ? -2 : -1;
	}

}
Exemplo n.º 2
0
bool Switch_Control::init(const char * ConfigFile)
{
	char ip[64];
	PropConfig cfg;
	if(cfg.init(ConfigFile)==false)  return false;

	SW_version = "V1.0.00.3";
	SW_netname=cfg.getValue("SWM.NETNAME");
	GetHostIp(SW_netname.c_str(),ip);
	SW_ip=ip;
	SW_listentport=cfg.getValue("SWM.LISTENTPORT");
    string SW_SeverList;
    SW_SeverList=cfg.getValue("SWM_SERVER.LIST");

	char SW_Net_ip[256]={0};
	strcpy(SW_Net_ip,SW_ip.c_str());
	
	ManagerFactory::instance()->Init(SW_Net_ip);

    //解析出所有的erver列表 格式:“ip:port,ip:port"
  	FindSeverInfo(SW_SeverList.c_str());

	swm_process_thread = 4;
	swm_process_thread = atoi(cfg.getValue("SWM.PROCESS_THREADS").c_str());
	log_file_path=cfg.getValue("SWM.LOG_FILE_PATH");
	log_file=cfg.getValue("SWM.LOG_FILE");
	log_level=atoi(cfg.getValue("SWM.LOG_SIZE").c_str());
	log_size=atoi(cfg.getValue("SWM.LOG_NUM").c_str());
	log_num=atoi(cfg.getValue("SWM.LOG_LEVEL").c_str());
	iAliveTickInterval = 0;
	iAliveTickInterval = atoi(cfg.getValue("SWM.ALIVE_INTERVAL").c_str());
	cout<<"ALIVE_INTERVAL ="<<iAliveTickInterval<<endl;
	iAliveTickInterval = iAliveTickInterval*1000;

	if(iAliveTickInterval < MinAliveTickInterval || iAliveTickInterval > MaxAliveTickInterval)
		iAliveTickInterval = MinAliveTickInterval*3;
	
	cout<<endl<<"load config is  ----------------------"<<endl;
	cout<<"SWM_Version ="<<SW_version<<endl;
	cout<<"SWM_ip ="<<SW_ip<<endl;
	cout<<"SWM_listentport ="<<SW_listentport<<endl;
	cout<<"SWM.PROCESS_THREADS ="<<swm_process_thread<<endl;
	cout<<"log_file_path ="<<log_file_path<<endl;
	cout<<"log_file ="<<log_file<<endl;
	cout<<"log_level ="<<log_level<<endl;
	cout<<"ALIVE_INTERVAL ="<<iAliveTickInterval<<endl;
	cout<<"load config over      ----------------------"<<endl;

	LogFactory::instance()->init(log_file_path.c_str(),
   					log_file.c_str(),
   					log_level,
   					LOGOUT_FILE,/*|LOGOUT_SCREEN,*/
   					log_size,//per 256 M
   					log_num);//10 files
	LogFactory::instance()->start();

	
	//init setting 
	LOG_INFO("INFO  - [SWM]:开始启动\n");
	LOG_INFO("INFO  - [SWM]:读取SWM配置文件\n");
	LOG_INFO_FORMAT("INFO  - [SWM]:程序版本[SWM.version] = %s\n",SW_version.c_str());
	LOG_INFO_FORMAT("INFO  - [SWM]:服务端IP[SWM.tcpserver.ip] = %s\n",SW_ip.c_str());
	LOG_INFO_FORMAT("INFO  - [SWM]:服务端口[SWM.tcpserver.port] = %s\n",SW_listentport.c_str());

	m_iMsiPort = atoi(SW_listentport.c_str());


	
//	Swm_Worker_Sever sever_Worker;
//	sever_Worker.setName("swm_sever");
//	if(!sever_Worker.open(m_iMsiPort,swm_process_thread))
//		return -1;
			LOG_INFO("INFO	- [SWM]: 启动成功 ........\n");



			//启动监听线程
		pthread_t tcp_recv_thread1;
		pthread_create(&tcp_recv_thread1, NULL, TCP_Accept_Control_thread, this);
		pthread_detach(tcp_recv_thread1);

		
		//启动监测列表线程
	pthread_t tcp_recv_thread4;
	pthread_create(&tcp_recv_thread4, NULL, ts_recv_Control_thread, this);
	pthread_detach(tcp_recv_thread4);


		//Sleep
	sleep(1);
	printf("---send reset msg\n");
	//发送重置命令
	ResetAllSwitch();

//	sever_Worker.wait();
	LogFactory::instance()->wait();

		printf("---send reset msg1\n");
/*
	m_pSwitchManager = new Switch_Manager;
	char SW_Net_ip[256]={0};
	strcpy(SW_Net_ip,SW_ip.c_str());
	m_pSwitchManager->Init(SW_Net_ip);
*/

	



	 return true;
}