/*
* 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
* @{
*/
/** @file
*/
#include <async.h>
#include <errno.h>
#include <unistd.h>
//#include <stdio.h>
#include <string.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
#include <sys/mman.h>
#include "../../err.h"
#include "../../messages.h"
#include "packet.h"
#include "packet_header.h"
#include "packet_client.h"
int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size );
packet_t packet_copy( int phone, services_t owner, const packet_t packet ){
packet_t new;
if( ! packet_is_valid( packet )) return NULL;
// new = ( packet_t ) mmap( NULL, packet->length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
// if( new == MAP_FAILED ) return NULL;
// memcpy( new, packet, packet->length );
new = packet_get_1( phone, owner, packet_get_data_length( packet ));
packet_copy_data( new, packet_get_data( packet ), packet_get_data_length( packet ));
return new;
}
int packet_copy_data( packet_t packet, const void * data, size_t length ){
if( ! packet_is_valid( packet )) return EINVAL;
if( packet->data_start + length >= packet->length ) return ENOMEM;
memcpy(( void * ) packet
+ packet
->data_start
, data
, length
);
if( packet->data_start + length > packet->data_end ){
packet->data_end = packet->data_start + length;
}
return EOK;
}
void * packet_prepend( packet_t packet, size_t length ){
if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) - 2 * packet->addr_len < length )) return NULL;
packet->data_start -= length;
return ( void * ) packet + packet->data_start;
}
void * packet_append( packet_t packet, size_t length ){
if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= packet->length )) return NULL;
packet->data_end += length;
return ( void * ) packet + packet->data_end - length;
}
int packet_trim( packet_t packet, size_t prefix, size_t sufix ){
if(( ! packet_is_valid( packet )) || ( prefix + sufix > packet->data_end - packet->data_start )) return EINVAL;
packet->data_start += prefix;
packet->data_end -= sufix;
return EOK;
}
packet_id_t packet_get_id( packet_t packet ){
return packet_is_valid( packet ) ? packet->packet_id : 0;
}
int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest ){
if( !( packet_is_valid( packet ) && src && dest )) return 0;
* src = ( void * ) packet + packet->src_addr;
* dest = ( void * ) packet + packet->dest_addr;
return packet->addr_len;
}
size_t packet_get_data_length( const packet_t packet ){
if( ! packet_is_valid( packet )) return 0;
return packet->data_end - packet->data_start;
}
void * packet_get_data( const packet_t packet ){
if( ! packet_is_valid( packet )) return NULL;
return ( void * ) packet + packet->data_start;
}
packet_mode_t packet_get_mode( const packet_t packet ){
if( packet_is_valid( packet )) return packet->mode;
return PM_ONEWAY;
}
int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len ){
size_t padding;
if( !( packet_is_valid( packet ) && ( packet->addr_len >= addr_len ))) return EINVAL;
padding = packet->addr_len - addr_len;
if( src ){
memcpy(( void * ) packet
+ packet
->src_addr
, src
, addr_len
);
memset(( void * ) packet
+ packet
->src_addr
+ addr_len
, 0, padding
);
}else{
memset(( void * ) packet
+ packet
->src_addr
+ addr_len
, 0, packet
->addr_len
);
}
if( dest ){
memcpy(( void * ) packet
+ packet
->dest_addr
, dest
, addr_len
);
memset(( void * ) packet
+ packet
->dest_addr
+ addr_len
, 0, padding
);
}else{
memset(( void * ) packet
+ packet
->dest_addr
+ addr_len
, 0, packet
->addr_len
);
}
return EOK;
}
int packet_set_mode( packet_t packet, packet_mode_t mode ){
if( ! packet_is_valid( packet )) return EINVAL;
packet->mode = mode;
return EOK;
}
int packet_set_owner( packet_t packet, services_t owner ){
if( ! packet_is_valid( packet )) return EINVAL;
packet->owner = owner;
return EOK;
}
int packet_translate( int phone, packet_ref packet, packet_id_t packet_id ){
ERROR_DECLARE;
unsigned int size;
if( ! packet ) return EINVAL;
* packet = pm_find( packet_id );
if( * packet ) return EOK;
ERROR_PROPAGATE( async_req_1_1( phone, NET_PACKET_GET_SIZE, packet_id, & size ));
return packet_return( phone, packet, packet_id, size );
}
int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size ){
ERROR_DECLARE;
aid_t message;
ipc_call_t answer;
message = async_send_1( phone, NET_PACKET_GET, packet_id, & answer );
* packet = ( packet_t ) as_get_mappable_page( size );
if( ERROR_OCCURED( ipc_share_in_start_0_0( phone, * packet, size ))
|| ERROR_OCCURED( pm_add( * packet ))){
munmap( * packet, size );
async_wait_for( message, NULL );
return ERROR_CODE;
}
async_wait_for( message, NULL );
return EOK;
}
packet_t packet_get_5( int phone, services_t owner, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_sufix ){
ERROR_DECLARE;
packet_id_t packet_id;
unsigned int size;
packet_t packet;
if( ERROR_OCCURED( async_req_5_2( phone, NET_PACKET_CREATE_5, owner, max_content, addr_len, max_prefix, max_sufix, & packet_id, & size ))
|| ERROR_OCCURED( packet_return( phone, & packet, packet_id, size ))){
return NULL;
}
return packet;
}
packet_t packet_get_1( int phone, services_t owner, size_t content ){
ERROR_DECLARE;
packet_id_t packet_id;
unsigned int size;
packet_t packet;
if( ERROR_OCCURED( async_req_2_2( phone, NET_PACKET_CREATE_1, owner, content, & packet_id, & size ))
|| ERROR_OCCURED( packet_return( phone, & packet, packet_id, size ))){
return NULL;
}
return packet;
}
void packet_release( int phone, packet_id_t packet_id ){
async_msg_1( phone, NET_PACKET_RELEASE, packet_id );
}
/** @}
*/