/*
 * Copyright (c) 2008 Lukas Mejdrech
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** @addtogroup net_tl
 *  @{
 */

/** @file
 *  Transport layer common functions.
 */

#ifndef __NET_TL_COMMON_H__
#define __NET_TL_COMMON_H__

#include "../structures/packet/packet.h"

#include "../include/inet.h"
#include "../include/socket_codes.h"

/** Type definition of the packet dimension.
 *  @see packet_dimension
 */
typedef struct packet_dimension	packet_dimension_t;

/** Type definition of the packet dimension pointer.
 *  @see packet_dimension
 */
typedef packet_dimension_t *	packet_dimension_ref;

/** Packet dimension.
 */
struct packet_dimension{
	/** Reserved packet prefix length.
	 */
	size_t			prefix;
	/** Maximal packet content length.
	 */
	size_t			content;
	/** Reserved packet suffix length.
	 */
	size_t			suffix;
	/** Maximal packet address length.
	 */
	size_t			addr_len;
};

/** Gets the address port.
 *  Supports AF_INET and AF_INET6 address families.
 *  @param addr The address to be updated. Input/output parameter.
 *  @param addrlen The address length. Input parameter.
 *  @param port The set port. Output parameter.
 *  @returns EOK on success.
 *  @returns EINVAL if the address length does not match the address family.
 *  @returns EAFNOSUPPORT if the address family is not supported.
 */
int	tl_get_address_port( const struct sockaddr * addr, int addrlen, uint16_t * port );

/** Sets the address port.
 *  Supports AF_INET and AF_INET6 address families.
 *  @param addr The address to be updated. Input/output parameter.
 *  @param addrlen The address length. Input parameter.
 *  @param port The port to be set. Input parameter.
 *  @returns EOK on success.
 *  @returns EINVAL if the address length does not match the address family.
 *  @returns EAFNOSUPPORT if the address family is not supported.
 */
int	tl_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port );

/** Prepares the packet for ICMP error notification.
 *  Keeps the first packet and releases all the others.
 *  Releases all the packets on error.
 *  @param packet_phone The packet server module phone. Input parameter.
 *  @param icmp_phone The ICMP module phone. Input parameter.
 *  @param packet The packet to be send. Input parameter.
 *  @param error The packet error reporting service. Prefixes the received packet. Input parameter.
 *  @returns EOK on success.
 *  @returns ENOENT if no packet may be sent.
 */
int	tl_prepare_icmp_packet( int packet_phone, int icmp_phone, packet_t packet, services_t error );

/** Receives data from the socket into a packet.
 *  @param packet_phone The packet server module phone. Input parameter.
 *  @param packet The new created packet. Output parameter.
 *  @param prefix Reserved packet data prefix length. Input parameter.
 *  @param dimension The packet dimension. Input parameter.
 *  @param addr The destination address. Input parameter.
 *  @param addrlen The address length. Input parameter.
 *  @returns Number of bytes received.
 *  @returns EINVAL if the client does not send data.
 *  @returns ENOMEM if there is not enough memory left.
 *  @returns Other error codes as defined for the ipc_data_read_finalize() function.
 */
int	tl_socket_read_packet_data( int packet_phone, packet_ref packet, size_t prefix, const packet_dimension_ref dimension, const struct sockaddr * addr, socklen_t addrlen );

#endif

/** @}
 */

