Пример #1
0
tls_result_t tls_host_create_buffer( ssl_context* ssl, uint8_t** buffer, uint16_t buffer_size )
{
    wiced_assert("", ssl->outgoing_packet == NULL);

    /* Round requested buffer size up to next 64 byte chunk (required if encryption is active) */
    buffer_size = (uint16_t) ROUND_UP(buffer_size, 64);

    /* Check if requested buffer fits within a single MTU */
    if (buffer_size < 1300) /* TODO: Fix this */
    {
        uint16_t actual_packet_size;
        if ( wiced_packet_create_tcp( ssl->send_context, buffer_size, (wiced_packet_t**) &ssl->outgoing_packet, buffer, &actual_packet_size ) != WICED_SUCCESS )
        {
            *buffer = NULL;
            return 1;
        }
    }
    else
    {
        /* Malloc space */
        *buffer = tls_host_malloc("tls", buffer_size);
        ssl->out_buffer_size = buffer_size;
    }

    return 0;
}
Пример #2
0
/*
 * Flush any data not yet written
 */
tls_result_t ssl_flush_output( ssl_context *ssl, uint8_t* buffer, uint32_t length )
{
    if ( ssl->transport_protocol == TLS_TCP_TRANSPORT )
    {
        if (ssl->outgoing_packet != NULL)
        {
            wiced_packet_set_data_end((wiced_packet_t*)ssl->outgoing_packet, buffer + length);
            tls_host_send_tcp_packet(ssl->send_context, ssl->outgoing_packet);
            ssl->outgoing_packet = NULL;
            ssl->out_buffer_size = 0;
        }
        else
        {
            uint16_t      actual_packet_size;
            tls_packet_t* temp_packet;
            uint8_t*      packet_buffer;
            uint8_t*      data = buffer;

            while (length != 0)
            {
                uint16_t amount_to_copy;
                if ( wiced_packet_create_tcp( ssl->send_context, (uint16_t)length, (wiced_packet_t**) &temp_packet, &packet_buffer, &actual_packet_size ) != WICED_SUCCESS )
                {
                    tls_host_free(buffer);
                    return 1;
                }
                if ( ssl->state == SSL_HANDSHAKE_OVER )
                {
                    /* this doesn't need the extra space for the encryption header that a normal TLS socket would use - remove it */
                    packet_buffer -= sizeof(tls_record_header_t);
                    wiced_packet_set_data_start((wiced_packet_t*) temp_packet, packet_buffer);
                }
                amount_to_copy = (uint16_t) MIN(length, actual_packet_size);
                packet_buffer = MEMCAT(packet_buffer, data, amount_to_copy);
                data   += amount_to_copy;
                length -= amount_to_copy;
                wiced_packet_set_data_end((wiced_packet_t*)temp_packet, packet_buffer );
                tls_host_send_tcp_packet(ssl->send_context, temp_packet);
            }

            tls_host_free(buffer);
        }
    }
#ifndef DISABLE_EAP_TLS
    else if ( ssl->transport_protocol == TLS_EAP_TRANSPORT )
    {
        supplicant_host_send_eap_tls_fragments( (supplicant_workspace_t*) ssl->send_context, buffer, length );
        ssl->out_buffer_size = 0;
        tls_host_free( buffer );
        buffer = NULL;
    }
#endif /* DISABLE_EAP_TLS */

    return TLS_SUCCESS;
}
Пример #3
0
wiced_result_t wiced_tcp_stream_write( wiced_tcp_stream_t* tcp_stream, const void* data, uint32_t data_length )
{
    wiced_assert("Bad args", tcp_stream != NULL);

    WICED_LINK_CHECK_TCP_SOCKET( tcp_stream->socket );

    while ( data_length != 0 )
    {
        uint16_t amount_to_write;

        /* Check if we don't have a packet */
        if ( tcp_stream->tx_packet == NULL )
        {
            wiced_result_t result;
            result = wiced_packet_create_tcp( tcp_stream->socket, (uint16_t) MIN( data_length, 0xffff ), &tcp_stream->tx_packet, &tcp_stream->tx_packet_data , &tcp_stream->tx_packet_space_available );
            if ( result != WICED_TCPIP_SUCCESS )
            {
                return result;
            }
        }

        /* Write data */
        amount_to_write = (uint16_t) MIN( data_length, tcp_stream->tx_packet_space_available );
        tcp_stream->tx_packet_data     = MEMCAT( tcp_stream->tx_packet_data, data, amount_to_write );

        /* Update variables */
        data_length                           = (uint16_t)(data_length - amount_to_write);
        tcp_stream->tx_packet_space_available = (uint16_t) ( tcp_stream->tx_packet_space_available - amount_to_write );
        data                                  = (void*)((uint32_t)data + amount_to_write);

        /* Check if the packet is full */
        if ( tcp_stream->tx_packet_space_available == 0 )
        {
            wiced_result_t result;

            /* Send the packet */
            wiced_packet_set_data_end( tcp_stream->tx_packet, (uint8_t*)tcp_stream->tx_packet_data );
            result = wiced_tcp_send_packet( tcp_stream->socket, tcp_stream->tx_packet );

            tcp_stream->tx_packet_data            = NULL;
            tcp_stream->tx_packet_space_available = 0;

            if ( result != WICED_TCPIP_SUCCESS )
            {
                wiced_packet_delete( tcp_stream->tx_packet );
                tcp_stream->tx_packet = NULL;
                return result;
            }

            tcp_stream->tx_packet = NULL;
        }
    }

    return WICED_TCPIP_SUCCESS;
}
Пример #4
0
int
rwl_write_eth_port(void* hndle, char* write_buf, unsigned long size, unsigned long *numwritten)
{
    uint16_t        available_data_length;
    wiced_packet_t* tx_packet;
    char*           tx_data;
    unsigned long temp_size = 0;
    unsigned long temp=0;

    if ( size == 0 )
     {
         return SUCCESS;
     }

    while (size > 0)
    {
    if (wiced_packet_create_tcp(&tcp_client_socket, 128, &tx_packet, (uint8_t **)&tx_data, &available_data_length) != WICED_SUCCESS)
      {
          WPRINT_APP_INFO(("TCP packet creation failed\n"));
          return WICED_ERROR;
      }
    if (size > available_data_length)
        {
        temp_size = available_data_length;
        }
    else
    {
        temp_size =size;
    }

       /*  Write the message into tx_data"*/
        memcpy(tx_data, (write_buf + temp), temp_size);

        wiced_packet_set_data_end( tx_packet, (uint8_t*)tx_data + temp_size );

        /* Send the TCP packet*/
        if ( wiced_tcp_send_packet( &tcp_client_socket, tx_packet ) != WICED_SUCCESS )
        {
            WPRINT_APP_INFO(("TCP packet send failed \n"));

             /*Delete packet, since the send failed*/
            wiced_packet_delete(tx_packet);
            return WICED_ERROR;
        }

        wiced_packet_delete(tx_packet);
        size = size - temp_size;
        temp = temp+temp_size;

     }

    return available_data_length;
}
Пример #5
0
static wiced_result_t tls_packetize_buffered_data( wiced_tls_context_t* context, wiced_packet_t** packet )
{
    uint8_t* data;
    uint16_t length;
    uint16_t available_length;
    uint16_t record_length;
    wiced_result_t result;
    uint32_t amount_to_copy;

    (void)available_length;

    record_length = (uint16_t)( htobe16(context->current_record->length) + sizeof(tls_record_header_t) );

    /* Get a buffer and fill with decrypted content */
    result = wiced_packet_create_tcp( context->send_context, (uint16_t) MIN(1024, record_length - context->defragmentation_buffer_bytes_processed), (wiced_packet_t**) packet, &data, &length );
    if ( result  != WICED_SUCCESS )
    {
        *packet = NULL;
        return result;
    }
    if ( context->state == SSL_HANDSHAKE_OVER )
    {
        /* this doesn't need the extra space for the encryption header that a normal TLS socket would use - remove it */
        data -= sizeof(tls_record_header_t);
        wiced_packet_set_data_start((wiced_packet_t*) *packet, data);
    }

    amount_to_copy = (uint32_t) MIN( length, record_length - context->defragmentation_buffer_bytes_processed );
    memcpy( data, &context->defragmentation_buffer[context->defragmentation_buffer_bytes_processed], amount_to_copy );
    wiced_packet_set_data_end( *packet, data + amount_to_copy );

    context->defragmentation_buffer_bytes_processed = (uint16_t) ( context->defragmentation_buffer_bytes_processed + amount_to_copy );

    /* Check if we've returned all the data to the user */
    if ( context->defragmentation_buffer_bytes_processed >= record_length )
    {
        tls_host_free_defragmentation_buffer(context->defragmentation_buffer);
        context->defragmentation_buffer = 0;
        context->current_record = NULL;
    }

    return WICED_SUCCESS;
}
Пример #6
0
static wiced_result_t tcp_server_process(  tcp_server_handle_t* server, wiced_packet_t* rx_packet )
{
    char*           request;
    uint16_t        request_length;
    uint16_t        available_data_length;
    wiced_packet_t* tx_packet;
    char*           tx_data;

    wiced_packet_get_data( rx_packet, 0, (uint8_t**) &request, &request_length, &available_data_length );

    /* Null terminate the received string */
    request[request_length] = '\x0';
    WPRINT_APP_INFO(("Received data: %s \n", request));

    /* Send echo back */
    if (wiced_packet_create_tcp(&server->socket, TCP_PACKET_MAX_DATA_LENGTH, &tx_packet, (uint8_t**)&tx_data, &available_data_length) != WICED_SUCCESS)
    {
        WPRINT_APP_INFO(("TCP packet creation failed\n"));
        return WICED_ERROR;
    }

    /* Write the message into tx_data"  */
    tx_data[request_length] = '\x0';
    memcpy(tx_data, request, request_length);

    /* Set the end of the data portion */
    wiced_packet_set_data_end(tx_packet, (uint8_t*)tx_data + request_length);

    /* Send the TCP packet */
    if (wiced_tcp_send_packet(&server->socket, tx_packet) != WICED_SUCCESS)
    {
        WPRINT_APP_INFO(("TCP packet send failed\n"));

        /* Delete packet, since the send failed */
        wiced_packet_delete(tx_packet);

        server->quit=WICED_TRUE;
        return WICED_ERROR;
    }
    WPRINT_APP_INFO(("Echo data: %s\n", tx_data));

    return WICED_SUCCESS;
}
Пример #7
0
wiced_result_t wiced_tcp_send_buffer( wiced_tcp_socket_t* socket, const void* buffer, uint16_t buffer_length )
{
    wiced_packet_t* packet = NULL;
    uint8_t* data_ptr = (uint8_t*)buffer;
    uint8_t* packet_data_ptr;
    uint16_t available_space;

    wiced_assert("Bad args", socket != NULL);

    WICED_LINK_CHECK_TCP_SOCKET( socket );

    /* Create a packet, copy the data and send it off */
    while ( buffer_length != 0 )
    {
        uint16_t data_to_write;
        wiced_result_t result = wiced_packet_create_tcp( socket, buffer_length, &packet, &packet_data_ptr, &available_space );
        if ( result != WICED_TCPIP_SUCCESS )
        {
            return result;
        }

        /* Write data */
        data_to_write   = MIN(buffer_length, available_space);
        packet_data_ptr = MEMCAT(packet_data_ptr, data_ptr, data_to_write);

        wiced_packet_set_data_end( packet, packet_data_ptr );

        /* Update variables */
        data_ptr       += data_to_write;
        buffer_length   = (uint16_t) ( buffer_length - data_to_write );
        available_space = (uint16_t) ( available_space - data_to_write );

        result = wiced_tcp_send_packet( socket, packet );
        if ( result != WICED_TCPIP_SUCCESS )
        {
            wiced_packet_delete( packet );
            return result;
        }
    }

    return WICED_TCPIP_SUCCESS;
}
Пример #8
0
tls_result_t tls_host_create_buffer( ssl_context* ssl, uint8_t** buffer, uint16_t buffer_size )
{
    wiced_assert("", ssl->outgoing_packet == NULL);

    /* Round requested buffer size up to next 64 byte chunk (required if encryption is active) */
    buffer_size = (uint16_t) ROUND_UP(buffer_size, 64);

    /* Check if requested buffer fits within a single MTU */
    if ( ( buffer_size < 1300) && ( ssl->transport_protocol == TLS_TCP_TRANSPORT ) ) /* TODO: Fix this */
    {
        uint16_t actual_packet_size;
        if ( wiced_packet_create_tcp( ssl->send_context, buffer_size, (wiced_packet_t**) &ssl->outgoing_packet, buffer, &actual_packet_size ) != WICED_SUCCESS )
        {
            *buffer = NULL;
            return 1;
        }
        if ( ssl->state == SSL_HANDSHAKE_OVER )
        {
            /* this doesn't need the extra space for the encryption header that a normal TLS socket would use - remove it */
            *buffer -= sizeof(tls_record_header_t);
            wiced_packet_set_data_start((wiced_packet_t*) ssl->outgoing_packet, *buffer);
        }
    }
    else
    {
        /* Requested size bigger than a MTU or TLS_EAP_TRANSPORT */
        /* Malloc space */
        *buffer = tls_host_malloc("tls", buffer_size);
        if ( *buffer == NULL )
        {
            return TLS_ERROR_OUT_OF_MEMORY;
        }
        memset( *buffer, 0, buffer_size );
        ssl->out_buffer_size = buffer_size;
    }

    return 0;
}
Пример #9
0
/*
 * Flush any data not yet written
 */
tls_result_t ssl_flush_output( ssl_context *ssl, uint8_t* buffer, uint32_t length )
{
    if (ssl->outgoing_packet != NULL)
    {
        wiced_packet_set_data_end((wiced_packet_t*)ssl->outgoing_packet, buffer + length);
        tls_host_send_packet(ssl->send_context, ssl->outgoing_packet);
        ssl->outgoing_packet = NULL;
        ssl->out_buffer_size = 0;
    }
    else
    {
        uint16_t      actual_packet_size;
        tls_packet_t* temp_packet;
        uint8_t*      packet_buffer;
        uint8_t*      data = buffer;

        while (length != 0)
        {
            uint16_t amount_to_copy;
            if ( wiced_packet_create_tcp( ssl->send_context, (uint16_t)length, (wiced_packet_t**) &temp_packet, &packet_buffer, &actual_packet_size ) != WICED_SUCCESS )
            {
                free(buffer);
                return 1;
            }
            amount_to_copy = (uint16_t) MIN(length, actual_packet_size);
            packet_buffer = MEMCAT(packet_buffer, data, amount_to_copy);
            data   += amount_to_copy;
            length -= amount_to_copy;
            wiced_packet_set_data_end((wiced_packet_t*)temp_packet, packet_buffer );
            tls_host_send_packet(ssl->send_context, temp_packet);
        }

        free(buffer);
    }

    return( 0 );
}
static wiced_result_t received_data_callback( wiced_tcp_socket_t* socket, void* arg )
{

	uint16_t        request_length;
	uint16_t        available_data_length;
	wiced_packet_t* temp_packet = NULL;

	if(wiced_tcp_receive( socket, &temp_packet, 500 ) == WICED_SUCCESS) // get the data from the client
	{
		char *rbuffer;
		dbEntry_t receive;
		char commandId;
		int err=1;
		char returnMessage[15];

		// get the pointer to the packet sent by the client and the data
		wiced_packet_get_data( temp_packet, 0, (uint8_t**) &rbuffer, &request_length, &available_data_length );
		sscanf(rbuffer,"%c%4x%2x%4x",&commandId,(unsigned int *)&receive.deviceId,(unsigned int *)&receive.regId,(unsigned int *)&receive.value);
		wiced_packet_delete( temp_packet ); // free the packet

		if(request_length >= 11 && request_length <= 13  ) //11 if no end 12 if CR 13 if CRLF
		{
			dbEntry_t *newDbEntry;
			switch(commandId)
			{
			case 'R': // they sent a Read command
				newDbEntry = dbFind(&receive); // look through the database to find a previous write of the deviceId/regId
				if(newDbEntry)
				{
					err=0;
					sprintf(returnMessage,"A%04X%02X%04X",(unsigned int)newDbEntry->deviceId,(unsigned int)newDbEntry->regId,(unsigned int)newDbEntry->value);
				}
				else
					err = 1;
				break;
			case 'W': // they sent a Write command
				sprintf(returnMessage,"A%04X%02X%04X",(unsigned int)receive.deviceId,(unsigned int)receive.regId,(unsigned int)receive.value);
				dbEntry_t *newDB;
				newDB = malloc(sizeof(dbEntry_t)); // make a new entry to put in the database
				memcpy(newDB,&receive,sizeof(dbEntry_t)); // copy the received data into the new entry
				dbSetValue(newDB); // save it.
				err = 0;
				break;
			default: // if they don't send a legal command then it is an error
				err = 1;
				break;
			}
		}

		// Print IP address of the client (peer) that sent the data and print to terminal
		uint32_t 		peerAddressV4;
		peerAddressV4 = (*socket).socket.nx_tcp_socket_connect_ip.nxd_ip_address.v4;
		WPRINT_APP_INFO(("%u.%u.%u.%u\t",
				(uint8_t)(peerAddressV4 >> 24),
				(uint8_t)(peerAddressV4 >> 16),
				(uint8_t)(peerAddressV4 >> 8),
				(uint8_t)(peerAddressV4 >> 0)));

		// Print the port that the peer connected from
		uint16_t	    peerPort;
		peerPort = (*socket).socket.nx_tcp_socket_connect_port;
		WPRINT_APP_INFO(("%d\t",peerPort));

		// Print the data that was sent by the client (peer)
		if(err)
		{
			strcpy(returnMessage,"X");
			WPRINT_APP_INFO(("X length=%d\n",available_data_length));
		}
		else
		{
			uint32_t count;
			linked_list_get_count(&db,&count);
			WPRINT_APP_INFO(("%c\t%4X\t%2X\t%4X\t%d\n",commandId,(unsigned int)receive.deviceId,(unsigned int)receive.regId,(unsigned int)receive.value,(int)count));
		}

		// send response packet
		wiced_packet_t* tx_packet;
		uint8_t *tx_data;
		wiced_packet_create_tcp(socket, strlen(returnMessage), &tx_packet, (uint8_t**)&tx_data, &available_data_length);
		memcpy(tx_data, returnMessage, strlen(returnMessage));
		wiced_packet_set_data_end(tx_packet, (uint8_t*)&tx_data[strlen(returnMessage)]);
		wiced_tcp_send_packet(socket, tx_packet);
		wiced_packet_delete(tx_packet);

		wiced_tcp_server_disconnect_socket(&tcp_server,socket);

	}