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 specification
|
---|
33 | *
|
---|
34 | * Either a host name or an address.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <inet/addr.h>
|
---|
40 | #include <inet/dnsr.h>
|
---|
41 | #include <inet/host.h>
|
---|
42 | #include <inet/hostname.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <str.h>
|
---|
46 |
|
---|
47 | /** Parse host string.
|
---|
48 | *
|
---|
49 | * @param str Host string
|
---|
50 | * @param rhost Place to store pointer to new host structure
|
---|
51 | * @param endptr Place to store pointer to next character in string or @c NULL
|
---|
52 | *
|
---|
53 | * @return EOK on success. EINVAL if @a str does not start with a valid
|
---|
54 | * host specification or if @a endptr is @c NULL and @a str contains
|
---|
55 | * extra characters at the end. ENOMEM if out of memory
|
---|
56 | */
|
---|
57 | errno_t inet_host_parse(const char *str, inet_host_t **rhost,
|
---|
58 | char **endptr)
|
---|
59 | {
|
---|
60 | inet_host_t *host;
|
---|
61 | inet_addr_t addr;
|
---|
62 | char *name;
|
---|
63 | char *aend;
|
---|
64 | errno_t rc;
|
---|
65 |
|
---|
66 | host = calloc(1, sizeof(inet_host_t));
|
---|
67 | if (host == NULL)
|
---|
68 | return ENOMEM;
|
---|
69 |
|
---|
70 | /* Try address */
|
---|
71 | rc = inet_addr_parse(str, &addr, &aend);
|
---|
72 | if (rc == EOK) {
|
---|
73 | host->hform = inet_host_addr;
|
---|
74 | host->host.addr = addr;
|
---|
75 | goto have_host;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /* Try <hostname> */
|
---|
80 | rc = inet_hostname_parse(str, &name, &aend);
|
---|
81 | if (rc == EOK) {
|
---|
82 | host->hform = inet_host_name;
|
---|
83 | host->host.name = name;
|
---|
84 | goto have_host;
|
---|
85 | }
|
---|
86 |
|
---|
87 | free(host);
|
---|
88 | return EINVAL;
|
---|
89 |
|
---|
90 | have_host:
|
---|
91 | if (*aend != '\0' && endptr == NULL) {
|
---|
92 | /* Extra characters */
|
---|
93 | free(host);
|
---|
94 | return EINVAL;
|
---|
95 | }
|
---|
96 |
|
---|
97 | *rhost = host;
|
---|
98 | if (endptr != NULL)
|
---|
99 | *endptr = (char *)aend;
|
---|
100 | return EOK;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Convert host structure to string representation.
|
---|
104 | *
|
---|
105 | * @param host Host structure
|
---|
106 | * @param rstr Place to store pointer to new string
|
---|
107 | * @return EOK on success, ENOMEM if out of memory
|
---|
108 | */
|
---|
109 | errno_t inet_host_format(inet_host_t *host, char **rstr)
|
---|
110 | {
|
---|
111 | errno_t rc;
|
---|
112 | char *str = NULL;
|
---|
113 |
|
---|
114 | switch (host->hform) {
|
---|
115 | case inet_host_addr:
|
---|
116 | rc = inet_addr_format(&host->host.addr, &str);
|
---|
117 | if (rc != EOK) {
|
---|
118 | assert(rc == ENOMEM);
|
---|
119 | return ENOMEM;
|
---|
120 | }
|
---|
121 |
|
---|
122 | break;
|
---|
123 | case inet_host_name:
|
---|
124 | str = str_dup(host->host.name);
|
---|
125 | if (str == NULL)
|
---|
126 | return ENOMEM;
|
---|
127 | break;
|
---|
128 | }
|
---|
129 |
|
---|
130 | *rstr = str;
|
---|
131 | return EOK;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Destroy host structure.
|
---|
135 | *
|
---|
136 | * @param host Host structure or @c NULL
|
---|
137 | */
|
---|
138 | void inet_host_destroy(inet_host_t *host)
|
---|
139 | {
|
---|
140 | if (host == NULL)
|
---|
141 | return;
|
---|
142 |
|
---|
143 | switch (host->hform) {
|
---|
144 | case inet_host_addr:
|
---|
145 | break;
|
---|
146 | case inet_host_name:
|
---|
147 | free(host->host.name);
|
---|
148 | break;
|
---|
149 | }
|
---|
150 |
|
---|
151 | free(host);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** Look up first address corresponding to host.
|
---|
155 | *
|
---|
156 | * If host contains a host name, name resolution is performed.
|
---|
157 | *
|
---|
158 | * @param host Host structure
|
---|
159 | * @param version Desired IP version (ip_any to get any version)
|
---|
160 | * @param addr Place to store address
|
---|
161 | *
|
---|
162 | * @reutrn EOK on success, ENOENT on resolurion failure
|
---|
163 | */
|
---|
164 | errno_t inet_host_lookup_one(inet_host_t *host, ip_ver_t version, inet_addr_t *addr)
|
---|
165 | {
|
---|
166 | dnsr_hostinfo_t *hinfo = NULL;
|
---|
167 | errno_t rc;
|
---|
168 |
|
---|
169 | switch (host->hform) {
|
---|
170 | case inet_host_addr:
|
---|
171 | *addr = host->host.addr;
|
---|
172 | break;
|
---|
173 | case inet_host_name:
|
---|
174 | rc = dnsr_name2host(host->host.name, &hinfo, version);
|
---|
175 | if (rc != EOK) {
|
---|
176 | dnsr_hostinfo_destroy(hinfo);
|
---|
177 | return ENOENT;
|
---|
178 | }
|
---|
179 |
|
---|
180 | *addr = hinfo->addr;
|
---|
181 | dnsr_hostinfo_destroy(hinfo);
|
---|
182 | break;
|
---|
183 | }
|
---|
184 |
|
---|
185 | return EOK;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /** Look up first address corresponding to host string.
|
---|
189 | *
|
---|
190 | * If host contains a host name, name resolution is performed.
|
---|
191 | *
|
---|
192 | * @param str Host string
|
---|
193 | * @param version Desired IP version (ip_any to get any version)
|
---|
194 | * @param addr Place to store address
|
---|
195 | * @param endptr Place to store pointer to next character in string or @c NULL
|
---|
196 | * @param errmsg Place to store pointer to error message (no need to free it)
|
---|
197 | * or @c NULL
|
---|
198 | *
|
---|
199 | * @return EOK on success. EINVAL if @a str has incorrect format or if
|
---|
200 | * @a endptr is @c NULL and @a str contains extra characters at
|
---|
201 | * the end. ENOENT if host was not found during resolution,
|
---|
202 | * ENOMEM if out of memory
|
---|
203 | */
|
---|
204 | errno_t inet_host_plookup_one(const char *str, ip_ver_t version, inet_addr_t *addr,
|
---|
205 | char **endptr, const char **errmsg)
|
---|
206 | {
|
---|
207 | inet_host_t *host = NULL;
|
---|
208 | char *eptr;
|
---|
209 | errno_t rc;
|
---|
210 |
|
---|
211 | rc = inet_host_parse(str, &host, endptr != NULL ? &eptr : NULL);
|
---|
212 | if (rc != EOK) {
|
---|
213 | switch (rc) {
|
---|
214 | case EINVAL:
|
---|
215 | if (errmsg != NULL)
|
---|
216 | *errmsg = "Invalid format";
|
---|
217 | goto error;
|
---|
218 | case ENOMEM:
|
---|
219 | if (errmsg != NULL)
|
---|
220 | *errmsg = "Out of memory";
|
---|
221 | goto error;
|
---|
222 | default:
|
---|
223 | assert(false);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | rc = inet_host_lookup_one(host, version, addr);
|
---|
228 | if (rc != EOK) {
|
---|
229 | /* XXX Distinguish between 'not found' and other error */
|
---|
230 | rc = ENOENT;
|
---|
231 | if (errmsg != NULL)
|
---|
232 | *errmsg = "Name resolution failed";
|
---|
233 | goto error;
|
---|
234 | }
|
---|
235 |
|
---|
236 | inet_host_destroy(host);
|
---|
237 | if (endptr != NULL)
|
---|
238 | *endptr = eptr;
|
---|
239 | return EOK;
|
---|
240 | error:
|
---|
241 | inet_host_destroy(host);
|
---|
242 | return rc;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /** @}
|
---|
246 | */
|
---|