Exemplo n.º 1
0
void socketio_destroy(CONCRETE_IO_HANDLE socket_io)
{
    LIST_ITEM_HANDLE first_pending_io;

    if (socket_io != NULL)
    {
        SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io;
        /* we cannot do much if the close fails, so just ignore the result */
        (void)closesocket(socket_io_instance->socket);

        /* clear allpending IOs */

        while ((first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list)) != NULL)
        {
            PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io);
            if (pending_socket_io != NULL)
            {
                free(pending_socket_io->bytes);
                free(pending_socket_io);
            }

            singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io);
        }

        singlylinkedlist_destroy(socket_io_instance->pending_io_list);
        if (socket_io_instance->hostname != NULL)
        {
            free(socket_io_instance->hostname);
        }

        free(socket_io);
    }
}
Exemplo n.º 2
0
BROKER_RESULT Broker_RemoveModule(BROKER_HANDLE broker, const MODULE* module)
{
    /*Codes_SRS_BROKER_13_048: [If `broker` or `module` is NULL the function shall return BROKER_INVALIDARG.]*/
    BROKER_RESULT result;
    if (broker == NULL || module == NULL)
    {
        /*Codes_SRS_BROKER_13_053: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ]*/
        result = BROKER_INVALIDARG;
        LogError("invalid parameter (NULL).");
    }
    else
    {
        /*Codes_SRS_BROKER_13_088: [This function shall acquire the lock on BROKER_HANDLE_DATA::modules_lock.]*/
        BROKER_HANDLE_DATA* broker_data = (BROKER_HANDLE_DATA*)broker;
        if (Lock(broker_data->modules_lock) != LOCK_OK)
        {
            /*Codes_SRS_BROKER_13_053: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
            LogError("Lock on broker_data->modules_lock failed");
            result = BROKER_ERROR;
        }
        else
        {
            /*Codes_SRS_BROKER_13_049: [Broker_RemoveModule shall perform a linear search for module in BROKER_HANDLE_DATA::modules.]*/
            LIST_ITEM_HANDLE module_info_item = singlylinkedlist_find(broker_data->modules, find_module_predicate, module);

            if (module_info_item == NULL)
            {
                /*Codes_SRS_BROKER_13_050: [Broker_RemoveModule shall unlock BROKER_HANDLE_DATA::modules_lock and return BROKER_ERROR if the module is not found in BROKER_HANDLE_DATA::modules.]*/
                LogError("Supplied module is not attached to the broker");
                result = BROKER_ERROR;
            }
            else
            {
                BROKER_MODULEINFO* module_info = (BROKER_MODULEINFO*)singlylinkedlist_item_get_value(module_info_item);
                if (stop_module(broker_data->publish_socket, module_info) == 0)
                {
                    deinit_module(module_info);
                }
                else
                {
                    LogError("unable to stop module");
                }

                /*Codes_SRS_BROKER_13_052: [The function shall remove the module from BROKER_HANDLE_DATA::modules.]*/
                singlylinkedlist_remove(broker_data->modules, module_info_item);
                free(module_info);

                /*Codes_SRS_BROKER_13_053: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                result = BROKER_OK;
            }

            /*Codes_SRS_BROKER_13_054: [This function shall release the lock on BROKER_HANDLE_DATA::modules_lock.]*/
            Unlock(broker_data->modules_lock);
        }
    }

    return result;
}
Exemplo n.º 3
0
BROKER_RESULT Broker_AddModule(BROKER_HANDLE broker, const MODULE* module)
{
    BROKER_RESULT result;

    /*Codes_SRS_BROKER_99_013: [If `broker` or `module` is NULL the function shall return BROKER_INVALIDARG.]*/
    if (broker == NULL || module == NULL)
    {
        result = BROKER_INVALIDARG;
        LogError("invalid parameter (NULL).");
    }
    /*Codes_SRS_BROKER_99_014: [If `module_handle` or `module_apis` are `NULL` the function shall return `BROKER_INVALIDARG`.]*/
    else if (module->module_apis == NULL || module->module_handle == NULL)
    {
        result = BROKER_INVALIDARG;
        LogError("invalid parameter (NULL).");
    }
    else
    {
        BROKER_MODULEINFO* module_info = (BROKER_MODULEINFO*)malloc(sizeof(BROKER_MODULEINFO));
        if (module_info == NULL)
        {
            LogError("Allocate module info failed");
            result = BROKER_ERROR;
        }
        else
        {
            if (init_module(module_info, module) != BROKER_OK)
            {
                /*Codes_SRS_BROKER_13_047: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                LogError("start_module failed");
                free(module_info->module);
                free(module_info);
                result = BROKER_ERROR;
            }
            else
            {
                /*Codes_SRS_BROKER_13_039: [This function shall acquire the lock on BROKER_HANDLE_DATA::modules_lock.]*/
                BROKER_HANDLE_DATA* broker_data = (BROKER_HANDLE_DATA*)broker;
                if (Lock(broker_data->modules_lock) != LOCK_OK)
                {
                    /*Codes_SRS_BROKER_13_047: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                    LogError("Lock on broker_data->modules_lock failed");
                    deinit_module(module_info);
                    free(module_info);
                    result = BROKER_ERROR;
                }
                else
                {
                    /*Codes_SRS_BROKER_13_045: [Broker_AddModule shall append the new instance of BROKER_MODULEINFO to BROKER_HANDLE_DATA::modules.]*/
                    LIST_ITEM_HANDLE moduleListItem = singlylinkedlist_add(broker_data->modules, module_info);
                    if (moduleListItem == NULL)
                    {
                        /*Codes_SRS_BROKER_13_047: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                        LogError("singlylinkedlist_add failed");
                        deinit_module(module_info);
                        free(module_info);
                        result = BROKER_ERROR;
                    }
                    else
                    {
                        if (start_module(module_info, broker_data->url) != BROKER_OK)
                        {
                            LogError("start_module failed");
                            deinit_module(module_info);
                            singlylinkedlist_remove(broker_data->modules, moduleListItem);
                            free(module_info);
                            result = BROKER_ERROR;
                        }
                        else
                        {
                            /*Codes_SRS_BROKER_13_047: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                            result = BROKER_OK;
                        }
                    }

                    /*Codes_SRS_BROKER_13_046: [This function shall release the lock on BROKER_HANDLE_DATA::modules_lock.]*/
                    Unlock(broker_data->modules_lock);
                }
            }

        }
    }

    return result;
}
Exemplo n.º 4
0
void socketio_dowork(CONCRETE_IO_HANDLE socket_io)
{
    int send_result;
    if (socket_io != NULL)
    {
        SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io;
        if (socket_io_instance->io_state == IO_STATE_OPEN)
        {
            int received = 1;

            LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list);
            while (first_pending_io != NULL)
            {
                PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io);
                if (pending_socket_io == NULL)
                {
                    LogError("Failure: retrieving socket from list");
                    indicate_error(socket_io_instance);
                    break;
                }

                /* TODO: we need to do more than a cast here to be 100% clean
                The following bug was filed: [WarnL4] socketio_win32 does not account for already sent bytes and there is a truncation of size from size_t to int */
                send_result = send(socket_io_instance->socket, (const char*)pending_socket_io->bytes, (int)pending_socket_io->size, 0);
                if (send_result != (int)pending_socket_io->size)
                {
                    int last_error = WSAGetLastError();
                    if (last_error != WSAEWOULDBLOCK)
                    {
                        free(pending_socket_io->bytes);
                        free(pending_socket_io);
                        (void)singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io);
                    }
                    else
                    {
                        /* try again */
                    }
                }
                else
                {
                    if (pending_socket_io->on_send_complete != NULL)
                    {
                        pending_socket_io->on_send_complete(pending_socket_io->callback_context, IO_SEND_OK);
                    }

                    free(pending_socket_io->bytes);
                    free(pending_socket_io);
                    if (singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io) != 0)
                    {
                        LogError("Failure: removing socket from list");
                        indicate_error(socket_io_instance);
                    }
                }

                first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list);
            }

            while (received > 0)
            {
                unsigned char* recv_bytes = malloc(RECEIVE_BYTES_VALUE);
                if (recv_bytes == NULL)
                {
                    LogError("Socketio_Failure: NULL allocating input buffer.");
                    indicate_error(socket_io_instance);
                }
                else
                {
                    received = recv(socket_io_instance->socket, (char*)recv_bytes, RECEIVE_BYTES_VALUE, 0);
                    if (received > 0)
                    {
                        if (socket_io_instance->on_bytes_received != NULL)
                        {
                            /* explictly ignoring here the result of the callback */
                            (void)socket_io_instance->on_bytes_received(socket_io_instance->on_bytes_received_context, recv_bytes, received);
                        }
                    }
                    else
                    {
                        int last_error = WSAGetLastError();
                        if (last_error != WSAEWOULDBLOCK && last_error != ERROR_SUCCESS)
                        {
                            LogError("Socketio_Failure: Recieving data from endpoint: %d.", last_error);
                            indicate_error(socket_io_instance);
                        }
                    }
                    free(recv_bytes);
                }
            }
        }
    }
}
Exemplo n.º 5
0
void tls_server_io_schannel_destroy(CONCRETE_IO_HANDLE tls_io)
{
    if (tls_io != NULL)
    {
        LIST_ITEM_HANDLE first_pending_io;
        TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
        if (tls_io_instance->credential_handle_allocated)
        {
            (void)FreeCredentialHandle(&tls_io_instance->credential_handle);
            tls_io_instance->credential_handle_allocated = false;
        }

        if (tls_io_instance->received_bytes != NULL)
        {
            free(tls_io_instance->received_bytes);
        }

        if (tls_io_instance->x509_schannel_handle != NULL)
        {
            x509_schannel_destroy(tls_io_instance->x509_schannel_handle);
        }

        if (tls_io_instance->x509certificate != NULL)
        {
            free(tls_io_instance->x509certificate);
        }

        if (tls_io_instance->x509privatekey != NULL)
        {
            free(tls_io_instance->x509privatekey);
        }

        xio_destroy(tls_io_instance->socket_io);
        free(tls_io_instance->host_name);

        first_pending_io = singlylinkedlist_get_head_item(tls_io_instance->pending_io_list);
        while (first_pending_io != NULL)
        {
            PENDING_SEND* pending_send = (PENDING_SEND*)singlylinkedlist_item_get_value(first_pending_io);
            if (pending_send == NULL)
            {
                LogError("Failure: retrieving socket from list");
                indicate_error(tls_io_instance);
                break;
            }
            else
            {
                if (pending_send->on_send_complete != NULL)
                {
                    pending_send->on_send_complete(pending_send->on_send_complete_context, IO_SEND_CANCELLED);
                }

                if (singlylinkedlist_remove(tls_io_instance->pending_io_list, first_pending_io) != 0)
                {
                    LogError("Failure: removing pending IO from list");
                }
            }
        }

        if (tls_io_instance->cert_context != NULL)
        {
            if (!CertFreeCertificateContext(tls_io_instance->cert_context))
            {
                LogError("Failure freeing certificate context");
            }
        }

        singlylinkedlist_destroy(tls_io_instance->pending_io_list);
        free(tls_io);
    }
}