Example #1
0
/**
 * @brief Tries to read toRead bytes from the specified transport
 *
 * Try to read toRead bytes from the transport to the stream.
 * In case it was not possible to read toRead bytes 0 is returned. The stream is always advanced by the
 * number of bytes read.
 *
 * The function assumes that the stream has enough capacity to hold the data.
 *
 * @param[in] transport rdpTransport
 * @param[in] s wStream
 * @param[in] toRead number of bytes to read
 * @return < 0 on error; 0 if not enough data is available (non blocking mode); 1 toRead bytes read
 */
static int transport_read_layer_bytes(rdpTransport* transport, wStream* s, unsigned int toRead)
{
	int status;
	status = transport_read_layer(transport, Stream_Pointer(s), toRead);
	if (status <= 0)
		return status;

	Stream_Seek(s, status);
	return status == toRead ? 1 : 0;
}
Example #2
0
int transport_read(rdpTransport* transport, wStream* s)
{
	int status;
	int pduLength;
	int streamPosition;
	int transport_status;

	pduLength = 0;
	transport_status = 0;

	if (!transport)
		return -1;

	if (!s)
		return -1;

	/* first check if we have header */
	streamPosition = Stream_GetPosition(s);

	if (streamPosition < 4)
	{
		status = transport_read_layer(transport, Stream_Buffer(s) + streamPosition, 4 - streamPosition);

		if (status < 0)
			return status;

		transport_status += status;

		if ((status + streamPosition) < 4)
			return transport_status;

		streamPosition += status;
	}

	/* if header is present, read in exactly one PDU */
	if (s->buffer[0] == 0x03)
	{
		/* TPKT header */

		pduLength = (s->buffer[2] << 8) | s->buffer[3];
	}
	else if (s->buffer[0] == 0x30)
	{
		/* TSRequest (NLA) */

		if (s->buffer[1] & 0x80)
		{
			if ((s->buffer[1] & ~(0x80)) == 1)
			{
				pduLength = s->buffer[2];
				pduLength += 3;
			}
			else if ((s->buffer[1] & ~(0x80)) == 2)
			{
				pduLength = (s->buffer[2] << 8) | s->buffer[3];
				pduLength += 4;
			}
			else
			{
				fprintf(stderr, "Error reading TSRequest!\n");
			}
		}
		else
		{
			pduLength = s->buffer[1];
			pduLength += 2;
		}
	}
	else
	{
		/* Fast-Path Header */

		if (s->buffer[1] & 0x80)
			pduLength = ((s->buffer[1] & 0x7F) << 8) | s->buffer[2];
		else
			pduLength = s->buffer[1];
	}

	status = transport_read_layer(transport, Stream_Buffer(s) + streamPosition, pduLength - streamPosition);

	if (status < 0)
		return status;

	transport_status += status;

#ifdef WITH_DEBUG_TRANSPORT
	/* dump when whole PDU is read */
	if (streamPosition + status >= pduLength)
	{
		fprintf(stderr, "Local < Remote\n");
		winpr_HexDump(Stream_Buffer(s), pduLength);
	}
#endif

	if (streamPosition + status >= pduLength)
	{
		WLog_Packet(transport->log, WLOG_TRACE, Stream_Buffer(s), pduLength, WLOG_PACKET_INBOUND);
	}

	return transport_status;
}
Example #3
0
int transport_read(rdpTransport* transport, STREAM* s)
{
    int status;
    int pdu_bytes;
    int stream_bytes;
    int transport_status;

    transport_status = 0;

    /* first check if we have header */
    stream_bytes = stream_get_length(s);

    if (stream_bytes < 4)
    {
        status = transport_read_layer(transport, s->data + stream_bytes,
                                      4 - stream_bytes);

        if (status < 0)
            return status;

        transport_status += status;

        if ((status + stream_bytes) < 4)
            return transport_status;

        stream_bytes += status;
    }

    pdu_bytes = 0;
    /* if header is present, read in exactly one PDU */
    if (s->data[0] == 0x03)
    {
        /* TPKT header */
        pdu_bytes = (s->data[2] << 8) | s->data[3];
    }
    else if (s->data[0] == 0x30)
    {
        /* TSRequest (NLA) */
        if (s->data[1] & 0x80)
        {
            if ((s->data[1] & ~(0x80)) == 1)
            {
                pdu_bytes = s->data[2];
                pdu_bytes += 3;
            }
            else if ((s->data[1] & ~(0x80)) == 2)
            {
                pdu_bytes = (s->data[2] << 8) | s->data[3];
                pdu_bytes += 4;
            }
            else
            {
                printf("Error reading TSRequest!\n");
            }
        }
        else
        {
            pdu_bytes = s->data[1];
            pdu_bytes += 2;
        }
    }
    else
    {
        /* Fast-Path Header */
        if (s->data[1] & 0x80)
            pdu_bytes = ((s->data[1] & 0x7f) << 8) | s->data[2];
        else
            pdu_bytes = s->data[1];
    }

    status = transport_read_layer(transport, s->data + stream_bytes,
                                  pdu_bytes - stream_bytes);

    if (status < 0)
        return status;

    transport_status += status;

#ifdef WITH_DEBUG_TRANSPORT
    /* dump when whole PDU is read */
    if (stream_bytes + status >= pdu_bytes)
    {
        printf("Local < Remote\n");
        freerdp_hexdump(s->data, pdu_bytes);
    }
#endif

    return transport_status;
}
Example #4
0
int transport_read(rdpTransport* transport, wStream* s)
{
	int status;
	int pdu_bytes;
	int stream_bytes;
	int transport_status;

	pdu_bytes = 0;
	transport_status = 0;

	/* first check if we have header */
	stream_bytes = Stream_GetPosition(s);

	if (stream_bytes < 4)
	{
		status = transport_read_layer(transport, s->buffer + stream_bytes, 4 - stream_bytes);

		if (status < 0)
			return status;

		transport_status += status;

		if ((status + stream_bytes) < 4)
			return transport_status;

		stream_bytes += status;
	}

	/* if header is present, read in exactly one PDU */
	if (s->buffer[0] == 0x03)
	{
		/* TPKT header */

		pdu_bytes = (s->buffer[2] << 8) | s->buffer[3];
	}
	else if (s->buffer[0] == 0x30)
	{
		/* TSRequest (NLA) */

		if (s->buffer[1] & 0x80)
		{
			if ((s->buffer[1] & ~(0x80)) == 1)
			{
				pdu_bytes = s->buffer[2];
				pdu_bytes += 3;
			}
			else if ((s->buffer[1] & ~(0x80)) == 2)
			{
				pdu_bytes = (s->buffer[2] << 8) | s->buffer[3];
				pdu_bytes += 4;
			}
			else
			{
				fprintf(stderr, "Error reading TSRequest!\n");
			}
		}
		else
		{
			pdu_bytes = s->buffer[1];
			pdu_bytes += 2;
		}
	}
	else
	{
		/* Fast-Path Header */

		if (s->buffer[1] & 0x80)
			pdu_bytes = ((s->buffer[1] & 0x7f) << 8) | s->buffer[2];
		else
			pdu_bytes = s->buffer[1];
	}

	status = transport_read_layer(transport, s->buffer + stream_bytes, pdu_bytes - stream_bytes);

	if (status < 0)
		return status;

	transport_status += status;

#ifdef WITH_DEBUG_TRANSPORT
	/* dump when whole PDU is read */
	if (stream_bytes + status >= pdu_bytes)
	{
		fprintf(stderr, "Local < Remote\n");
		winpr_HexDump(s->buffer, pdu_bytes);
	}
#endif

	return transport_status;
}