source: mainline/uspace/lib/c/generic/inet/addr.c@ 3e66428

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e66428 was 3495654, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Factor out internet address parsing and formatting.

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 * Copyright (c) 2013 Jiri Svoboda
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/** @file Internet address parsing and formatting.
33 */
34
35#include <errno.h>
36#include <inet/addr.h>
37#include <stdio.h>
38
39/** Parse network address.
40 *
41 * @param text Network address in CIDR notation (a.b.c.d/w)
42 * @param naddr Place to store network address
43 *
44 * @return EOK on success, EINVAL if input is not in valid format
45 */
46int inet_naddr_parse(const char *text, inet_naddr_t *naddr)
47{
48 unsigned long a[4], bits;
49 char *cp = (char *)text;
50 int i;
51
52 for (i = 0; i < 3; i++) {
53 a[i] = strtoul(cp, &cp, 10);
54 if (*cp != '.')
55 return EINVAL;
56 ++cp;
57 }
58
59 a[3] = strtoul(cp, &cp, 10);
60 if (*cp != '/')
61 return EINVAL;
62 ++cp;
63
64 bits = strtoul(cp, &cp, 10);
65 if (*cp != '\0')
66 return EINVAL;
67
68 naddr->ipv4 = 0;
69 for (i = 0; i < 4; i++) {
70 if (a[i] > 255)
71 return EINVAL;
72 naddr->ipv4 = (naddr->ipv4 << 8) | a[i];
73 }
74
75 if (bits > 31)
76 return EINVAL;
77
78 naddr->bits = bits;
79 return EOK;
80}
81
82/** Parse node address.
83 *
84 * @param text Network address in dot notation (a.b.c.d)
85 * @param addr Place to store node address
86 *
87 * @return EOK on success, EINVAL if input is not in valid format
88 */
89int inet_addr_parse(const char *text, inet_addr_t *addr)
90{
91 unsigned long a[4];
92 char *cp = (char *)text;
93 int i;
94
95 for (i = 0; i < 3; i++) {
96 a[i] = strtoul(cp, &cp, 10);
97 if (*cp != '.')
98 return EINVAL;
99 ++cp;
100 }
101
102 a[3] = strtoul(cp, &cp, 10);
103 if (*cp != '\0')
104 return EINVAL;
105
106 addr->ipv4 = 0;
107 for (i = 0; i < 4; i++) {
108 if (a[i] > 255)
109 return EINVAL;
110 addr->ipv4 = (addr->ipv4 << 8) | a[i];
111 }
112
113 return EOK;
114}
115
116/** Format network address.
117 *
118 * @param naddr Network address
119 * @param bufp Place to store pointer to formatted string (CIDR notation)
120 *
121 * @return EOK on success, ENOMEM if out of memory.
122 */
123int inet_naddr_format(inet_naddr_t *naddr, char **bufp)
124{
125 int rc;
126
127 rc = asprintf(bufp, "%d.%d.%d.%d/%d", naddr->ipv4 >> 24,
128 (naddr->ipv4 >> 16) & 0xff, (naddr->ipv4 >> 8) & 0xff,
129 naddr->ipv4 & 0xff, naddr->bits);
130
131 if (rc < 0)
132 return ENOMEM;
133
134 return EOK;
135}
136
137/** Format node address.
138 *
139 * @param addr Node address
140 * @param bufp Place to store pointer to formatted string (dot notation)
141 *
142 * @return EOK on success, ENOMEM if out of memory.
143 */
144int inet_addr_format(inet_addr_t *addr, char **bufp)
145{
146 int rc;
147
148 rc = asprintf(bufp, "%d.%d.%d.%d", addr->ipv4 >> 24,
149 (addr->ipv4 >> 16) & 0xff, (addr->ipv4 >> 8) & 0xff,
150 addr->ipv4 & 0xff);
151
152 if (rc < 0)
153 return ENOMEM;
154
155 return EOK;
156}
157
158/** @}
159 */
Note: See TracBrowser for help on using the repository browser.