Rev 4350 | Rev 4578 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4350 | Rev 4559 | ||
---|---|---|---|
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 net |
29 | /** @addtogroup net |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** @file |
33 | /** @file |
34 | * Internet protocol address conversion functions implementation. |
34 | * Internet protocol address conversion functions implementation. |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <errno.h> |
37 | #include <errno.h> |
38 | #include <mem.h> |
38 | #include <mem.h> |
39 | #include <stdio.h> |
39 | #include <stdio.h> |
40 | 40 | ||
41 | #include "include/sockaddr.h" |
41 | #include "include/inet.h" |
42 | #include "include/socket.h" |
42 | #include "include/socket.h" |
43 | 43 | ||
44 | int inet_pton( uint16_t family, const char * address, uint8_t * data ){ |
44 | int inet_pton( uint16_t family, const char * address, uint8_t * data ){ |
45 | const char * next; |
45 | const char * next; |
46 | char * last; |
46 | char * last; |
47 | int index; |
47 | int index; |
48 | int count; |
48 | int count; |
49 | int base; |
49 | int base; |
50 | size_t bytes; |
50 | size_t bytes; |
51 | size_t shift; |
51 | size_t shift; |
52 | unsigned long value; |
52 | unsigned long value; |
53 | 53 | ||
54 | if( ! data ) return EINVAL; |
54 | if( ! data ) return EINVAL; |
55 | switch( family ){ |
55 | switch( family ){ |
56 | case AF_INET: |
56 | case AF_INET: |
57 | count = 4; |
57 | count = 4; |
58 | base = 10; |
58 | base = 10; |
59 | bytes = 1; |
59 | bytes = 1; |
60 | break; |
60 | break; |
61 | case AF_INET6: |
61 | case AF_INET6: |
62 | count = 16; |
62 | count = 16; |
63 | base = 16; |
63 | base = 16; |
64 | bytes = 4; |
64 | bytes = 4; |
65 | break; |
65 | break; |
66 | default: |
66 | default: |
67 | return ENOTSUP; |
67 | return ENOTSUP; |
68 | } |
68 | } |
69 | if( ! address ){ |
69 | if( ! address ){ |
70 | bzero( data, count ); |
70 | bzero( data, count ); |
71 | return ENOENT; |
71 | return ENOENT; |
72 | } |
72 | } |
73 | next = address; |
73 | next = address; |
74 | index = 0; |
74 | index = 0; |
75 | do{ |
75 | do{ |
76 | if( next && ( * next )){ |
76 | if( next && ( * next )){ |
77 | if( index ) ++ next; |
77 | if( index ) ++ next; |
78 | value = strtoul( next, & last, base ); |
78 | value = strtoul( next, & last, base ); |
79 | next = last; |
79 | next = last; |
80 | shift = bytes - 1; |
80 | shift = bytes - 1; |
81 | do{ |
81 | do{ |
82 | // like little endian |
82 | // like little endian |
83 | data[ index + shift ] = value; |
83 | data[ index + shift ] = value; |
84 | value >>= 8; |
84 | value >>= 8; |
85 | }while( shift -- ); |
85 | }while( shift -- ); |
86 | index += bytes; |
86 | index += bytes; |
87 | }else{ |
87 | }else{ |
88 | bzero( data + index, count - index ); |
88 | bzero( data + index, count - index ); |
89 | return EOK; |
89 | return EOK; |
90 | } |
90 | } |
91 | }while( index < count ); |
91 | }while( index < count ); |
92 | return EOK; |
92 | return EOK; |
93 | } |
93 | } |
94 | 94 | ||
95 | int inet_ntop( uint16_t family, const uint8_t * data, char * address, size_t length ){ |
95 | int inet_ntop( uint16_t family, const uint8_t * data, char * address, size_t length ){ |
96 | if(( ! data ) || ( ! address )) return EINVAL; |
96 | if(( ! data ) || ( ! address )) return EINVAL; |
97 | switch( family ){ |
97 | switch( family ){ |
98 | case AF_INET: if( length < INET_ADDRSTRLEN ) return ENOMEM; |
98 | case AF_INET: if( length < INET_ADDRSTRLEN ) return ENOMEM; |
99 | snprintf( address, length, "%hhu.%hhu.%hhu.%hhu", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ] ); |
99 | snprintf( address, length, "%hhu.%hhu.%hhu.%hhu", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ] ); |
100 | return EOK; |
100 | return EOK; |
101 | case AF_INET6: if( length < INET6_ADDRSTRLEN ) return ENOMEM; |
101 | case AF_INET6: if( length < INET6_ADDRSTRLEN ) return ENOMEM; |
102 | snprintf( address, length, "%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ], data[ 12 ], data[ 13 ], data[ 14 ], data[ 15 ] ); |
102 | snprintf( address, length, "%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ], data[ 12 ], data[ 13 ], data[ 14 ], data[ 15 ] ); |
103 | return EOK; |
103 | return EOK; |
104 | default: return ENOTSUP; |
104 | default: return ENOTSUP; |
105 | } |
105 | } |
106 | } |
106 | } |
107 | 107 | ||
108 | /** @} |
108 | /** @} |
109 | */ |
109 | */ |
110 | 110 |