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 net
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | * Internet protocol address conversion functions implementation.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <mem.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <string.h>
|
---|
41 |
|
---|
42 | #include "include/in.h"
|
---|
43 | #include "include/in6.h"
|
---|
44 | #include "include/inet.h"
|
---|
45 | #include "include/socket_codes.h"
|
---|
46 |
|
---|
47 | int inet_ntop(uint16_t family, const uint8_t * data, char * address, size_t length){
|
---|
48 | if((! data) || (! address)){
|
---|
49 | return EINVAL;
|
---|
50 | }
|
---|
51 |
|
---|
52 | switch(family){
|
---|
53 | case AF_INET:
|
---|
54 | // check the output buffer size
|
---|
55 | if(length < INET_ADDRSTRLEN){
|
---|
56 | return ENOMEM;
|
---|
57 | }
|
---|
58 | // fill the buffer with the IPv4 address
|
---|
59 | snprintf(address, length, "%hhu.%hhu.%hhu.%hhu", data[0], data[1], data[2], data[3]);
|
---|
60 | return EOK;
|
---|
61 | case AF_INET6:
|
---|
62 | // check the output buffer size
|
---|
63 | if(length < INET6_ADDRSTRLEN){
|
---|
64 | return ENOMEM;
|
---|
65 | }
|
---|
66 | // fill the buffer with the IPv6 address
|
---|
67 | 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]);
|
---|
68 | return EOK;
|
---|
69 | default:
|
---|
70 | return ENOTSUP;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | int inet_pton(uint16_t family, const char * address, uint8_t * data){
|
---|
75 | /** The base number of the values.
|
---|
76 | */
|
---|
77 | int base;
|
---|
78 | /** The number of bytes per a section.
|
---|
79 | */
|
---|
80 | size_t bytes;
|
---|
81 | /** The number of bytes of the address data.
|
---|
82 | */
|
---|
83 | int count;
|
---|
84 |
|
---|
85 | const char * next;
|
---|
86 | char * last;
|
---|
87 | int index;
|
---|
88 | size_t shift;
|
---|
89 | unsigned long value;
|
---|
90 |
|
---|
91 | if(! data){
|
---|
92 | return EINVAL;
|
---|
93 | }
|
---|
94 |
|
---|
95 | // set the processing parameters
|
---|
96 | switch(family){
|
---|
97 | case AF_INET:
|
---|
98 | count = 4;
|
---|
99 | base = 10;
|
---|
100 | bytes = 1;
|
---|
101 | break;
|
---|
102 | case AF_INET6:
|
---|
103 | count = 16;
|
---|
104 | base = 16;
|
---|
105 | bytes = 4;
|
---|
106 | break;
|
---|
107 | default:
|
---|
108 | return ENOTSUP;
|
---|
109 | }
|
---|
110 |
|
---|
111 | // erase if no address
|
---|
112 | if(! address){
|
---|
113 | bzero(data, count);
|
---|
114 | return ENOENT;
|
---|
115 | }
|
---|
116 |
|
---|
117 | // process the string from the beginning
|
---|
118 | next = address;
|
---|
119 | index = 0;
|
---|
120 | do{
|
---|
121 | // if the actual character is set
|
---|
122 | if(next && (*next)){
|
---|
123 |
|
---|
124 | // if not on the first character
|
---|
125 | if(index){
|
---|
126 | // move to the next character
|
---|
127 | ++ next;
|
---|
128 | }
|
---|
129 |
|
---|
130 | // parse the actual integral value
|
---|
131 | value = strtoul(next, &last, base);
|
---|
132 | // remember the last problematic character
|
---|
133 | // should be either '.' or ':' but is ignored to be more generic
|
---|
134 | next = last;
|
---|
135 |
|
---|
136 | // fill the address data byte by byte
|
---|
137 | shift = bytes - 1;
|
---|
138 | do{
|
---|
139 | // like little endian
|
---|
140 | data[index + shift] = value;
|
---|
141 | value >>= 8;
|
---|
142 | }while(shift --);
|
---|
143 |
|
---|
144 | index += bytes;
|
---|
145 | }else{
|
---|
146 | // erase the rest of the address
|
---|
147 | bzero(data + index, count - index);
|
---|
148 | return EOK;
|
---|
149 | }
|
---|
150 | }while(index < count);
|
---|
151 |
|
---|
152 | return EOK;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /** @}
|
---|
156 | */
|
---|