source: mainline/uspace/lib/net/generic/packet_client.c@ c69d327

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c69d327 was c69d327, checked in by Jakub Jermar <jakub@…>, 15 years ago

Move the common packet interface to libc.

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