1 /* MIT License
2 *
3 * Copyright (c) 1998, 2011 Massachusetts Institute of Technology
4 * Copyright (c) The c-ares project and its contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * SPDX-License-Identifier: MIT
26 */
27
28 #include "ares_setup.h"
29
30 #ifdef HAVE_NETINET_IN_H
31 # include <netinet/in.h>
32 #endif
33
34 #include "ares_nameser.h"
35
36 #include "ares.h"
37 #include "ares_private.h" /* for the memdebug */
38
ares__expand_name_validated(const unsigned char * encoded,const unsigned char * abuf,size_t alen,char ** s,size_t * enclen,ares_bool_t is_hostname)39 ares_status_t ares__expand_name_validated(const unsigned char *encoded,
40 const unsigned char *abuf,
41 size_t alen, char **s, size_t *enclen,
42 ares_bool_t is_hostname)
43 {
44 ares_status_t status;
45 ares__buf_t *buf = NULL;
46 size_t start_len;
47
48 if (encoded == NULL || abuf == NULL || alen == 0 || enclen == NULL) {
49 return ARES_EBADNAME; /* EFORMERR would be better */
50 }
51
52 if (encoded < abuf || encoded >= abuf + alen) {
53 return ARES_EBADNAME; /* EFORMERR would be better */
54 }
55
56 *enclen = 0;
57
58 /* NOTE: we allow 's' to be NULL to skip it */
59 if (s) {
60 *s = NULL;
61 }
62
63 buf = ares__buf_create_const(abuf, alen);
64
65 if (buf == NULL) {
66 return ARES_ENOMEM;
67 }
68
69 status = ares__buf_set_position(buf, (size_t)(encoded - abuf));
70 if (status != ARES_SUCCESS) {
71 goto done;
72 }
73
74 start_len = ares__buf_len(buf);
75 status = ares__dns_name_parse(buf, s, is_hostname);
76 if (status != ARES_SUCCESS) {
77 goto done;
78 }
79
80 *enclen = start_len - ares__buf_len(buf);
81
82 done:
83 ares__buf_destroy(buf);
84 return status;
85 }
86
ares_expand_name(const unsigned char * encoded,const unsigned char * abuf,int alen,char ** s,long * enclen)87 int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
88 int alen, char **s, long *enclen)
89 {
90 /* Keep public API compatible */
91 size_t enclen_temp = 0;
92 ares_status_t status;
93
94 if (alen < 0) {
95 return ARES_EBADRESP;
96 }
97
98 status = ares__expand_name_validated(encoded, abuf, (size_t)alen, s,
99 &enclen_temp, ARES_FALSE);
100 *enclen = (long)enclen_temp;
101 return (int)status;
102 }
103
104 /* Like ares_expand_name_validated but returns EBADRESP in case of invalid
105 * input. */
ares__expand_name_for_response(const unsigned char * encoded,const unsigned char * abuf,size_t alen,char ** s,size_t * enclen,ares_bool_t is_hostname)106 ares_status_t ares__expand_name_for_response(const unsigned char *encoded,
107 const unsigned char *abuf,
108 size_t alen, char **s,
109 size_t *enclen,
110 ares_bool_t is_hostname)
111 {
112 ares_status_t status =
113 ares__expand_name_validated(encoded, abuf, alen, s, enclen, is_hostname);
114 if (status == ARES_EBADNAME) {
115 status = ARES_EBADRESP;
116 }
117 return status;
118 }
119