OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [inet/] [hostid.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
#define __FORCE_GLIBC
2
#include <features.h>
3
#include <stdio.h>
4
#include <string.h>
5
#include <errno.h>
6
#include <sys/param.h>
7
#include <netinet/in.h>
8
#include <netdb.h>
9
#include <fcntl.h>
10
#include <unistd.h>
11
 
12
 
13
#define HOSTID "/etc/hostid"
14
 
15
int sethostid(long int new_id)
16
{
17
        int fd;
18
        int ret;
19
 
20
        if (geteuid() || getuid()) return __set_errno(EPERM);
21
        if ((fd=open(HOSTID,O_CREAT|O_WRONLY,0644))<0) return -1;
22
        ret = write(fd,(void *)&new_id,sizeof(new_id)) == sizeof(new_id)
23
                ? 0 : -1;
24
        close (fd);
25
        return ret;
26
}
27
 
28
long int gethostid(void)
29
{
30
        char host[MAXHOSTNAMELEN + 1];
31
        int fd, id;
32
 
33
        /* If hostid was already set the we can return that value.
34
         * It is not an error if we cannot read this file. It is not even an
35
         * error if we cannot read all the bytes, we just carry on trying...
36
         */
37
        if ((fd=open(HOSTID,O_RDONLY))>=0 && read(fd,(void *)&id,sizeof(id)))
38
        {
39
                close (fd);
40
                return id;
41
        }
42
        if (fd >= 0) close (fd);
43
 
44
        /* Try some methods of returning a unique 32 bit id. Clearly IP
45
         * numbers, if on the internet, will have a unique address. If they
46
         * are not on the internet then we can return 0 which means they should
47
         * really set this number via a sethostid() call. If their hostname
48
         * returns the loopback number (i.e. if they have put their hostname
49
         * in the /etc/hosts file with 127.0.0.1) then all such hosts will
50
         * have a non-unique hostid, but it doesn't matter anyway and
51
         * gethostid() will return a non zero number without the need for
52
         * setting one anyway.
53
         *                                              Mitch
54
         */
55
        if (gethostname(host,MAXHOSTNAMELEN)>=0 && *host) {
56
                struct hostent *hp;
57
                struct in_addr in;
58
 
59
                if ((hp = gethostbyname(host)) == (struct hostent *)NULL)
60
 
61
                /* This is not a error if we get here, as all it means is that
62
                 * this host is not on a network and/or they have not
63
                 * configured their network properly. So we return the unset
64
                 * hostid which should be 0, meaning that they should set it !!
65
                 */
66
                        return 0;
67
                else {
68
                        memcpy((char *) &in, (char *) hp->h_addr, hp->h_length);
69
 
70
                        /* Just so it doesn't look exactly like the IP addr */
71
                        return(in.s_addr<<16|in.s_addr>>16);
72
                }
73
        }
74
        else return 0;
75
 
76
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.