1 #ifdef __LITEOS_A__ 2 #define _GNU_SOURCE 3 4 #include <netdb.h> 5 #include <string.h> 6 #include <unistd.h> 7 8 #define HOSTID_NUM 16 9 #else 10 #include <unistd.h> 11 #endif 12 gethostidnull13long gethostid() 14 { 15 #ifdef __LITEOS_A__ 16 int ret; 17 struct in_addr in; 18 struct hostent *hst = NULL; 19 char **p = NULL; 20 char host[128] = {0}; 21 22 ret = gethostname(host, sizeof(host)); 23 if (ret || host[0] == '\0') { 24 return -1; 25 } 26 27 hst = gethostbyname(host); 28 if (hst == NULL) { 29 return -1; 30 } 31 32 p = hst->h_addr_list; 33 memcpy(&in.s_addr, *p, sizeof(in.s_addr)); 34 35 return (long)((in.s_addr << HOSTID_NUM) | (in.s_addr >> HOSTID_NUM)); 36 #else 37 return 0; 38 #endif 39 } 40