/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2010, 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
 * are also available at http://curl.haxx.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.
 *
 ***************************************************************************/

#include "setup.h"

#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif

#include "urldata.h"
#include "hostip.h"
#include "curl_gethostname.h"

/*
 * 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
 * of NTLM authentication, which exercises libcurl's MD4 and DES code.
 *
 * 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.
 *
 * 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.
 */

int Curl_gethostname(struct connectdata *conn, char *name,
                     GETHOSTNAME_TYPE_ARG2 namelen, bool fqdn)
{
#ifndef HAVE_GETHOSTNAME

  /* Allow compilation and return failure when unavailable */
  (void) name;
  (void) namelen;
  return -1;

#else
  int res;
  char hostname[HOSTNAME_MAX + 1];
  const char *dot;
  struct Curl_dns_entry *hostaddr;

  /* NOTE: 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. */

#ifdef DEBUGBUILD
  /* Override host name when environment variable CURL_GETHOSTNAME is set */
  const char *force_hostname = getenv("CURL_GETHOSTNAME");
  if(force_hostname) {
    strncpy(hostname, force_hostname, sizeof(hostname));
    hostname[sizeof(hostname) - 1] = '\0';
    res = 0;
  }
  else
    res = gethostname(hostname, sizeof(hostname));
#else
  res = gethostname(hostname, sizeof(hostname));
#endif

  if(res != 0)
    return CURLE_COULDNT_RESOLVE_HOST;

  /* Is the hostname is already fully qualified? */
  dot = strchr(hostname, '.');
  if(dot) {
    /* Do we want the fully qualified domain name? */
    if(fqdn) {
      /* Copy the fully qualifed name to the specified buffer */
      strncpy(name, hostname, namelen);
      name[namelen - 1] = '\0';
    }
    else {
      /* Copy only the machine name to the specified buffer */
      size_t size = dot - hostname;
      strncpy(name, hostname, namelen > size ? size : namelen);
      name[(namelen > size ? size : namelen) - 1] = '\0';
    }

    return CURLE_OK;
  }

  /* Simply return hostname if we don't want it to be fully qualified? */
  if(!fqdn) {
    strncpy(name, hostname, namelen);
    name[namelen - 1] = '\0';

    return CURLE_OK;
  }

  /* Attempt to resolve the hostname into a fqdn */
  res = Curl_resolv(conn, hostname, PORT_HTTP, &hostaddr);
  if(res == CURLRESOLV_ERROR)
    return CURLE_COULDNT_RESOLVE_HOST;
  else if(res == CURLRESOLV_PENDING) {
    /* Wait for the resolver to resolve */
    res = Curl_resolver_wait_resolv(conn, &hostaddr);
    if(res != CURLE_OK)
      return res;
  }

  /* Do we have a canonical name? */
  if(hostaddr->addr->ai_canonname == NULL) {
    /* Release our usage count on the dns entry */
    Curl_resolv_unlock(conn->data, hostaddr);

    return CURLE_COULDNT_RESOLVE_HOST;
  }

  /* Copy the fully qualified name to the specified buffer */
  strncpy(name, hostaddr->addr->ai_canonname, namelen);
  name[namelen - 1] = '\0';

  /* Release our usage count on the dns entry */
  Curl_resolv_unlock(conn->data, hostaddr);

  return CURLE_OK;
#endif
}

