source: mainline/uspace/lib/net/tl/tl_common.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: 6.4 KB
RevLine 
[21580dd]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_tl
30 * @{
31 */
32
33/** @file
34 * Transport layer common functions implementation.
35 * @see tl_common.h
36 */
37
38#include <async.h>
39#include <ipc/services.h>
40
[849ed54]41#include <net_err.h>
42#include <packet/packet.h>
43#include <packet/packet_client.h>
44#include <net_device.h>
45#include <icmp_interface.h>
46#include <in.h>
47#include <in6.h>
48#include <inet.h>
49#include <ip_interface.h>
50#include <socket_codes.h>
51#include <socket_errno.h>
52#include <tl_common.h>
[21580dd]53
[aadf01e]54DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
[21580dd]55
[aadf01e]56int tl_get_address_port(const struct sockaddr * addr, int addrlen, uint16_t * port){
57 const struct sockaddr_in * address_in;
58 const struct sockaddr_in6 * address_in6;
[21580dd]59
[aadf01e]60 if((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr))){
61 return EINVAL;
62 }
63 switch(addr->sa_family){
[21580dd]64 case AF_INET:
[aadf01e]65 if(addrlen != sizeof(struct sockaddr_in)){
66 return EINVAL;
67 }
68 address_in = (struct sockaddr_in *) addr;
69 *port = ntohs(address_in->sin_port);
[21580dd]70 break;
71 case AF_INET6:
[aadf01e]72 if(addrlen != sizeof(struct sockaddr_in6)){
73 return EINVAL;
74 }
75 address_in6 = (struct sockaddr_in6 *) addr;
76 *port = ntohs(address_in6->sin6_port);
[21580dd]77 break;
78 default:
79 return EAFNOSUPPORT;
80 }
81 return EOK;
82}
83
[aadf01e]84int tl_get_ip_packet_dimension(int ip_phone, packet_dimensions_ref packet_dimensions, device_id_t device_id, packet_dimension_ref * packet_dimension){
[21580dd]85 ERROR_DECLARE;
86
[aadf01e]87 if(! packet_dimension){
88 return EBADMEM;
89 }
[21580dd]90
[aadf01e]91 *packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
92 if(! * packet_dimension){
[21580dd]93 // ask for and remember them if not found
[aadf01e]94 *packet_dimension = malloc(sizeof(** packet_dimension));
95 if(! * packet_dimension){
96 return ENOMEM;
97 }
98 if(ERROR_OCCURRED(ip_packet_size_req(ip_phone, device_id, * packet_dimension))){
99 free(*packet_dimension);
[21580dd]100 return ERROR_CODE;
101 }
[aadf01e]102 ERROR_CODE = packet_dimensions_add(packet_dimensions, device_id, * packet_dimension);
103 if(ERROR_CODE < 0){
104 free(*packet_dimension);
[21580dd]105 return ERROR_CODE;
106 }
107 }
108 return EOK;
109}
110
[aadf01e]111int tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions, device_id_t device_id, size_t content){
112 packet_dimension_ref packet_dimension;
[b4ff8a6d]113
[aadf01e]114 packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
115 if(! packet_dimension){
116 return ENOENT;
117 }
[b4ff8a6d]118 packet_dimension->content = content;
[aadf01e]119 if(device_id != DEVICE_INVALID_ID){
120 packet_dimension = packet_dimensions_find(packet_dimensions, DEVICE_INVALID_ID);
121 if(packet_dimension){
122 if(packet_dimension->content >= content){
[b4ff8a6d]123 packet_dimension->content = content;
124 }else{
[aadf01e]125 packet_dimensions_exclude(packet_dimensions, DEVICE_INVALID_ID);
[b4ff8a6d]126 }
127 }
128 }
129 return EOK;
130}
131
[aadf01e]132int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port){
133 struct sockaddr_in * address_in;
134 struct sockaddr_in6 * address_in6;
135 size_t length;
[21580dd]136
[aadf01e]137 if(addrlen < 0){
138 return EINVAL;
139 }
140 length = (size_t) addrlen;
141 if(length < sizeof(struct sockaddr)){
142 return EINVAL;
143 }
144 switch(addr->sa_family){
[21580dd]145 case AF_INET:
[aadf01e]146 if(length != sizeof(struct sockaddr_in)){
147 return EINVAL;
148 }
149 address_in = (struct sockaddr_in *) addr;
150 address_in->sin_port = htons(port);
[21580dd]151 return EOK;
152 case AF_INET6:
[aadf01e]153 if(length != sizeof(struct sockaddr_in6)){
154 return EINVAL;
155 }
156 address_in6 = (struct sockaddr_in6 *) addr;
157 address_in6->sin6_port = htons(port);
[21580dd]158 return EOK;
159 default:
160 return EAFNOSUPPORT;
161 }
162}
163
[aadf01e]164int tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet, services_t error){
165 packet_t next;
166 uint8_t * src;
167 int length;
[21580dd]168
169 // detach the first packet and release the others
[aadf01e]170 next = pq_detach(packet);
171 if(next){
172 pq_release(packet_phone, packet_get_id(next));
173 }
174 length = packet_get_addr(packet, &src, NULL);
175 if((length > 0)
176 && (! error)
177 && (icmp_phone >= 0)
[21580dd]178 // set both addresses to the source one (avoids the source address deletion before setting the destination one)
[aadf01e]179 && (packet_set_addr(packet, src, src, (size_t) length) == EOK)){
[21580dd]180 return EOK;
181 }else{
[aadf01e]182 pq_release(packet_phone, packet_get_id(packet));
[21580dd]183 }
184 return ENOENT;
185}
186
[aadf01e]187int tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix, const packet_dimension_ref dimension, const struct sockaddr * addr, socklen_t addrlen){
[21580dd]188 ERROR_DECLARE;
189
[aadf01e]190 ipc_callid_t callid;
191 size_t length;
192 void * data;
[21580dd]193
[aadf01e]194 if(! dimension){
195 return EINVAL;
196 }
[21580dd]197 // get the data length
[aadf01e]198 if(! async_data_write_receive(&callid, &length)){
199 return EINVAL;
200 }
[21580dd]201 // get a new packet
[aadf01e]202 *packet = packet_get_4(packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix);
203 if(! packet){
204 return ENOMEM;
205 }
[21580dd]206 // allocate space in the packet
[aadf01e]207 data = packet_suffix(*packet, length);
208 if(! data){
209 pq_release(packet_phone, packet_get_id(*packet));
[21580dd]210 return ENOMEM;
211 }
212 // read the data into the packet
[aadf01e]213 if(ERROR_OCCURRED(async_data_write_finalize(callid, data, length))
[21580dd]214 // set the packet destination address
[aadf01e]215 || ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr, addrlen))){
216 pq_release(packet_phone, packet_get_id(*packet));
[21580dd]217 return ERROR_CODE;
218 }
[aadf01e]219 return (int) length;
[21580dd]220}
221
222/** @}
223 */
Note: See TracBrowser for help on using the repository browser.