|
@@ -1,3 +1,4 @@
|
|
|
+
|
|
|
|
|
|
* Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
|
|
|
*
|
|
@@ -6,15 +7,13 @@
|
|
|
|
|
|
#define __FORCE_GLIBC
|
|
|
#include <features.h>
|
|
|
-#include <stdio.h>
|
|
|
-#include <string.h>
|
|
|
#include <errno.h>
|
|
|
-#include <sys/param.h>
|
|
|
-#include <netinet/in.h>
|
|
|
-#include <netdb.h>
|
|
|
-#include <fcntl.h>
|
|
|
#include <unistd.h>
|
|
|
-
|
|
|
+#include <sys/types.h>
|
|
|
+#include <fcntl.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#include <string.h>
|
|
|
+#include <netdb.h>
|
|
|
|
|
|
#define HOSTID "/etc/hostid"
|
|
|
|
|
@@ -35,23 +34,23 @@ int sethostid(long int new_id)
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
+#define _addr(a) (((struct sockaddr_in*)a->ai_addr)->sin_addr.s_addr)
|
|
|
long int gethostid(void)
|
|
|
{
|
|
|
- char host[MAXHOSTNAMELEN + 1];
|
|
|
- int fd, id;
|
|
|
+ char host[HOST_NAME_MAX + 1];
|
|
|
+ int fd, id = 0;
|
|
|
|
|
|
|
|
|
* It is not an error if we cannot read this file. It is not even an
|
|
|
* error if we cannot read all the bytes, we just carry on trying...
|
|
|
*/
|
|
|
fd = open(HOSTID, O_RDONLY);
|
|
|
- if (fd >= 0 && read(fd, &id, sizeof(id)))
|
|
|
- {
|
|
|
- close (fd);
|
|
|
- return id;
|
|
|
+ if (fd >= 0) {
|
|
|
+ int i = read(fd, &id, sizeof(id));
|
|
|
+ close(fd);
|
|
|
+ if (i > 0)
|
|
|
+ return id;
|
|
|
}
|
|
|
- if (fd >= 0) close (fd);
|
|
|
-
|
|
|
|
|
|
* numbers, if on the internet, will have a unique address. If they
|
|
|
* are not on the internet then we can return 0 which means they should
|
|
@@ -63,32 +62,18 @@ long int gethostid(void)
|
|
|
* setting one anyway.
|
|
|
* Mitch
|
|
|
*/
|
|
|
- if (gethostname(host, MAXHOSTNAMELEN) >= 0 && *host) {
|
|
|
- struct hostent *hp;
|
|
|
- struct in_addr in;
|
|
|
- struct hostent ghbn_h;
|
|
|
- char ghbn_buf[sizeof(struct in_addr) +
|
|
|
- sizeof(struct in_addr *)*2 +
|
|
|
- sizeof(char *)*((2 + 5 +
|
|
|
- 1)) +
|
|
|
- 256 + 32];
|
|
|
- int ghbn_errno;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- gethostbyname_r(host, &ghbn_h, ghbn_buf, sizeof(ghbn_buf), &hp, &ghbn_errno);
|
|
|
-
|
|
|
- if (hp == NULL) {
|
|
|
-
|
|
|
- * this host is not on a network and/or they have not
|
|
|
- * configured their network properly. So we return the unset
|
|
|
- * hostid which should be 0, meaning that they should set it !!
|
|
|
- */
|
|
|
- return 0;
|
|
|
+ if (gethostname(host, HOST_NAME_MAX) >= 0 && *host) {
|
|
|
+ struct addrinfo hints, *results, *addr;
|
|
|
+ memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
+ if (!getaddrinfo(host, NULL, &hints, &results)) {
|
|
|
+ for (addr = results; addr; addr = results->ai_next) {
|
|
|
+
|
|
|
+ IP addr */
|
|
|
+ id = _addr(addr) << 16 | _addr(addr) >> 16;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ freeaddrinfo(results);
|
|
|
}
|
|
|
- memcpy(&in, hp->h_addr, hp->h_length);
|
|
|
-
|
|
|
- return (in.s_addr<<16 | in.s_addr>>16);
|
|
|
}
|
|
|
- return 0;
|
|
|
+ return id;
|
|
|
}
|