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 | */
|
---|
58 | int 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 | int 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 |
|
---|
105 | have_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 | */
|
---|
139 | int inet_hostport_format(inet_hostport_t *hp, char **rstr)
|
---|
140 | {
|
---|
141 | int rc;
|
---|
142 | char *astr, *str;
|
---|
143 | char *hstr = NULL;
|
---|
144 |
|
---|
145 | switch (hp->hform) {
|
---|
146 | case inet_host_addr:
|
---|
147 | rc = inet_addr_format(&hp->host.addr, &astr);
|
---|
148 | if (rc != EOK) {
|
---|
149 | assert(rc == ENOMEM);
|
---|
150 | return ENOMEM;
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (hp->host.addr.version != ip_v4) {
|
---|
154 | hstr = astr;
|
---|
155 | } else {
|
---|
156 | rc = asprintf(&hstr, "[%s]", astr);
|
---|
157 | if (rc < 0) {
|
---|
158 | free(astr);
|
---|
159 | return ENOMEM;
|
---|
160 | }
|
---|
161 |
|
---|
162 | free(astr);
|
---|
163 | }
|
---|
164 |
|
---|
165 | break;
|
---|
166 | case inet_host_name:
|
---|
167 | hstr = str_dup(hp->host.name);
|
---|
168 | if (hstr == NULL)
|
---|
169 | return ENOMEM;
|
---|
170 | break;
|
---|
171 | }
|
---|
172 |
|
---|
173 | rc = asprintf(&str, "%s:%u", hstr, hp->port);
|
---|
174 | if (rc < 0)
|
---|
175 | return ENOMEM;
|
---|
176 |
|
---|
177 | free(hstr);
|
---|
178 | *rstr = str;
|
---|
179 | return EOK;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Destroy host:port structure.
|
---|
183 | *
|
---|
184 | * @param hp Host:port or @c NULL
|
---|
185 | */
|
---|
186 | void inet_hostport_destroy(inet_hostport_t *hp)
|
---|
187 | {
|
---|
188 | if (hp == NULL)
|
---|
189 | return;
|
---|
190 |
|
---|
191 | switch (hp->hform) {
|
---|
192 | case inet_host_addr:
|
---|
193 | break;
|
---|
194 | case inet_host_name:
|
---|
195 | free(hp->host.name);
|
---|
196 | break;
|
---|
197 | }
|
---|
198 |
|
---|
199 | free(hp);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /** Look up first endpoint corresponding to host:port.
|
---|
203 | *
|
---|
204 | * If host:port contains a host name, name resolution is performed.
|
---|
205 | *
|
---|
206 | * @param hp Host:port structure
|
---|
207 | * @param version Desired IP version (ip_any to get any version)
|
---|
208 | * @param ep Place to store endpoint
|
---|
209 | *
|
---|
210 | * @return EOK on success, ENOENT on resolution failure
|
---|
211 | */
|
---|
212 | int inet_hostport_lookup_one(inet_hostport_t *hp, ip_ver_t version,
|
---|
213 | inet_ep_t *ep)
|
---|
214 | {
|
---|
215 | dnsr_hostinfo_t *hinfo = NULL;
|
---|
216 | int rc;
|
---|
217 |
|
---|
218 | inet_ep_init(ep);
|
---|
219 |
|
---|
220 | switch (hp->hform) {
|
---|
221 | case inet_host_addr:
|
---|
222 | ep->addr = hp->host.addr;
|
---|
223 | break;
|
---|
224 | case inet_host_name:
|
---|
225 | rc = dnsr_name2host(hp->host.name, &hinfo, ip_any);
|
---|
226 | if (rc != EOK) {
|
---|
227 | dnsr_hostinfo_destroy(hinfo);
|
---|
228 | return ENOENT;
|
---|
229 | }
|
---|
230 |
|
---|
231 | ep->addr = hinfo->addr;
|
---|
232 | dnsr_hostinfo_destroy(hinfo);
|
---|
233 | break;
|
---|
234 | }
|
---|
235 |
|
---|
236 | ep->port = hp->port;
|
---|
237 | return EOK;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /** Look up first endpoint corresponding to host:port string.
|
---|
241 | *
|
---|
242 | * If host:port contains a host name, name resolution is performed.
|
---|
243 | *
|
---|
244 | * @param str Host:port string
|
---|
245 | * @param version Desired IP version (ip_any to get any version)
|
---|
246 | * @param ep Place to store endpoint
|
---|
247 | * @param endptr Place to store pointer to next character in string or @c NULL
|
---|
248 | * @param errmsg Place to store pointer to error message (no need to free it)
|
---|
249 | * or @c NULL
|
---|
250 | *
|
---|
251 | * @return EOK on success. EINVAL if @a str has incorrect format or if
|
---|
252 | * @a endptr is @c NULL and @a str contains extra characters at
|
---|
253 | * the end. ENOENT if host was not found during resolution,
|
---|
254 | * ENOMEM if out of memory
|
---|
255 | */
|
---|
256 | int inet_hostport_plookup_one(const char *str, ip_ver_t version, inet_ep_t *ep,
|
---|
257 | char **endptr, const char **errmsg)
|
---|
258 | {
|
---|
259 | inet_hostport_t *hp = NULL;
|
---|
260 | char *eptr;
|
---|
261 | int rc;
|
---|
262 |
|
---|
263 | rc = inet_hostport_parse(str, &hp, endptr != NULL ? &eptr : NULL);
|
---|
264 | if (rc != EOK) {
|
---|
265 | switch (rc) {
|
---|
266 | case EINVAL:
|
---|
267 | if (errmsg != NULL)
|
---|
268 | *errmsg = "Invalid format";
|
---|
269 | goto error;
|
---|
270 | case ENOMEM:
|
---|
271 | if (errmsg != NULL)
|
---|
272 | *errmsg = "Out of memory";
|
---|
273 | goto error;
|
---|
274 | case EOK:
|
---|
275 | break;
|
---|
276 | default:
|
---|
277 | assert(false);
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | rc = inet_hostport_lookup_one(hp, version, ep);
|
---|
282 | if (rc != EOK) {
|
---|
283 | /* XXX Distinguish between 'not found' and other error */
|
---|
284 | rc = ENOENT;
|
---|
285 | if (errmsg != NULL)
|
---|
286 | *errmsg = "Name resolution failed";
|
---|
287 | goto error;
|
---|
288 | }
|
---|
289 |
|
---|
290 | inet_hostport_destroy(hp);
|
---|
291 | if (endptr != NULL)
|
---|
292 | *endptr = eptr;
|
---|
293 | return EOK;
|
---|
294 | error:
|
---|
295 | inet_hostport_destroy(hp);
|
---|
296 | return rc;
|
---|
297 | }
|
---|
298 |
|
---|
299 | /** @}
|
---|
300 | */
|
---|