11cb0ef41Sopenharmony_ci/* MIT License
21cb0ef41Sopenharmony_ci *
31cb0ef41Sopenharmony_ci * Copyright (c) 1998, 2011 Massachusetts Institute of Technology
41cb0ef41Sopenharmony_ci * Copyright (c) The c-ares project and its contributors
51cb0ef41Sopenharmony_ci *
61cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
71cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal
81cb0ef41Sopenharmony_ci * in the Software without restriction, including without limitation the rights
91cb0ef41Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
101cb0ef41Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is
111cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions:
121cb0ef41Sopenharmony_ci *
131cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice (including the next
141cb0ef41Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
151cb0ef41Sopenharmony_ci * Software.
161cb0ef41Sopenharmony_ci *
171cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
181cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
191cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
201cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
211cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
221cb0ef41Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
231cb0ef41Sopenharmony_ci * SOFTWARE.
241cb0ef41Sopenharmony_ci *
251cb0ef41Sopenharmony_ci * SPDX-License-Identifier: MIT
261cb0ef41Sopenharmony_ci */
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci#include "ares_setup.h"
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci#ifdef HAVE_NETINET_IN_H
311cb0ef41Sopenharmony_ci#  include <netinet/in.h>
321cb0ef41Sopenharmony_ci#endif
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci#include "ares_nameser.h"
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci#include "ares.h"
371cb0ef41Sopenharmony_ci#include "ares_private.h" /* for the memdebug */
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciares_status_t ares__expand_name_validated(const unsigned char *encoded,
401cb0ef41Sopenharmony_ci                                          const unsigned char *abuf,
411cb0ef41Sopenharmony_ci                                          size_t alen, char **s, size_t *enclen,
421cb0ef41Sopenharmony_ci                                          ares_bool_t is_hostname)
431cb0ef41Sopenharmony_ci{
441cb0ef41Sopenharmony_ci  ares_status_t status;
451cb0ef41Sopenharmony_ci  ares__buf_t  *buf = NULL;
461cb0ef41Sopenharmony_ci  size_t        start_len;
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  if (encoded == NULL || abuf == NULL || alen == 0 || enclen == NULL) {
491cb0ef41Sopenharmony_ci    return ARES_EBADNAME; /* EFORMERR would be better */
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  if (encoded < abuf || encoded >= abuf + alen) {
531cb0ef41Sopenharmony_ci    return ARES_EBADNAME; /* EFORMERR would be better */
541cb0ef41Sopenharmony_ci  }
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  *enclen = 0;
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  /* NOTE: we allow 's' to be NULL to skip it */
591cb0ef41Sopenharmony_ci  if (s) {
601cb0ef41Sopenharmony_ci    *s = NULL;
611cb0ef41Sopenharmony_ci  }
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  buf = ares__buf_create_const(abuf, alen);
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  if (buf == NULL) {
661cb0ef41Sopenharmony_ci    return ARES_ENOMEM;
671cb0ef41Sopenharmony_ci  }
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  status = ares__buf_set_position(buf, (size_t)(encoded - abuf));
701cb0ef41Sopenharmony_ci  if (status != ARES_SUCCESS) {
711cb0ef41Sopenharmony_ci    goto done;
721cb0ef41Sopenharmony_ci  }
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  start_len = ares__buf_len(buf);
751cb0ef41Sopenharmony_ci  status    = ares__dns_name_parse(buf, s, is_hostname);
761cb0ef41Sopenharmony_ci  if (status != ARES_SUCCESS) {
771cb0ef41Sopenharmony_ci    goto done;
781cb0ef41Sopenharmony_ci  }
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  *enclen = start_len - ares__buf_len(buf);
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_cidone:
831cb0ef41Sopenharmony_ci  ares__buf_destroy(buf);
841cb0ef41Sopenharmony_ci  return status;
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ciint ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
881cb0ef41Sopenharmony_ci                     int alen, char **s, long *enclen)
891cb0ef41Sopenharmony_ci{
901cb0ef41Sopenharmony_ci  /* Keep public API compatible */
911cb0ef41Sopenharmony_ci  size_t        enclen_temp = 0;
921cb0ef41Sopenharmony_ci  ares_status_t status;
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  if (alen < 0) {
951cb0ef41Sopenharmony_ci    return ARES_EBADRESP;
961cb0ef41Sopenharmony_ci  }
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  status  = ares__expand_name_validated(encoded, abuf, (size_t)alen, s,
991cb0ef41Sopenharmony_ci                                        &enclen_temp, ARES_FALSE);
1001cb0ef41Sopenharmony_ci  *enclen = (long)enclen_temp;
1011cb0ef41Sopenharmony_ci  return (int)status;
1021cb0ef41Sopenharmony_ci}
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci/* Like ares_expand_name_validated  but returns EBADRESP in case of invalid
1051cb0ef41Sopenharmony_ci * input. */
1061cb0ef41Sopenharmony_ciares_status_t ares__expand_name_for_response(const unsigned char *encoded,
1071cb0ef41Sopenharmony_ci                                             const unsigned char *abuf,
1081cb0ef41Sopenharmony_ci                                             size_t alen, char **s,
1091cb0ef41Sopenharmony_ci                                             size_t     *enclen,
1101cb0ef41Sopenharmony_ci                                             ares_bool_t is_hostname)
1111cb0ef41Sopenharmony_ci{
1121cb0ef41Sopenharmony_ci  ares_status_t status =
1131cb0ef41Sopenharmony_ci    ares__expand_name_validated(encoded, abuf, alen, s, enclen, is_hostname);
1141cb0ef41Sopenharmony_ci  if (status == ARES_EBADNAME) {
1151cb0ef41Sopenharmony_ci    status = ARES_EBADRESP;
1161cb0ef41Sopenharmony_ci  }
1171cb0ef41Sopenharmony_ci  return status;
1181cb0ef41Sopenharmony_ci}
119