int main (int argc, char *argv[]) { struct ether_addr *valp, val; int result, r; char hostname[32], buf[64], *p; valp = ether_aton ("12:34:56:78:9a:bc"); printf ("ether_aton (\"12:34:56:78:9a:bc\") = %hhx:%hhx:%hhx:%hhx:%hhx:%hhx\n", valp->ether_addr_octet[0], valp->ether_addr_octet[1], valp->ether_addr_octet[2], valp->ether_addr_octet[3], valp->ether_addr_octet[4], valp->ether_addr_octet[5]); result = (valp->ether_addr_octet[0] != 0x12 || valp->ether_addr_octet[1] != 0x34 || valp->ether_addr_octet[2] != 0x56 || valp->ether_addr_octet[3] != 0x78 || valp->ether_addr_octet[4] != 0x9a || valp->ether_addr_octet[5] != 0xbc); if ((r = ether_line ("0:c0:f0:46:5f:97 host.ether.com \t# comment", &val, hostname)) == 0) { ether_ntoa_r (&val, buf); p = strchr (buf, '\0'); *p++ = ' '; strcpy (p, hostname); printf ("ether_line (\"0:c0:f0:46:5f:97 host.ether.com\") = \"%s\"\n", buf); result |= strcmp ("0:c0:f0:46:5f:97 host.ether.com", buf) != 0; } else { printf ("ether_line (\"0:c0:f0:46:5f:97 host.ether.com\") = %d\n", r); result |= 1; } r = ether_line ("0:c0:2:d0 foo.bar ", &val, hostname); printf ("ether_line (\"0:c0:2:d0 foo.bar \") = %d\n", r); result |= r != -1; r = ether_line ("0:c0:2:d0:1a:2a ", &val, hostname); printf ("ether_line (\"0:c0:2:d0:1a:2a \") = %d\n", r); result |= r != -1; return result; }
static int do_test (void) { struct ether_addr a; char buf[1000]; if (ether_line ("00:01:02:03:04:05 aaaaa \n", &a, buf) != 0) { puts ("ether_line failed"); return 1; } int res = 0; int i; for (i = 0; i < ETH_ALEN; ++i) { printf ("%02x%s", (int) a.ether_addr_octet[i], i + 1 == ETH_ALEN ? "" : ":"); if (a.ether_addr_octet[i] != i) { printf ("octet %d is %d, expected %d\n", i, (int) a.ether_addr_octet[i], i); res = 1; } } printf (" \"%s\"\n", buf); res |= strcmp (buf, "aaaaa") != 0; return res; }
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"); 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; }
static void test_ether_line_bad_2(void) { struct ether_addr e; char hostname[256]; testnum++; if (ether_line(ether_line_bad_2_string, &e, hostname) == 0) NOTOK("returned success"); OK(); }
/* * Map an ethernet address to a hostname. Use either /etc/ethers or NIS/YP. */ int ether_ntohost(char *hostname, const struct ether_addr *e) { FILE *fp; char buf[BUFSIZ + 2]; struct ether_addr local_ether; char local_host[MAXHOSTNAMELEN]; #ifdef YP char *result; int resultlen; char *ether_a; char *yp_domain; #endif if ((fp = fopen(_PATH_ETHERS, "r")) == NULL) return (1); while (fgets(buf,BUFSIZ,fp)) { if (buf[0] == '#') continue; #ifdef YP if (buf[0] == '+') { if (yp_get_default_domain(&yp_domain)) continue; ether_a = ether_ntoa(e); if (yp_match(yp_domain, "ethers.byaddr", ether_a, strlen(ether_a), &result, &resultlen)) { continue; } strncpy(buf, result, resultlen); buf[resultlen] = '\0'; free(result); } #endif if (!ether_line(buf, &local_ether, local_host)) { if (!bcmp((char *)&local_ether.octet[0], (char *)&e->octet[0], 6)) { /* We have a match. */ strcpy(hostname, local_host); fclose(fp); return(0); } } } fclose(fp); return (1); }
static void test_ether_line(void) { struct ether_addr e; char hostname[256]; testnum++; if (ether_line(ether_line_string, &e, hostname) != 0) NOTOK("returned error"); if (bcmp(&e, ðer_line_addr, ETHER_ADDR_LEN) != 0) NOTOK("bad address"); if (strcmp(hostname, ether_line_hostname) != 0) { printf("hostname: %s\n", hostname); NOTOK("bad hostname"); } OK(); }
int main(int argc, char **argv) { /* * The hostname buffer must be at least as large as the line buffer * to avoid buffer overflows when ether_line(3SOCKET) is called. * We simply use the same size for both buffers to be safe. */ char line[MAXHOSTNAMELEN + 256], *lf, hostname[sizeof (line)]; struct ether_addr e; FILE *in; if (argc > 1) { in = fopen(argv[1], "r"); if (in == NULL) { fprintf(stderr, "%s: can't open %s\n", argv[0], argv[1]); return (1); } } else { in = stdin; } while (fgets(line, sizeof (line), in) != NULL) { lf = strchr(line, '\n'); if (lf != NULL) *lf = '\0'; if ((line[0] == '#') || (line[0] == '\0')) continue; if (ether_line(line, &e, hostname) == 0) { (void) fprintf(stdout, "%s\t%s\n", ether_ntoa(&e), hostname); } else { (void) fprintf(stderr, "%s: ignoring line: %s\n", argv[0], line); } } return (0); }
int main(int argc, char *argv[]) { FILE *data_file; char data_line[1024]; int line_no = 0; int len; char *p, *k, *v; struct ether_addr eth_addr; char hostname[256]; if (argc > 2) usage(); if (argc == 2) { data_file = fopen(argv[1], "r"); if (data_file == NULL) { fprintf(stderr, "stdethers: can't open %s\n", argv[1]); exit(1); } } else data_file = stdin; while (read_line(data_file, data_line, sizeof(data_line))) { line_no++; len = strlen(data_line); if (len > 0) { if (data_line[0] == '#') continue; } /* * Check if we have the whole line */ if (data_line[len-1] != '\n') { if (argc == 2) fprintf(stderr, "line %d in \"%s\" is too long", line_no, argv[1]); else fprintf(stderr, "line %d in \"stdin\" is too long", line_no); } else data_line[len-1] = '\0'; p = (char *) &data_line; k = p; /* save start of key */ while (!isspace(*p)) /* find first "space" */ p++; while (isspace(*p)) /* move over "space" */ p++; v = p; /* save start of value */ while (*p != '\0') /* find end of string */ p++; if (ether_line(data_line, ð_addr, hostname) == 0) fprintf(stdout, "%s\t%s\n", NTOA(ð_addr), hostname); else fprintf(stderr, "stdethers: ignoring line %d: \"%s\"\n", line_no, data_line); } return(0); }