getaddrinfo.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * libhostile
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <config.h>
  22. /*
  23. Random getaddrinfo failing library for testing getaddrinfo() failures.
  24. LD_PRELOAD="/usr/lib/libdl.so ./util/libhostile_getaddrinfo.so" ./binary
  25. */
  26. #include <dlfcn.h>
  27. #include <assert.h>
  28. #include <errno.h>
  29. #include <stdbool.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <time.h>
  33. #include <unistd.h>
  34. #include <libhostile/initialize.h>
  35. __thread bool is_in_getaddrinfo= 0;
  36. static int not_until= 500;
  37. static struct function_st __function;
  38. static pthread_once_t function_lookup_once = PTHREAD_ONCE_INIT;
  39. static void set_local(void)
  40. {
  41. __function= set_function("getaddrinfo", "HOSTILE_GETADDRINFO");
  42. }
  43. bool is_getaddrinfo(void)
  44. {
  45. return is_in_getaddrinfo;
  46. }
  47. int getaddrinfo(const char *node, const char *service,
  48. const struct addrinfo *hints,
  49. struct addrinfo **res)
  50. {
  51. hostile_initialize();
  52. (void) pthread_once(&function_lookup_once, set_local);
  53. if (__function.frequency)
  54. {
  55. if (--not_until < 0 && random() % __function.frequency)
  56. {
  57. #if 0
  58. perror("HOSTILE CLOSE() of socket during getaddrinfo()");
  59. #endif
  60. return EAI_FAIL;
  61. }
  62. }
  63. is_in_getaddrinfo= true;
  64. int ret= __function.function.getaddrinfo(node, service, hints, res);
  65. is_in_getaddrinfo= false;
  66. return ret;
  67. }