Esempio n. 1
0
WONStatus PeerAuthClient::HandleChallenge1(ReadBuffer &theChallenge, ByteBufferPtr &challenge2)
{
	unsigned short aSecretLenWithLen = theChallenge.ReadShort();
	ByteBufferPtr aSecretBuf = mPeerData->GetPrivateKey().Decrypt(theChallenge.ReadBytes(aSecretLenWithLen),aSecretLenWithLen);
	if(aSecretBuf.get()==NULL)
		return WS_PeerAuthClient_Challenge1DecryptFailure;
		
	unsigned short aSecretLen = aSecretBuf->data()[0] | (aSecretBuf->data()[1]<<8);
	if(aSecretLen>aSecretBuf->length()-2)
		return WS_PeerAuthClient_Challenge1InvalidSecretLen;
		
	mSecretA.Create(8);		
	if(!mSecretB.SetKey(aSecretBuf->data()+2, aSecretLen))
		return WS_PeerAuthClient_Challenge1InvalidSecretKey;

	unsigned short aCertLen = theChallenge.ReadShort();

	if(mUseAuth2)
	{
		mServerCertificate = new Auth2Certificate(theChallenge.ReadBytes(aCertLen),aCertLen);
		if(!mServerCertificate->IsValid())
			return WS_PeerAuthClient_Challenge1CertificateUnpackFailure;
	}
	else
	{
		mServerCertificate = new AuthCertificate(theChallenge.ReadBytes(aCertLen),aCertLen);
		if(!mServerCertificate->IsValid())
			return WS_PeerAuthClient_Challenge1CertificateUnpackFailure;
	}

	if(!mPeerData->Verify(mServerCertificate.get()))
		return WS_PeerAuthClient_Challenge1CertificateVerifyFailure;

	return GetChallenge2(challenge2);
}
Esempio n. 2
0
void ContextTransport::OnMessage(uint64_t nodeID, ReadBuffer msg)
{
    int         nread;
    char        proto;
    
    Log_Trace("%R", &msg);
    
    if (msg.GetLength() < 2)
        ASSERT_FAIL();

    nread = msg.Readf("%c:", &proto);
    if (nread < 2)
        ASSERT_FAIL();

    msg.Advance(2);
    
    switch (proto)
    {
        case PROTOCOL_CLUSTER:
            OnClusterMessage(nodeID, msg);
            break;
        case PROTOCOL_QUORUM:
            OnQuorumMessage(nodeID, msg);
            break;
        default:
            ASSERT_FAIL();
            break;
    }
}
Esempio n. 3
0
void HTTPSession::Print(const ReadBuffer& line)
{
    Buffer      header;
    ReadBuffer  tmp;
    
    if (!conn)
        return;

    if (!headerSent)
    {
        if (type == JSON)
            json.Start();
        else
            conn->WriteHeader(HTTP_STATUS_CODE_OK);

        headerSent = true;
    }
    
    if (type == JSON)
    {
        tmp = "response";
        json.PrintString(tmp);
        json.PrintColon();
        json.PrintString(line);
    }
    else
    {
        conn->Write(line.GetBuffer(), line.GetLength());
        conn->Print("\n");
    }
}
Esempio n. 4
0
 void staticReset(NTA_ReadBufferHandle handle)
 {
   NTA_CHECK(handle != NULL);
   
   ReadBuffer * rb = reinterpret_cast<ReadBuffer *>(handle);
   return rb->reset();
 }  
Esempio n. 5
0
void HTTPSession::SetType(Type type_)
{
    const char* mime;
    ReadBuffer  mimeType;
    
    type = type_;

    switch (type)
    {
    case PLAIN:
        mime = MIME_TYPE_TEXT_PLAIN;
        break;
    case HTML:
        mime = MIME_TYPE_TEXT_HTML;
        break;
    case JSON:
        mime = MIME_TYPE_APPLICATION_JSON;
        break;
    default:
        mime = MIME_TYPE_TEXT_PLAIN;
        break;
    }

    if (mimeType.GetLength() == 0)
        mimeType.Wrap(mime);
    
    conn->SetContentType(mimeType);
}
Esempio n. 6
0
void ConfigQuorumContext::OnMessage(ReadBuffer buffer)
{
    char proto;
    
    Log_Trace("%R", &buffer);

    if (buffer.GetLength() < 2)
        ASSERT_FAIL();

    proto = buffer.GetCharAt(0);
    ASSERT(buffer.GetCharAt(1) == ':');
    
    switch(proto)
    {
        case PAXOSLEASE_PROTOCOL_ID:        // 'L':
            OnPaxosLeaseMessage(buffer);
            break;
        case PAXOS_PROTOCOL_ID:             // 'P':
            OnPaxosMessage(buffer);
            break;
        case CATCHUP_PROTOCOL_ID:           // 'C'
            OnCatchupMessage(buffer);
            break;
        default:
            ASSERT_FAIL();
            break;
    }
}
WONStatus RoutingRegisterClientOp::HandleReply(unsigned char theMsgType, ReadBuffer &theMsg)
{
	WONStatus aStatus;

	if(theMsgType==RoutingGetClientListReply || theMsgType==RoutingGetClientListReplyEx)
	{
		if(!(mRegisterFlags&RoutingRegisterClientFlag_GetClientListEx)) // clientlistex includes clientlist ( == 0x14)
			return WS_RoutingOp_DontWantReply;

		RoutingGetClientListOpPtr anOp = new RoutingGetClientListOp(mConnection);
		aStatus = anOp->HandleReply(theMsgType,theMsg);
		mClientMap = anOp->GetClientMap();
		mReplyCount++;
	}
	else if(theMsgType==RoutingRegisterClientReply)
	{
		aStatus = (WONStatus)theMsg.ReadShort();
		if(aStatus==WS_Success)
		{
			mClientId = theMsg.ReadShort();
			theMsg.ReadWString(mClientName);
			mReconnectId = theMsg.ReadLong();
		}
		mReplyCount++;
	}
	else
		return WS_RoutingOp_DontWantReply;

	if(aStatus!=WS_Success || mReplyCount==mNumRepliesNeeded)
		return aStatus;
	else
		return WS_RoutingOp_NeedMoreReplies;
}
Esempio n. 8
0
 NTA_Size staticReadBufferGetSize(NTA_ReadBufferHandle handle)
 {
   NTA_CHECK(handle != NULL);
   
   ReadBuffer * rb = reinterpret_cast<ReadBuffer *>(handle);
   return rb->getSize();
 }
short LobbyGame::HandleJoinGameReply(ReadBuffer &theMsg)
{
	short aStatus = LobbyGameStatus_None;
	try
	{
		aStatus = theMsg.ReadShort();

		if(aStatus!=LobbyGameStatus_Success)
			return aStatus;

		unsigned short aNumPlayers = theMsg.ReadShort();
		for(int i=0; i<aNumPlayers; i++)
		{
			unsigned short aClientId = theMsg.ReadShort();
			LobbyClient *aClient = GetClient(aClientId);
			if(aClient!=NULL)
			{
				LobbyPlayerPtr aPlayer = LobbyPlayer::CreatePlayer();
				if(aPlayer->ReadData(theMsg))
					aClient->SetPlayer(aPlayer);
			}
		}

		HandleJoinGameReplyHook(theMsg);
	}
	catch(ReadBufferException&)
	{
		aStatus = LobbyGameStatus_UnpackFailure;
	}

	return aStatus;
}
Esempio n. 10
0
void
GLScreenBuffer::DeprecatedReadback(SharedSurface_GL* src, gfxImageSurface* dest)
{
    MOZ_ASSERT(src && dest);
    MOZ_ASSERT(ToIntSize(dest->GetSize()) == src->Size());
    MOZ_ASSERT(dest->Format() == (src->HasAlpha() ? gfxImageFormat::ARGB32
                                                  : gfxImageFormat::RGB24));

    mGL->MakeCurrent();

    bool needsSwap = src != SharedSurf();
    if (needsSwap) {
        SharedSurf()->UnlockProd();
        src->LockProd();
    }

    ReadBuffer* buffer = CreateRead(src);
    MOZ_ASSERT(buffer);

    ScopedBindFramebuffer autoFB(mGL, buffer->FB());
    ReadPixelsIntoImageSurface(mGL, dest);

    delete buffer;

    if (needsSwap) {
        src->UnlockProd();
        SharedSurf()->LockProd();
    }
}
Esempio n. 11
0
 const NTA_Byte * staticGetData(NTA_ReadBufferHandle handle)
 {
   NTA_CHECK(handle != NULL);
   
   ReadBuffer * rb = reinterpret_cast<ReadBuffer *>(handle);
   return rb->getData();
 }
Esempio n. 12
0
 NTA_Int32 staticReadReal64Array(NTA_ReadBufferHandle handle, NTA_Real64 * value, NTA_Size size)
 {
   if (!handle || !value || size <= 0)
     return -1;
   
   ReadBuffer * rb = reinterpret_cast<ReadBuffer *>(handle);
   return rb->read(value, size);
 }
Esempio n. 13
0
 NTA_Int32 staticReadByteArray(NTA_ReadBufferHandle handle, NTA_Byte * value, NTA_Size * size)
 {
   if (!handle || !value || !size || *size <= 0)
     return -1;
   
   ReadBuffer * rb = reinterpret_cast<ReadBuffer *>(handle);
   return rb->read(value, *size);
 }
Esempio n. 14
0
 static NTA_Int32 staticReadUInt32(NTA_ReadBufferHandle handle, NTA_UInt32 * value)
 {
   if (!handle || !value)
     return -1;
   
   ReadBuffer * rb = reinterpret_cast<ReadBuffer *>(handle);
   return rb->read(*value);
 }
Esempio n. 15
0
LimitReadBuffer::LimitReadBuffer(ReadBuffer & in, size_t limit, bool throw_exception, std::string exception_message)
    : ReadBuffer(in.position(), 0), in(in), limit(limit), throw_exception(throw_exception), exception_message(std::move(exception_message))
{
    size_t remaining_bytes_in_buffer = in.buffer().end() - in.position();
    if (remaining_bytes_in_buffer > limit)
        remaining_bytes_in_buffer = limit;

    working_buffer = Buffer(in.position(), in.position() + remaining_bytes_in_buffer);
}
/** Проверка на распространённый случай ошибки - использование Windows перевода строки.
  */
static void checkForCarriageReturn(ReadBuffer & istr)
{
	if (istr.position()[0] == '\r' || (istr.position() != istr.buffer().begin() && istr.position()[-1] == '\r'))
		throw Exception("\nYou have carriage return (\\r, 0x0D, ASCII 13) at end of first row."
			"\nIt's like your input data has DOS/Windows style line separators, that are illegal in TabSeparated format."
			" You must transform your file to Unix format."
			"\nBut if you really need carriage return at end of string value of last column, you need to escape it as \\r.",
			ErrorCodes::INCORRECT_DATA);
}
Esempio n. 17
0
void DataTypeNullable::deserializeTextEscaped(IColumn & column, ReadBuffer & istr) const
{
    /// Little tricky, because we cannot discriminate null from first character.

    if (istr.eof())
        throw Exception("Unexpected end of stream, while parsing value of Nullable type", ErrorCodes::CANNOT_READ_ALL_DATA);

    /// This is not null, surely.
    if (*istr.position() != '\\')
    {
        safeDeserialize(column,
            [] { return false; },
            [this, &istr] (IColumn & nested) { nested_data_type->deserializeTextEscaped(nested, istr); } );
    }
    else
    {
        /// Now we know, that data in buffer starts with backslash.
        ++istr.position();

        if (istr.eof())
            throw Exception("Unexpected end of stream, while parsing value of Nullable type, after backslash", ErrorCodes::CANNOT_READ_ALL_DATA);

        safeDeserialize(column,
            [&istr]
            {
                if (*istr.position() == 'N')
                {
                    ++istr.position();
                    return true;
                }
                return false;
            },
            [this, &istr] (IColumn & nested)
            {
                if (istr.position() != istr.buffer().begin())
                {
                    /// We could step back to consume backslash again.
                    --istr.position();
                    nested_data_type->deserializeTextEscaped(nested, istr);
                }
                else
                {
                    /// Otherwise, we need to place backslash back in front of istr.
                    ReadBufferFromMemory prefix("\\", 1);
                    ConcatReadBuffer prepended_istr(prefix, istr);

                    nested_data_type->deserializeTextEscaped(nested, prepended_istr);

                    /// Synchronise cursor position in original buffer.

                    if (prepended_istr.count() > 1)
                        istr.position() = prepended_istr.position();
                }
            });
    }
}
WONStatus RoutingClientLeftGroupOp::HandleReply(unsigned char theMsgType, ReadBuffer &theMsg)
{
	if(theMsgType!=RoutingClientLeftGroup)
		return WS_RoutingOp_DontWantReply;

	mGroupId = theMsg.ReadShort();
	mClientId = theMsg.ReadShort();

	return WS_Success;
}
WONStatus RoutingGroupNameChangedOp::HandleReply(unsigned char theMsgType, ReadBuffer &theMsg)
{
	if(theMsgType!=RoutingGroupNameChanged)
		return WS_RoutingOp_DontWantReply;

	mGroupId = theMsg.ReadShort();
	theMsg.ReadWString(mNewGroupName);

	return WS_Success;
}
Esempio n. 20
0
ssize_t RtpsDev::read(struct file *filp, char *buffer, size_t buflen)
{
	int i, ret;
	uint16_t packet_len, payload_len;

	if (!_had_data) {
		return 0;
	}

	lock(Read);
	ret = _read_buffer->read(_fd);

	if (ret < 0) {
		goto end;
	}

	ret = 0;

	if (_read_buffer->buf_size < HEADER_SIZE) {
		goto end;        // starting ">>>" + topic + seq + lenhigh + lenlow + crchigh + crclow
	}

	// Search for a rtps packet on buffer to send it
	i = 0;

	while ((unsigned)i < (_read_buffer->buf_size - HEADER_SIZE) && (memcmp(_read_buffer->buffer + i, ">>>", 3) != 0)) {
		i++;
	}

	// We need at least the first six bytes to get packet len
	if ((unsigned)i >= _read_buffer->buf_size - HEADER_SIZE) {
		goto end;
	}

	payload_len = ((uint16_t)_read_buffer->buffer[i + 5] << 8) | _read_buffer->buffer[i + 6];
	packet_len = payload_len + HEADER_SIZE;

	// packet is bigger than what we've read, better luck next time
	if ((unsigned)i + packet_len > _read_buffer->buf_size) {
		goto end;
	}

	// buffer should be big enough to hold a rtps packet
	if (packet_len > buflen) {
		ret = -EMSGSIZE;
		goto end;
	}

	_read_buffer->move(buffer, i, packet_len);
	ret = packet_len;

end:
	unlock(Read);
	return ret;
}
WONStatus RoutingGroupJoinAttemptOp::HandleReply(unsigned char theMsgType, ReadBuffer &theMsg)
{
	if(theMsgType!=RoutingGroupJoinAttempt)
		return WS_RoutingOp_DontWantReply;

	mClientId = theMsg.ReadShort();
	mGroupId = theMsg.ReadShort();
	theMsg.ReadWString(mComment);
	mJoinGroupFlags = theMsg.ReadByte();

	return WS_Success;
}
Esempio n. 22
0
void StorageMemoKeyValue::Delete(ReadBuffer key_)
{
    if (buffer != NULL)
        free(buffer);
    
    keyLength = key_.GetLength();
    valueLength = DELETE_LENGTH_VALUE;

    buffer = (char*) malloc(GetLength());
    
    memcpy(buffer, key_.GetBuffer(), keyLength);
}
WONStatus RoutingGroupInvitationOp::HandleReply(unsigned char theMsgType, ReadBuffer &theMsg)
{
	if(theMsgType!=RoutingGroupInvitation)
		return WS_RoutingOp_DontWantReply;

	mGroupId = theMsg.ReadShort();
	mCaptainId = theMsg.ReadShort();
	mAmInvited = theMsg.ReadBool();
	theMsg.ReadWString(mComment);

	return WS_Success;
}
Esempio n. 24
0
void QuorumDatabase::SetUint64(const char* name, uint64_t u64)
{
    ReadBuffer  key(name);
    Buffer      tmp;
    ReadBuffer  value;
    int         ret;

    tmp.Writef("%U", u64);
    value.Wrap(tmp);

    ret = paxosShard->Set(key, value);
}
Esempio n. 25
0
void QuorumDatabase::SetAcceptedValue(uint64_t paxosID, ReadBuffer value)
{
    Buffer      key;
    ReadBuffer  rbKey;

    ASSERT(value.GetLength() > 0);

    key.Writef("accepted:%021U", paxosID);
    rbKey.Wrap(key);

    logShard->Set(rbKey, value);
}
WONStatus RoutingYouWereBannedOp::HandleReply(unsigned char theMsgType, ReadBuffer &theMsg)
{
	if(theMsgType!=RoutingYouWereBanned)
		return WS_RoutingOp_DontWantReply;

	mGroupId = theMsg.ReadShort();
	mAmBanned = theMsg.ReadBool();
	theMsg.ReadWString(mBanComment);
	mBanTime = theMsg.ReadLong();

	return WS_Success;
}
Esempio n. 27
0
void StorageMemoKeyValue::Set(ReadBuffer key_, ReadBuffer value_)
{
    if (buffer != NULL)
        free(buffer);
    
    keyLength = key_.GetLength();
    valueLength = value_.GetLength();

    buffer = (char*) malloc(GetLength());
    
    memcpy(buffer, key_.GetBuffer(), keyLength);
    memcpy(buffer + keyLength, value_.GetBuffer(), valueLength);
}
Esempio n. 28
0
void ConfigQuorumContext::OnAppend(uint64_t paxosID, Buffer& value, bool ownAppend)
{
    bool            ret;
    ReadBuffer      rbValue;
    ConfigMessage   message;

    nextValue.Clear();

    rbValue.Wrap(value);
    ret = message.Read(rbValue);
    ASSERT(ret);
    quorumProcessor->OnAppend(paxosID, message, ownAppend);
}
Esempio n. 29
0
 Int32 readT(T * value, Size size)  const
 {
   ReadBuffer * r = const_cast<ReadBuffer *>(this);
   try
   {
     for (Size i = 0; i < size; ++i)
       r->read(value[i]);
     return 0;
   }
   catch (...)
   {
     return -1;
   }
 }
Esempio n. 30
0
 NTA_Int32 staticReadString(NTA_ReadBufferHandle handle, 
     NTA_Byte ** value, 
     NTA_UInt32 * size,
     NTA_Byte *(*fAlloc)(NTA_UInt32),
     void (*fDealloc)(NTA_Byte *)
   )
 {
   if (!handle || !value) {
     return -1;
   }
   
   ReadBuffer * rb = reinterpret_cast<ReadBuffer *>(handle);
   return rb->readString(*value, *size, fAlloc, fDealloc);
 }