#include <stdlib.h>#include <string.h>#include "portability.h"#include "utility.h"#include "proxylib.h"
Go to the source code of this file.
Defines | |
| #define | GS_HOST_NAME_MAX 1024 |
| #define | GS_DOMAIN_NAME_MAX 1024 |
Functions | |
| char * | gs_fallback_get_machine_name () |
| char * | gs_get_machine_name () |
This file contains code to get the machine's hostname.
Definition in file get_machine_name.c.
| #define GS_DOMAIN_NAME_MAX 1024 |
Definition at line 18 of file get_machine_name.c.
| #define GS_HOST_NAME_MAX 1024 |
Definition at line 17 of file get_machine_name.c.
| char * gs_fallback_get_machine_name | ( | ) |
Definition at line 61 of file get_machine_name.c.
{
char name[GS_HOST_NAME_MAX], domain[GS_DOMAIN_NAME_MAX],
hostname[GS_HOST_NAME_MAX+GS_DOMAIN_NAME_MAX], *retval;
if(gethostname(name, GS_HOST_NAME_MAX-1) == -1)
return(strdup("localhost.localdomain"));
name[GS_HOST_NAME_MAX-1] = 0;
if(strlen(name) == 0)
sprintf(name, "localhost");
if(!strstr(name,".")) {
if(getdomainname(domain, GS_DOMAIN_NAME_MAX-1) == -1)
return(strdup("localhost.localdomain"));
domain[GS_DOMAIN_NAME_MAX-1] = 0;
if((strlen(domain) == 0) || !strcmp(domain, "(none)"))
sprintf(domain, "localdomain");
sprintf(hostname, "%s.%s", name, domain);
retval = strdup(hostname);
}
else
retval = strdup(name);
return retval ? retval : strdup("localhost.localdomain");
}

| char* gs_get_machine_name | ( | ) |
Get the machine's hostname.
Definition at line 29 of file get_machine_name.c.
{
const unsigned char *p;
static char buf[16];
struct hostent *hp;
ipaddr_t myip;
myip = proxy_get_my_ipaddr();
p = (const unsigned char *) &myip;
sprintf(buf, "%u.%u.%u.%u",
(unsigned int) (p[0] & 0xff), (unsigned int) (p[1] & 0xff),
(unsigned int) (p[2] & 0xff), (unsigned int) (p[3] & 0xff));
if(myip > 0) {
if((hp = gethostbyaddr((char*)&myip, sizeof(myip), AF_INET)) != NULL) {
if(!strstr(hp->h_name, ".")) {
hp = gethostbyname(hp->h_name);
if(hp && strstr(hp->h_name, "."))
return strdup(hp->h_name);
}
else
return strdup(hp->h_name);
}
}
/* if the above failed, fall back to the old method */
return gs_fallback_get_machine_name();
}


1.6.3-20100507