res_isourserver.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Legacy function for expected server checking.
  2. Copyright (C) 2016-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /*
  16. * Copyright (c) 1985, 1989, 1993
  17. * The Regents of the University of California. All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. * 4. Neither the name of the University nor the names of its contributors
  28. * may be used to endorse or promote products derived from this software
  29. * without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  32. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  35. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  41. * SUCH DAMAGE.
  42. */
  43. /*
  44. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  45. *
  46. * Permission to use, copy, modify, and distribute this software for any
  47. * purpose with or without fee is hereby granted, provided that the above
  48. * copyright notice and this permission notice appear in all copies, and that
  49. * the name of Digital Equipment Corporation not be used in advertising or
  50. * publicity pertaining to distribution of the document or software without
  51. * specific, written prior permission.
  52. *
  53. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  54. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  55. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  56. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  57. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  58. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  59. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  60. * SOFTWARE.
  61. */
  62. /*
  63. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  64. *
  65. * Permission to use, copy, modify, and distribute this software for any
  66. * purpose with or without fee is hereby granted, provided that the above
  67. * copyright notice and this permission notice appear in all copies.
  68. *
  69. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  70. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  71. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  72. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  73. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  74. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  75. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  76. * SOFTWARE.
  77. */
  78. #include <resolv-internal.h>
  79. #include <resolv.h>
  80. #include <string.h>
  81. /* author: paul vixie, 29may94 */
  82. static int
  83. res_ourserver_p (const res_state statp, const struct sockaddr_in6 *inp)
  84. {
  85. int ns;
  86. if (inp->sin6_family == AF_INET)
  87. {
  88. struct sockaddr_in *in4p = (struct sockaddr_in *) inp;
  89. in_port_t port = in4p->sin_port;
  90. in_addr_t addr = in4p->sin_addr.s_addr;
  91. for (ns = 0; ns < statp->nscount; ns++)
  92. {
  93. const struct sockaddr_in *srv =
  94. (struct sockaddr_in *) __res_get_nsaddr (statp, ns);
  95. if ((srv->sin_family == AF_INET) &&
  96. (srv->sin_port == port) &&
  97. (srv->sin_addr.s_addr == INADDR_ANY ||
  98. srv->sin_addr.s_addr == addr))
  99. return 1;
  100. }
  101. } else if (inp->sin6_family == AF_INET6)
  102. {
  103. for (ns = 0; ns < statp->nscount; ns++)
  104. {
  105. const struct sockaddr_in6 *srv
  106. = (struct sockaddr_in6 *) __res_get_nsaddr (statp, ns);
  107. if ((srv->sin6_family == AF_INET6) &&
  108. (srv->sin6_port == inp->sin6_port) &&
  109. !(memcmp (&srv->sin6_addr, &in6addr_any,
  110. sizeof (struct in6_addr)) &&
  111. memcmp (&srv->sin6_addr, &inp->sin6_addr,
  112. sizeof (struct in6_addr))))
  113. return 1;
  114. }
  115. }
  116. return 0;
  117. }
  118. int
  119. res_isourserver (const struct sockaddr_in *inp)
  120. {
  121. return res_ourserver_p (&_res, (const struct sockaddr_in6 *) inp);
  122. }