Rev 4395 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3901 | mejdrech | 1 | /* |
3912 | mejdrech | 2 | * Copyright (c) 2009 Lukas Mejdrech |
3901 | 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 | |||
3912 | mejdrech | 29 | /** @addtogroup packet |
3901 | mejdrech | 30 | * @{ |
31 | */ |
||
32 | |||
33 | /** @file |
||
3912 | mejdrech | 34 | * Packet client implementation. |
3901 | mejdrech | 35 | */ |
36 | |||
37 | #include <errno.h> |
||
4192 | mejdrech | 38 | #include <mem.h> |
3901 | mejdrech | 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 | |||
3912 | mejdrech | 60 | void * packet_prefix( packet_t packet, size_t length ){ |
4505 | mejdrech | 61 | if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) - 2 * ( packet->dest_addr - packet->src_addr ) < length )) return NULL; |
3901 | mejdrech | 62 | packet->data_start -= length; |
63 | return ( void * ) packet + packet->data_start; |
||
64 | } |
||
65 | |||
3912 | mejdrech | 66 | void * packet_suffix( packet_t packet, size_t length ){ |
3901 | mejdrech | 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 | |||
3912 | mejdrech | 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; |
||
3901 | mejdrech | 75 | packet->data_start += prefix; |
3912 | mejdrech | 76 | packet->data_end -= suffix; |
3901 | mejdrech | 77 | return EOK; |
78 | } |
||
79 | |||
3912 | mejdrech | 80 | packet_id_t packet_get_id( const packet_t packet ){ |
3901 | mejdrech | 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 ){ |
||
4395 | mejdrech | 85 | if( ! packet_is_valid( packet )) return EINVAL; |
3912 | mejdrech | 86 | if( ! packet->addr_len ) return 0; |
4395 | mejdrech | 87 | if( src ) * src = ( void * ) packet + packet->src_addr; |
88 | if( dest ) * dest = ( void * ) packet + packet->dest_addr; |
||
3901 | mejdrech | 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; |
||
4395 | mejdrech | 104 | size_t allocated; |
3901 | mejdrech | 105 | |
3912 | mejdrech | 106 | if( ! packet_is_valid( packet )) return EINVAL; |
4395 | mejdrech | 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; |
||
3901 | mejdrech | 111 | if( src ){ |
112 | memcpy(( void * ) packet + packet->src_addr, src, addr_len ); |
||
4243 | mejdrech | 113 | if( padding ) bzero(( void * ) packet + packet->src_addr + addr_len, padding ); |
3901 | mejdrech | 114 | }else{ |
4395 | mejdrech | 115 | bzero(( void * ) packet + packet->src_addr, allocated ); |
3901 | mejdrech | 116 | } |
117 | if( dest ){ |
||
118 | memcpy(( void * ) packet + packet->dest_addr, dest, addr_len ); |
||
4243 | mejdrech | 119 | if( padding ) bzero(( void * ) packet + packet->dest_addr + addr_len, padding ); |
3901 | mejdrech | 120 | }else{ |
4395 | mejdrech | 121 | bzero(( void * ) packet + packet->dest_addr, allocated ); |
3901 | mejdrech | 122 | } |
123 | return EOK; |
||
124 | } |
||
125 | |||
126 | /** @} |
||
127 | */ |