summaryrefslogtreecommitdiff
path: root/test/inet/tst-ethers-line.c
blob: 182faf0981900086a98860120082761f0901f297 (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
#include <netinet/ether.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

/* glibc 2.4 has no ETHER_FILE_NAME, host compile fails without this */
#ifndef ETHER_FILE_NAME
#define ETHER_FILE_NAME "/etc/ethers"
#endif

#define ETHER_LINE_LEN 256

/* This test requires /etc/ethers to exist
 * and to have nonzero length. You should create it manually,
 * if it doesn't exist.
 */

int main(void)
{
	struct ether_addr addr;
	char hostname[ETHER_LINE_LEN];
	int fd, i;
	const char *ethers;
	struct stat statb;

	if ((fd = open(ETHER_FILE_NAME, O_RDONLY)) == -1) {
		perror ("Cannot open file /etc/ethers");
		exit(1);
	}

	if (fstat(fd, &statb)) {
		perror("Stat failed");
		exit(1);
	}
	ethers = mmap(NULL, statb.st_size, PROT_READ, MAP_SHARED, fd, 0);

	if (ethers == MAP_FAILED) {
		perror("File mapping failed");
		exit(1);
	}

	ether_line(ethers, &addr, hostname);

	for (i = 0; i < 6; i++) {
		printf("%02x", addr.ether_addr_octet[i]);
		if (i < 5)
			printf(":");
	}
	printf(" %s\n", hostname);

	return 0;
}