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