• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define _GNU_SOURCE
2 
3 #include <netdb.h>
4 #include <string.h>
5 #include <unistd.h>
6 
7 #define HOSTID_NUM   16
8 
gethostid()9 long gethostid()
10 {
11 	int ret;
12 	struct in_addr in;
13 	struct hostent *hst = NULL;
14 	char **p = NULL;
15 	char host[128] = {0};
16 
17 	ret = gethostname(host, sizeof(host));
18 	if (ret || host[0] == '\0') {
19 		return -1;
20 	}
21 
22 	hst = gethostbyname(host);
23 	if (hst == NULL) {
24 		return -1;
25 	}
26 
27 	p = hst->h_addr_list;
28 	memcpy(&in.s_addr, *p, sizeof(in.s_addr));
29 
30 	return (long)((in.s_addr << HOSTID_NUM) | (in.s_addr >> HOSTID_NUM));
31 }
32