source: mainline/uspace/lib/net/il/ip_client.c@ d9e2e0e

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

Include socket and networking error codes in the standard library's errno.h.

  • 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 ip
30 * @{
31 */
32
33/** @file
34 * IP client interface implementation.
35 * @see ip_client.h
36 */
37
38#include <errno.h>
39#include <sys/types.h>
40
[849ed54]41#include <ip_client.h>
42#include <packet/packet.h>
43#include <packet/packet_client.h>
44#include <ip_header.h>
[21580dd]45
[a64c64d]46size_t ip_client_header_length(packet_t packet){
47 ip_header_ref header;
48
49 header = (ip_header_ref) packet_get_data(packet);
50 if((! header)
51 || (packet_get_data_length(packet) < sizeof(ip_header_t))){
52 return 0;
53 }
54 return IP_HEADER_LENGTH(header);
55}
56
[14f1db0]57int ip_client_get_pseudo_header(ip_protocol_t protocol, struct sockaddr * src, socklen_t srclen, struct sockaddr * dest, socklen_t destlen, size_t data_length, void **header, size_t * headerlen){
[a64c64d]58 ipv4_pseudo_header_ref header_in;
59 struct sockaddr_in * address_in;
60
61 if(!(header && headerlen)){
62 return EBADMEM;
63 }
64 if(!(src && dest && (srclen > 0) && ((size_t) srclen >= sizeof(struct sockaddr)) && (srclen == destlen) && (src->sa_family == dest->sa_family))){
65 return EINVAL;
66 }
67
68 switch(src->sa_family){
69 case AF_INET:
70 if(srclen != sizeof(struct sockaddr_in)){
71 return EINVAL;
72 }
73 *headerlen = sizeof(*header_in);
74 header_in = (ipv4_pseudo_header_ref) malloc(*headerlen);
75 if(! header_in){
76 return ENOMEM;
77 }
78 bzero(header_in, * headerlen);
79 address_in = (struct sockaddr_in *) dest;
80 header_in->destination_address = address_in->sin_addr.s_addr;
81 address_in = (struct sockaddr_in *) src;
82 header_in->source_address = address_in->sin_addr.s_addr;
83 header_in->protocol = protocol;
84 header_in->data_length = htons(data_length);
[14f1db0]85 *header = header_in;
[a64c64d]86 return EOK;
87 // TODO IPv6
88/* case AF_INET6:
89 if(addrlen != sizeof(struct sockaddr_in6)){
90 return EINVAL;
91 }
92 address_in6 = (struct sockaddr_in6 *) addr;
93 return EOK;
94*/ default:
95 return EAFNOSUPPORT;
96 }
97}
98
[aadf01e]99int ip_client_prepare_packet(packet_t packet, ip_protocol_t protocol, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, size_t ipopt_length){
100 ip_header_ref header;
101 uint8_t * data;
102 size_t padding;
[21580dd]103
[a64c64d]104 // compute the padding if IP options are set
105 // multiple of 4 bytes
[21580dd]106 padding = ipopt_length % 4;
[aadf01e]107 if(padding){
[21580dd]108 padding = 4 - padding;
109 ipopt_length += padding;
110 }
[a64c64d]111
112 // prefix the header
[aadf01e]113 data = (uint8_t *) packet_prefix(packet, sizeof(ip_header_t) + padding);
114 if(! data){
115 return ENOMEM;
116 }
[a64c64d]117
118 // add the padding
[aadf01e]119 while(padding --){
120 data[sizeof(ip_header_t) + padding] = IPOPT_NOOP;
121 }
[a64c64d]122
123 // set the header
[aadf01e]124 header = (ip_header_ref) data;
125 header->header_length = IP_COMPUTE_HEADER_LENGTH(sizeof(ip_header_t) + ipopt_length);
126 header->ttl = (ttl ? ttl : IPDEFTTL); //(((ttl) <= MAXTTL) ? ttl : MAXTTL) : IPDEFTTL;
[21580dd]127 header->tos = tos;
128 header->protocol = protocol;
[a64c64d]129
[aadf01e]130 if(dont_fragment){
131 header->flags = IPFLAG_DONT_FRAGMENT;
132 }
[21580dd]133 return EOK;
134}
135
[aadf01e]136int ip_client_process_packet(packet_t packet, ip_protocol_t * protocol, ip_ttl_t * ttl, ip_tos_t * tos, int * dont_fragment, size_t * ipopt_length){
137 ip_header_ref header;
[21580dd]138
[aadf01e]139 header = (ip_header_ref) packet_get_data(packet);
140 if((! header)
141 || (packet_get_data_length(packet) < sizeof(ip_header_t))){
[21580dd]142 return ENOMEM;
143 }
[a64c64d]144
[aadf01e]145 if(protocol){
146 *protocol = header->protocol;
147 }
148 if(ttl){
149 *ttl = header->ttl;
150 }
151 if(tos){
152 *tos = header->tos;
153 }
154 if(dont_fragment){
155 *dont_fragment = header->flags &IPFLAG_DONT_FRAGMENT;
156 }
157 if(ipopt_length){
158 *ipopt_length = IP_HEADER_LENGTH(header) - sizeof(ip_header_t);
159 return sizeof(ip_header_t);
[21580dd]160 }else{
[aadf01e]161 return IP_HEADER_LENGTH(header);
[21580dd]162 }
163}
164
[14f1db0]165int ip_client_set_pseudo_header_data_length(void *header, size_t headerlen, size_t data_length){
[aadf01e]166 ipv4_pseudo_header_ref header_in;
[21580dd]167
[aadf01e]168 if(! header){
169 return EBADMEM;
170 }
[a64c64d]171
[aadf01e]172 if(headerlen == sizeof(ipv4_pseudo_header_t)){
173 header_in = (ipv4_pseudo_header_ref) header;
174 header_in->data_length = htons(data_length);
[21580dd]175 return EOK;
[a64c64d]176 // TODO IPv6
[21580dd]177 }else{
178 return EINVAL;
179 }
180}
181
182/** @}
183 */
Note: See TracBrowser for help on using the repository browser.