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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 66cb7a2 was a2e3ee6, checked in by Martin Decky <martin@…>, 12 years ago

use new network address infrastructure (towards IPv6 support)

  • Property mode set to 100644
File size: 4.2 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#include <inet/addr.h>
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 * @return EOK on success.
54 * @return EINVAL if the data or address parameter is NULL.
55 * @return ENOMEM if the character buffer is not long enough.
56 * @return 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 output buffer size */
67 if (length < INET_ADDRSTRLEN)
68 return ENOMEM;
69
70 /* Fill buffer with 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 output buffer size */
78 if (length < INET6_ADDRSTRLEN)
79 return ENOMEM;
80
81 /* Fill buffer with 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
96static int inet_pton4(const char *address, uint8_t *data)
97{
98 memset(data, 0, 4);
99
100 const char *cur = address;
101 size_t i = 0;
102
103 while (i < 4) {
104 int rc = str_uint8_t(cur, &cur, 10, false, &data[i]);
105 if (rc != EOK)
106 return rc;
107
108 i++;
109
110 if (*cur == 0)
111 break;
112
113 if (*cur != '.')
114 return EINVAL;
115
116 if (i < 4)
117 cur++;
118 }
119
120 if ((i == 4) && (*cur != 0))
121 return EINVAL;
122
123 return EOK;
124}
125
126static int inet_pton6(const char *address, uint8_t *data)
127{
128 // FIXME TODO
129 return ENOTSUP;
130}
131
132/** Parses the character string into the address.
133 *
134 * @param[in] family The address family.
135 * @param[in] address The character buffer to be parsed.
136 * @param[out] data The address data to be filled.
137 *
138 * @return EOK on success.
139 * @return EINVAL if the data parameter is NULL.
140 * @return ENOENT if the address parameter is NULL.
141 * @return ENOTSUP if the address family is not supported.
142 *
143 */
144int inet_pton(uint16_t family, const char *address, uint8_t *data)
145{
146 switch (family) {
147 case AF_INET:
148 return inet_pton4(address, data);
149 case AF_INET6:
150 return inet_pton6(address, data);
151 default:
152 /** Unknown address family */
153 return ENOTSUP;
154 }
155}
156
157/** @}
158 */
Note: See TracBrowser for help on using the repository browser.