Ejemplo n.º 1
0
void OpenSSLContext::sendPendingDataToApplication() {
	ByteArray data;
	data.resize(SSL_READ_BUFFERSIZE);
	int ret = SSL_read(handle_, data.getData(), data.getSize());
	while (ret > 0) {
		data.resize(ret);
		onDataForApplication(data);
		data.resize(SSL_READ_BUFFERSIZE);
		ret = SSL_read(handle_, data.getData(), data.getSize());
	}
	if (ret < 0 && SSL_get_error(handle_, ret) != SSL_ERROR_WANT_READ) {
		state_ = Error;
		onError();
	}
}
Ejemplo n.º 2
0
ByteArray OpenSSLContext::getFinishMessage() const {
	ByteArray data;
	data.resize(MAX_FINISHED_SIZE);
	size_t size = SSL_get_finished(handle_, data.getData(), data.getSize());
	data.resize(size);
	return data;
}
Ejemplo n.º 3
0
        void dataReceived(ASocket* socket, ByteArray& data, uint32 totalSize) {
			uint32 code;
			bool ok = T::getHeaderInfos(data, code);

			if (ok)
				_delegate->packetInProgress(code, (float32)data.getSize() / totalSize);
        }
Ejemplo n.º 4
0
void SDKControl::onSDKConfigGet(long long clientID, SDKConfigMessage* data)
{
	auto client(GameServer::getInstance()->getLogicServer()->getClient(clientID));
	if (!client)
	{
		Log::w("client is disconnect before sql execute");
		return;
	}

	SDKConfigMessage* replyData = data;
	if (!data)
	{
		replyData = new SDKConfigMessage;
	}
	ByteArray packet;
	MessageHead head;
	head.command = CMD_S2C_SDK_CONFIG;
	head.pack(packet);
	replyData->pack(packet);

	// rewrite packet size
	head.packSize = (unsigned short)packet.getSize();
	packet.position = 0;
	head.pack(packet);

	client->send(packet);
	client->flush();
	if (!data)
	{
		delete replyData;
	}
}
Ejemplo n.º 5
0
void OpenSSLContext::handleDataFromApplication(const ByteArray& data) {
	if (SSL_write(handle_, data.getData(), data.getSize()) >= 0) {
		sendPendingDataToNetwork();
	}
	else {
		state_ = Error;
		onError();
	}
}
Ejemplo n.º 6
0
//------------------------------------------------------------------------------
void Usb::interruptHandler(ByteArray& byteArray)
{
    int size = byteArray.getSize();
    
    for (int i = 0; i < size; i++)
    {
        myRxBuffer.write(byteArray[i]);
    }
}
Ejemplo n.º 7
0
void	  ByteArray::append(ByteArray const& cpy) {
  uint64 bufsize = cpy.getSize();
  uint64 pos = getSize();
  std::vector<char> buf = cpy.getBuffer();
  
  _buffer->resize(getSize() + bufsize);
  for (uint32 i = 0; i < bufsize; ++i)
    (*_buffer)[pos++] = buf[i];
}
Ejemplo n.º 8
0
bool	Bullet::init(IGame* game, ByteArray const& params, float32 xStart) {
    if ((_game = game) == NULL) {
        return (false);
    }
    
    std::stringstream	data(std::stringstream::binary | std::stringstream::in | std::stringstream::out);
	data.write(params.getData(), params.getSize());
    
    Vec2 size;
    Vec3 from;
    Vec2 to;
    char *spriteName;
    uint32 spriteSize;
    char frameIndex;
    
    _xStart = xStart;
    
    data.read(reinterpret_cast<char*>(&spriteSize), sizeof(spriteSize));
    
    spriteName = new char[spriteSize];
	data.read(spriteName, spriteSize*sizeof(*spriteName));
	std::string name = std::string(spriteName, spriteSize);
	delete []spriteName;
    
    data.read(reinterpret_cast<char*>(&frameIndex), sizeof(frameIndex));
    
    data.read(reinterpret_cast<char*>(&size), sizeof(size));
    data.read(reinterpret_cast<char*>(&from), sizeof(from));
    data.read(reinterpret_cast<char*>(&_direction), sizeof(_direction));
    _direction.normalize();
    data.read(reinterpret_cast<char*>(&_speed), sizeof(_speed));
    
    ISprite	*sprite = _game->getLevelSprite(name);
	if (sprite == NULL)
		return false;
	if ((_graphicElement = game->createGraphicElement()) == NULL)
		return false;
	if ((_physicElement = game->createPhysicElement()) == NULL)
		return false;
    
    
	_graphicElement->setPosition(from);
	_graphicElement->setSize(size);
	_graphicElement->setRotation(0);
	_graphicElement->setSprite(sprite);
	_graphicElement->setSpriteFrameIndex(frameIndex);
	_graphicElement->setType(IGraphicElement::Static);
	game->addGraphicElement(_graphicElement);
	_physicElement->setPosition(from);
	_physicElement->setSize(size);
	_physicElement->setRotation(0);
	_physicElement->setType(IPhysicElement::Static);
	game->addPhysicElement(_physicElement);
    
    return true;
}
Ejemplo n.º 9
0
bool		ByteArray::operator!=(ByteArray const& cmp) {
  uint64	bufsize = cmp.getSize();
  uint64	currentSize = getSize();
  if (bufsize != currentSize)
    return true;
  std::vector<char> buf = cmp.getBuffer();
  for (uint32 i = 0; i < bufsize; ++i)
	  if (buf[i] != _buffer->at(i))
	      return true;
  return false;
}
Ejemplo n.º 10
0
OpenSSLCertificate::OpenSSLCertificate(const ByteArray& der) {
#if OPENSSL_VERSION_NUMBER <= 0x009070cfL
	unsigned char* p = const_cast<unsigned char*>(der.getData());
#else
	const unsigned char* p = der.getData();
#endif
	cert = boost::shared_ptr<X509>(d2i_X509(NULL, &p, der.getSize()), X509_free);
	if (!cert) {
		SWIFT_LOG(warning) << "Error creating certificate from DER data" << std::endl;
	}
	parse();
}
Ejemplo n.º 11
0
ByteArray*  AUdpSocketIO::getData(void) {
  SCOPELOCK(this);

  if (!(this->_input.first.empty())) {
    ByteArray* datagram = this->_input.first.front();
    this->_input.first.pop();
    this->_input.second -= datagram->getSize();
    return datagram;
  } else {
    return nullptr;
  }
}
//------------------------------------------------------------------------------
bool ImuSetConfigResponseBinaryMessage::parseMessageData(const ByteArray& data)
{
    if (data.getSize() < 1)
    {
        return false;
    }

    ByteArrayParser byteArrayParser(data,
                                    ENDIAN_BIG,
                                    ByteArrayParser::PARSE_DIRECTION_FORWARD);

    return (byteArrayParser.parse(myMessage.error));
}
Ejemplo n.º 13
0
void OpenSSLContext::handleDataFromNetwork(const ByteArray& data) {
	BIO_write(readBIO_, data.getData(), data.getSize());
	switch (state_) {
		case Connecting:
			doConnect();
			break;
		case Connected:
			sendPendingDataToApplication();
			break;
		case Start: assert(false); break;
		case Error: /*assert(false);*/ break;
	}
}
Ejemplo n.º 14
0
void Graphic::Image::loadFromData(ByteArray const& data) {
    sf::Image image;

    if (!image.loadFromMemory((void*)data.getData(), data.getSize())) {
        throw new Graphic::Exception("Cannot load image from data");
    }
    _width = image.getSize().x;
    _height = image.getSize().y;
    uint32 dataSize = _width * _height * 4;
    if (_pixels)
        delete[] _pixels;
    _pixels = new uint8[dataSize];
    memcpy((void*)_pixels, image.getPixelsPtr(), dataSize);
}
Ejemplo n.º 15
0
//------------------------------------------------------------------------------
SerialPort::Error SerialPortWindows::driverTx(const ByteArray& byteArray)
{
	DWORD bytesWritten = 0;

	if (!WriteFile(mySerialHandle,
				   byteArray.getDataConst(),
				   byteArray.getSize(),
				   &bytesWritten,
				   NULL))
	{
		return Error(ERROR_CODE_TX_FAILED);
	}

	return Error(ERROR_CODE_NONE);
}
Ejemplo n.º 16
0
//------------------------------------------------------------------------------
SerialPort::Error SerialPortWindows::driverGetRxBytes(ByteArray& byteArray,
													  const unsigned int nBytes)
{
	DWORD bytesReceived = 0;

	// TODO Fix this so it's non-blocking (overlapped I/O?)...

	if (!SetCommMask(mySerialHandle, EV_RXCHAR))
	{
	    // Error?
	}

//	DWORD dwCommEvent;
//
//	if (!WaitCommEvent(mySerialHandle, &dwCommEvent, NULL))
//	{
//	    return Error(ERROR_CODE_RX_FAILED);
//	}

	if (nBytes == 0)
	{
//	    byteArray.setSize(driverRxBytesAvailable());

		if (!ReadFile(mySerialHandle,
					  byteArray.getData(),
					  byteArray.getSize(),
					  &bytesReceived,
					  NULL))
		{
			return Error(ERROR_CODE_RX_FAILED);
		}
	}
	else
	{
		if (!ReadFile(mySerialHandle,
					  byteArray.getData(),
					  nBytes,
					  &bytesReceived,
					  NULL))
		{
			return Error(ERROR_CODE_RX_FAILED);
		}
	}

	byteArray.setSize(bytesReceived);

	return Error(ERROR_CODE_NONE);
}
boost::optional<ByteArray> SCRAMSHA1ClientAuthenticator::getResponse() const {
	if (step == Initial) {
		return getGS2Header() + getInitialBareClientMessage();
	}
	else if (step == Proof) {
		ByteArray clientKey = HMACSHA1::getResult(saltedPassword, "Client Key");
		ByteArray storedKey = SHA1::getHash(clientKey);
		ByteArray clientSignature = HMACSHA1::getResult(storedKey, authMessage);
		ByteArray clientProof = clientKey;
		for (unsigned int i = 0; i < clientProof.getSize(); ++i) {
			clientProof[i] ^= clientSignature[i];
		}
		ByteArray result = getFinalMessageWithoutProof() + ",p=" + Base64::encode(clientProof);
		return result;
	}
	else {
		return boost::optional<ByteArray>();
	}
}
Ejemplo n.º 18
0
void ConsoleProfiler::onTimer1000()
{
	ServerSocket* logicServer = GameServer::getInstance()->getLogicServer();
	ServerSocket* consoleServer = GameServer::getInstance()->getConsoleServer();
	DBQueue* dbQueue = GameServer::getInstance()->getDBQueue();

	ByteArray packet;
	MessageHead head;
	head.command = CMD_CONSOLE_SUBSCRIBE;
	head.pack(packet);

	ConsoleSubscribeMessage msg;
	msg.memoryPeak = Profiler::getPeakRSS();
	msg.memoryUsed = Profiler::getCurrentRSS();
	msg.numDBRequests = dbQueue->getQueueLength();
	msg.numOnline = logicServer->numOnlines();
#ifdef _WIN32
	msg.ioDataPoolSize = logicServer->getIODataPoolSize();
	msg.ioDataPostedSize = logicServer->getIODataPostedSize();
#endif
	msg.pack(packet);
	head.packSize = packet.getSize();
	packet.position = 0;
	head.pack(packet);

	auto iter = allSubscribe.begin();
	while (iter != allSubscribe.end())
	{
		long long clientID = *iter;
		auto client = consoleServer->getClient(clientID);
		if (!client)
		{
			iter = allSubscribe.erase(iter);
		}
		else
		{
			client->send(packet);
			client->flush();
			++iter;
		}
	}
}
//------------------------------------------------------------------------------
bool ImuGetConfigResponseBinaryMessage::parseMessageData(const ByteArray& data)
{
    if (data.getSize() < 26)
    {
        return false;
    }

    ByteArrayParser byteArrayParser(data,
                                    ENDIAN_BIG,
                                    ByteArrayParser::PARSE_DIRECTION_FORWARD);

    return (byteArrayParser.parse(myMessage.index)                  &&
            byteArrayParser.parse(myMessage.measurementMode)        &&
            byteArrayParser.parse(myMessage.accelMeasurementRange)  &&
            byteArrayParser.parse(myMessage.accelMeasurementRateHz) &&
            byteArrayParser.parse(myMessage.gyroMeasurementRange)   &&
            byteArrayParser.parse(myMessage.gyroMeasurementRateHz)  &&
            byteArrayParser.parse(myMessage.magMeasurementRange)    &&
            byteArrayParser.parse(myMessage.magMeasurementRateHz));
}
LinkLocalServiceInfo LinkLocalServiceInfo::createFromTXTRecord(const ByteArray& record) {
    LinkLocalServiceInfo info;
    size_t i = 0;
    while (i < record.getSize()) {
        std::pair<std::string,std::string> entry = readEntry(record, &i);
        if (entry.first.empty()) {
            break;
        }
        else if (entry.first == "1st") {
            info.setFirstName(entry.second);
        }
        else if (entry.first == "last") {
            info.setLastName(entry.second);
        }
        else if (entry.first == "email") {
            info.setEMail(entry.second);
        }
        else if (entry.first == "jid") {
            info.setJID(JID(entry.second));
        }
        else if (entry.first == "msg") {
            info.setMessage(entry.second);
        }
        else if (entry.first == "nick") {
            info.setNick(entry.second);
        }
        else if (entry.first == "port.p2pj") {
            info.setPort(boost::lexical_cast<int>(entry.second));
        }
        else if (entry.first == "status") {
            if (entry.second == "away") {
                info.setStatus(Away);
            }
            else if (entry.second == "dnd") {
                info.setStatus(DND);
            }
        }
    }
    return info;
}
std::pair<std::string,std::string> LinkLocalServiceInfo::readEntry(const ByteArray& record, size_t* index) {
    size_t& i = *index;
    std::string key;
    std::string value;

    size_t entryEnd = i + 1 + record[i];
    ++i;
    bool inKey = true;
    while (i < entryEnd && i < record.getSize()) {
        if (inKey) {
            if (record[i] == '=') {
                inKey = false;
            }
            else {
                key += record[i];
            }
        }
        else {
            value += record[i];
        }
        ++i;
    }
    return std::make_pair(key, value);
}
Ejemplo n.º 22
0
void OpenSSLCertificate::parse() {
	if (!cert) {
		return;
	}

	// Subject name
	X509_NAME* subjectName = X509_get_subject_name(cert.get());
	if (subjectName) {
		// Subject name
		ByteArray subjectNameData;
		subjectNameData.resize(256);
		X509_NAME_oneline(X509_get_subject_name(cert.get()), reinterpret_cast<char*>(subjectNameData.getData()), subjectNameData.getSize());
		this->subjectName = std::string(reinterpret_cast<const char*>(subjectNameData.getData()));

		// Common name
		int cnLoc = X509_NAME_get_index_by_NID(subjectName, NID_commonName, -1);
		while (cnLoc != -1) {
			X509_NAME_ENTRY* cnEntry = X509_NAME_get_entry(subjectName, cnLoc);
			ASN1_STRING* cnData = X509_NAME_ENTRY_get_data(cnEntry);
			commonNames.push_back(ByteArray(cnData->data, cnData->length).toString());
			cnLoc = X509_NAME_get_index_by_NID(subjectName, NID_commonName, cnLoc);
		}
	}

	// subjectAltNames
	int subjectAltNameLoc = X509_get_ext_by_NID(cert.get(), NID_subject_alt_name, -1);
	if(subjectAltNameLoc != -1) {
		X509_EXTENSION* extension = X509_get_ext(cert.get(), subjectAltNameLoc);
		boost::shared_ptr<GENERAL_NAMES> generalNames(reinterpret_cast<GENERAL_NAMES*>(X509V3_EXT_d2i(extension)), GENERAL_NAMES_free);
		boost::shared_ptr<ASN1_OBJECT> xmppAddrObject(OBJ_txt2obj(ID_ON_XMPPADDR_OID, 1), ASN1_OBJECT_free);
		boost::shared_ptr<ASN1_OBJECT> dnsSRVObject(OBJ_txt2obj(ID_ON_DNSSRV_OID, 1), ASN1_OBJECT_free);
		for (int i = 0; i < sk_GENERAL_NAME_num(generalNames.get()); ++i) {
			GENERAL_NAME* generalName = sk_GENERAL_NAME_value(generalNames.get(), i);
			if (generalName->type == GEN_OTHERNAME) {
				OTHERNAME* otherName = generalName->d.otherName;
				if (OBJ_cmp(otherName->type_id, xmppAddrObject.get()) == 0) {
					// XmppAddr
					if (otherName->value->type != V_ASN1_UTF8STRING) {
						continue;
					}
					ASN1_UTF8STRING* xmppAddrValue = otherName->value->value.utf8string;
					addXMPPAddress(ByteArray(ASN1_STRING_data(xmppAddrValue), ASN1_STRING_length(xmppAddrValue)).toString());
				}
				else if (OBJ_cmp(otherName->type_id, dnsSRVObject.get()) == 0) {
					// SRVName
					if (otherName->value->type != V_ASN1_IA5STRING) {
						continue;
					}
					ASN1_IA5STRING* srvNameValue = otherName->value->value.ia5string;
					addSRVName(ByteArray(ASN1_STRING_data(srvNameValue), ASN1_STRING_length(srvNameValue)).toString());
				}
			}
			else if (generalName->type == GEN_DNS) {
				// DNSName
				addDNSName(ByteArray(ASN1_STRING_data(generalName->d.dNSName), ASN1_STRING_length(generalName->d.dNSName)).toString());
			}
		}
	}
}
Ejemplo n.º 23
0
//------------------------------------------------------------------------------
ComProtocol::ParseStatus ComProtocolNmea::driverParseData(
                                                   const ByteArray& rxByteArray,
                                                   ByteArray& txByteArray,
                                                   unsigned int& bytesConsumed)
{
	int startIndex = 0;
	int i;
	int nRxBytes = rxByteArray.getSize();

	ParseStatus parseStatus = PARSE_STATUS_MID_MESSAGE;
	bytesConsumed = 0;

	if (nRxBytes < startFlagSize)
	{
	    return PARSE_STATUS_MID_MESSAGE;
	}

	// Look through all incoming bytes for start flag
	while (startIndex < nRxBytes)
	{
		// Look for start flag
		for (i = startIndex; i < nRxBytes; i++)
		{
			if (memcmp(&(rxByteArray[i]), startFlag, startFlagSize) == 0)
			{
				break;
			}
		}

		// If true, we've searched beyond the end of the message
		if (i == nRxBytes)
		{
		    bytesConsumed = rxByteArray.getSize();

			return PARSE_STATUS_NOT_A_MESSAGE;
		}

		startIndex = i + startFlagSize;

		// Look for end flag
		for (i = startIndex; i < nRxBytes; i++)
		{
			if (memcmp(&(rxByteArray[i]), endFlag, endFlagSize) == 0)
			{
				break;
			}
		}

		// If true, we've searched beyond the end of the message
		if (i == nRxBytes)
		{
			return PARSE_STATUS_MID_MESSAGE;
		}

		int endIndex = i;

		Message message;

		// Look for message string
		for (i = 0; i < ARRAY_SIZE(messageStringMap); i++)
		{
			if (memcmp(&(rxByteArray[startIndex]),
			           messageStringMap[i],
			           strlen(messageStringMap[i])) == 0)
			{
				break;
			}
		}

		// Verify message string was found
		if (i == ARRAY_SIZE(messageStringMap))
		{
			parseStatus = PARSE_STATUS_INVALID_MESSAGE;

			bytesConsumed += endIndex - startIndex + endFlagSize + 1;
		}
		else
		{
			message = (Message) i;

			// Bytes are consumed whether message is supported or not
			bytesConsumed += endIndex - startIndex + 1;

			switch (message)
			{
			    case MESSAGE_GSA:
			    {
			        ByteArray messageByteArray =
                        rxByteArray.subArray(startIndex + 6,
                                             endIndex - startIndex - 6 + 1);
                    parseStatus = parseGsaMessage(messageByteArray);

                    break;
			    }
				case MESSAGE_RMC:
				{
                    ByteArray messageByteArray =
                        rxByteArray.subArray(startIndex + 6,
                                             endIndex - startIndex - 6 + 1);
                    parseStatus = parseRmcMessage(messageByteArray);

					break;
				}
				default:
				{
				    parseStatus = PARSE_STATUS_UNSUPPORTED_MESSAGE;

				    break;
				}
			}
		}

		startIndex = endIndex + endFlagSize + 1;
	}

	return parseStatus;
}
Ejemplo n.º 24
0
//------------------------------------------------------------------------------
Usb::Error UsbStm32f4xx::driverTx(const ByteArray& byteArray)
{
    VCP_DataTx(byteArray.getItems(), byteArray.getSize());

    return ERROR_NONE;
}