net.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * net.c: sockets abstraction and DNS related things
  14. *
  15. * Note: sockets created with net_socket are placed in non-blocking mode,
  16. * however this API simulates that the calls are blocking.
  17. *
  18. * Rob McCool
  19. */
  20. #include "netsite.h"
  21. #include <nspr.h>
  22. #include "util.h"
  23. #include <string.h>
  24. #include <arpa/inet.h> /* inet_ntoa */
  25. #include <netdb.h> /* hostent stuff */
  26. #ifdef NEED_GHN_PROTO
  27. extern "C" int gethostname (char *name, size_t namelen);
  28. #endif
  29. #ifdef LINUX
  30. #include <sys/ioctl.h> /* ioctl */
  31. #endif
  32. #include "libadmin/libadmin.h"
  33. /* ---------------------------- util_hostname ----------------------------- */
  34. #include <sys/param.h>
  35. /* Defined in dns.cpp */
  36. char *net_find_fqdn(PRHostEnt *p);
  37. NSAPI_PUBLIC char *util_hostname(void)
  38. {
  39. char str[MAXHOSTNAMELEN];
  40. PRHostEnt hent;
  41. char buf[PR_NETDB_BUF_SIZE];
  42. PRStatus err;
  43. gethostname(str, MAXHOSTNAMELEN);
  44. err = PR_GetHostByName(
  45. str,
  46. buf,
  47. PR_NETDB_BUF_SIZE,
  48. &hent);
  49. if (err == PR_FAILURE)
  50. return NULL;
  51. return net_find_fqdn(&hent);
  52. }