curl/lib/curl_gethostname.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
3.2 KiB
C
Raw Normal View History

/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2020-11-04 21:02:01 +08:00
* are also available at https://curl.se/docs/copyright.html.
*
* 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.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
build: fix circular header inclusion with other packages This commit renames lib/setup.h to lib/curl_setup.h and renames lib/setup_once.h to lib/curl_setup_once.h. Removes the need and usage of a header inclusion guard foreign to libcurl. [1] Removes the need and presence of an alarming notice we carried in old setup_once.h [2] ---------------------------------------- 1 - lib/setup_once.h used __SETUP_ONCE_H macro as header inclusion guard up to commit ec691ca3 which changed this to HEADER_CURL_SETUP_ONCE_H, this single inclusion guard is enough to ensure that inclusion of lib/setup_once.h done from lib/setup.h is only done once. Additionally lib/setup.h has always used __SETUP_ONCE_H macro to protect inclusion of setup_once.h even after commit ec691ca3, this was to avoid a circular header inclusion triggered when building a c-ares enabled version with c-ares sources available which also has a setup_once.h header. Commit ec691ca3 exposes the real nature of __SETUP_ONCE_H usage in lib/setup.h, it is a header inclusion guard foreign to libcurl belonging to c-ares's setup_once.h The renaming this commit does, fixes the circular header inclusion, and as such removes the need and usage of a header inclusion guard foreign to libcurl. Macro __SETUP_ONCE_H no longer used in libcurl. 2 - Due to the circular interdependency of old lib/setup_once.h and the c-ares setup_once.h header, old file lib/setup_once.h has carried back from 2006 up to now days an alarming and prominent notice about the need of keeping libcurl's and c-ares's setup_once.h in sync. Given that this commit fixes the circular interdependency, the need and presence of mentioned notice is removed. All mentioned interdependencies come back from now old days when the c-ares project lived inside a curl subdirectory. This commit removes last traces of such fact.
2013-01-07 02:06:49 +08:00
#include "curl_setup.h"
#include "curl_gethostname.h"
/*
* Curl_gethostname() is a wrapper around gethostname() which allows
* overriding the hostname that the function would normally return.
* This capability is used by the test suite to verify exact matching
* of NTLM authentication, which exercises libcurl's MD4 and DES code
* as well as by the SMTP module when a hostname is not provided.
*
* For libcurl debug enabled builds hostname overriding takes place
* when environment variable CURL_GETHOSTNAME is set, using the value
* held by the variable to override returned hostname.
*
* Note: The function always returns the un-qualified hostname rather
* than being provider dependent.
*
* 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 hostname.
*
* For libcurl static library release builds no overriding takes place.
*/
int Curl_gethostname(char * const name, GETHOSTNAME_TYPE_ARG2 namelen)
{
#ifndef HAVE_GETHOSTNAME
/* Allow compilation and return failure when unavailable */
(void) name;
(void) namelen;
return -1;
#else
int err;
char *dot;
#ifdef DEBUGBUILD
/* Override hostname when environment variable CURL_GETHOSTNAME is set */
const char *force_hostname = getenv("CURL_GETHOSTNAME");
if(force_hostname) {
lib: fix compiler warnings (gcc) Seen when setting `ENABLE_DEBUG=ON` and `-DDEBUGBUILD` for mingw-w64 gcc 13.2.0 CMake unity builds in 'Release' configurations. ``` curl/lib/curl_gethostname.c:71:5: error: 'strncpy' specified bound 1025 equals destination size [-Werror=stringop-truncation] 71 | strncpy(name, force_hostname, namelen); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from curl/_bld/lib/CMakeFiles/libcurl_object.dir/Unity/unity_0_c.c:175: In function 'hostcache_timestamp_remove', inlined from 'Curl_hash_clean_with_criterium' at curl/lib/hash.c:265:19, inlined from 'Curl_hash_clean_with_criterium' at curl/lib/hash.c:247:1, inlined from 'hostcache_prune' at curl/lib/hostip.c:228:3, inlined from 'Curl_hostcache_prune' at curl/lib/hostip.c:256:21: curl/lib/hostip.c:205:12: error: 'now' may be used uninitialized [-Werror=maybe-uninitialized] 205 | time_t age = prune->now - c->timestamp; | ^~~ curl/lib/hostip.c: In function 'Curl_hostcache_prune': curl/lib/hostip.c:241:10: note: 'now' was declared here 241 | time_t now; | ^~~ In function 'hostcache_timestamp_remove', inlined from 'fetch_addr' at curl/lib/hostip.c:310:8: curl/lib/hostip.c:205:23: error: 'user.now' may be used uninitialized [-Werror=maybe-uninitialized] 205 | time_t age = prune->now - c->timestamp; | ~~~~~^~~~~ curl/lib/hostip.c: In function 'fetch_addr': curl/lib/hostip.c:304:33: note: 'user' declared here 304 | struct hostcache_prune_data user; | ^~~~ In file included from curl/_bld/lib/CMakeFiles/libcurl_object.dir/Unity/unity_0_c.c:40: curl/lib/cf-socket.c: In function 'cf_socket_send': curl/lib/cf-socket.c:1294:10: error: 'c' may be used uninitialized [-Werror=maybe-uninitialized] 1294 | if(c >= ((100-ctx->wblock_percent)*256/100)) { | ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ curl/lib/cf-socket.c:1292:19: note: 'c' was declared here 1292 | unsigned char c; | ^ In file included from curl/_bld/lib/CMakeFiles/libcurl_object.dir/Unity/unity_0_c.c:364: In function 'tftp_state_timeout', inlined from 'tftp_multi_statemach' at curl/lib/tftp.c:1230:27: curl/lib/tftp.c:1208:5: error: 'current' may be used uninitialized [-Werror=maybe-uninitialized] 1208 | if(current > state->rx_time + state->retry_time) { | ^ curl/lib/tftp.c: In function 'tftp_multi_statemach': curl/lib/tftp.c:1192:10: note: 'current' was declared here 1192 | time_t current; | ^~~~~~~ ``` Ref: https://ci.appveyor.com/project/curlorg/curl/builds/49792835/job/91c8dj5qb36spfe0#L112 Ref: https://github.com/curl/curl/actions/runs/9082968838/job/24960616145#step:12:62 Ref: #13592 Closes #13643
2024-05-12 05:38:25 +08:00
strncpy(name, force_hostname, namelen - 1);
err = 0;
}
else {
name[0] = '\0';
err = gethostname(name, namelen);
}
#else /* DEBUGBUILD */
/* 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. */
name[0] = '\0';
err = gethostname(name, namelen);
#endif
name[namelen - 1] = '\0';
if(err)
return err;
/* Truncate domain, leave only machine name */
dot = strchr(name, '.');
if(dot)
*dot = '\0';
return 0;
#endif
}