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

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

Move the rest of net_messages.h to standard library.

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