summaryrefslogtreecommitdiff
path: root/libc/inet/hostid.c
blob: c6d6204ae7c1e63ed5b4b9efdedcc6475befb2ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
 *
 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
 */

#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 <not-cancel.h>


#define HOSTID "/etc/hostid"

#ifdef __USE_BSD
int sethostid(long int new_id)
{
	int fd;
	int ret;

	if (geteuid() || getuid())
		return __set_errno(EPERM);
	fd = open_not_cancel(HOSTID, O_CREAT|O_WRONLY, 0644);
	if (fd < 0)
		return fd;
	ret = write_not_cancel(fd, &new_id, sizeof(new_id)) == sizeof(new_id) ? 0 : -1;
	close_not_cancel_no_status (fd);
	return ret;
}
#endif

long int gethostid(void)
{
	char host[MAXHOSTNAMELEN + 1];
	int fd, id;

	/* 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_not_cancel_2(HOSTID, O_RDONLY);
	if (fd >= 0 && read_not_cancel(fd, &id, sizeof(id)))
	{
		close_not_cancel_no_status (fd);
		return id;
	}
	if (fd >= 0) close_not_cancel_no_status (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
	 * really set this number via a sethostid() call. If their hostname
	 * returns the loopback number (i.e. if they have put their hostname
	 * in the /etc/hosts file with 127.0.0.1) then all such hosts will
	 * have a non-unique hostid, but it doesn't matter anyway and
	 * gethostid() will return a non zero number without the need for
	 * 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@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;
		}
		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;
}