source: mainline/uspace/lib/c/generic/net/inet.c@ e4554d4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e4554d4 was e4554d4, checked in by Jakub Jermar <jakub@…>, 15 years ago

Move inet.[ch], in.h and in6.h to standard library.

  • Property mode set to 100644
File size: 4.9 KB
Line 
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 libc
30 * @{
31 */
32
33/** @file
34 * Internet protocol address conversion functions implementation.
35 */
36
37#include <net/socket_codes.h>
38#include <net/in.h>
39#include <net/in6.h>
40#include <net/inet.h>
41
42#include <errno.h>
43#include <mem.h>
44#include <stdio.h>
45#include <str.h>
46
47/** Prints the address into the character buffer.
48 *
49 * @param[in] family The address family.
50 * @param[in] data The address data.
51 * @param[out] address The character buffer to be filled.
52 * @param[in] length The buffer length.
53 * @returns EOK on success.
54 * @returns EINVAL if the data or address parameter is NULL.
55 * @returns ENOMEM if the character buffer is not long enough.
56 * @returns ENOTSUP if the address family is not supported.
57 */
58int
59inet_ntop(uint16_t family, const uint8_t *data, char *address, size_t length)
60{
61 if ((!data) || (!address))
62 return EINVAL;
63
64 switch (family) {
65 case AF_INET:
66 // check the output buffer size
67 if (length < INET_ADDRSTRLEN)
68 return ENOMEM;
69
70 // fill the buffer with the IPv4 address
71 snprintf(address, length, "%hhu.%hhu.%hhu.%hhu",
72 data[0], data[1], data[2], data[3]);
73
74 return EOK;
75
76 case AF_INET6:
77 // check the output buffer size
78 if (length < INET6_ADDRSTRLEN)
79 return ENOMEM;
80
81 // fill the buffer with the IPv6 address
82 snprintf(address, length,
83 "%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:"
84 "%hhx%hhx:%hhx%hhx",
85 data[0], data[1], data[2], data[3], data[4], data[5],
86 data[6], data[7], data[8], data[9], data[10], data[11],
87 data[12], data[13], data[14], data[15]);
88
89 return EOK;
90
91 default:
92 return ENOTSUP;
93 }
94}
95
96/** Parses the character string into the address.
97 *
98 * If the string is shorter than the full address, zero bytes are added.
99 *
100 * @param[in] family The address family.
101 * @param[in] address The character buffer to be parsed.
102 * @param[out] data The address data to be filled.
103 * @returns EOK on success.
104 * @returns EINVAL if the data parameter is NULL.
105 * @returns ENOENT if the address parameter is NULL.
106 * @returns ENOTSUP if the address family is not supported.
107 */
108int inet_pton(uint16_t family, const char *address, uint8_t *data)
109{
110 /** The base number of the values. */
111 int base;
112 /** The number of bytes per a section. */
113 size_t bytes;
114 /** The number of bytes of the address data. */
115 int count;
116
117 const char *next;
118 char *last;
119 int index;
120 size_t shift;
121 unsigned long value;
122
123 if (!data)
124 return EINVAL;
125
126 // set the processing parameters
127 switch (family) {
128 case AF_INET:
129 count = 4;
130 base = 10;
131 bytes = 1;
132 break;
133
134 case AF_INET6:
135 count = 16;
136 base = 16;
137 bytes = 4;
138 break;
139
140 default:
141 return ENOTSUP;
142 }
143
144 // erase if no address
145 if (!address) {
146 bzero(data, count);
147 return ENOENT;
148 }
149
150 // process the string from the beginning
151 next = address;
152 index = 0;
153 do {
154 // if the actual character is set
155 if (next && *next) {
156
157 // if not on the first character
158 if (index) {
159 // move to the next character
160 ++next;
161 }
162
163 // parse the actual integral value
164 value = strtoul(next, &last, base);
165 // remember the last problematic character
166 // should be either '.' or ':' but is ignored to be more
167 // generic
168 next = last;
169
170 // fill the address data byte by byte
171 shift = bytes - 1;
172 do {
173 // like little endian
174 data[index + shift] = value;
175 value >>= 8;
176 } while(shift --);
177
178 index += bytes;
179 } else {
180 // erase the rest of the address
181 bzero(data + index, count - index);
182 return EOK;
183 }
184 } while (index < count);
185
186 return EOK;
187}
188
189/** @}
190 */
Note: See TracBrowser for help on using the repository browser.