source: mainline/uspace/lib/c/generic/inet/hostport.c@ 47e00b83

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 47e00b83 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 * Copyright (c) 2016 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 host:port specification
33 *
34 * As in RFC 1738 Uniform Resouce Locators (URL) and RFC 2732 Format for
35 * literal IPv6 Addresses in URLs
36 */
37
38#include <errno.h>
39#include <inet/addr.h>
40#include <inet/dnsr.h>
41#include <inet/endpoint.h>
42#include <inet/hostname.h>
43#include <inet/hostport.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <str.h>
47
48/** Parse host:port string.
49 *
50 * @param str Host:port string
51 * @param rhp Place to store pointer to new hostport structure
52 * @param endptr Place to store pointer to next character in string or @c NULL
53 *
54 * @return EOK on success. EINVAL if @a str does not start with a valid
55 * host:port or if @a endptr is @c NULL and @a str contains
56 * extra characters at the end. ENOMEM if out of memory
57 */
58errno_t inet_hostport_parse(const char *str, inet_hostport_t **rhp,
59 char **endptr)
60{
61 inet_hostport_t *hp;
62 inet_addr_t addr;
63 uint16_t port;
64 char *name;
65 char *aend;
66 const char *pend;
67 errno_t rc;
68
69 hp = calloc(1, sizeof(inet_hostport_t));
70 if (hp == NULL)
71 return ENOMEM;
72
73 if (str[0] == '[') {
74 /* Try [<ip-addr-literal>] */
75 rc = inet_addr_parse(str + 1, &addr, &aend);
76 if (rc == EOK) {
77 if (*aend == ']') {
78 hp->hform = inet_host_addr;
79 hp->host.addr = addr;
80 ++aend;
81 }
82 goto have_host;
83 }
84 }
85
86 /* Try <ipv4-addr> */
87 rc = inet_addr_parse(str, &addr, &aend);
88 if (rc == EOK) {
89 hp->hform = inet_host_addr;
90 hp->host.addr = addr;
91 goto have_host;
92 }
93
94 /* Try <hostname> */
95 rc = inet_hostname_parse(str, &name, &aend);
96 if (rc == EOK) {
97 hp->hform = inet_host_name;
98 hp->host.name = name;
99 goto have_host;
100 }
101
102 free(hp);
103 return EINVAL;
104
105have_host:
106 if (*aend == ':') {
107 /* Port number */
108 rc = str_uint16_t(aend + 1, &pend, 10, false, &port);
109 if (rc != EOK) {
110 free(hp);
111 return EINVAL;
112 }
113 } else {
114 /* Port number omitted */
115 port = 0;
116 pend = aend;
117 }
118
119 hp->port = port;
120
121 if (*pend != '\0' && endptr == NULL) {
122 /* Extra characters */
123 free(hp);
124 return EINVAL;
125 }
126
127 *rhp = hp;
128 if (endptr != NULL)
129 *endptr = (char *)pend;
130 return EOK;
131}
132
133/** Convert host:port to string representation.
134 *
135 * @param hp Host:port structure
136 * @param rstr Place to store pointer to new string
137 * @return EOK on success, ENOMEM if out of memory
138 */
139errno_t inet_hostport_format(inet_hostport_t *hp, char **rstr)
140{
141 errno_t rc;
142 int ret;
143 char *astr, *str;
144 char *hstr = NULL;
145
146 switch (hp->hform) {
147 case inet_host_addr:
148 rc = inet_addr_format(&hp->host.addr, &astr);
149 if (rc != EOK) {
150 assert(rc == ENOMEM);
151 return ENOMEM;
152 }
153
154 if (hp->host.addr.version != ip_v4) {
155 hstr = astr;
156 } else {
157 ret = asprintf(&hstr, "[%s]", astr);
158 if (ret < 0) {
159 free(astr);
160 return ENOMEM;
161 }
162
163 free(astr);
164 }
165
166 break;
167 case inet_host_name:
168 hstr = str_dup(hp->host.name);
169 if (hstr == NULL)
170 return ENOMEM;
171 break;
172 }
173
174 ret = asprintf(&str, "%s:%u", hstr, hp->port);
175 if (ret < 0)
176 return ENOMEM;
177
178 free(hstr);
179 *rstr = str;
180 return EOK;
181}
182
183/** Destroy host:port structure.
184 *
185 * @param hp Host:port or @c NULL
186 */
187void inet_hostport_destroy(inet_hostport_t *hp)
188{
189 if (hp == NULL)
190 return;
191
192 switch (hp->hform) {
193 case inet_host_addr:
194 break;
195 case inet_host_name:
196 free(hp->host.name);
197 break;
198 }
199
200 free(hp);
201}
202
203/** Look up first endpoint corresponding to host:port.
204 *
205 * If host:port contains a host name, name resolution is performed.
206 *
207 * @param hp Host:port structure
208 * @param version Desired IP version (ip_any to get any version)
209 * @param ep Place to store endpoint
210 *
211 * @return EOK on success, ENOENT on resolution failure
212 */
213errno_t inet_hostport_lookup_one(inet_hostport_t *hp, ip_ver_t version,
214 inet_ep_t *ep)
215{
216 dnsr_hostinfo_t *hinfo = NULL;
217 errno_t rc;
218
219 inet_ep_init(ep);
220
221 switch (hp->hform) {
222 case inet_host_addr:
223 ep->addr = hp->host.addr;
224 break;
225 case inet_host_name:
226 rc = dnsr_name2host(hp->host.name, &hinfo, ip_any);
227 if (rc != EOK) {
228 dnsr_hostinfo_destroy(hinfo);
229 return ENOENT;
230 }
231
232 ep->addr = hinfo->addr;
233 dnsr_hostinfo_destroy(hinfo);
234 break;
235 }
236
237 ep->port = hp->port;
238 return EOK;
239}
240
241/** Look up first endpoint corresponding to host:port string.
242 *
243 * If host:port contains a host name, name resolution is performed.
244 *
245 * @param str Host:port string
246 * @param version Desired IP version (ip_any to get any version)
247 * @param ep Place to store endpoint
248 * @param endptr Place to store pointer to next character in string or @c NULL
249 * @param errmsg Place to store pointer to error message (no need to free it)
250 * or @c NULL
251 *
252 * @return EOK on success. EINVAL if @a str has incorrect format or if
253 * @a endptr is @c NULL and @a str contains extra characters at
254 * the end. ENOENT if host was not found during resolution,
255 * ENOMEM if out of memory
256 */
257errno_t inet_hostport_plookup_one(const char *str, ip_ver_t version, inet_ep_t *ep,
258 char **endptr, const char **errmsg)
259{
260 inet_hostport_t *hp = NULL;
261 char *eptr;
262 errno_t rc;
263
264 rc = inet_hostport_parse(str, &hp, endptr != NULL ? &eptr : NULL);
265 if (rc != EOK) {
266 switch (rc) {
267 case EINVAL:
268 if (errmsg != NULL)
269 *errmsg = "Invalid format";
270 goto error;
271 case ENOMEM:
272 if (errmsg != NULL)
273 *errmsg = "Out of memory";
274 goto error;
275 case EOK:
276 break;
277 default:
278 assert(false);
279 }
280 }
281
282 rc = inet_hostport_lookup_one(hp, version, ep);
283 if (rc != EOK) {
284 /* XXX Distinguish between 'not found' and other error */
285 rc = ENOENT;
286 if (errmsg != NULL)
287 *errmsg = "Name resolution failed";
288 goto error;
289 }
290
291 inet_hostport_destroy(hp);
292 if (endptr != NULL)
293 *endptr = eptr;
294 return EOK;
295error:
296 inet_hostport_destroy(hp);
297 return rc;
298}
299
300/** @}
301 */
Note: See TracBrowser for help on using the repository browser.