Subversion Repositories HelenOS

Rev

Rev 3912 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Lukas Mejdrech
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup net
  30.  * @{
  31.  */
  32.  
  33. /** @file
  34.  */
  35.  
  36. #include <errno.h>
  37. #include <unistd.h>
  38. #include <string.h>
  39.  
  40. #include <ipc/ipc.h>
  41. #include <sys/mman.h>
  42.  
  43. #include "../../err.h"
  44.  
  45. #include "packet.h"
  46.  
  47. #define PACKET_MAGIC_VALUE  0x11227788
  48.  
  49. struct packet{
  50.     size_t  length;
  51.     size_t  max_prefix;
  52.     size_t  max_content;
  53.     size_t  data_start;
  54.     size_t  data_end;
  55.     int magic_value;
  56. };
  57.  
  58. int packet_is_valid( packet_t packet );
  59.  
  60. packet_t packet_create( size_t max_prefix, size_t max_content, size_t max_sufix ){
  61.     size_t      length;
  62.     packet_t    packet;
  63.  
  64.     length = max_prefix + max_content + max_sufix;
  65.     packet = ( packet_t ) mmap( NULL, sizeof( struct packet ) + length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
  66.     if( packet == MAP_FAILED ) return NULL;
  67.     packet->length = length;
  68.     packet->max_prefix = max_prefix;
  69.     packet->max_content = max_content;
  70.     packet->data_start = sizeof( struct packet ) + packet->max_prefix;
  71.     packet->data_end = packet->data_start;
  72.     packet->magic_value = PACKET_MAGIC_VALUE;
  73.     return packet;
  74. }
  75.  
  76. int packet_is_valid( packet_t packet ){
  77.     return packet && ( packet->magic_value == PACKET_MAGIC_VALUE );
  78. }
  79.  
  80. packet_t packet_copy( packet_t packet ){
  81.     packet_t    new;
  82.  
  83.     if( ! packet_is_valid( packet )) return NULL;
  84.     new = ( packet_t ) mmap( NULL, packet->length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
  85.     if( new == MAP_FAILED ) return NULL;
  86.     memcpy( new, packet, packet->length );
  87.     return new;
  88. }
  89.  
  90. int packet_copy_data( packet_t packet, void * data, size_t length ){
  91.     if( ! packet_is_valid( packet )) return EINVAL;
  92.     if( packet->data_start + length >= (( size_t ) packet ) + packet->length ) return ENOMEM;
  93.     memcpy( packet + packet->data_start, data, length );
  94.     if( packet->data_start + length > packet->data_end ){
  95.         packet->data_end = packet->data_start + length;
  96.     }
  97.     return EOK;
  98. }
  99.  
  100. void * packet_prepend( packet_t packet, size_t length ){
  101.     if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) < length )) return NULL;
  102.     packet->data_start -= length;
  103.     return packet + packet->data_start;
  104. }
  105.  
  106. void * packet_append( packet_t packet, size_t length ){
  107.     if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= (( size_t ) packet ) + packet->length )) return NULL;
  108.     packet->data_end += length;
  109.     return packet + packet->data_end - length;
  110. }
  111.  
  112. int packet_trim( packet_t packet, size_t prefix, size_t sufix ){
  113.     if(( ! packet_is_valid( packet )) || ( prefix + sufix > packet->data_end - packet->data_start )) return EINVAL;
  114.     packet->data_start += prefix;
  115.     packet->data_end -= sufix;
  116.     return EOK;
  117. }
  118.  
  119. int packet_send( packet_t packet, int phone ){
  120.     if( ! packet_is_valid( packet )) return EINVAL;
  121.     return ipc_share_out_start( phone, packet, PROTO_READ | PROTO_WRITE );
  122. }
  123.  
  124. int packet_receive( packet_ref packet ){
  125.     ERROR_DECLARE;
  126.  
  127.     ipc_callid_t    callid;
  128.     size_t      size;
  129.     int     flags;
  130.  
  131.     if( ! packet ) return EINVAL;
  132.     if( ! ipc_share_out_receive( & callid, & size, & flags )) return EINVAL;
  133.     * packet = ( packet_t ) as_get_mappable_page( size );
  134.     if( !( * packet )) return ENOMEM;
  135.     if( ERROR_OCCURED( ipc_share_out_finalize( callid, * packet ))){
  136.         munmap( * packet, size );
  137.         return ERROR_CODE;
  138.     }
  139.     return EOK;
  140. }
  141.  
  142. int packet_destroy( packet_t packet ){
  143.     if( ! packet_is_valid( packet )) return EINVAL;
  144.     return munmap( packet, sizeof( struct packet ) + packet->length );
  145. }
  146.  
  147. size_t packet_get_data_length( packet_t packet ){
  148.     if( ! packet_is_valid( packet )) return 0;
  149.     return packet->data_end - packet->data_start;
  150. }
  151.  
  152. void * packet_get_data( packet_t packet ){
  153.     if( ! packet_is_valid( packet )) return NULL;
  154.     return packet + packet->data_start;
  155. }
  156.  
  157. /** @}
  158.  */
  159.