/* ethers.c * * Copyright (c) 2005 SeaD * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include "ipguard.h" #define STR_SIZE 128 #define ADDR_MAX 20 void ethers_init(void) { FILE *ethers_file; char str[STR_SIZE]; char mac[ADDR_MAX], ip[ADDR_MAX]; register int n, m; if (!(ethers_file = fopen(ethers_name, "r"))) { snprintf(s, 128, "fopen(%s):", ethers_name); log_str(ERROR, s, strerror(errno)); return; } while (fgets(str, STR_SIZE, ethers_file)) { for (mac[0] = ip[0] = '\0', n = 0; n < STR_SIZE; n++) { if ((str[n] != ' ') && (str[n] != '\t')) break; } if ((str[n] == '+') || (str[n] == '#') || (str[n] == '\n')) continue; for (m = 0; (m < ADDR_MAX) && (n < STR_SIZE); m++, n++) { mac[m] = str[n]; if ((str[n] == ' ') || (str[n] == '\t')) { mac[m] = '\0'; break; } } for (; n < STR_SIZE; n++) { if ((str[n] != ' ') && (str[n] != '\t')) break; } for (m = 0; (m < ADDR_MAX) && (n < STR_SIZE); m++, n++) { ip[m] = str[n]; if ((str[n] == ' ') || (str[n] == '\t') || (str[n] == '\n') || (str[n] == '#')) { ip[m] = '\0'; break; } } if (!mac) { if (verbose) log_str(WARNING, "wrong first address", mac); continue; } if (!ip) { if (verbose) log_str(WARNING, "wrong second address", ip); continue; } if (strchr(mac, '.')) { strncpy(str, mac, ADDR_MAX); strncpy(mac, ip, ADDR_MAX); strncpy(ip, str, ADDR_MAX); } if (!strchr(mac, ':')) { log_str(WARNING, "wrong mac address", ip); continue; } if (!strchr(ip, '.')) { log_str(WARNING, "wrong ip address", ip); continue; } pair_add(mac, ip); } if (fclose(ethers_file)) { snprintf(s, 128, "fclose(%s):", ethers_name); log_str(ERROR, s, strerror(errno)); exit(EXIT_FAILURE); } } void ethers_reinit(void) { pair_destroy(); pair_init(iface); ethers_init(); log_str(NOTICE, "ethers file rereaded:", ethers_name); }