Beispiel #1
0
static void remove_pending_message_by_index(MESSAGE_SENDER_INSTANCE* message_sender_instance, size_t index)
{
	MESSAGE_WITH_CALLBACK** new_messages;

	if (message_sender_instance->messages[index]->message != NULL)
	{
		message_destroy(message_sender_instance->messages[index]->message);
		message_sender_instance->messages[index]->message = NULL;
	}

	amqpalloc_free(message_sender_instance->messages[index]);

	if (message_sender_instance->message_count - index > 1)
	{
		(void)memmove(&message_sender_instance->messages[index], &message_sender_instance->messages[index + 1], sizeof(MESSAGE_WITH_CALLBACK*) * (message_sender_instance->message_count - index - 1));
	}

	message_sender_instance->message_count--;

	if (message_sender_instance->message_count > 0)
	{
		new_messages = (MESSAGE_WITH_CALLBACK**)amqpalloc_realloc(message_sender_instance->messages, sizeof(MESSAGE_WITH_CALLBACK*) * (message_sender_instance->message_count));
		if (new_messages != NULL)
		{
			message_sender_instance->messages = new_messages;
		}
	}
	else
	{
		amqpalloc_free(message_sender_instance->messages);
		message_sender_instance->messages = NULL;
	}
}
Beispiel #2
0
void messagesender_destroy(MESSAGE_SENDER_HANDLE message_sender)
{
	if (message_sender != NULL)
	{
		MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
		size_t i;

		messagesender_close(message_sender_instance);

		for (i = 0; i < message_sender_instance->message_count; i++)
		{
			if (message_sender_instance->messages[i]->on_message_send_complete != NULL)
			{
				message_sender_instance->messages[i]->on_message_send_complete(message_sender_instance->messages[i]->context, MESSAGE_SEND_ERROR);
			}

			message_destroy(message_sender_instance->messages[i]->message);
			amqpalloc_free(message_sender_instance->messages[i]);
		}

		if (message_sender_instance->messages != NULL)
		{
			amqpalloc_free(message_sender_instance->messages);
		}

		amqpalloc_free(message_sender);
	}
}
static void remove_operation_message_by_index(AMQP_MANAGEMENT_INSTANCE* amqp_management_instance, size_t index)
{
	message_destroy(amqp_management_instance->operation_messages[index]->message);
	amqpalloc_free(amqp_management_instance->operation_messages[index]);

	if (amqp_management_instance->operation_message_count - index > 1)
	{
		memmove(&amqp_management_instance->operation_messages[index], &amqp_management_instance->operation_messages[index + 1], sizeof(OPERATION_MESSAGE_INSTANCE*));
	}

	if (amqp_management_instance->operation_message_count == 1)
	{
		amqpalloc_free(amqp_management_instance->operation_messages);
		amqp_management_instance->operation_messages = NULL;
	}
	else
	{
		OPERATION_MESSAGE_INSTANCE** new_operation_messages = (OPERATION_MESSAGE_INSTANCE**)amqpalloc_realloc(amqp_management_instance->operation_messages, sizeof(OPERATION_MESSAGE_INSTANCE*) * (amqp_management_instance->operation_message_count - 1));
		if (new_operation_messages != NULL)
		{
			amqp_management_instance->operation_messages = new_operation_messages;
		}
	}

	amqp_management_instance->operation_message_count--;
}
void amqpmanagement_destroy(AMQP_MANAGEMENT_HANDLE amqp_management)
{
	if (amqp_management != NULL)
	{
		(void)amqpmanagement_close(amqp_management);

		if (amqp_management->operation_message_count > 0)
		{
			size_t i;
			for (i = 0; i < amqp_management->operation_message_count; i++)
			{
				message_destroy(amqp_management->operation_messages[i]->message);
				amqpalloc_free(amqp_management->operation_messages[i]);
			}

			amqpalloc_free(amqp_management->operation_messages);
		}

		link_destroy(amqp_management->sender_link);
		link_destroy(amqp_management->receiver_link);
		messagesender_destroy(amqp_management->message_sender);
		messagereceiver_destroy(amqp_management->message_receiver);
		amqpalloc_free(amqp_management);
	}
}
Beispiel #5
0
void saslplain_destroy(CONCRETE_SASL_MECHANISM_HANDLE sasl_mechanism_concrete_handle)
{
	if (sasl_mechanism_concrete_handle != NULL)
	{
		/* Codes_SRS_SASL_PLAIN_01_005: [saslplain_destroy shall free all resources associated with the SASL mechanism.] */
		SASL_PLAIN_INSTANCE* sasl_plain_instance = (SASL_PLAIN_INSTANCE*)sasl_mechanism_concrete_handle;
		if (sasl_plain_instance->init_bytes != NULL)
		{
			amqpalloc_free(sasl_plain_instance->init_bytes);
		}

		amqpalloc_free(sasl_plain_instance);
	}
}
Beispiel #6
0
/* Codes_SRS_AMQP_FRAME_CODEC_01_011: [amqp_frame_codec_create shall create an instance of an amqp_frame_codec and return a non-NULL handle to it.] */
AMQP_FRAME_CODEC_HANDLE amqp_frame_codec_create(FRAME_CODEC_HANDLE frame_codec, AMQP_FRAME_RECEIVED_CALLBACK frame_received_callback,
        AMQP_EMPTY_FRAME_RECEIVED_CALLBACK empty_frame_received_callback, AMQP_FRAME_CODEC_ERROR_CALLBACK amqp_frame_codec_error_callback, void* callback_context)
{
    AMQP_FRAME_CODEC_INSTANCE* result;

    /* Codes_SRS_AMQP_FRAME_CODEC_01_012: [If any of the arguments frame_codec, frame_received_callback, amqp_frame_codec_error_callback or empty_frame_received_callback is NULL, amqp_frame_codec_create shall return NULL.] */
    if ((frame_codec == NULL) ||
            (frame_received_callback == NULL) ||
            (empty_frame_received_callback == NULL) ||
            (amqp_frame_codec_error_callback == NULL))
    {
        result = NULL;
    }
    else
    {
        result = (AMQP_FRAME_CODEC_INSTANCE*)amqpalloc_malloc(sizeof(AMQP_FRAME_CODEC_INSTANCE));
        /* Codes_SRS_AMQP_FRAME_CODEC_01_020: [If allocating memory for the new amqp_frame_codec fails, then amqp_frame_codec_create shall fail and return NULL.] */
        if (result != NULL)
        {
            result->frame_codec = frame_codec;
            result->frame_received_callback = frame_received_callback;
            result->empty_frame_received_callback = empty_frame_received_callback;
            result->error_callback = amqp_frame_codec_error_callback;
            result->callback_context = callback_context;
            result->decode_state = AMQP_FRAME_DECODE_FRAME;

            /* Codes_SRS_AMQP_FRAME_CODEC_01_018: [amqp_frame_codec_create shall create a decoder to be used for decoding AMQP values.] */
            result->decoder = amqpvalue_decoder_create(amqp_value_decoded, result);
            if (result->decoder == NULL)
            {
                /* Codes_SRS_AMQP_FRAME_CODEC_01_019: [If creating the decoder fails, amqp_frame_codec_create shall fail and return NULL.] */
                amqpalloc_free(result);
                result = NULL;
            }
            else
            {
                /* Codes_SRS_AMQP_FRAME_CODEC_01_013: [amqp_frame_codec_create shall subscribe for AMQP frames with the given frame_codec.] */
                if (frame_codec_subscribe(frame_codec, FRAME_TYPE_AMQP, frame_received, result) != 0)
                {
                    /* Codes_SRS_AMQP_FRAME_CODEC_01_014: [If subscribing for AMQP frames fails, amqp_frame_codec_create shall fail and return NULL.] */
                    amqpvalue_decoder_destroy(result->decoder);
                    amqpalloc_free(result);
                    result = NULL;
                }
            }
        }
    }

    return result;
}
Beispiel #7
0
SASL_FRAME_CODEC_HANDLE sasl_frame_codec_create(FRAME_CODEC_HANDLE frame_codec, ON_SASL_FRAME_RECEIVED on_sasl_frame_received, ON_SASL_FRAME_CODEC_ERROR on_sasl_frame_codec_error, void* callback_context)
{
	SASL_FRAME_CODEC_INSTANCE* result;

	/* Codes_SRS_SASL_FRAME_CODEC_01_019: [If any of the arguments frame_codec, on_sasl_frame_received or on_sasl_frame_codec_error is NULL, sasl_frame_codec_create shall return NULL.] */
	if ((frame_codec == NULL) ||
		(on_sasl_frame_received == NULL) ||
		(on_sasl_frame_codec_error == NULL))
	{
		result = NULL;
	}
	else
	{
		/* Codes_SRS_SASL_FRAME_CODEC_01_018: [sasl_frame_codec_create shall create an instance of an sasl_frame_codec and return a non-NULL handle to it.] */
		result = (SASL_FRAME_CODEC_INSTANCE*)amqpalloc_malloc(sizeof(SASL_FRAME_CODEC_INSTANCE));
		if (result != NULL)
		{
			result->frame_codec = frame_codec;
			result->on_sasl_frame_received = on_sasl_frame_received;
			result->on_sasl_frame_codec_error = on_sasl_frame_codec_error;
			result->callback_context = callback_context;
			result->decode_state = SASL_FRAME_DECODE_FRAME;

			/* Codes_SRS_SASL_FRAME_CODEC_01_022: [sasl_frame_codec_create shall create a decoder to be used for decoding SASL values.] */
			result->decoder = amqpvalue_decoder_create(amqp_value_decoded, result);
			if (result->decoder == NULL)
			{
				/* Codes_SRS_SASL_FRAME_CODEC_01_023: [If creating the decoder fails, sasl_frame_codec_create shall fail and return NULL.] */
				amqpalloc_free(result);
				result = NULL;
			}
			else
			{
				/* Codes_SRS_SASL_FRAME_CODEC_01_020: [sasl_frame_codec_create shall subscribe for SASL frames with the given frame_codec.] */
				/* Codes_SRS_SASL_FRAME_CODEC_01_001: [A SASL frame has a type code of 0x01.] */
				if (frame_codec_subscribe(frame_codec, FRAME_TYPE_SASL, frame_received, result) != 0)
				{
					/* Codes_SRS_SASL_FRAME_CODEC_01_021: [If subscribing for SASL frames fails, sasl_frame_codec_create shall fail and return NULL.] */
					amqpvalue_decoder_destroy(result->decoder);
					amqpalloc_free(result);
					result = NULL;
				}
			}
		}
	}

	return result;
}
Beispiel #8
0
void session_destroy_link_endpoint(LINK_ENDPOINT_HANDLE link_endpoint)
{
	/* Codes_SRS_SESSION_01_050: [If link_endpoint is NULL, session_destroy_link_endpoint shall do nothing.] */
	if (link_endpoint != NULL)
	{
		LINK_ENDPOINT_INSTANCE* endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint;
		SESSION_INSTANCE* session_instance = endpoint_instance->session;
		uint64_t i;

		/* Codes_SRS_SESSION_01_049: [session_destroy_link_endpoint shall free all resources associated with the endpoint.] */
		for (i = 0; i < session_instance->link_endpoint_count; i++)
		{
			if (session_instance->link_endpoints[i] == link_endpoint)
			{
				break;
			}
		}

		if (i < session_instance->link_endpoint_count)
		{
			LINK_ENDPOINT_INSTANCE** new_endpoints;

			(void)memmove(&session_instance->link_endpoints[i], &session_instance->link_endpoints[i + 1], (session_instance->link_endpoint_count - (uint32_t)i - 1) * sizeof(LINK_ENDPOINT_INSTANCE*));
			session_instance->link_endpoint_count--;

			if (session_instance->link_endpoint_count == 0)
			{
				amqpalloc_free(session_instance->link_endpoints);
				session_instance->link_endpoints = NULL;
			}
			else
			{
				new_endpoints = (LINK_ENDPOINT_INSTANCE**)amqpalloc_realloc(session_instance->link_endpoints, sizeof(LINK_ENDPOINT_INSTANCE*) * session_instance->link_endpoint_count);
				if (new_endpoints != NULL)
				{
					session_instance->link_endpoints = new_endpoints;
				}
			}
		}

		if (endpoint_instance->name != NULL)
		{
			amqpalloc_free(endpoint_instance->name);
		}

		amqpalloc_free(endpoint_instance);
	}
}
Beispiel #9
0
void tickcounter_destroy(TICK_COUNTER_HANDLE tick_counter)
{
	if (tick_counter != NULL)
	{
		amqpalloc_free(tick_counter);
	}
}
Beispiel #10
0
void saslmssbcbs_destroy(CONCRETE_SASL_MECHANISM_HANDLE sasl_mechanism_concrete_handle)
{
	if (sasl_mechanism_concrete_handle != NULL)
	{
		amqpalloc_free(sasl_mechanism_concrete_handle);
	}
}
void socketlistener_destroy(SOCKET_LISTENER_HANDLE socket_listener)
{
	if (socket_listener != NULL)
	{
		socketlistener_stop(socket_listener);
		amqpalloc_free(socket_listener);
	}
}
void headerdetectio_destroy(CONCRETE_IO_HANDLE header_detect_io)
{
	if (header_detect_io != NULL)
	{
		HEADER_DETECT_IO_INSTANCE* header_detect_io_instance = (HEADER_DETECT_IO_INSTANCE*)header_detect_io;
		(void)headerdetectio_close(header_detect_io, NULL, NULL);
		xio_destroy(header_detect_io_instance->underlying_io);
		amqpalloc_free(header_detect_io);
	}
}
Beispiel #13
0
void session_destroy(SESSION_HANDLE session)
{
	/* Codes_SRS_SESSION_01_036: [If session is NULL, session_destroy shall do nothing.] */
	if (session != NULL)
	{
		SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session;

		session_end(session, NULL, NULL);

		/* Codes_SRS_SESSION_01_034: [session_destroy shall free all resources allocated by session_create.] */
		/* Codes_SRS_SESSION_01_035: [The endpoint created in session_create shall be freed by calling connection_destroy_endpoint.] */
		connection_destroy_endpoint(session_instance->endpoint);
		if (session_instance->link_endpoints != NULL)
		{
			amqpalloc_free(session_instance->link_endpoints);
		}

		amqpalloc_free(session);
	}
}
Beispiel #14
0
SESSION_HANDLE session_create(CONNECTION_HANDLE connection, ON_LINK_ATTACHED on_link_attached, void* callback_context)
{
	SESSION_INSTANCE* result;

	if (connection == NULL)
	{
		/* Codes_SRS_SESSION_01_031: [If connection is NULL, session_create shall fail and return NULL.] */
		result = NULL;
	}
	else
	{
		/* Codes_SRS_SESSION_01_030: [session_create shall create a new session instance and return a non-NULL handle to it.] */
		result = amqpalloc_malloc(sizeof(SESSION_INSTANCE));
		/* Codes_SRS_SESSION_01_042: [If allocating memory for the session fails, session_create shall fail and return NULL.] */
		if (result != NULL)
		{
			result->connection = connection;
			result->link_endpoints = NULL;
			result->link_endpoint_count = 0;
			result->handle_max = 4294967295u;

			/* Codes_SRS_SESSION_01_057: [The delivery ids shall be assigned starting at 0.] */
			/* Codes_SRS_SESSION_01_017: [The nextoutgoing-id MAY be initialized to an arbitrary value ] */
			result->next_outgoing_id = 0;

            result->desired_incoming_window = 1;
            result->incoming_window = 1;
			result->outgoing_window = 1;
			result->handle_max = 4294967295u;
			result->remote_incoming_window = 0;
			result->remote_outgoing_window = 0;
			result->previous_session_state = SESSION_STATE_UNMAPPED;
			result->is_underlying_connection_open = 0;
			result->session_state = SESSION_STATE_UNMAPPED;
			result->on_link_attached = on_link_attached;
			result->on_link_attached_callback_context = callback_context;

			/* Codes_SRS_SESSION_01_032: [session_create shall create a new session endpoint by calling connection_create_endpoint.] */
			result->endpoint = connection_create_endpoint(connection);
			if (result->endpoint == NULL)
			{
				/* Codes_SRS_SESSION_01_033: [If connection_create_endpoint fails, session_create shall fail and return NULL.] */
				amqpalloc_free(result);
				result = NULL;
			}
			else
			{
				session_set_state(result, SESSION_STATE_UNMAPPED);
			}
		}
	}

	return result;
}
Beispiel #15
0
static void log_message_chunk(MESSAGE_SENDER_INSTANCE* message_sender_instance, const char* name, AMQP_VALUE value)
{
	if (message_sender_instance->logger_log != NULL)
	{
		char* value_as_string = NULL;
		LOG(message_sender_instance->logger_log, 0, "%s", name);
		LOG(message_sender_instance->logger_log, 0, "%s", (value_as_string = amqpvalue_to_string(value)));
		if (value_as_string != NULL)
		{
			amqpalloc_free(value_as_string);
		}
	}
}
Beispiel #16
0
void saslmechanism_destroy(SASL_MECHANISM_HANDLE sasl_mechanism)
{
	if (sasl_mechanism != NULL)
	{
		SASL_MECHANISM_INSTANCE* sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)sasl_mechanism;

		/* Codes_SRS_SASL_MECHANISM_01_008: [saslmechanism_destroy shall also call the concrete_sasl_mechanism_destroy function that is member of the sasl_mechanism_interface_description argument passed to saslmechanism_create, while passing as argument to concrete_sasl_mechanism_destroy the result of the underlying concrete SASL mechanism handle.] */
		sasl_mechanism_instance->sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy(sasl_mechanism_instance->concrete_sasl_mechanism_handle);

		/* Codes_SRS_SASL_MECHANISM_01_007: [saslmechanism_destroy shall free all resources associated with the SASL mechanism handle.] */
		amqpalloc_free(sasl_mechanism_instance);
	}
}
Beispiel #17
0
void amqp_frame_codec_destroy(AMQP_FRAME_CODEC_HANDLE amqp_frame_codec)
{
    if (amqp_frame_codec != NULL)
    {
        /* Codes_SRS_AMQP_FRAME_CODEC_01_017: [amqp_frame_codec_destroy shall unsubscribe from receiving AMQP frames from the frame_codec that was passed to amqp_frame_codec_create.] */
        (void)frame_codec_unsubscribe(amqp_frame_codec->frame_codec, FRAME_TYPE_AMQP);

        /* Codes_SRS_AMQP_FRAME_CODEC_01_021: [The decoder created in amqp_frame_codec_create shall be destroyed by amqp_frame_codec_destroy.] */
        amqpvalue_decoder_destroy(amqp_frame_codec->decoder);

        /* Codes_SRS_AMQP_FRAME_CODEC_01_015: [amqp_frame_codec_destroy shall free all resources associated with the amqp_frame_codec instance.] */
        amqpalloc_free(amqp_frame_codec);
    }
}
Beispiel #18
0
void saslclientio_destroy(CONCRETE_IO_HANDLE sasl_client_io)
{
	if (sasl_client_io != NULL)
	{
		SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io;

		/* Codes_SRS_SASLCLIENTIO_01_007: [saslclientio_destroy shall free all resources associated with the SASL client IO handle.] */
		/* Codes_SRS_SASLCLIENTIO_01_086: [saslclientio_destroy shall destroy the sasl_frame_codec created in saslclientio_create by calling sasl_frame_codec_destroy.] */
		sasl_frame_codec_destroy(sasl_client_io_instance->sasl_frame_codec);

		/* Codes_SRS_SASLCLIENTIO_01_091: [saslclientio_destroy shall destroy the frame_codec created in saslclientio_create by calling frame_codec_destroy.] */
		frame_codec_destroy(sasl_client_io_instance->frame_codec);
		amqpalloc_free(sasl_client_io);
	}
}
Beispiel #19
0
void sasl_frame_codec_destroy(SASL_FRAME_CODEC_HANDLE sasl_frame_codec)
{
	/* Codes_SRS_SASL_FRAME_CODEC_01_026: [If sasl_frame_codec is NULL, sasl_frame_codec_destroy shall do nothing.] */
	if (sasl_frame_codec != NULL)
	{
		/* Codes_SRS_SASL_FRAME_CODEC_01_025: [sasl_frame_codec_destroy shall free all resources associated with the sasl_frame_codec instance.] */
		SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)sasl_frame_codec;

		/* Codes_SRS_SASL_FRAME_CODEC_01_027: [sasl_frame_codec_destroy shall unsubscribe from receiving SASL frames from the frame_codec that was passed to sasl_frame_codec_create.] */
		(void)frame_codec_unsubscribe(sasl_frame_codec_instance->frame_codec, FRAME_TYPE_SASL);

		/* Codes_SRS_SASL_FRAME_CODEC_01_028: [The decoder created in sasl_frame_codec_create shall be destroyed by sasl_frame_codec_destroy.] */
		amqpvalue_decoder_destroy(sasl_frame_codec_instance->decoder);
		amqpalloc_free(sasl_frame_codec_instance);
	}
}
Beispiel #20
0
static void log_outgoing_frame(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance, AMQP_VALUE performative)
{
	if (sasl_client_io_instance->logger_log != NULL)
	{
		AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(performative);
		if (descriptor != NULL)
		{
			LOG(sasl_client_io_instance->logger_log, 0, "-> ");
			LOG(sasl_client_io_instance->logger_log, 0, (char*)get_frame_type_as_string(descriptor));
			char* performative_as_string = NULL;
			LOG(sasl_client_io_instance->logger_log, LOG_LINE, (performative_as_string = amqpvalue_to_string(performative)));
			if (performative_as_string != NULL)
			{
				amqpalloc_free(performative_as_string);
			}
		}
	}
}
Beispiel #21
0
SASL_MECHANISM_HANDLE saslmechanism_create(const SASL_MECHANISM_INTERFACE_DESCRIPTION* sasl_mechanism_interface_description, void* sasl_mechanism_create_parameters)
{
	SASL_MECHANISM_INSTANCE* sasl_mechanism_instance;

	/* Codes_SRS_SASL_MECHANISM_01_004: [If the argument sasl_mechanism_interface_description is NULL, saslmechanism_create shall return NULL.] */
	if ((sasl_mechanism_interface_description == NULL) ||
		/* Codes_SRS_SASL_MECHANISM_01_005: [If any sasl_mechanism_interface_description member is NULL, sasl_mechanism_create shall fail and return NULL.] */
		(sasl_mechanism_interface_description->concrete_sasl_mechanism_create == NULL) ||
		(sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy == NULL) ||
		(sasl_mechanism_interface_description->concrete_sasl_mechanism_get_init_bytes == NULL) ||
		(sasl_mechanism_interface_description->concrete_sasl_mechanism_get_mechanism_name == NULL))
	{
		sasl_mechanism_instance = NULL;
	}
	else
	{
		sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)amqpalloc_malloc(sizeof(SASL_MECHANISM_INSTANCE));

		if (sasl_mechanism_instance != NULL)
		{
			sasl_mechanism_instance->sasl_mechanism_interface_description = sasl_mechanism_interface_description;

			/* Codes_SRS_SASL_MECHANISM_01_002: [In order to instantiate the concrete SASL mechanism implementation the function concrete_sasl_mechanism_create from the sasl_mechanism_interface_description shall be called, passing the sasl_mechanism_create_parameters to it.] */
			sasl_mechanism_instance->concrete_sasl_mechanism_handle = sasl_mechanism_instance->sasl_mechanism_interface_description->concrete_sasl_mechanism_create((void*)sasl_mechanism_create_parameters);
			if (sasl_mechanism_instance->concrete_sasl_mechanism_handle == NULL)
			{
				/* Codes_SRS_SASL_MECHANISM_01_003: [If the underlying concrete_sasl_mechanism_create call fails, saslmechanism_create shall return NULL.] */
				amqpalloc_free(sasl_mechanism_instance);
				sasl_mechanism_instance = NULL;
			}
		}
	}

	/* Codes_SRS_SASL_MECHANISM_01_001: [saslmechanism_create shall return on success a non-NULL handle to a new SASL mechanism interface.] */
	return (SASL_MECHANISM_HANDLE)sasl_mechanism_instance;
}
Beispiel #22
0
/* Codes_SRS_SESSION_01_051: [session_send_transfer shall send a transfer frame with the performative indicated in the transfer argument.] */
SESSION_SEND_TRANSFER_RESULT session_send_transfer(LINK_ENDPOINT_HANDLE link_endpoint, TRANSFER_HANDLE transfer, PAYLOAD* payloads, size_t payload_count, delivery_number* delivery_id, ON_SEND_COMPLETE on_send_complete, void* callback_context)
{
	SESSION_SEND_TRANSFER_RESULT result;

	/* Codes_SRS_SESSION_01_054: [If link_endpoint or transfer is NULL, session_send_transfer shall fail and return a non-zero value.] */
	if ((link_endpoint == NULL) ||
		(transfer == NULL))
	{
		result = SESSION_SEND_TRANSFER_ERROR;
	}
	else
	{
		LINK_ENDPOINT_INSTANCE* link_endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint;
		SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)link_endpoint_instance->session;

		/* Codes_SRS_SESSION_01_059: [When session_send_transfer is called while the session is not in the MAPPED state, session_send_transfer shall fail and return a non-zero value.] */
		if (session_instance->session_state != SESSION_STATE_MAPPED)
		{
			result = SESSION_SEND_TRANSFER_ERROR;
		}
		else
		{
			uint32_t payload_size = 0;
			size_t i;

			for (i = 0; i < payload_count; i++)
			{
				payload_size += payloads[i].length;
			}

			if (session_instance->remote_incoming_window == 0)
			{
				result = SESSION_SEND_TRANSFER_BUSY;
			}
			else
			{
				/* Codes_SRS_SESSION_01_012: [The session endpoint assigns each outgoing transfer frame an implicit transfer-id from a session scoped sequence.] */
				/* Codes_SRS_SESSION_01_027: [sending a transfer Upon sending a transfer, the sending endpoint will increment its next-outgoing-id] */
				*delivery_id = session_instance->next_outgoing_id;
				if ((transfer_set_handle(transfer, link_endpoint_instance->output_handle) != 0) ||
					(transfer_set_delivery_id(transfer, *delivery_id) != 0))
				{
					/* Codes_SRS_SESSION_01_058: [When any other error occurs, session_send_transfer shall fail and return a non-zero value.] */
					result = SESSION_SEND_TRANSFER_ERROR;
				}
				else
				{
					AMQP_VALUE transfer_value;

					transfer_value = amqpvalue_create_transfer(transfer);
					if (transfer_value == NULL)
					{
						/* Codes_SRS_SESSION_01_058: [When any other error occurs, session_send_transfer shall fail and return a non-zero value.] */
						result = SESSION_SEND_TRANSFER_ERROR;
					}
					else
					{
						uint32_t available_frame_size;
						size_t encoded_size;

						if ((connection_get_remote_max_frame_size(session_instance->connection, &available_frame_size) != 0) ||
							(amqpvalue_get_encoded_size(transfer_value, &encoded_size) != 0))
						{
							result = SESSION_SEND_TRANSFER_ERROR;
						}
						else
						{
							uint32_t payload_size = 0;
							size_t i;

							for (i = 0; i < payload_count; i++)
							{
								payload_size += payloads[i].length;
							}

							available_frame_size -= encoded_size;
							available_frame_size -= 8;

							if (available_frame_size >= payload_size)
							{
								/* Codes_SRS_SESSION_01_055: [The encoding of the frame shall be done by calling connection_encode_frame and passing as arguments: the connection handle associated with the session, the transfer performative and the payload chunks passed to session_send_transfer.] */
								if (connection_encode_frame(session_instance->endpoint, transfer_value, payloads, payload_count, on_send_complete, callback_context) != 0)
								{
									/* Codes_SRS_SESSION_01_056: [If connection_encode_frame fails then session_send_transfer shall fail and return a non-zero value.] */
									result = SESSION_SEND_TRANSFER_ERROR;
								}
								else
								{
									/* Codes_SRS_SESSION_01_018: [is incremented after each successive transfer according to RFC-1982 [RFC1982] serial number arithmetic.] */
									session_instance->next_outgoing_id++;
									session_instance->remote_incoming_window--;
									session_instance->outgoing_window--;

									/* Codes_SRS_SESSION_01_053: [On success, session_send_transfer shall return 0.] */
									result = SESSION_SEND_TRANSFER_OK;
								}
							}
							else
							{
								size_t current_payload_index = 0;
								uint32_t current_payload_pos = 0;

								/* break it down into different deliveries */
								while (payload_size > 0)
								{
									uint32_t transfer_frame_payload_count = 0;
									uint32_t current_transfer_frame_payload_size = payload_size;
									uint32_t byte_counter;
									size_t temp_current_payload_index = current_payload_index;
									uint32_t temp_current_payload_pos = current_payload_pos;
									AMQP_VALUE multi_transfer_amqp_value;
									bool more;

									if (current_transfer_frame_payload_size > available_frame_size)
									{
										current_transfer_frame_payload_size = available_frame_size;
									}

									if (available_frame_size >= payload_size)
									{
										more = false;
									}
									else
									{
										more = true;
									}

									if (transfer_set_more(transfer, more) != 0)
									{
										break;
									}

									multi_transfer_amqp_value = amqpvalue_create_transfer(transfer);
									if (multi_transfer_amqp_value == NULL)
									{
										break;
									}

									byte_counter = current_transfer_frame_payload_size;
									while (byte_counter > 0)
									{
										if (payloads[temp_current_payload_index].length - temp_current_payload_pos >= byte_counter)
										{
											/* more data than we need */
											temp_current_payload_pos += byte_counter;
											byte_counter = 0;
										}
										else
										{
											byte_counter -= payloads[temp_current_payload_index].length - temp_current_payload_pos;
											temp_current_payload_index++;
											temp_current_payload_pos = 0;
										}
									}

									transfer_frame_payload_count = temp_current_payload_index - current_payload_index + 1;
									PAYLOAD* transfer_frame_payloads = (PAYLOAD*)amqpalloc_malloc(transfer_frame_payload_count * sizeof(PAYLOAD));
									if (transfer_frame_payloads == NULL)
									{
										amqpvalue_destroy(multi_transfer_amqp_value);
										break;
									}

									/* copy data */
									byte_counter = current_transfer_frame_payload_size;
									transfer_frame_payload_count = 0;

									while (byte_counter > 0)
									{
										if (payloads[current_payload_index].length - current_payload_pos > byte_counter)
										{
											/* more data than we need */
											transfer_frame_payloads[transfer_frame_payload_count].bytes = payloads[current_payload_index].bytes + current_payload_pos;
											transfer_frame_payloads[transfer_frame_payload_count].length = byte_counter;
											current_payload_pos += byte_counter;
											byte_counter = 0;
										}
										else
										{
											/* copy entire payload and move to the next */
											transfer_frame_payloads[transfer_frame_payload_count].bytes = payloads[current_payload_index].bytes + current_payload_pos;
											transfer_frame_payloads[transfer_frame_payload_count].length = payloads[current_payload_index].length - current_payload_pos;
											byte_counter -= payloads[current_payload_index].length - current_payload_pos;
											current_payload_index++;
											current_payload_pos = 0;
										}

										transfer_frame_payload_count++;
									}

									if (connection_encode_frame(session_instance->endpoint, multi_transfer_amqp_value, transfer_frame_payloads, transfer_frame_payload_count, on_send_complete, callback_context) != 0)
									{
										amqpalloc_free(transfer_frame_payloads);
										amqpvalue_destroy(multi_transfer_amqp_value);
										break;
									}

									amqpalloc_free(transfer_frame_payloads);
									amqpvalue_destroy(multi_transfer_amqp_value);
									payload_size -= current_transfer_frame_payload_size;
								}

								if (payload_size > 0)
								{
									result = SESSION_SEND_TRANSFER_ERROR;
								}
								else
								{
									/* Codes_SRS_SESSION_01_018: [is incremented after each successive transfer according to RFC-1982 [RFC1982] serial number arithmetic.] */
									session_instance->next_outgoing_id++;
									session_instance->remote_incoming_window--;
									session_instance->outgoing_window--;

									result = SESSION_SEND_TRANSFER_OK;
								}
							}
						}

						amqpvalue_destroy(transfer_value);
					}
				}
			}
		}
	}

	return result;
}
Beispiel #23
0
int messagesender_send(MESSAGE_SENDER_HANDLE message_sender, MESSAGE_HANDLE message, ON_MESSAGE_SEND_COMPLETE on_message_send_complete, void* callback_context)
{
	int result;

	if ((message_sender == NULL) ||
		(message == NULL))
	{
		result = __LINE__;
	}
	else
	{
		MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
		if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_ERROR)
		{
			result = __LINE__;
		}
		else
		{
			MESSAGE_WITH_CALLBACK* message_with_callback = (MESSAGE_WITH_CALLBACK*)amqpalloc_malloc(sizeof(MESSAGE_WITH_CALLBACK));
			if (message_with_callback == NULL)
			{
				result = __LINE__;
			}
			else
			{
				MESSAGE_WITH_CALLBACK** new_messages = (MESSAGE_WITH_CALLBACK**)amqpalloc_realloc(message_sender_instance->messages, sizeof(MESSAGE_WITH_CALLBACK*) * (message_sender_instance->message_count + 1));
				if (new_messages == NULL)
				{
					amqpalloc_free(message_with_callback);
					result = __LINE__;
				}
				else
				{
					result = 0;

					message_sender_instance->messages = new_messages;
					if (message_sender_instance->message_sender_state != MESSAGE_SENDER_STATE_OPEN)
					{
						message_with_callback->message = message_clone(message);
						if (message_with_callback->message == NULL)
						{
							amqpalloc_free(message_with_callback);
							result = __LINE__;
						}

						message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT;
					}
					else
					{
						message_with_callback->message = NULL;
						message_with_callback->message_send_state = MESSAGE_SEND_STATE_PENDING;
					}

					if (result == 0)
					{
						message_with_callback->on_message_send_complete = on_message_send_complete;
						message_with_callback->context = callback_context;
						message_with_callback->message_sender = message_sender_instance;

						message_sender_instance->messages[message_sender_instance->message_count] = message_with_callback;
						message_sender_instance->message_count++;

						if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPEN)
						{
							switch (send_one_message(message_sender_instance, message_with_callback, message))
							{
							default:
							case SEND_ONE_MESSAGE_ERROR:
								remove_pending_message_by_index(message_sender_instance, message_sender_instance->message_count - 1);
								result = __LINE__;
								break;

							case SEND_ONE_MESSAGE_BUSY:
								message_with_callback->message = message_clone(message);
								if (message_with_callback->message == NULL)
								{
									amqpalloc_free(message_with_callback);
									result = __LINE__;
								}
								else
								{
									message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT;
									result = 0;
								}

								break;

							case SEND_ONE_MESSAGE_OK:
								result = 0;
								break;
							}
						}
					}
				}
			}
		}
	}

	return result;
}
Beispiel #24
0
static SEND_ONE_MESSAGE_RESULT send_one_message(MESSAGE_SENDER_INSTANCE* message_sender_instance, MESSAGE_WITH_CALLBACK* message_with_callback, MESSAGE_HANDLE message)
{
	SEND_ONE_MESSAGE_RESULT result;

	size_t encoded_size;
	size_t total_encoded_size = 0;
	MESSAGE_BODY_TYPE message_body_type;
    message_format message_format;

	if ((message_get_body_type(message, &message_body_type) != 0) ||
        (message_get_message_format(message, &message_format) != 0))
	{
		result = SEND_ONE_MESSAGE_ERROR;
	}
	else
	{
		// header
		HEADER_HANDLE header;
		AMQP_VALUE header_amqp_value;
		PROPERTIES_HANDLE properties;
		AMQP_VALUE properties_amqp_value;
		AMQP_VALUE application_properties;
		AMQP_VALUE application_properties_value;
		AMQP_VALUE body_amqp_value = NULL;
        size_t body_data_count;

		message_get_header(message, &header);
		header_amqp_value = amqpvalue_create_header(header);
		if (header != NULL)
		{
			amqpvalue_get_encoded_size(header_amqp_value, &encoded_size);
			total_encoded_size += encoded_size;
		}

		// properties
		message_get_properties(message, &properties);
		properties_amqp_value = amqpvalue_create_properties(properties);
		if (properties != NULL)
		{
			amqpvalue_get_encoded_size(properties_amqp_value, &encoded_size);
			total_encoded_size += encoded_size;
		}

		// application properties
		message_get_application_properties(message, &application_properties);
		application_properties_value = amqpvalue_create_application_properties(application_properties);
		if (application_properties != NULL)
		{
			amqpvalue_get_encoded_size(application_properties_value, &encoded_size);
			total_encoded_size += encoded_size;
		}

		result = SEND_ONE_MESSAGE_OK;

		// body - amqp data
		switch (message_body_type)
		{
			default:
				result = SEND_ONE_MESSAGE_ERROR;
				break;

			case MESSAGE_BODY_TYPE_VALUE:
			{
				AMQP_VALUE message_body_amqp_value;
				if (message_get_inplace_body_amqp_value(message, &message_body_amqp_value) != 0)
				{
					result = SEND_ONE_MESSAGE_ERROR;
				}
				else
				{
					body_amqp_value = amqpvalue_create_amqp_value(message_body_amqp_value);
					if ((body_amqp_value == NULL) ||
						(amqpvalue_get_encoded_size(body_amqp_value, &encoded_size) != 0))
					{
						result = SEND_ONE_MESSAGE_ERROR;
					}
					else
					{
						total_encoded_size += encoded_size;
					}
				}

				break;
			}

			case MESSAGE_BODY_TYPE_DATA:
			{
				BINARY_DATA binary_data;
                size_t i;

                if (message_get_body_amqp_data_count(message, &body_data_count) != 0)
                {
                    result = SEND_ONE_MESSAGE_ERROR;
                }
                else
                {
                    for (i = 0; i < body_data_count; i++)
                    {
                        if (message_get_body_amqp_data(message, i, &binary_data) != 0)
                        {
                            result = SEND_ONE_MESSAGE_ERROR;
                        }
                        else
                        {
                            amqp_binary binary_value = { binary_data.bytes, binary_data.length };
                            AMQP_VALUE body_amqp_data = amqpvalue_create_data(binary_value);
                            if (body_amqp_data == NULL)
                            {
                                result = SEND_ONE_MESSAGE_ERROR;
                            }
                            else
                            {
                                if (amqpvalue_get_encoded_size(body_amqp_data, &encoded_size) != 0)
                                {
                                    result = SEND_ONE_MESSAGE_ERROR;
                                }
                                else
                                {
                                    total_encoded_size += encoded_size;
                                }

                                amqpvalue_destroy(body_amqp_data);
                            }
                        }
                    }
                }
				break;
			}
		}

		if (result == 0)
		{
			void* data_bytes = amqpalloc_malloc(total_encoded_size);
			PAYLOAD payload = { data_bytes, 0 };
			result = SEND_ONE_MESSAGE_OK;

			if (header != NULL)
			{
				if (amqpvalue_encode(header_amqp_value, encode_bytes, &payload) != 0)
				{
					result = SEND_ONE_MESSAGE_ERROR;
				}

				log_message_chunk(message_sender_instance, "Header:", header_amqp_value);
			}

			if ((result == SEND_ONE_MESSAGE_OK) && (properties != NULL))
			{
				if (amqpvalue_encode(properties_amqp_value, encode_bytes, &payload) != 0)
				{
					result = SEND_ONE_MESSAGE_ERROR;
				}

				log_message_chunk(message_sender_instance, "Properties:", properties_amqp_value);
			}

			if ((result == SEND_ONE_MESSAGE_OK) && (application_properties != NULL))
			{
				if (amqpvalue_encode(application_properties_value, encode_bytes, &payload) != 0)
				{
					result = SEND_ONE_MESSAGE_ERROR;
				}

				log_message_chunk(message_sender_instance, "Application properties:", application_properties_value);
			}

			if (result == SEND_ONE_MESSAGE_OK)
			{
				switch (message_body_type)
				{
				case MESSAGE_BODY_TYPE_VALUE:
				{
					if (amqpvalue_encode(body_amqp_value, encode_bytes, &payload) != 0)
					{
						result = SEND_ONE_MESSAGE_ERROR;
					}

					log_message_chunk(message_sender_instance, "Body - amqp value:", body_amqp_value);
					break;
				}
				case MESSAGE_BODY_TYPE_DATA:
				{
                    BINARY_DATA binary_data;
                    size_t i;

                    for (i = 0; i < body_data_count; i++)
                    {
                        if (message_get_body_amqp_data(message, i, &binary_data) != 0)
                        {
                            result = SEND_ONE_MESSAGE_ERROR;
                        }
                        else
                        {
                            amqp_binary binary_value = { binary_data.bytes, binary_data.length };
                            AMQP_VALUE body_amqp_data = amqpvalue_create_data(binary_value);
                            if (body_amqp_data == NULL)
                            {
                                result = SEND_ONE_MESSAGE_ERROR;
                            }
                            else
                            {
                                if (amqpvalue_encode(body_amqp_data, encode_bytes, &payload) != 0)
                                {
                                    result = SEND_ONE_MESSAGE_ERROR;
                                    break;
                                }

                                amqpvalue_destroy(body_amqp_data);
                            }
                        }
                    }
					break;
				}
				}
			}

			if (result == SEND_ONE_MESSAGE_OK)
			{
				message_with_callback->message_send_state = MESSAGE_SEND_STATE_PENDING;
				switch (link_transfer(message_sender_instance->link, message_format, &payload, 1, on_delivery_settled, message_with_callback))
				{
				default:
				case LINK_TRANSFER_ERROR:
					if (message_with_callback->on_message_send_complete != NULL)
					{
						message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_ERROR);
					}

					result = SEND_ONE_MESSAGE_ERROR;
					break;

				case LINK_TRANSFER_BUSY:
					message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT;
					result = SEND_ONE_MESSAGE_BUSY;
					break;

				case LINK_TRANSFER_OK:
					result = SEND_ONE_MESSAGE_OK;
					break;
				}
			}

			amqpalloc_free(data_bytes);

			if (body_amqp_value != NULL)
			{
				amqpvalue_destroy(body_amqp_value);
			}

			amqpvalue_destroy(application_properties);
			amqpvalue_destroy(application_properties_value);
			amqpvalue_destroy(properties_amqp_value);
			properties_destroy(properties);
		}
	}

	return result;
}
Beispiel #25
0
CONCRETE_IO_HANDLE saslclientio_create(void* io_create_parameters, LOGGER_LOG logger_log)
{
	SASLCLIENTIO_CONFIG* sasl_client_io_config = io_create_parameters;
	SASL_CLIENT_IO_INSTANCE* result;

	/* Codes_SRS_SASLCLIENTIO_01_005: [If xio_create_parameters is NULL, saslclientio_create shall fail and return NULL.] */
	if ((sasl_client_io_config == NULL) ||
		/* Codes_SRS_SASLCLIENTIO_01_092: [If any of the sasl_mechanism or underlying_io members of the configuration structure are NULL, saslclientio_create shall fail and return NULL.] */
		(sasl_client_io_config->underlying_io == NULL) ||
		(sasl_client_io_config->sasl_mechanism == NULL))
	{
		result = NULL;
	}
	else
	{
		result = amqpalloc_malloc(sizeof(SASL_CLIENT_IO_INSTANCE));
		/* Codes_SRS_SASLCLIENTIO_01_006: [If memory cannot be allocated for the new instance, saslclientio_create shall fail and return NULL.] */
		if (result != NULL)
		{
			result->underlying_io = sasl_client_io_config->underlying_io;
			if (result->underlying_io == NULL)
			{
				amqpalloc_free(result);
				result = NULL;
			}
			else
			{
				/* Codes_SRS_SASLCLIENTIO_01_089: [saslclientio_create shall create a frame_codec to be used for encoding/decoding frames bycalling frame_codec_create and passing the underlying_io as argument.] */
				result->frame_codec = frame_codec_create(on_frame_codec_error, result, logger_log);
				if (result->frame_codec == NULL)
				{
					/* Codes_SRS_SASLCLIENTIO_01_090: [If frame_codec_create fails, then saslclientio_create shall fail and return NULL.] */
					amqpalloc_free(result);
					result = NULL;
				}
				else
				{
					/* Codes_SRS_SASLCLIENTIO_01_084: [saslclientio_create shall create a sasl_frame_codec to be used for SASL frame encoding/decoding by calling sasl_frame_codec_create and passing the just created frame_codec as argument.] */
					result->sasl_frame_codec = sasl_frame_codec_create(result->frame_codec, sasl_frame_received_callback, on_sasl_frame_codec_error, result);
					if (result->sasl_frame_codec == NULL)
					{
						frame_codec_destroy(result->frame_codec);
						amqpalloc_free(result);
						result = NULL;
					}
					else
					{
						/* Codes_SRS_SASLCLIENTIO_01_004: [saslclientio_create shall return on success a non-NULL handle to a new SASL client IO instance.] */
						result->on_bytes_received = NULL;
						result->on_io_open_complete = NULL;
						result->on_io_error = NULL;
						result->on_io_close_complete = NULL;
						result->logger_log = logger_log;
						result->open_callback_context = NULL;
						result->close_callback_context = NULL;
						result->sasl_mechanism = sasl_client_io_config->sasl_mechanism;

						result->io_state = IO_STATE_NOT_OPEN;
					}
				}
			}
		}
	}

	return result;
}
Beispiel #26
0
CONCRETE_SASL_MECHANISM_HANDLE saslplain_create(void* config)
{
	SASL_PLAIN_INSTANCE* result;

	if (config == NULL)
	{
		/* Codes_SRS_SASL_PLAIN_01_003: [If the config argument is NULL, then saslplain_create shall fail and return NULL.] */
		result = NULL;
	}
	else
	{
		SASL_PLAIN_CONFIG* sasl_plain_config = (SASL_PLAIN_CONFIG*)config;

		/* Codes_SRS_SASL_PLAIN_01_004: [If either the authcid or passwd member of the config structure is NULL, then saslplain_create shall fail and return NULL.] */
		if ((sasl_plain_config->authcid == NULL) ||
			(sasl_plain_config->passwd == NULL))
		{
			result = NULL;
		}
		else
		{
			size_t authzid_length = sasl_plain_config->authzid == NULL ? 0 : strlen(sasl_plain_config->authzid);
			size_t authcid_length = strlen(sasl_plain_config->authcid);
			size_t passwd_length = strlen(sasl_plain_config->passwd);

			/* Codes_SRS_SASL_PLAIN_01_020: [   authcid   = 1*SAFE ; MUST accept up to 255 octets] */
			if ((authcid_length > 255) || (authcid_length == 0) ||
				/* Codes_SRS_SASL_PLAIN_01_021: [   authzid   = 1*SAFE ; MUST accept up to 255 octets] */
				(authzid_length > 255) ||
				/* Codes_SRS_SASL_PLAIN_01_022: [   passwd    = 1*SAFE ; MUST accept up to 255 octets] */
				(passwd_length > 255) || (passwd_length == 0))
			{
				result = NULL;
			}
			else
			{
				/* Codes_SRS_SASL_PLAIN_01_001: [saslplain_create shall return on success a non-NULL handle to a new SASL plain mechanism.] */
				result = amqpalloc_malloc(sizeof(SASL_PLAIN_INSTANCE));
				/* Codes_SRS_SASL_PLAIN_01_002: [If allocating the memory needed for the saslplain instance fails then saslplain_create shall return NULL.] */
				if (result != NULL)
				{
					/* Ignore UTF8 for now */
					result->init_bytes = (unsigned char*)amqpalloc_malloc(authzid_length + authcid_length + passwd_length + 2);
					if (result->init_bytes == NULL)
					{
						/* Codes_SRS_SASL_PLAIN_01_002: [If allocating the memory needed for the saslplain instance fails then saslplain_create shall return NULL.] */
						amqpalloc_free(result);
						result = NULL;
					}
					else
					{
						/* Codes_SRS_SASL_PLAIN_01_016: [The mechanism consists of a single message, a string of [UTF-8] encoded [Unicode] characters, from the client to the server.] */
						/* Codes_SRS_SASL_PLAIN_01_017: [The client presents the authorization identity (identity to act as), followed by a NUL (U+0000) character, followed by the authentication identity (identity whose password will be used), followed by a NUL (U+0000) character, followed by the clear-text password.] */
						/* Codes_SRS_SASL_PLAIN_01_019: [   message   = [authzid] UTF8NUL authcid UTF8NUL passwd] */
						/* Codes_SRS_SASL_PLAIN_01_023: [The authorization identity (authzid), authentication identity (authcid), password (passwd), and NUL character deliminators SHALL be transferred as [UTF-8] encoded strings of [Unicode] characters.] */
						/* Codes_SRS_SASL_PLAIN_01_024: [As the NUL (U+0000) character is used as a deliminator, the NUL (U+0000) character MUST NOT appear in authzid, authcid, or passwd productions.] */

						/* Codes_SRS_SASL_PLAIN_01_018: [As with other SASL mechanisms, the client does not provide an authorization identity when it wishes the server to derive an identity from the credentials and use that as the authorization identity.] */
						if (authzid_length > 0)
						{
							(void)memcpy(result->init_bytes, sasl_plain_config->authzid, authzid_length);
						}

						result->init_bytes[authzid_length] = 0;
						(void)memcpy(result->init_bytes + authzid_length + 1, sasl_plain_config->authcid, authcid_length);
						result->init_bytes[authzid_length + authcid_length + 1] = 0;
						(void)memcpy(result->init_bytes + authzid_length + authcid_length + 2, sasl_plain_config->passwd, passwd_length);
						result->init_bytes_length = authzid_length + authcid_length + passwd_length + 2;
					}
				}
			}
		}
	}

	return result;
}
Beispiel #27
0
/* Codes_SRS_SASL_FRAME_CODEC_01_029: [sasl_frame_codec_encode_frame shall encode the frame header and sasl_frame_value AMQP value in a SASL frame and on success it shall return 0.] */
int sasl_frame_codec_encode_frame(SASL_FRAME_CODEC_HANDLE sasl_frame_codec, const AMQP_VALUE sasl_frame_value, ON_BYTES_ENCODED on_bytes_encoded, void* callback_context)
{
	int result;
	SASL_FRAME_CODEC_INSTANCE* sasl_frame_codec_instance = (SASL_FRAME_CODEC_INSTANCE*)sasl_frame_codec;

	/* Codes_SRS_SASL_FRAME_CODEC_01_030: [If sasl_frame_codec or sasl_frame_value is NULL, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
	if ((sasl_frame_codec == NULL) ||
		(sasl_frame_value == NULL))
	{
		/* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
		result = __LINE__;
	}
	else
	{
		AMQP_VALUE descriptor;
		uint64_t sasl_frame_descriptor_ulong;
                size_t encoded_size;

		if (((descriptor = amqpvalue_get_inplace_descriptor(sasl_frame_value)) == NULL) ||
			(amqpvalue_get_ulong(descriptor, &sasl_frame_descriptor_ulong) != 0) ||
			/* Codes_SRS_SASL_FRAME_CODEC_01_047: [The frame body of a SASL frame MUST contain exactly one AMQP type, whose type encoding MUST have provides=“sasl-frame”.] */
			(sasl_frame_descriptor_ulong < SASL_MECHANISMS) ||
			(sasl_frame_descriptor_ulong > SASL_OUTCOME))
		{
			/* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
			result = __LINE__;
		}
		/* Codes_SRS_SASL_FRAME_CODEC_01_032: [The payload frame size shall be computed based on the encoded size of the sasl_frame_value and its fields.] */
		/* Codes_SRS_SASL_FRAME_CODEC_01_033: [The encoded size of the sasl_frame_value and its fields shall be obtained by calling amqpvalue_get_encoded_size.] */
		else if ((amqpvalue_get_encoded_size(sasl_frame_value, &encoded_size) != 0) ||
			/* Codes_SRS_SASL_FRAME_CODEC_01_016: [The maximum size of a SASL frame is defined by MIN-MAX-FRAME-SIZE.] */
			(encoded_size > MIX_MAX_FRAME_SIZE - 8))
		{
			/* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
			result = __LINE__;
		}
		else
		{
			unsigned char* sasl_frame_bytes = (unsigned char*)amqpalloc_malloc(encoded_size);
			if (sasl_frame_bytes == NULL)
			{
				result = __LINE__;
			}
			else
			{
				PAYLOAD payload;

                payload.bytes = sasl_frame_bytes;
                payload.length = 0;

				if (amqpvalue_encode(sasl_frame_value, encode_bytes, &payload) != 0)
				{
					result = __LINE__;
				}
				else
				{
					/* Codes_SRS_SASL_FRAME_CODEC_01_031: [sasl_frame_codec_encode_frame shall encode the frame header and its contents by using frame_codec_encode_frame.] */
					/* Codes_SRS_SASL_FRAME_CODEC_01_012: [Bytes 6 and 7 of the header are ignored.] */
					/* Codes_SRS_SASL_FRAME_CODEC_01_013: [Implementations SHOULD set these to 0x00.] */
					/* Codes_SRS_SASL_FRAME_CODEC_01_014: [The extended header is ignored.] */
					/* Codes_SRS_SASL_FRAME_CODEC_01_015: [Implementations SHOULD therefore set DOFF to 0x02.] */
					if (frame_codec_encode_frame(sasl_frame_codec_instance->frame_codec, FRAME_TYPE_SASL, &payload, 1, NULL, 0, on_bytes_encoded, callback_context) != 0)
					{
						/* Codes_SRS_SASL_FRAME_CODEC_01_034: [If any error occurs during encoding, sasl_frame_codec_encode_frame shall fail and return a non-zero value.] */
						result = __LINE__;
					}
					else
					{
						result = 0;
					}
				}

				amqpalloc_free(sasl_frame_bytes);
			}
		}
	}

	return result;
}
AMQP_MANAGEMENT_HANDLE amqpmanagement_create(SESSION_HANDLE session, const char* management_node, ON_AMQP_MANAGEMENT_STATE_CHANGED on_amqp_management_state_changed, void* callback_context)
{
	AMQP_MANAGEMENT_INSTANCE* result;

	if (session == NULL)
	{
		result = NULL;
	}
	else
	{
		result = (AMQP_MANAGEMENT_INSTANCE*)amqpalloc_malloc(sizeof(AMQP_MANAGEMENT_INSTANCE));
		if (result != NULL)
		{
			result->session = session;
			result->sender_connected = 0;
			result->receiver_connected = 0;
			result->operation_message_count = 0;
			result->operation_messages = NULL;
			result->on_amqp_management_state_changed = on_amqp_management_state_changed;
			result->callback_context = callback_context;

			AMQP_VALUE source = messaging_create_source(management_node);
			if (source == NULL)
			{
				amqpalloc_free(result);
				result = NULL;
			}
			else
			{
				AMQP_VALUE target = messaging_create_target(management_node);
				if (target == NULL)
				{
					amqpalloc_free(result);
					result = NULL;
				}
				else
				{
					static const char* sender_suffix = "-sender";

					char* sender_link_name = (char*)amqpalloc_malloc(strlen(management_node) + strlen(sender_suffix) + 1);
					if (sender_link_name == NULL)
					{
						result = NULL;
					}
					else
					{
						static const char* receiver_suffix = "-receiver";

						(void)strcpy(sender_link_name, management_node);
						(void)strcat(sender_link_name, sender_suffix);

						char* receiver_link_name = (char*)amqpalloc_malloc(strlen(management_node) + strlen(receiver_suffix) + 1);
						if (receiver_link_name == NULL)
						{
							result = NULL;
						}
						else
						{
							(void)strcpy(receiver_link_name, management_node);
							(void)strcat(receiver_link_name, receiver_suffix);

							result->sender_link = link_create(session, "cbs-sender", role_sender, source, target);
							if (result->sender_link == NULL)
							{
								amqpalloc_free(result);
								result = NULL;
							}
							else
							{
								result->receiver_link = link_create(session, "cbs-receiver", role_receiver, source, target);
								if (result->receiver_link == NULL)
								{
									link_destroy(result->sender_link);
									amqpalloc_free(result);
									result = NULL;
								}
								else
								{
									if ((link_set_max_message_size(result->sender_link, 65535) != 0) ||
										(link_set_max_message_size(result->receiver_link, 65535) != 0))
									{
										link_destroy(result->sender_link);
										link_destroy(result->receiver_link);
										amqpalloc_free(result);
										result = NULL;
									}
									else
									{
										result->message_sender = messagesender_create(result->sender_link, on_message_sender_state_changed, result, NULL);
										if (result->message_sender == NULL)
										{
											link_destroy(result->sender_link);
											link_destroy(result->receiver_link);
											amqpalloc_free(result);
											result = NULL;
										}
										else
										{
											result->message_receiver = messagereceiver_create(result->receiver_link, on_message_receiver_state_changed, result);
											if (result->message_receiver == NULL)
											{
												messagesender_destroy(result->message_sender);
												link_destroy(result->sender_link);
												link_destroy(result->receiver_link);
												amqpalloc_free(result);
												result = NULL;
											}
											else
											{
												result->next_message_id = 0;
											}
										}
									}
								}
							}

							amqpalloc_free(receiver_link_name);
						}

						amqpalloc_free(sender_link_name);
					}

					amqpvalue_destroy(target);
				}

				amqpvalue_destroy(source);
			}
		}
	}

	return result;
}
Beispiel #29
0
LINK_ENDPOINT_HANDLE session_create_link_endpoint(SESSION_HANDLE session, const char* name)
{
	LINK_ENDPOINT_INSTANCE* result;

	/* Codes_SRS_SESSION_01_044: [If session, name or frame_received_callback is NULL, session_create_link_endpoint shall fail and return NULL.] */
	if ((session == NULL) ||
		(name == NULL))
	{
		result = NULL;
	}
	else
	{
		/* Codes_SRS_SESSION_01_043: [session_create_link_endpoint shall create a link endpoint associated with a given session and return a non-NULL handle to it.] */
		SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session;

		result = (LINK_ENDPOINT_INSTANCE*)amqpalloc_malloc(sizeof(LINK_ENDPOINT_INSTANCE));
		/* Codes_SRS_SESSION_01_045: [If allocating memory for the link endpoint fails, session_create_link_endpoint shall fail and return NULL.] */
		if (result != NULL)
		{
			/* Codes_SRS_SESSION_01_046: [An unused handle shall be assigned to the link endpoint.] */
			handle selected_handle = 0;
			size_t i;

			for (i = 0; i < session_instance->link_endpoint_count; i++)
			{
				if (session_instance->link_endpoints[i]->output_handle > selected_handle)
				{
					break;
				}

				selected_handle++;
			}

			result->on_session_state_changed = NULL;
			result->on_session_flow_on = NULL;
			result->frame_received_callback = NULL;
			result->callback_context = NULL;
			result->output_handle = selected_handle;
			result->name = amqpalloc_malloc(strlen(name) + 1);
			if (result->name == NULL)
			{
				/* Codes_SRS_SESSION_01_045: [If allocating memory for the link endpoint fails, session_create_link_endpoint shall fail and return NULL.] */
				amqpalloc_free(result);
				result = NULL;
			}
			else
			{
				LINK_ENDPOINT_INSTANCE** new_link_endpoints;
				strcpy(result->name, name);
				result->session = session;

				new_link_endpoints = amqpalloc_realloc(session_instance->link_endpoints, sizeof(LINK_ENDPOINT_INSTANCE*) * (session_instance->link_endpoint_count + 1));
				if (new_link_endpoints == NULL)
				{
					/* Codes_SRS_SESSION_01_045: [If allocating memory for the link endpoint fails, session_create_link_endpoint shall fail and return NULL.] */
					amqpalloc_free(result);
					result = NULL;
				}
				else
				{
					session_instance->link_endpoints = new_link_endpoints;

					if (session_instance->link_endpoint_count - selected_handle > 0)
					{
						(void)memmove(&session_instance->link_endpoints[selected_handle], &session_instance->link_endpoints[selected_handle + 1], (session_instance->link_endpoint_count - selected_handle) * sizeof(LINK_ENDPOINT_INSTANCE*));
					}

					session_instance->link_endpoints[selected_handle] = result;
					session_instance->link_endpoint_count++;
				}
			}
		}
	}

	return result;
}
int amqpmanagement_start_operation(AMQP_MANAGEMENT_HANDLE amqp_management, const char* operation, const char* type, const char* locales, MESSAGE_HANDLE message, ON_OPERATION_COMPLETE on_operation_complete, void* context)
{
	int result;

	if ((amqp_management == NULL) ||
		(operation == NULL))
	{
		result = __LINE__;
	}
	else
	{
		AMQP_VALUE application_properties;
		if (message_get_application_properties(message, &application_properties) != 0)
		{
			result = __LINE__;
		}
		else
		{
			if ((add_string_key_value_pair_to_map(application_properties, "operation", operation) != 0) ||
				(add_string_key_value_pair_to_map(application_properties, "type", type) != 0) ||
				((locales != NULL) && (add_string_key_value_pair_to_map(application_properties, "locales", locales) != 0)))
			{
				result = __LINE__;
			}
			else
			{
				if ((message_set_application_properties(message, application_properties) != 0) ||
					(set_message_id(message, amqp_management->next_message_id) != 0))
				{
					result = __LINE__;
				}
				else
				{
					OPERATION_MESSAGE_INSTANCE* pending_operation_message = amqpalloc_malloc(sizeof(OPERATION_MESSAGE_INSTANCE));
					if (pending_operation_message == NULL)
					{
						result = __LINE__;
					}
					else
					{
						pending_operation_message->message = message_clone(message);
						pending_operation_message->callback_context = context;
						pending_operation_message->on_operation_complete = on_operation_complete;
						pending_operation_message->operation_state = OPERATION_STATE_NOT_SENT;
						pending_operation_message->message_id = amqp_management->next_message_id;

						amqp_management->next_message_id++;

						OPERATION_MESSAGE_INSTANCE** new_operation_messages = amqpalloc_realloc(amqp_management->operation_messages, (amqp_management->operation_message_count + 1) * sizeof(OPERATION_MESSAGE_INSTANCE*));
						if (new_operation_messages == NULL)
						{
							message_destroy(message);
							amqpalloc_free(pending_operation_message);
							result = __LINE__;
						}
						else
						{
							amqp_management->operation_messages = new_operation_messages;
							amqp_management->operation_messages[amqp_management->operation_message_count] = pending_operation_message;
							amqp_management->operation_message_count++;

							if (send_operation_messages(amqp_management) != 0)
							{
								if (on_operation_complete != NULL)
								{
									on_operation_complete(context, OPERATION_RESULT_CBS_ERROR, 0, NULL);
								}

								result = __LINE__;
							}
							else
							{
								result = 0;
							}
						}
					}
				}
			}

			amqpvalue_destroy(application_properties);
		}
	}

	return result;
}