Subversion Repositories HelenOS

Rev

Rev 3990 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3901 mejdrech 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 <async.h>
37
#include <errno.h>
38
#include <unistd.h>
39
//#include <stdio.h>
40
#include <string.h>
41
 
42
#include <ipc/ipc.h>
43
#include <ipc/services.h>
44
#include <sys/mman.h>
45
 
46
#include "../../err.h"
47
#include "../../messages.h"
48
 
49
#include "packet.h"
50
#include "packet_header.h"
51
#include "packet_client.h"
52
 
53
int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size );
54
 
55
packet_t packet_copy( int phone, services_t owner, const packet_t packet ){
56
    packet_t    new;
57
 
58
    if( ! packet_is_valid( packet )) return NULL;
59
//  new = ( packet_t ) mmap( NULL, packet->length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
60
//  if( new == MAP_FAILED ) return NULL;
61
//  memcpy( new, packet, packet->length );
62
    new = packet_get_1( phone, owner, packet_get_data_length( packet ));
63
    packet_copy_data( new, packet_get_data( packet ), packet_get_data_length( packet ));
64
    return new;
65
}
66
 
67
int packet_copy_data( packet_t packet, const void * data, size_t length ){
68
    if( ! packet_is_valid( packet )) return EINVAL;
69
    if( packet->data_start + length >= packet->length ) return ENOMEM;
70
    memcpy(( void * ) packet + packet->data_start, data, length );
71
    if( packet->data_start + length > packet->data_end ){
72
        packet->data_end = packet->data_start + length;
73
    }
74
    return EOK;
75
}
76
 
77
void * packet_prepend( packet_t packet, size_t length ){
78
    if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) - 2 * packet->addr_len < length )) return NULL;
79
    packet->data_start -= length;
80
    return ( void * ) packet + packet->data_start;
81
}
82
 
83
void * packet_append( packet_t packet, size_t length ){
84
    if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= packet->length )) return NULL;
85
    packet->data_end += length;
86
    return ( void * ) packet + packet->data_end - length;
87
}
88
 
89
int packet_trim( packet_t packet, size_t prefix, size_t sufix ){
90
    if(( ! packet_is_valid( packet )) || ( prefix + sufix > packet->data_end - packet->data_start )) return EINVAL;
91
    packet->data_start += prefix;
92
    packet->data_end -= sufix;
93
    return EOK;
94
}
95
 
96
packet_id_t packet_get_id( packet_t packet ){
97
    return packet_is_valid( packet ) ? packet->packet_id : 0;
98
}
99
 
100
int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest ){
101
    if( !( packet_is_valid( packet ) && src && dest )) return 0;
102
    * src = ( void * ) packet + packet->src_addr;
103
    * dest = ( void * ) packet + packet->dest_addr;
104
    return packet->addr_len;
105
}
106
 
107
size_t packet_get_data_length( const packet_t packet ){
108
    if( ! packet_is_valid( packet )) return 0;
109
    return packet->data_end - packet->data_start;
110
}
111
 
112
void * packet_get_data( const packet_t packet ){
113
    if( ! packet_is_valid( packet )) return NULL;
114
    return ( void * ) packet + packet->data_start;
115
}
116
 
117
packet_mode_t packet_get_mode( const packet_t packet ){
118
    if( packet_is_valid( packet )) return packet->mode;
119
    return PM_ONEWAY;
120
}
121
 
122
int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len ){
123
    size_t  padding;
124
 
125
    if( !( packet_is_valid( packet ) && ( packet->addr_len >= addr_len ))) return EINVAL;
126
    padding = packet->addr_len - addr_len;
127
    if( src ){
128
        memcpy(( void * ) packet + packet->src_addr, src, addr_len );
129
        memset(( void * ) packet + packet->src_addr + addr_len, 0, padding );
130
    }else{
131
        memset(( void * ) packet + packet->src_addr + addr_len, 0, packet->addr_len );
132
    }
133
    if( dest ){
134
        memcpy(( void * ) packet + packet->dest_addr, dest, addr_len );
135
        memset(( void * ) packet + packet->dest_addr + addr_len, 0, padding );
136
    }else{
137
        memset(( void * ) packet + packet->dest_addr + addr_len, 0, packet->addr_len );
138
    }
139
    return EOK;
140
}
141
 
142
int packet_set_mode( packet_t packet, packet_mode_t mode ){
143
    if( ! packet_is_valid( packet )) return EINVAL;
144
    packet->mode = mode;
145
    return EOK;
146
}
147
 
148
int packet_set_owner( packet_t packet, services_t owner ){
149
    if( ! packet_is_valid( packet )) return EINVAL;
150
    packet->owner = owner;
151
    return EOK;
152
}
153
 
154
int packet_translate( int phone, packet_ref packet, packet_id_t packet_id ){
155
    ERROR_DECLARE;
156
 
157
    unsigned int        size;
158
 
159
    if( ! packet ) return EINVAL;
160
    * packet = pm_find( packet_id );
161
    if( * packet ) return EOK;
162
    ERROR_PROPAGATE( async_req_1_1( phone, NET_PACKET_GET_SIZE, packet_id, & size ));
163
    return packet_return( phone, packet, packet_id, size );
164
}
165
 
166
int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size ){
167
    ERROR_DECLARE;
168
 
169
    aid_t       message;
170
    ipc_call_t  answer;
171
 
172
    message = async_send_1( phone, NET_PACKET_GET, packet_id, & answer );
173
    * packet = ( packet_t ) as_get_mappable_page( size );
174
    if( ERROR_OCCURED( ipc_share_in_start_0_0( phone, * packet, size ))
175
    || ERROR_OCCURED( pm_add( * packet ))){
176
        munmap( * packet, size );
177
        async_wait_for( message, NULL );
178
        return ERROR_CODE;
179
    }
180
    async_wait_for( message, NULL );
181
    return EOK;
182
}
183
 
184
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 ){
185
    ERROR_DECLARE;
186
 
187
    packet_id_t packet_id;
188
    unsigned int size;
189
    packet_t packet;
190
 
191
    if( ERROR_OCCURED( async_req_5_2( phone, NET_PACKET_CREATE_5, owner, max_content, addr_len, max_prefix, max_sufix, & packet_id, & size ))
192
    || ERROR_OCCURED( packet_return( phone, & packet, packet_id, size ))){
193
        return NULL;
194
    }
195
    return packet;
196
}
197
 
198
packet_t packet_get_1( int phone, services_t owner, size_t content ){
199
    ERROR_DECLARE;
200
 
201
    packet_id_t packet_id;
202
    unsigned int    size;
203
    packet_t    packet;
204
 
205
    if( ERROR_OCCURED( async_req_2_2( phone, NET_PACKET_CREATE_1, owner, content, & packet_id, & size ))
206
    || ERROR_OCCURED( packet_return( phone, & packet, packet_id, size ))){
207
        return NULL;
208
    }
209
    return packet;
210
}
211
 
212
void packet_release( int phone, packet_id_t packet_id ){
213
    async_msg_1( phone, NET_PACKET_RELEASE, packet_id );
214
}
215
 
216
/** @}
217
 */