Example #1
0
/* C_OpenSession()
 * opens a session between an application and a token in a particular slot.
 * slotID is the slot’s ID; flags indicates the type of session; pApplication is an 
 * application defined pointer to be passed to the notification callback; Notify is 
 * the address of the notification
 */
CK_DEFINE_FUNCTION(CK_RV, C_OpenSession)(
			CK_SLOT_ID            slotID,        /* the slot's ID */
			CK_FLAGS              flags,         /* from CK_SESSION_INFO */
			CK_VOID_PTR           pApplication,  /* passed to callback */
			CK_NOTIFY             Notify,        /* callback function */
			CK_SESSION_HANDLE_PTR phSession)     /* gets session handle */
{
	// Check that the library has been initialized
	if (!LibraryIsInitialized())
		return CKR_CRYPTOKI_NOT_INITIALIZED;

	// Check that the session handle pointer is not NULL
	if (phSession == NULL)
		return CKR_ARGUMENTS_BAD;

	// Check that the slotID is valid
	if (slotID != 0)
		return CKR_SLOT_ID_INVALID;

	// Check that CKF_SERIAL_SESSION flag is set (for legacy reasons)
	if ((flags & CKF_SERIAL_SESSION) == 0)
		return CKR_SESSION_PARALLEL_NOT_SUPPORTED;

	// Try to create a new session
	std::pair<SessionMap::iterator, bool> iNew = CreateNewSession();
	if (!iNew.second)
		return CKR_GENERAL_ERROR;

	// Set the session handle and notification callbacks
	*phSession = iNew.first->first;
	iNew.first->second.SetNotificationCallbacks(Notify, pApplication);

	return CKR_OK;
}
Example #2
0
void CPrivateSendServer::ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv, CConnman& connman)
{
    if(!fMasterNode) return;
    if(fLiteMode) return; // ignore all Monoeci related functionality
    if(!masternodeSync.IsBlockchainSynced()) return;

    if(strCommand == NetMsgType::DSACCEPT) {

        if(pfrom->nVersion < MIN_PRIVATESEND_PEER_PROTO_VERSION) {
            LogPrintf("DSACCEPT -- incompatible version! nVersion: %d\n", pfrom->nVersion);
            PushStatus(pfrom, STATUS_REJECTED, ERR_VERSION, connman);
            return;
        }

        if(IsSessionReady()) {
            // too many users in this session already, reject new ones
            LogPrintf("DSACCEPT -- queue is already full!\n");
            PushStatus(pfrom, STATUS_ACCEPTED, ERR_QUEUE_FULL, connman);
            return;
        }

        int nDenom;
        CTransaction txCollateral;
        vRecv >> nDenom >> txCollateral;

        LogPrint("privatesend", "DSACCEPT -- nDenom %d (%s)  txCollateral %s", nDenom, CPrivateSend::GetDenominationsToString(nDenom), txCollateral.ToString());

        masternode_info_t mnInfo;
        if(!mnodeman.GetMasternodeInfo(activeMasternode.outpoint, mnInfo)) {
            PushStatus(pfrom, STATUS_REJECTED, ERR_MN_LIST, connman);
            return;
        }

        if(vecSessionCollaterals.size() == 0 && mnInfo.nLastDsq != 0 &&
            mnInfo.nLastDsq + mnodeman.CountEnabled(MIN_PRIVATESEND_PEER_PROTO_VERSION)/5 > mnodeman.nDsqCount)
        {
            LogPrintf("DSACCEPT -- last dsq too recent, must wait: addr=%s\n", pfrom->addr.ToString());
            PushStatus(pfrom, STATUS_REJECTED, ERR_RECENT, connman);
            return;
        }

        PoolMessage nMessageID = MSG_NOERR;

        bool fResult = nSessionID == 0  ? CreateNewSession(nDenom, txCollateral, nMessageID, connman)
                                        : AddUserToExistingSession(nDenom, txCollateral, nMessageID);
        if(fResult) {
            LogPrintf("DSACCEPT -- is compatible, please submit!\n");
            PushStatus(pfrom, STATUS_ACCEPTED, nMessageID, connman);
            return;
        } else {
            LogPrintf("DSACCEPT -- not compatible with existing transactions!\n");
            PushStatus(pfrom, STATUS_REJECTED, nMessageID, connman);
            return;
        }

    } else if(strCommand == NetMsgType::DSQUEUE) {
Example #3
0
CNetCom* CNetMgr::AddSession( const GUID *guidId )
{_STT();
	// Acquire lock
	CTlLocalLock ll( *this );
	if ( !ll.IsLocked() ) return NULL;

	// Create new session
	CNetCom* pNc = CreateNewSession();
	if ( pNc == NULL ) return NULL;

	// Add to the list
	return AddSession( guidId, pNc );
}
Example #4
0
void SessionServer::OnStart(DWORD dwArgc, PWSTR *pszArgv)
{
    InitializeFileLogger();

    // Log out an obvious piece of text to help distinguish between server sessions.
    LogInfo("***********************************");
    LogInfo("****** Sharing Service OnStart ******");
    LogInfo("***********************************");

	// Log a service start message to the Application log.
	WriteEventLogEntry(L"Sharing Server starting", EVENTLOG_INFORMATION_TYPE);
	LogInfo("Server Info: \n\tBuild Version: %ls \n\tSchema Version: %i", XTOOLS_VERSION_STRING, kXToolsSchemaVersion);

	// TODO: use different machines, etc, based on command line parameters
	XT_UNREFERENCED_PARAM(dwArgc);
	XT_UNREFERENCED_PARAM(pszArgv);

	// Start listening for new connections
	m_listenerReceipt = m_socketMgr->AcceptConnections(kSessionServerPort, kSessionServerMaxConnections, this);
	LogInfo("Listening for session list connections on port %i of all network devices of the local machine.", kSessionServerPort);
	LogInfo("Local IP addresses are:");

	IPAddressList addressList = m_socketMgr->GetLocalMachineAddresses();
	for (size_t i = 0; i < addressList.size(); ++i)
	{
		LogInfo("\t%s", addressList[i].ToString().c_str());
	}
	
	// Allocate a pool of ports to use for sessions
	m_portPool = new PortPool(kSessionServerPort-1, 256);

	// TODO: Read from a configuration file for persistent sessions. 
	XTVERIFY(CreateNewSession("Default", SessionType::PERSISTENT) != NULL);

	// Start a thread to run the main service logic. 
	m_serverThread = new MemberFuncThread(&SessionServer::ServerThreadFunc, this);
}
Example #5
0
void SessionServer::OnNewSessionRequest(const NewSessionRequest& request, const NetworkConnectionPtr& connection)
{
	std::string name = request.GetSessionName();

	XSessionImplPtr session;
	std::string failureReason;

	// Cannot create a session with a name that is too short
	if (name.length() < kMinSessionNameLength)
	{
		failureReason = "Session name must have at least " + std::to_string(kMinSessionNameLength) + " letters";
	}

	// Cannot create a session with a name that is too long
	else if (name.length() > kMaxSessionNameLength)
	{
		failureReason = "Session name cannot be more than " + std::to_string(kMaxSessionNameLength) + " letters";
	}
	else
	{
		// Check to make sure that the requested session name is not already taken
		for (size_t i = 0; i < m_sessions.size(); ++i)
		{
			if (m_sessions[i]->GetName() == name)
			{
				failureReason = "A session with that name already exists";
				break;
			}
		}
	}

	if (failureReason.empty())
	{
		session = CreateNewSession(name, request.GetSessionType());
	}
	
	// If the session was successfully created...
	if (session)
	{
		// Report success.
		std::string address = m_socketMgr->GetLocalAddressForRemoteClient(connection->GetSocket());
		uint16 port = session->GetPort();

		NewSessionReply reply(
			session->GetId(), 
			session->GetType(),
			name, 
			address, 
			port);

		NetworkOutMessagePtr response = connection->CreateMessage(MessageID::SessionControl);

		response->Write(reply.ToJSONString());

		connection->Send(response);
	}
	else
	{
		// Report failure
		NewSessionReply reply(failureReason);

		NetworkOutMessagePtr response = connection->CreateMessage(MessageID::SessionControl);

		response->Write(reply.ToJSONString());

		connection->Send(response);
	}
}