2
0

curl_gethostname.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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 "setup.h"
  23. #ifdef HAVE_UNISTD_H
  24. # include <unistd.h>
  25. #endif
  26. #include "curl_gethostname.h"
  27. /* Hostname buffer size */
  28. #define HOSTNAME_MAX 1024
  29. /*
  30. * Curl_gethostname() is a wrapper around gethostname() which allows
  31. * overriding the host name that the function would normally return.
  32. * This capability is used by the test suite to verify exact matching
  33. * of NTLM authentication, which exercises libcurl's MD4 and DES code
  34. * as well as by the SMTP module when a hostname is not provided.
  35. *
  36. * For libcurl debug enabled builds host name overriding takes place
  37. * when environment variable CURL_GETHOSTNAME is set, using the value
  38. * held by the variable to override returned host name.
  39. *
  40. * Note: The function always returns the un-qualified hostname rather
  41. * than being provider dependent.
  42. *
  43. * For libcurl shared library release builds the test suite preloads
  44. * another shared library named libhostname using the LD_PRELOAD
  45. * mechanism which intercepts, and might override, the gethostname()
  46. * function call. In this case a given platform must support the
  47. * LD_PRELOAD mechanism and additionally have environment variable
  48. * CURL_GETHOSTNAME set in order to override the returned host name.
  49. *
  50. * For libcurl static library release builds no overriding takes place.
  51. */
  52. int Curl_gethostname(char *name, GETHOSTNAME_TYPE_ARG2 namelen) {
  53. #ifndef HAVE_GETHOSTNAME
  54. /* Allow compilation and return failure when unavailable */
  55. (void) name;
  56. (void) namelen;
  57. return -1;
  58. #else
  59. int err = 0;
  60. char* dot = NULL;
  61. char hostname[HOSTNAME_MAX + 1];
  62. #ifdef DEBUGBUILD
  63. /* Override host name when environment variable CURL_GETHOSTNAME is set */
  64. const char *force_hostname = getenv("CURL_GETHOSTNAME");
  65. if(force_hostname) {
  66. strncpy(hostname, force_hostname, sizeof(hostname));
  67. hostname[sizeof(hostname) - 1] = '\0';
  68. }
  69. else
  70. err = gethostname(hostname, sizeof(hostname));
  71. #else /* DEBUGBUILD */
  72. /* The call to system's gethostname() might get intercepted by the
  73. libhostname library when libcurl is built as a non-debug shared
  74. library when running the test suite. */
  75. err = gethostname(hostname, sizeof(hostname));
  76. #endif
  77. if(err != 0)
  78. return err;
  79. /* Is the hostname fully qualified? */
  80. dot = strchr(hostname, '.');
  81. if(dot) {
  82. /* Copy only the machine name to the specified buffer */
  83. size_t size = dot - hostname;
  84. strncpy(name, hostname, namelen > size ? size : namelen);
  85. name[(namelen > size ? size : namelen) - 1] = '\0';
  86. }
  87. else {
  88. /* Copy the hostname to the specified buffer */
  89. strncpy(name, hostname, namelen);
  90. name[namelen - 1] = '\0';
  91. }
  92. return 0;
  93. #endif
  94. }