예제 #1
0
    wiced_result_t accept(wiced_tcp_socket_t* socket) {
        wiced_result_t result;
        if ((result=wiced_tcp_accept(socket))==WICED_SUCCESS) {
            os_mutex_recursive_lock(accept_lock);

            int idx = index(socket);
            if (idx>=0) {
                clients[idx] = new tcp_server_client_t(this, socket);
                to_accept.append(idx);
            }
            os_mutex_recursive_unlock(accept_lock);
        }
        return result;
    }
예제 #2
0
void*
rwl_open_transport(int remote_type, char *port, int ReadTotalTimeout, int debug)
{
    /* not invoked for dongle transports */
    UNUSED_PARAMETER(remote_type);
    UNUSED_PARAMETER(ReadTotalTimeout);
    UNUSED_PARAMETER(debug);

#ifdef  RWL_SOCKET
    if (wiced_tcp_accept( &tcp_client_socket ) != WICED_SUCCESS)
    {
        return NULL;
    }
#endif

    return port;
}
// The nonsecure server thread
static void tcp_server_nonsecure_thread_main(wiced_thread_arg_t arg)
{
    wiced_result_t result;
    wiced_tcp_stream_t stream;                      // The TCP stream
    wiced_tcp_socket_t socket;
    uint8_t rbuffer[MAX_LEGAL_MSG];

    char returnMessage[128]; // better use less than 128 bytes
    // setup the server by creating the socket and hooking it to the correct TCP Port
    result = wiced_tcp_create_socket(&socket, INTERFACE);
    if(WICED_SUCCESS != result)
    {
        WPRINT_APP_INFO(("Create socket failed\n"));
        return; // this is a bad outcome
    }

    wiced_tcp_stream_init(&stream,&socket);
    if(WICED_SUCCESS != result)
    {
        WPRINT_APP_INFO(("Init stream failed\n"));
        return; // this is a bad outcome
    }

    result = wiced_tcp_listen( &socket, TCP_SERVER_NONSECURE_LISTEN_PORT );
    if(WICED_SUCCESS != result)
    {
        WPRINT_APP_INFO(("Listen socket failed\n"));
        return;
    }


    while (1 )
    {

        result = wiced_tcp_accept( &socket ); // this halts the thread until there is a connection

        if(result != WICED_SUCCESS) // this occurs if the accept times out
            continue;

        nonsecureConnectionCount += 1;

        /// Figure out which client is talking to us... and on which port
        wiced_ip_address_t peerAddress;
        uint16_t	peerPort;
        wiced_tcp_server_peer(&socket,&peerAddress,&peerPort);

        uint32_t dataReadCount;
        wiced_tcp_stream_read_with_count(&stream,&rbuffer,MAX_LEGAL_MSG,100,&dataReadCount); // timeout in 100 ms

        processClientCommand(rbuffer, dataReadCount ,returnMessage);

        displayResult(peerAddress,peerPort,returnMessage);


        // send response and close things up
        wiced_tcp_stream_write(&stream,returnMessage,strlen(returnMessage));
        wiced_tcp_stream_flush(&stream);
        wiced_tcp_disconnect(&socket); // disconnect the connection

        wiced_tcp_stream_deinit(&stream); // clear the stream if any crap left
        wiced_tcp_stream_init(&stream,&socket); // setup for next connection

    }
}
예제 #4
0
static void tcp_server_thread_main(uint32_t arg)
{
    tcp_server_handle_t* server = (tcp_server_handle_t*) arg;

    while ( quit != WICED_TRUE )
    {
        wiced_packet_t* temp_packet = NULL;

        /* Wait for a connection */
        wiced_result_t result = wiced_tcp_accept( &server->socket );

#ifdef TCP_KEEPALIVE_ENABLED
        result = wiced_tcp_enable_keepalive(&server->socket, TCP_SERVER_KEEP_ALIVE_INTERVAL, TCP_SERVER_KEEP_ALIVE_PROBES, TCP_SERVER_KEEP_ALIVE_TIME );
        if( result != WICED_SUCCESS )
        {
            WPRINT_APP_INFO(("Keep alive initialization failed \n"));
        }
#endif /* TCP_KEEPALIVE_ENABLED */

        if ( result == WICED_SUCCESS )
        {
            /* Receive the query from the TCP client */
            if (wiced_tcp_receive( &server->socket, &temp_packet, WICED_WAIT_FOREVER ) == WICED_SUCCESS)
            {
                /* Process the client request */
                tcp_server_process( server, temp_packet );

                /* Delete the packet, we're done with it */
                wiced_packet_delete( temp_packet );

#ifdef TCP_KEEPALIVE_ENABLED
                WPRINT_APP_INFO(("Waiting for data on a socket\n"));
                /* Check keepalive: wait to see whether the keepalive protocol has commenced */
                /* This is achieved by waiting forever for a packet to be received on the TCP connection*/
                if (wiced_tcp_receive( &server->socket, &temp_packet, WICED_WAIT_FOREVER ) == WICED_SUCCESS)
                {
                    tcp_server_process( server, temp_packet );
                    /* Release the packet, we don't need it any more */
                    wiced_packet_delete( temp_packet );
                }
                else
                {
                    WPRINT_APP_INFO(("Connection has been dropped by networking stack\n\n"));
                }
#endif /* TCP_KEEPALIVE_ENABLED */

            }
            else
            {
                /* Send failed or connection has been lost, close the existing connection and */
                /* get ready to accept the next one */
                wiced_tcp_disconnect( &server->socket );
            }
        }
    }
    WPRINT_APP_INFO(("Disconnect\n"));

    wiced_tcp_disconnect( &server->socket );

    WICED_END_OF_CURRENT_THREAD( );
}
예제 #5
0
static void ota_server_thread_main(uint32_t arg)
{
    ota_server_t*          server          = (ota_server_t*) arg;
    uint32_t               total_body_size = 0;
    int                    i               = 0;

    server->reboot_required = WICED_FALSE;
    while ( server->quit != WICED_TRUE )
    {
        wiced_packet_t* temp_packet = NULL;

        /* Wait for a connection */
        wiced_result_t result = wiced_tcp_accept( &server->socket );
        if ( result == WICED_SUCCESS )
        {
            for(;;)
            {
                if ( wiced_tcp_receive( &server->socket, &temp_packet, WICED_WAIT_FOREVER ) == WICED_SUCCESS )
                {
                    char* request_string;
                    uint16_t request_length;
                    uint16_t available_data_length;

                    if( server->state == READING_HEADER )
                    {
                        uint8_t         temp   = 0;
                        wiced_result_t  result = WICED_ERROR;

                        if ( temp_packet == NULL )
                        {
                            goto disconnect;
                        }
                        init_request(server);

                        server->request.request_packets[server->request.current_packet_index] = temp_packet;


                        wiced_packet_get_data(temp_packet, 0, (uint8_t**)&request_string, &request_length, &available_data_length);

                        /* Check that this is a GET or POST request, abort everything else */
                        if ( ( strstr( request_string, "GET" ) == 0 ) && ( strstr( request_string, "POST") == 0 ) )
                        {
                            result = WICED_ERROR;
                            wiced_packet_delete(temp_packet);
                            goto disconnect;
                        }

                        /* Get header pointer and header size */
                        server->request.header_ptr = (uint8_t*)request_string;
                        server->request.header_size = ( (char*)strstr( (char*)request_string, crlfcrlf ) + strlen( crlfcrlf ) ) - (char*)request_string;
                        if( server->request.header_size == strlen( crlfcrlf ) )
                        {
                            goto disconnect;
                        }

                        /* Get content length */
                        server->request.content_length = 0;
                        if( strstr( request_string, "Content-Length") != NULL )
                        {
                            uint8_t* content_length_value = (uint8_t*)strstr( request_string, "Content-Length") + strlen("Content-Length:");
                            server->request.content_length = atoi((const char*)content_length_value);
                        }

                        temp = request_string[ server->request.header_size ];
                        request_string[ server->request.header_size ] ='\0';
                        request_string[ server->request.header_size ] = temp;

                        /* Get request type and the url */
                        result = get_http_request_type_and_url( (uint8_t*)request_string, request_length, &server->request);
                        if ( result == WICED_ERROR )
                        {
                           goto disconnect;
                        }

                        server->state = READING_BODY;
                    }

                    if( server->state == READING_BODY )
                    {
                        http_body_chunk_t*          current_body_chunk = &server->request.body_chunks[server->request.current_packet_index];

                        if( server->request.current_packet_index != 0 )
                        {
                            server->request.request_packets[server->request.current_packet_index] = temp_packet;
                        }

                        wiced_packet_get_data(temp_packet, 0, (uint8_t**)&request_string, &request_length, &available_data_length);

                        if( server->request.current_packet_index == 0 )
                        {
                            current_body_chunk->data = server->request.header_ptr + server->request.header_size;
                            current_body_chunk->size = ( request_string + request_length ) - (char*)current_body_chunk->data;
                        }
                        else
                        {
                            current_body_chunk->data = (uint8_t*)request_string;
                            current_body_chunk->size = request_length;
                        }
                        /* calculate total combined size of all body chunks which belongs to this message */
                        total_body_size = 0;

                        for( i = 0; i < ( server->request.current_packet_index + 1 ) ; i++ )
                        {
                            total_body_size+= server->request.body_chunks[i].size;
                        }

                        server->request.current_packet_index++;

                        /* Check whether the combined size of the previous chunks and the current one is equal to the content length received in the first packet */
                        if( total_body_size == server->request.content_length )
                        {
                            ota_server_process_request( server, &server->socket );
                            /* Delete all packets belonging to the message */
                            for( i = 0; i < server->request.current_packet_index; i++ )
                            {
                                wiced_packet_delete(server->request.request_packets[i]);
                            }
                            server->state = READING_HEADER;
                            break;
                        }
                    }
                }
                else
                {
                    goto disconnect;
                }
            }
        }
disconnect:
        wiced_tcp_disconnect( &server->socket );
    }

    wiced_tcp_delete_socket( &server->socket );
    if( server->reboot_required == WICED_TRUE )
    {
        /* Give some for the response to be sent properly */
        wiced_rtos_delay_milliseconds(2000);

        /* Perform a reboot!!! */
        wiced_framework_reboot();
    }
    WICED_END_OF_CURRENT_THREAD( );
}
static void tcp_server_thread_main(wiced_thread_arg_t arg)
{
    wiced_bool_t wwepSecurity = (wiced_bool_t)arg;

    wiced_result_t result;
    wiced_tcp_stream_t stream;                      // The TCP stream
    wiced_tcp_socket_t socket;
    platform_dct_security_t *dct_security;
    wiced_tls_identity_t tls_identity;
    wiced_tls_context_t tls_context;
    uint8_t rbuffer[MAX_LEGAL_MSG];

    char returnMessage[128]; // better use less than 128 bytes
    // setup the server by creating the socket and hooking it to the correct TCP Port
    result = wiced_tcp_create_socket(&socket, INTERFACE);
    if(WICED_SUCCESS != result)
    {
        WPRINT_APP_INFO(("Create socket failed\n"));
        return; // this is a bad outcome
    }

    if(wwepSecurity == WICED_TRUE)
    {
        WPRINT_APP_INFO(("Starting secure\n"));

    }
    else
    {
        WPRINT_APP_INFO(("Starting non-secure\n"));
    }

    result = wiced_tcp_listen( &socket, (wwepSecurity == WICED_TRUE)?TCP_SERVER_SECURE_LISTEN_PORT:TCP_SERVER_NONSECURE_LISTEN_PORT );
    if(WICED_SUCCESS != result)
    {
        WPRINT_APP_INFO(("Listen socket failed\n"));
        return;
    }

    if(wwepSecurity == WICED_TRUE)
    {
        /* Lock the DCT to allow us to access the certificate and key */
        WPRINT_APP_INFO(( "Read the certificate Key from DCT\n" ));
        result = wiced_dct_read_lock( (void**) &dct_security, WICED_FALSE, DCT_SECURITY_SECTION, 0, sizeof( *dct_security ) );
        if ( result != WICED_SUCCESS )
        {
            WPRINT_APP_INFO(("Unable to lock DCT to read certificate\n"));
            return;
        }

        /* Setup TLS identity */
        result = wiced_tls_init_identity( &tls_identity, dct_security->private_key, strlen( dct_security->private_key ), (uint8_t*) dct_security->certificate, strlen( dct_security->certificate ) );
        if ( result != WICED_SUCCESS )
        {
            WPRINT_APP_INFO(( "Unable to initialize TLS identity. Error = [%d]\n", result ));
            return;
        }

    }
    else
    {
        wiced_tcp_stream_init(&stream,&socket);
        if(WICED_SUCCESS != result)
        {
            WPRINT_APP_INFO(("Init stream failed\n"));
            return; // this is a bad outcome
        }
    }

    while (1 )
    {
        if(wwepSecurity == WICED_TRUE)
        {
            result = wiced_tls_init_context( &tls_context, &tls_identity, NULL );
            if(result != WICED_SUCCESS)
            {
                WPRINT_APP_INFO(("Init context failed %d",result));
                return;
            }

            result = wiced_tcp_enable_tls(&socket,&tls_context);

            if(result != WICED_SUCCESS)
            {
                WPRINT_APP_INFO(("Enabling TLS failed %d",result));
                return;
            }

            wiced_tcp_stream_init(&stream,&socket);
            if(WICED_SUCCESS != result)
            {
                WPRINT_APP_INFO(("Init stream failed\n"));
                return; // this is a bad outcome
            }
        }

        result = wiced_tcp_accept( &socket ); // this halts the thread until there is a connection

        if(result != WICED_SUCCESS) // this occurs if the accept times out
            continue;

        if(wwepSecurity == WICED_TRUE)
            secureConnectionCount += 1;
        else
            nonsecureConnectionCount += 1;

        /// Figure out which client is talking to us... and on which port
        wiced_ip_address_t peerAddress;
        uint16_t	peerPort;
        wiced_tcp_server_peer(&socket,&peerAddress,&peerPort);

        uint32_t dataReadCount;
        wiced_tcp_stream_read_with_count(&stream,&rbuffer,MAX_LEGAL_MSG,100,&dataReadCount); // timeout in 100 ms
        processClientCommand(rbuffer, dataReadCount ,returnMessage);

        displayResult(peerAddress,peerPort,returnMessage);


        // send response and close things up
        wiced_tcp_stream_write(&stream,returnMessage,strlen(returnMessage));
        wiced_tcp_stream_flush(&stream);
        wiced_tcp_disconnect(&socket); // disconnect the connection

        if(wwepSecurity == WICED_TRUE)
        {
            wiced_tls_deinit_context(&tls_context);
        }

        wiced_tcp_stream_deinit(&stream); // clear the stream if any crap left
        wiced_tcp_stream_init(&stream,&socket); // setup for next connection

    }
}