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