xref: /third_party/node/deps/cares/include/ares_dns.h (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci/* MIT License
21cb0ef41Sopenharmony_ci *
31cb0ef41Sopenharmony_ci * Copyright (c) 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#ifndef HEADER_CARES_DNS_H
281cb0ef41Sopenharmony_ci#define HEADER_CARES_DNS_H
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci/*
311cb0ef41Sopenharmony_ci * NOTE TO INTEGRATORS:
321cb0ef41Sopenharmony_ci *
331cb0ef41Sopenharmony_ci * This header is made public due to legacy projects relying on it.
341cb0ef41Sopenharmony_ci * Please do not use the macros within this header, or include this
351cb0ef41Sopenharmony_ci * header in your project as it may be removed in the future.
361cb0ef41Sopenharmony_ci */
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci/*
401cb0ef41Sopenharmony_ci * Macro DNS__16BIT reads a network short (16 bit) given in network
411cb0ef41Sopenharmony_ci * byte order, and returns its value as an unsigned short.
421cb0ef41Sopenharmony_ci */
431cb0ef41Sopenharmony_ci#define DNS__16BIT(p)                                                \
441cb0ef41Sopenharmony_ci  ((unsigned short)((unsigned int)0xffff &                           \
451cb0ef41Sopenharmony_ci                    (((unsigned int)((unsigned char)(p)[0]) << 8U) | \
461cb0ef41Sopenharmony_ci                     ((unsigned int)((unsigned char)(p)[1])))))
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci/*
491cb0ef41Sopenharmony_ci * Macro DNS__32BIT reads a network long (32 bit) given in network
501cb0ef41Sopenharmony_ci * byte order, and returns its value as an unsigned int.
511cb0ef41Sopenharmony_ci */
521cb0ef41Sopenharmony_ci#define DNS__32BIT(p)                                              \
531cb0ef41Sopenharmony_ci  ((unsigned int)(((unsigned int)((unsigned char)(p)[0]) << 24U) | \
541cb0ef41Sopenharmony_ci                  ((unsigned int)((unsigned char)(p)[1]) << 16U) | \
551cb0ef41Sopenharmony_ci                  ((unsigned int)((unsigned char)(p)[2]) << 8U) |  \
561cb0ef41Sopenharmony_ci                  ((unsigned int)((unsigned char)(p)[3]))))
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci#define DNS__SET16BIT(p, v)                       \
591cb0ef41Sopenharmony_ci  (((p)[0] = (unsigned char)(((v) >> 8) & 0xff)), \
601cb0ef41Sopenharmony_ci   ((p)[1] = (unsigned char)((v) & 0xff)))
611cb0ef41Sopenharmony_ci#define DNS__SET32BIT(p, v)                        \
621cb0ef41Sopenharmony_ci  (((p)[0] = (unsigned char)(((v) >> 24) & 0xff)), \
631cb0ef41Sopenharmony_ci   ((p)[1] = (unsigned char)(((v) >> 16) & 0xff)), \
641cb0ef41Sopenharmony_ci   ((p)[2] = (unsigned char)(((v) >> 8) & 0xff)),  \
651cb0ef41Sopenharmony_ci   ((p)[3] = (unsigned char)((v) & 0xff)))
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci#if 0
681cb0ef41Sopenharmony_ci/* we cannot use this approach on systems where we can't access 16/32 bit
691cb0ef41Sopenharmony_ci   data on un-aligned addresses */
701cb0ef41Sopenharmony_ci#  define DNS__16BIT(p)       ntohs(*(unsigned short *)(p))
711cb0ef41Sopenharmony_ci#  define DNS__32BIT(p)       ntohl(*(unsigned long *)(p))
721cb0ef41Sopenharmony_ci#  define DNS__SET16BIT(p, v) *(unsigned short *)(p) = htons(v)
731cb0ef41Sopenharmony_ci#  define DNS__SET32BIT(p, v) *(unsigned long *)(p) = htonl(v)
741cb0ef41Sopenharmony_ci#endif
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci/* Macros for parsing a DNS header */
771cb0ef41Sopenharmony_ci#define DNS_HEADER_QID(h)     DNS__16BIT(h)
781cb0ef41Sopenharmony_ci#define DNS_HEADER_QR(h)      (((h)[2] >> 7) & 0x1)
791cb0ef41Sopenharmony_ci#define DNS_HEADER_OPCODE(h)  (((h)[2] >> 3) & 0xf)
801cb0ef41Sopenharmony_ci#define DNS_HEADER_AA(h)      (((h)[2] >> 2) & 0x1)
811cb0ef41Sopenharmony_ci#define DNS_HEADER_TC(h)      (((h)[2] >> 1) & 0x1)
821cb0ef41Sopenharmony_ci#define DNS_HEADER_RD(h)      ((h)[2] & 0x1)
831cb0ef41Sopenharmony_ci#define DNS_HEADER_RA(h)      (((h)[3] >> 7) & 0x1)
841cb0ef41Sopenharmony_ci#define DNS_HEADER_Z(h)       (((h)[3] >> 4) & 0x7)
851cb0ef41Sopenharmony_ci#define DNS_HEADER_RCODE(h)   ((h)[3] & 0xf)
861cb0ef41Sopenharmony_ci#define DNS_HEADER_QDCOUNT(h) DNS__16BIT((h) + 4)
871cb0ef41Sopenharmony_ci#define DNS_HEADER_ANCOUNT(h) DNS__16BIT((h) + 6)
881cb0ef41Sopenharmony_ci#define DNS_HEADER_NSCOUNT(h) DNS__16BIT((h) + 8)
891cb0ef41Sopenharmony_ci#define DNS_HEADER_ARCOUNT(h) DNS__16BIT((h) + 10)
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci/* Macros for constructing a DNS header */
921cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_QID(h, v) DNS__SET16BIT(h, v)
931cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_QR(h, v)  ((h)[2] |= (unsigned char)(((v) & 0x1) << 7))
941cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_OPCODE(h, v) \
951cb0ef41Sopenharmony_ci  ((h)[2] |= (unsigned char)(((v) & 0xf) << 3))
961cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_AA(h, v)      ((h)[2] |= (unsigned char)(((v) & 0x1) << 2))
971cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_TC(h, v)      ((h)[2] |= (unsigned char)(((v) & 0x1) << 1))
981cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_RD(h, v)      ((h)[2] |= (unsigned char)((v) & 0x1))
991cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_RA(h, v)      ((h)[3] |= (unsigned char)(((v) & 0x1) << 7))
1001cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_Z(h, v)       ((h)[3] |= (unsigned char)(((v) & 0x7) << 4))
1011cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_RCODE(h, v)   ((h)[3] |= (unsigned char)((v) & 0xf))
1021cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_QDCOUNT(h, v) DNS__SET16BIT((h) + 4, v)
1031cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_ANCOUNT(h, v) DNS__SET16BIT((h) + 6, v)
1041cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_NSCOUNT(h, v) DNS__SET16BIT((h) + 8, v)
1051cb0ef41Sopenharmony_ci#define DNS_HEADER_SET_ARCOUNT(h, v) DNS__SET16BIT((h) + 10, v)
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci/* Macros for parsing the fixed part of a DNS question */
1081cb0ef41Sopenharmony_ci#define DNS_QUESTION_TYPE(q)  DNS__16BIT(q)
1091cb0ef41Sopenharmony_ci#define DNS_QUESTION_CLASS(q) DNS__16BIT((q) + 2)
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci/* Macros for constructing the fixed part of a DNS question */
1121cb0ef41Sopenharmony_ci#define DNS_QUESTION_SET_TYPE(q, v)  DNS__SET16BIT(q, v)
1131cb0ef41Sopenharmony_ci#define DNS_QUESTION_SET_CLASS(q, v) DNS__SET16BIT((q) + 2, v)
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci/* Macros for parsing the fixed part of a DNS resource record */
1161cb0ef41Sopenharmony_ci#define DNS_RR_TYPE(r)  DNS__16BIT(r)
1171cb0ef41Sopenharmony_ci#define DNS_RR_CLASS(r) DNS__16BIT((r) + 2)
1181cb0ef41Sopenharmony_ci#define DNS_RR_TTL(r)   DNS__32BIT((r) + 4)
1191cb0ef41Sopenharmony_ci#define DNS_RR_LEN(r)   DNS__16BIT((r) + 8)
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci/* Macros for constructing the fixed part of a DNS resource record */
1221cb0ef41Sopenharmony_ci#define DNS_RR_SET_TYPE(r, v)  DNS__SET16BIT(r, v)
1231cb0ef41Sopenharmony_ci#define DNS_RR_SET_CLASS(r, v) DNS__SET16BIT((r) + 2, v)
1241cb0ef41Sopenharmony_ci#define DNS_RR_SET_TTL(r, v)   DNS__SET32BIT((r) + 4, v)
1251cb0ef41Sopenharmony_ci#define DNS_RR_SET_LEN(r, v)   DNS__SET16BIT((r) + 8, v)
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci#endif /* HEADER_CARES_DNS_H */
128