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