2010-07-29 18:47:49 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2016-11-23 01:53:24 -05:00
|
|
|
* Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2010-07-29 18:47:49 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2010-07-29 18:47:49 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2010-08-07 08:33:14 -04:00
|
|
|
|
2010-07-29 18:47:49 -04:00
|
|
|
#include "curl_gethostname.h"
|
|
|
|
|
2010-08-07 08:33:14 -04:00
|
|
|
/*
|
|
|
|
* Curl_gethostname() is a wrapper around gethostname() which allows
|
|
|
|
* overriding the host name that the function would normally return.
|
|
|
|
* This capability is used by the test suite to verify exact matching
|
2011-09-13 17:19:47 -04:00
|
|
|
* of NTLM authentication, which exercises libcurl's MD4 and DES code
|
|
|
|
* as well as by the SMTP module when a hostname is not provided.
|
2010-08-07 08:33:14 -04:00
|
|
|
*
|
|
|
|
* For libcurl debug enabled builds host name overriding takes place
|
|
|
|
* when environment variable CURL_GETHOSTNAME is set, using the value
|
|
|
|
* held by the variable to override returned host name.
|
|
|
|
*
|
2011-09-13 17:19:47 -04:00
|
|
|
* Note: The function always returns the un-qualified hostname rather
|
|
|
|
* than being provider dependent.
|
|
|
|
*
|
2010-08-07 08:33:14 -04:00
|
|
|
* For libcurl shared library release builds the test suite preloads
|
|
|
|
* another shared library named libhostname using the LD_PRELOAD
|
|
|
|
* mechanism which intercepts, and might override, the gethostname()
|
|
|
|
* function call. In this case a given platform must support the
|
|
|
|
* LD_PRELOAD mechanism and additionally have environment variable
|
|
|
|
* CURL_GETHOSTNAME set in order to override the returned host name.
|
|
|
|
*
|
|
|
|
* For libcurl static library release builds no overriding takes place.
|
|
|
|
*/
|
2010-07-29 18:47:49 -04:00
|
|
|
|
2016-11-23 02:49:04 -05:00
|
|
|
int Curl_gethostname(char *name, GETHOSTNAME_TYPE_ARG2 namelen)
|
|
|
|
{
|
2010-08-07 08:33:14 -04:00
|
|
|
#ifndef HAVE_GETHOSTNAME
|
2010-07-29 18:47:49 -04:00
|
|
|
|
2010-08-07 08:33:14 -04:00
|
|
|
/* Allow compilation and return failure when unavailable */
|
|
|
|
(void) name;
|
|
|
|
(void) namelen;
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#else
|
2011-10-13 17:00:24 -04:00
|
|
|
int err;
|
2016-11-23 01:53:24 -05:00
|
|
|
char *dot;
|
2010-08-07 08:33:14 -04:00
|
|
|
|
|
|
|
#ifdef DEBUGBUILD
|
|
|
|
|
|
|
|
/* Override host name when environment variable CURL_GETHOSTNAME is set */
|
|
|
|
const char *force_hostname = getenv("CURL_GETHOSTNAME");
|
2010-07-29 18:47:49 -04:00
|
|
|
if(force_hostname) {
|
2011-10-13 17:00:24 -04:00
|
|
|
strncpy(name, force_hostname, namelen);
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
name[0] = '\0';
|
|
|
|
err = gethostname(name, namelen);
|
2010-07-29 18:47:49 -04:00
|
|
|
}
|
2010-08-07 08:33:14 -04:00
|
|
|
|
2011-09-13 17:19:47 -04:00
|
|
|
#else /* DEBUGBUILD */
|
2010-08-07 08:33:14 -04:00
|
|
|
|
|
|
|
/* The call to system's gethostname() might get intercepted by the
|
|
|
|
libhostname library when libcurl is built as a non-debug shared
|
|
|
|
library when running the test suite. */
|
2011-10-13 17:00:24 -04:00
|
|
|
name[0] = '\0';
|
|
|
|
err = gethostname(name, namelen);
|
2011-09-13 17:19:47 -04:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-10-13 17:00:24 -04:00
|
|
|
name[namelen - 1] = '\0';
|
|
|
|
|
|
|
|
if(err)
|
2011-09-13 17:19:47 -04:00
|
|
|
return err;
|
|
|
|
|
2011-10-13 17:00:24 -04:00
|
|
|
/* Truncate domain, leave only machine name */
|
|
|
|
dot = strchr(name, '.');
|
|
|
|
if(dot)
|
|
|
|
*dot = '\0';
|
2010-07-29 18:47:49 -04:00
|
|
|
|
2011-09-13 17:19:47 -04:00
|
|
|
return 0;
|
2010-07-29 18:47:49 -04:00
|
|
|
#endif
|
2010-08-07 08:33:14 -04:00
|
|
|
|
2010-07-29 18:47:49 -04:00
|
|
|
}
|