curl_gethostname.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "curl_gethostname.h"
  26. /*
  27. * Curl_gethostname() is a wrapper around gethostname() which allows
  28. * overriding the hostname that the function would normally return.
  29. * This capability is used by the test suite to verify exact matching
  30. * of NTLM authentication, which exercises libcurl's MD4 and DES code
  31. * as well as by the SMTP module when a hostname is not provided.
  32. *
  33. * For libcurl debug enabled builds hostname overriding takes place
  34. * when environment variable CURL_GETHOSTNAME is set, using the value
  35. * held by the variable to override returned hostname.
  36. *
  37. * Note: The function always returns the un-qualified hostname rather
  38. * than being provider dependent.
  39. */
  40. int Curl_gethostname(char * const name, GETHOSTNAME_TYPE_ARG2 namelen)
  41. {
  42. #ifndef HAVE_GETHOSTNAME
  43. /* Allow compilation and return failure when unavailable */
  44. (void) name;
  45. (void) namelen;
  46. return -1;
  47. #else
  48. int err;
  49. char *dot;
  50. #ifdef DEBUGBUILD
  51. /* Override hostname when environment variable CURL_GETHOSTNAME is set */
  52. const char *force_hostname = getenv("CURL_GETHOSTNAME");
  53. if(force_hostname) {
  54. if(strlen(force_hostname) < (size_t)namelen)
  55. strcpy(name, force_hostname);
  56. else
  57. return 1; /* can't do it */
  58. err = 0;
  59. }
  60. else {
  61. name[0] = '\0';
  62. err = gethostname(name, namelen);
  63. }
  64. #else /* DEBUGBUILD */
  65. name[0] = '\0';
  66. #ifdef __AMIGA__
  67. err = gethostname((unsigned char *)name, namelen);
  68. #else
  69. err = gethostname(name, namelen);
  70. #endif
  71. #endif
  72. name[namelen - 1] = '\0';
  73. if(err)
  74. return err;
  75. /* Truncate domain, leave only machine name */
  76. dot = strchr(name, '.');
  77. if(dot)
  78. *dot = '\0';
  79. return 0;
  80. #endif
  81. }