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