source: mainline/uspace/lib/socket/packet/packet_client.c@ 849ed54

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 849ed54 was 849ed54, checked in by Martin Decky <martin@…>, 15 years ago

Networking work:
Split the networking stack into end-user library (libsocket) and two helper libraries (libnet and libnetif).
Don't use over-the-hand compiling and linking, but rather separation of conserns.
There might be still some issues and the non-modular networking architecture is currently broken, but this will be fixed soon.

  • Property mode set to 100644
File size: 5.1 KB
Line 
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
41#include <sys/mman.h>
42
43#include <net_messages.h>
44#include <packet/packet.h>
45#include <packet/packet_header.h>
46#include <packet/packet_client.h>
47
48int packet_copy_data(packet_t packet, const void * data, size_t length){
49 if(! packet_is_valid(packet)){
50 return EINVAL;
51 }
52 if(packet->data_start + length >= packet->length){
53 return ENOMEM;
54 }
55 memcpy((void *) packet + packet->data_start, data, length);
56 if(packet->data_start + length > packet->data_end){
57 packet->data_end = packet->data_start + length;
58 }
59 return EOK;
60}
61
62void * packet_prefix(packet_t packet, size_t length){
63 if((! packet_is_valid(packet)) || (packet->data_start - sizeof(struct packet) - 2 * (packet->dest_addr - packet->src_addr) < length)){
64 return NULL;
65 }
66 packet->data_start -= length;
67 return (void *) packet + packet->data_start;
68}
69
70void * packet_suffix(packet_t packet, size_t length){
71 if((! packet_is_valid(packet)) || (packet->data_end + length >= packet->length)){
72 return NULL;
73 }
74 packet->data_end += length;
75 return (void *) packet + packet->data_end - length;
76}
77
78int packet_trim(packet_t packet, size_t prefix, size_t suffix){
79 if(! packet_is_valid(packet)){
80 return EINVAL;
81 }
82 if(prefix + suffix > PACKET_DATA_LENGTH(packet)){
83 return ENOMEM;
84 }
85 packet->data_start += prefix;
86 packet->data_end -= suffix;
87 return EOK;
88}
89
90packet_id_t packet_get_id(const packet_t packet){
91 return packet_is_valid(packet) ? packet->packet_id : 0;
92}
93
94int packet_get_addr(const packet_t packet, uint8_t ** src, uint8_t ** dest){
95 if(! packet_is_valid(packet)){
96 return EINVAL;
97 }
98 if(! packet->addr_len){
99 return 0;
100 }
101 if(src){
102 *src = (void *) packet + packet->src_addr;
103 }
104 if(dest){
105 *dest = (void *) packet + packet->dest_addr;
106 }
107 return packet->addr_len;
108}
109
110size_t packet_get_data_length(const packet_t packet){
111 if(! packet_is_valid(packet)){
112 return 0;
113 }
114 return PACKET_DATA_LENGTH(packet);
115}
116
117void * packet_get_data(const packet_t packet){
118 if(! packet_is_valid(packet)){
119 return NULL;
120 }
121 return (void *) packet + packet->data_start;
122}
123
124int packet_set_addr(packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len){
125 size_t padding;
126 size_t allocated;
127
128 if(! packet_is_valid(packet)){
129 return EINVAL;
130 }
131 allocated = PACKET_MAX_ADDRESS_LENGTH(packet);
132 if(allocated < addr_len){
133 return ENOMEM;
134 }
135 padding = allocated - addr_len;
136 packet->addr_len = addr_len;
137 if(src){
138 memcpy((void *) packet + packet->src_addr, src, addr_len);
139 if(padding){
140 bzero((void *) packet + packet->src_addr + addr_len, padding);
141 }
142 }else{
143 bzero((void *) packet + packet->src_addr, allocated);
144 }
145 if(dest){
146 memcpy((void *) packet + packet->dest_addr, dest, addr_len);
147 if(padding){
148 bzero((void *) packet + packet->dest_addr + addr_len, padding);
149 }
150 }else{
151 bzero((void *) packet + packet->dest_addr, allocated);
152 }
153 return EOK;
154}
155
156packet_t packet_get_copy(int phone, packet_t packet){
157 packet_t copy;
158 uint8_t * src;
159 uint8_t * dest;
160 size_t addrlen;
161
162 if(! packet_is_valid(packet)){
163 return NULL;
164 }
165 // get a new packet
166 copy = packet_get_4(phone, PACKET_DATA_LENGTH(packet), PACKET_MAX_ADDRESS_LENGTH(packet), packet->max_prefix, PACKET_MIN_SUFFIX(packet));
167 if(! copy){
168 return NULL;
169 }
170 // get addresses
171 addrlen = packet_get_addr(packet, &src, &dest);
172 // copy data
173 if((packet_copy_data(copy, packet_get_data(packet), PACKET_DATA_LENGTH(packet)) == EOK)
174 // copy addresses if present
175 && ((addrlen <= 0) || (packet_set_addr(copy, src, dest, addrlen) == EOK))){
176 copy->order = packet->order;
177 copy->metric = packet->metric;
178 return copy;
179 }else{
180 pq_release(phone, copy->packet_id);
181 return NULL;
182 }
183}
184
185/** @}
186 */
Note: See TracBrowser for help on using the repository browser.