Rev 4728 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4728 | Rev 4743 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Lukas Mejdrech |
2 | * Copyright (c) 2009 Lukas Mejdrech |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup ip |
29 | /** @addtogroup ip |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** @file |
33 | /** @file |
34 | * IP client interface implementation. |
34 | * IP client interface implementation. |
35 | * @see ip_client.h |
35 | * @see ip_client.h |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <errno.h> |
38 | #include <errno.h> |
39 | 39 | ||
40 | #include <sys/types.h> |
40 | #include <sys/types.h> |
41 | 41 | ||
42 | #include "../../include/ip_client.h" |
42 | #include "../../include/ip_client.h" |
43 | #include "../../include/socket_errno.h" |
43 | #include "../../include/socket_errno.h" |
44 | 44 | ||
45 | #include "../../structures/packet/packet.h" |
45 | #include "../../structures/packet/packet.h" |
46 | #include "../../structures/packet/packet_client.h" |
46 | #include "../../structures/packet/packet_client.h" |
47 | 47 | ||
48 | #include "ip_header.h" |
48 | #include "ip_header.h" |
49 | 49 | ||
50 | 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 ){ |
50 | 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 ){ |
51 | ip_header_ref header; |
51 | ip_header_ref header; |
52 | uint8_t * data; |
52 | uint8_t * data; |
53 | size_t padding; |
53 | size_t padding; |
54 | 54 | ||
55 | padding = ipopt_length % 4; |
55 | padding = ipopt_length % 4; |
56 | if( padding ){ |
56 | if( padding ){ |
57 | padding = 4 - padding; |
57 | padding = 4 - padding; |
58 | ipopt_length += padding; |
58 | ipopt_length += padding; |
59 | } |
59 | } |
60 | data = ( uint8_t * ) packet_prefix( packet, sizeof( ip_header_t ) + padding ); |
60 | data = ( uint8_t * ) packet_prefix( packet, sizeof( ip_header_t ) + padding ); |
61 | if( ! data ) return ENOMEM; |
61 | if( ! data ) return ENOMEM; |
62 | while( padding -- ) data[ sizeof( ip_header_t ) + padding ] = IPOPT_NOOP; |
62 | while( padding -- ) data[ sizeof( ip_header_t ) + padding ] = IPOPT_NOOP; |
63 | header = ( ip_header_ref ) data; |
63 | header = ( ip_header_ref ) data; |
64 | header->header_length = IP_COMPUTE_HEADER_LENGTH( sizeof( ip_header_t ) + ipopt_length ); |
64 | header->header_length = IP_COMPUTE_HEADER_LENGTH( sizeof( ip_header_t ) + ipopt_length ); |
65 | header->ttl = ttl ? (( ttl <= MAXTTL ) ? ttl : MAXTTL ) : IPDEFTTL; |
65 | header->ttl = ttl ? (( ttl <= MAXTTL ) ? ttl : MAXTTL ) : IPDEFTTL; |
66 | header->tos = tos; |
66 | header->tos = tos; |
67 | header->protocol = protocol; |
67 | header->protocol = protocol; |
68 | if( dont_fragment ) header->flags = IPFLAG_DONT_FRAGMENT; |
68 | if( dont_fragment ) header->flags = IPFLAG_DONT_FRAGMENT; |
69 | return EOK; |
69 | return EOK; |
70 | } |
70 | } |
71 | 71 | ||
72 | 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 ){ |
72 | 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 ){ |
73 | ip_header_ref header; |
73 | ip_header_ref header; |
74 | 74 | ||
75 | header = ( ip_header_ref ) packet_get_data( packet ); |
75 | header = ( ip_header_ref ) packet_get_data( packet ); |
76 | if(( ! header ) |
76 | if(( ! header ) |
77 | || ( packet_get_data_length( packet ) < sizeof( ip_header_t ))){ |
77 | || ( packet_get_data_length( packet ) < sizeof( ip_header_t ))){ |
78 | return ENOMEM; |
78 | return ENOMEM; |
79 | } |
79 | } |
80 | if( protocol ) * protocol = header->protocol; |
80 | if( protocol ) * protocol = header->protocol; |
81 | if( ttl ) * ttl = header->ttl; |
81 | if( ttl ) * ttl = header->ttl; |
82 | if( tos ) * tos = header->tos; |
82 | if( tos ) * tos = header->tos; |
83 | if( dont_fragment ) * dont_fragment = header->flags & IPFLAG_DONT_FRAGMENT; |
83 | if( dont_fragment ) * dont_fragment = header->flags & IPFLAG_DONT_FRAGMENT; |
84 | if( ipopt_length ){ |
84 | if( ipopt_length ){ |
85 | * ipopt_length = IP_HEADER_LENGTH( header ) - sizeof( ip_header_t ); |
85 | * ipopt_length = IP_HEADER_LENGTH( header ) - sizeof( ip_header_t ); |
86 | return sizeof( ip_header_t ); |
86 | return sizeof( ip_header_t ); |
87 | }else{ |
87 | }else{ |
88 | return IP_HEADER_LENGTH( header ); |
88 | return IP_HEADER_LENGTH( header ); |
89 | } |
89 | } |
90 | } |
90 | } |
91 | 91 | ||
92 | size_t ip_client_header_length( packet_t packet ){ |
92 | size_t ip_client_header_length( packet_t packet ){ |
93 | ip_header_ref header; |
93 | ip_header_ref header; |
94 | 94 | ||
95 | header = ( ip_header_ref ) packet_get_data( packet ); |
95 | header = ( ip_header_ref ) packet_get_data( packet ); |
96 | if(( ! header ) |
96 | if(( ! header ) |
97 | || ( packet_get_data_length( packet ) < sizeof( ip_header_t ))){ |
97 | || ( packet_get_data_length( packet ) < sizeof( ip_header_t ))){ |
98 | return 0; |
98 | return 0; |
99 | } |
99 | } |
100 | return IP_HEADER_LENGTH( header ); |
100 | return IP_HEADER_LENGTH( header ); |
101 | } |
101 | } |
102 | 102 | ||
103 | int ip_client_set_pseudo_header_data_length( ip_pseudo_header_ref header, size_t headerlen, size_t data_length ){ |
103 | int ip_client_set_pseudo_header_data_length( ip_pseudo_header_ref header, size_t headerlen, size_t data_length ){ |
104 | ipv4_pseudo_header_ref header_in; |
104 | ipv4_pseudo_header_ref header_in; |
105 | 105 | ||
106 | if( headerlen == sizeof( ipv4_pseudo_header_t )){ |
106 | if( headerlen == sizeof( ipv4_pseudo_header_t )){ |
107 | header_in = ( ipv4_pseudo_header_ref ) header; |
107 | header_in = ( ipv4_pseudo_header_ref ) header; |
108 | header_in->data_length = htons( data_length ); |
108 | header_in->data_length = htons( data_length ); |
109 | return EOK; |
109 | return EOK; |
110 | }else{ |
110 | }else{ |
111 | return EINVAL; |
111 | return EINVAL; |
112 | } |
112 | } |
113 | } |
113 | } |
114 | 114 | ||
115 | 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 ){ |
115 | 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 ){ |
116 | ipv4_pseudo_header_ref header_in; |
116 | ipv4_pseudo_header_ref header_in; |
117 | struct sockaddr_in * address_in; |
117 | struct sockaddr_in * address_in; |
118 | 118 | ||
119 | if( !( header && headerlen )) return EBADMEM; |
119 | if( !( header && headerlen )) return EBADMEM; |
120 | if( !( src && dest && ( srclen >= sizeof( struct sockaddr )) && ( srclen == destlen ) && ( src->sa_family == dest->sa_family ))) return EINVAL; |
120 | if( !( src && dest && ( srclen > 0 ) && (( size_t ) srclen >= sizeof( struct sockaddr )) && ( srclen == destlen ) && ( src->sa_family == dest->sa_family ))) return EINVAL; |
121 | switch( src->sa_family ){ |
121 | switch( src->sa_family ){ |
122 | case AF_INET: |
122 | case AF_INET: |
123 | if( srclen != sizeof( struct sockaddr_in )) return EINVAL; |
123 | if( srclen != sizeof( struct sockaddr_in )) return EINVAL; |
124 | * headerlen = sizeof( * header_in ); |
124 | * headerlen = sizeof( * header_in ); |
125 | header_in = ( ipv4_pseudo_header_ref ) malloc( * headerlen ); |
125 | header_in = ( ipv4_pseudo_header_ref ) malloc( * headerlen ); |
126 | if( ! header_in ) return ENOMEM; |
126 | if( ! header_in ) return ENOMEM; |
127 | bzero( header_in, * headerlen ); |
127 | bzero( header_in, * headerlen ); |
128 | address_in = ( struct sockaddr_in * ) dest; |
128 | address_in = ( struct sockaddr_in * ) dest; |
129 | header_in->destination_address = address_in->sin_addr.s_addr; |
129 | header_in->destination_address = address_in->sin_addr.s_addr; |
130 | address_in = ( struct sockaddr_in * ) src; |
130 | address_in = ( struct sockaddr_in * ) src; |
131 | header_in->source_address = address_in->sin_addr.s_addr; |
131 | header_in->source_address = address_in->sin_addr.s_addr; |
132 | header_in->protocol = protocol; |
132 | header_in->protocol = protocol; |
133 | header_in->data_length = htons( data_length ); |
133 | header_in->data_length = htons( data_length ); |
134 | * header = ( ip_pseudo_header_ref ) header_in; |
134 | * header = ( ip_pseudo_header_ref ) header_in; |
135 | return EOK; |
135 | return EOK; |
136 | // TODO IPv6 |
136 | // TODO IPv6 |
137 | /* case AF_INET6: |
137 | /* case AF_INET6: |
138 | if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL; |
138 | if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL; |
139 | address_in6 = ( struct sockaddr_in6 * ) addr; |
139 | address_in6 = ( struct sockaddr_in6 * ) addr; |
140 | return EOK; |
140 | return EOK; |
141 | */ default: |
141 | */ default: |
142 | return EAFNOSUPPORT; |
142 | return EAFNOSUPPORT; |
143 | } |
143 | } |
144 | } |
144 | } |
145 | 145 | ||
146 | /** @} |
146 | /** @} |
147 | */ |
147 | */ |
148 | 148 |