[git commit nptl] gethostid: switch to getaddrinfo and shrink
Bernhard Reutner-Fischer
rep.dot.nop at gmail.com
Sun Jan 24 05:32:10 UTC 2010
commit: http://git.uclibc.org/uClibc/commit/?id=64b256b98412206cb6da4ca6491528aa360e56bf
branch: http://git.uclibc.org/uClibc/commit/?id=refs/heads/nptl
text data bss dec hex filename
382 0 0 382 17e libc/inet/hostid.os.old
326 0 0 326 146 libc/inet/hostid.os
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop at gmail.com>
---
libc/inet/hostid.c | 67 ++++++++++++++++++++-------------------------------
1 files changed, 26 insertions(+), 41 deletions(-)
diff --git a/libc/inet/hostid.c b/libc/inet/hostid.c
index 99346d7..ac4c56c 100644
--- a/libc/inet/hostid.c
+++ b/libc/inet/hostid.c
@@ -1,3 +1,4 @@
+/* vi: set sw=4 ts=4: */
/*
* Copyright (C) 2000-2006 Erik Andersen <andersen at 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;
/* If hostid was already set then we can return that value.
* 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);
-
/* Try some methods of returning a unique 32 bit id. Clearly IP
* 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/*MAX_ALIASES*/ +
- 1)/*ALIAS_DIM*/) +
- 256/*namebuffer*/ + 32/* margin */];
- int ghbn_errno;
-
- /* replace gethostbyname() with gethostbyname_r() - ron at zing.net */
- /*if ((hp = gethostbyname(host)) == (struct hostent *)NULL)*/
- gethostbyname_r(host, &ghbn_h, ghbn_buf, sizeof(ghbn_buf), &hp, &ghbn_errno);
-
- if (hp == NULL) {
- /* This is not a error if we get here, as all it means is that
- * 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) {
+ /* Just so it doesn't look exactly like the
+ IP addr */
+ id = _addr(addr) << 16 | _addr(addr) >> 16;
+ break;
+ }
+ freeaddrinfo(results);
}
- memcpy(&in, hp->h_addr, hp->h_length);
- /* Just so it doesn't look exactly like the IP addr */
- return (in.s_addr<<16 | in.s_addr>>16);
}
- return 0;
+ return id;
}
--
1.6.3.3
More information about the uClibc-cvs
mailing list