curl_gethostname.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "curl_gethostname.h"
  24. /*
  25. * Curl_gethostname() is a wrapper around gethostname() which allows
  26. * overriding the host name that the function would normally return.
  27. * This capability is used by the test suite to verify exact matching
  28. * of NTLM authentication, which exercises libcurl's MD4 and DES code
  29. * as well as by the SMTP module when a hostname is not provided.
  30. *
  31. * For libcurl debug enabled builds host name overriding takes place
  32. * when environment variable CURL_GETHOSTNAME is set, using the value
  33. * held by the variable to override returned host name.
  34. *
  35. * Note: The function always returns the un-qualified hostname rather
  36. * than being provider dependent.
  37. *
  38. * For libcurl shared library release builds the test suite preloads
  39. * another shared library named libhostname using the LD_PRELOAD
  40. * mechanism which intercepts, and might override, the gethostname()
  41. * function call. In this case a given platform must support the
  42. * LD_PRELOAD mechanism and additionally have environment variable
  43. * CURL_GETHOSTNAME set in order to override the returned host name.
  44. *
  45. * For libcurl static library release builds no overriding takes place.
  46. */
  47. int Curl_gethostname(char *name, GETHOSTNAME_TYPE_ARG2 namelen) {
  48. #ifndef HAVE_GETHOSTNAME
  49. /* Allow compilation and return failure when unavailable */
  50. (void) name;
  51. (void) namelen;
  52. return -1;
  53. #else
  54. int err;
  55. char* dot;
  56. #ifdef DEBUGBUILD
  57. /* Override host name when environment variable CURL_GETHOSTNAME is set */
  58. const char *force_hostname = getenv("CURL_GETHOSTNAME");
  59. if(force_hostname) {
  60. strncpy(name, force_hostname, namelen);
  61. err = 0;
  62. }
  63. else {
  64. name[0] = '\0';
  65. err = gethostname(name, namelen);
  66. }
  67. #else /* DEBUGBUILD */
  68. /* The call to system's gethostname() might get intercepted by the
  69. libhostname library when libcurl is built as a non-debug shared
  70. library when running the test suite. */
  71. name[0] = '\0';
  72. err = gethostname(name, namelen);
  73. #endif
  74. name[namelen - 1] = '\0';
  75. if(err)
  76. return err;
  77. /* Truncate domain, leave only machine name */
  78. dot = strchr(name, '.');
  79. if(dot)
  80. *dot = '\0';
  81. return 0;
  82. #endif
  83. }