aboutsummaryrefslogtreecommitdiffstats
path: root/usr.sbin/rarpd/ether_addr.c
blob: 197c1571556265ec7503baa8a3d426b06df444bb (plain) (blame)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * rarpd support routines
 *
 * Written by Bill Paul <wpaul@ctr.columbia.edu>
 * Center for Telecommunications Research
 * Columbia University, New York City
 *
 * This code is public domain. There is no copyright. There are no
 * distribution or usage restrictions. There are no strings attached.
 *
 * Have a party.
 *
 *	$Id$
 */


#include <stdio.h>

#ifndef _PATH_ETHERS
#define _PATH_ETHERS "/etc/ethers"
#endif

/*
 * This should be defined in <netinet/if_ether.h> but it isn't.
 */
struct ether_addr {
	unsigned char octet[6];
};

/*
 * Parse a string of text containing an ethernet address and hostname
 * and separate it into its component parts.
 */
int ether_line(l, e, hostname)
        char *l;
	struct ether_addr *e;
	char *hostname;
{
        int i, o[6];

        i = sscanf(l, "%x:%x:%x:%x:%x:%x %s", &o[0], &o[1], &o[2],
                                              &o[3], &o[4], &o[5],
					      hostname);
	if (i != 7)
                return (-1);

        for (i=0; i<6; i++)
                e->octet[i] = o[i];
        return (0);
}

/*
 * Map an ethernet address to a hostname. Use either /etc/ethers or
 * NIS/YP.
 */

int ether_ntohost(hostname, e)
	char *hostname;
	struct ether_addr *e;
{
	FILE *fp;
	static char buf[BUFSIZ];
	static struct ether_addr local_ether;
	static char *local_host;
	static char *result;
	int resultlen, i;
	extern int yp_get_default_domain();
	extern int yp_match();
	static char *yp_domain;
	static char ether_a[BUFSIZ];

	if ((fp = fopen(_PATH_ETHERS, "r")) == NULL) {
                        perror(_PATH_ETHERS);
                        return (-1);
	}

	while (fgets(buf,BUFSIZ,fp)) {
		if (buf[0] == '+') {
			if (yp_get_default_domain(&yp_domain))
				return(-1);
			sprintf(ether_a,"%x:%x:%x:%x:%x:%x",
						e->octet[0], e->octet[1],
						e->octet[2], e->octet[3],
						e->octet[4], e->octet[5]);
			if (yp_match(yp_domain, "ethers.byaddr", ether_a,
				strlen(ether_a),&result, &resultlen))
				return(-1);
			if (ether_line(result, &local_ether,
					&local_host) == 0) {
				strcpy(hostname, (char *)&local_host);
				return(0);
			} else
				return(-1);
		}
		if (ether_line(&buf, &local_ether, &local_host) == 0) {
			for (i = 0; i < 6; i++)
				if (local_ether.octet[i] != e->octet[i])
					goto nomatch;
			/* We have a match */
			strcpy(hostname, (char *)&local_host);
			fclose(fp);
			return(0);
		}
nomatch:
	}

return (-1);
}