Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (c) 2009 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 packet
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  Packet client implementation.
  35.  */
  36.  
  37. #include <errno.h>
  38. #include <mem.h>
  39. #include <unistd.h>
  40. //#include <stdio.h>
  41.  
  42. #include <sys/mman.h>
  43.  
  44. #include "../../messages.h"
  45.  
  46. #include "packet.h"
  47. #include "packet_header.h"
  48. #include "packet_client.h"
  49.  
  50. int packet_copy_data( packet_t packet, const void * data, size_t length ){
  51.     if( ! packet_is_valid( packet )) return EINVAL;
  52.     if( packet->data_start + length >= packet->length ) return ENOMEM;
  53.     memcpy(( void * ) packet + packet->data_start, data, length );
  54.     if( packet->data_start + length > packet->data_end ){
  55.         packet->data_end = packet->data_start + length;
  56.     }
  57.     return EOK;
  58. }
  59.  
  60. void * packet_prefix( packet_t packet, size_t length ){
  61.     if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) - 2 * ( packet->dest_addr - packet->src_addr ) < length )) return NULL;
  62.     packet->data_start -= length;
  63.     return ( void * ) packet + packet->data_start;
  64. }
  65.  
  66. void * packet_suffix( packet_t packet, size_t length ){
  67.     if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= packet->length )) return NULL;
  68.     packet->data_end += length;
  69.     return ( void * ) packet + packet->data_end - length;
  70. }
  71.  
  72. int packet_trim( packet_t packet, size_t prefix, size_t suffix ){
  73.     if( ! packet_is_valid( packet )) return EINVAL;
  74.     if( prefix + suffix > packet->data_end - packet->data_start ) return ENOMEM;
  75.     packet->data_start += prefix;
  76.     packet->data_end -= suffix;
  77.     return EOK;
  78. }
  79.  
  80. packet_id_t packet_get_id( const packet_t packet ){
  81.     return packet_is_valid( packet ) ? packet->packet_id : 0;
  82. }
  83.  
  84. int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest ){
  85.     if( ! packet_is_valid( packet )) return EINVAL;
  86.     if( ! packet->addr_len ) return 0;
  87.     if( src ) * src = ( void * ) packet + packet->src_addr;
  88.     if( dest ) * dest = ( void * ) packet + packet->dest_addr;
  89.     return packet->addr_len;
  90. }
  91.  
  92. size_t packet_get_data_length( const packet_t packet ){
  93.     if( ! packet_is_valid( packet )) return 0;
  94.     return packet->data_end - packet->data_start;
  95. }
  96.  
  97. void * packet_get_data( const packet_t packet ){
  98.     if( ! packet_is_valid( packet )) return NULL;
  99.     return ( void * ) packet + packet->data_start;
  100. }
  101.  
  102. int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len ){
  103.     size_t  padding;
  104.     size_t  allocated;
  105.  
  106.     if( ! packet_is_valid( packet )) return EINVAL;
  107.     allocated = packet->dest_addr - packet->src_addr;
  108.     if( allocated < addr_len ) return ENOMEM;
  109.     padding = allocated - addr_len;
  110.     packet->addr_len = addr_len;
  111.     if( src ){
  112.         memcpy(( void * ) packet + packet->src_addr, src, addr_len );
  113.         if( padding ) bzero(( void * ) packet + packet->src_addr + addr_len, padding );
  114.     }else{
  115.         bzero(( void * ) packet + packet->src_addr, allocated );
  116.     }
  117.     if( dest ){
  118.         memcpy(( void * ) packet + packet->dest_addr, dest, addr_len );
  119.         if( padding ) bzero(( void * ) packet + packet->dest_addr + addr_len, padding );
  120.     }else{
  121.         bzero(( void * ) packet + packet->dest_addr, allocated );
  122.     }
  123.     return EOK;
  124. }
  125.  
  126. /** @}
  127.  */
  128.