2 * Copyright (c) 2013 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
32 /** @file Internet address parsing and formatting.
36 #include <inet/addr.h>
39 /** Parse network address.
41 * @param text Network address in CIDR notation (a.b.c.d/w)
42 * @param naddr Place to store network address
44 * @return EOK on success, EINVAL if input is not in valid format
46 int inet_naddr_parse(const char *text
, inet_naddr_t
*naddr
)
48 unsigned long a
[4], bits
;
49 char *cp
= (char *)text
;
52 for (i
= 0; i
< 3; i
++) {
53 a
[i
] = strtoul(cp
, &cp
, 10);
59 a
[3] = strtoul(cp
, &cp
, 10);
64 bits
= strtoul(cp
, &cp
, 10);
69 for (i
= 0; i
< 4; i
++) {
72 naddr
->ipv4
= (naddr
->ipv4
<< 8) | a
[i
];
82 /** Parse node address.
84 * @param text Network address in dot notation (a.b.c.d)
85 * @param addr Place to store node address
87 * @return EOK on success, EINVAL if input is not in valid format
89 int inet_addr_parse(const char *text
, inet_addr_t
*addr
)
92 char *cp
= (char *)text
;
95 for (i
= 0; i
< 3; i
++) {
96 a
[i
] = strtoul(cp
, &cp
, 10);
102 a
[3] = strtoul(cp
, &cp
, 10);
107 for (i
= 0; i
< 4; i
++) {
110 addr
->ipv4
= (addr
->ipv4
<< 8) | a
[i
];
116 /** Format network address.
118 * @param naddr Network address
119 * @param bufp Place to store pointer to formatted string (CIDR notation)
121 * @return EOK on success, ENOMEM if out of memory.
123 int inet_naddr_format(inet_naddr_t
*naddr
, char **bufp
)
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
);
137 /** Format node address.
139 * @param addr Node address
140 * @param bufp Place to store pointer to formatted string (dot notation)
142 * @return EOK on success, ENOMEM if out of memory.
144 int inet_addr_format(inet_addr_t
*addr
, char **bufp
)
148 rc
= asprintf(bufp
, "%d.%d.%d.%d", addr
->ipv4
>> 24,
149 (addr
->ipv4
>> 16) & 0xff, (addr
->ipv4
>> 8) & 0xff,