udp: use new network address infrastructure (towards IPv6 support)
[helenos.git] / uspace / lib / c / generic / inet / addr.c
blob2659a09156f0f62778ddb16968dd3d69daf4365f
1 /*
2 * Copyright (c) 2013 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
29 /** @addtogroup libc
30 * @{
32 /** @file Internet address parsing and formatting.
35 #include <errno.h>
36 #include <inet/addr.h>
37 #include <stdio.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;
50 int i;
52 for (i = 0; i < 3; i++) {
53 a[i] = strtoul(cp, &cp, 10);
54 if (*cp != '.')
55 return EINVAL;
56 ++cp;
59 a[3] = strtoul(cp, &cp, 10);
60 if (*cp != '/')
61 return EINVAL;
62 ++cp;
64 bits = strtoul(cp, &cp, 10);
65 if (*cp != '\0')
66 return EINVAL;
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];
75 if (bits > 31)
76 return EINVAL;
78 naddr->bits = bits;
79 return EOK;
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)
91 unsigned long a[4];
92 char *cp = (char *)text;
93 int i;
95 for (i = 0; i < 3; i++) {
96 a[i] = strtoul(cp, &cp, 10);
97 if (*cp != '.')
98 return EINVAL;
99 ++cp;
102 a[3] = strtoul(cp, &cp, 10);
103 if (*cp != '\0')
104 return EINVAL;
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];
113 return EOK;
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)
125 int rc;
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);
131 if (rc < 0)
132 return ENOMEM;
134 return EOK;
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)
146 int rc;
148 rc = asprintf(bufp, "%d.%d.%d.%d", addr->ipv4 >> 24,
149 (addr->ipv4 >> 16) & 0xff, (addr->ipv4 >> 8) & 0xff,
150 addr->ipv4 & 0xff);
152 if (rc < 0)
153 return ENOMEM;
155 return EOK;
158 /** @}