4 * Programmatic interface for the /etc/ethers file
6 * Copyright 2007 by Matthew Wilcox <matthew@wil.cx>
8 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
14 #include <netinet/ether.h>
16 #define ETHER_LINE_LEN 256
19 * Internal function which returns a pointer to the part of the line
20 * with the start of the hostname, or NULL if we couldn't parse the line.
21 * Note that this line may have a comment symbol on it somewhere; if so
22 * it will return NULL if the # is before or within the ether_addr, and
23 * succeed if the # is before or within the host. It's up to the callers
24 * to be aware of this.
26 * I would have preferred to write a NUL to the location of the comment
27 * character, but ether_line takes a const argument. See __ether_line_w.
29 static const char *__ether_line(const char *line, struct ether_addr *addr)
31 struct ether_addr *res = ether_aton_r(line, addr);
35 while (*line && (*line != ' ') && (*line != '\t'))
37 while (*line && ((*line == ' ') || (*line == '\t')))
39 return (*line) ? line : NULL;
43 * Strips out the comment before calling __ether_line. We can do this,
44 * since we know the buffer is writable.
46 static const char *__ether_line_w(char *line, struct ether_addr *addr)
48 char *end = strchr(line, '#');
50 end = strchr(line, '\n');
53 return __ether_line(line, addr);
56 int ether_line(const char *line, struct ether_addr *addr, char *hostname)
58 const char *name = __ether_line(line, addr);
63 if ((*name == '#') || isspace(*name))
65 *hostname++ = *name++;
72 int ether_ntohost(char *hostname, const struct ether_addr *addr)
76 char buf[ETHER_LINE_LEN];
78 fp = fopen(ETHER_FILE_NAME, "r");
82 while (fgets(buf, sizeof(buf), fp)) {
83 struct ether_addr tmp_addr;
84 const char *cp = __ether_line_w(buf, &tmp_addr);
87 if (memcmp(addr, &tmp_addr, sizeof(tmp_addr)))
99 int ether_hostton(const char *hostname, struct ether_addr *addr)
103 char buf[ETHER_LINE_LEN];
105 fp = fopen(ETHER_FILE_NAME, "r");
109 while (fgets(buf, sizeof(buf), fp)) {
110 const char *cp = __ether_line_w(buf, addr);
113 if (strcasecmp(hostname, cp))