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