From 55515bb7a1c354f2d981ca5c0d4582f682713f1f Mon Sep 17 00:00:00 2001 From: liaohanqin Date: Mon, 9 Aug 2021 10:04:12 +0800 Subject: [PATCH] refactor: Optimize device check code, reuse it. --- src/config.c | 25 ++++++------------------- src/devinfo.c | 48 ++++++++++++++++++++++++++++++++---------------- src/ipwatchd.h | 1 + 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/config.c b/src/config.c index edb1925..32d9ded 100644 --- a/src/config.c +++ b/src/config.c @@ -1,15 +1,15 @@ /* IPwatchD - IP conflict detection tool for Linux * Copyright (C) 2007-2018 Jaroslav Imrich - * + * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 2 as published by the Free Software Foundation. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, @@ -70,9 +70,6 @@ int ipwd_read_config (const char *filename) char variable[100]; char value[400]; - pcap_t *h_pcap = NULL; - char errbuf[PCAP_ERRBUF_SIZE]; - int iface_len = 0; // Initialize structures with default values @@ -224,7 +221,7 @@ int ipwd_read_config (const char *filename) ipwd_message (IPWD_MSG_TYPE_ERROR, "Configuration parse error : file %s specified as user_script does not exist", value); return (IPWD_RV_ERROR); } - + if ((config.script = (char *) malloc ((strlen (value) + 1) * sizeof (char))) == NULL) { ipwd_message (IPWD_MSG_TYPE_ERROR, "Configuration parse error : malloc for user_script failed"); @@ -293,23 +290,13 @@ int ipwd_read_config (const char *filename) ipwd_message (IPWD_MSG_TYPE_ERROR, "Not enough parameters in configuration file on line %d", linenum); return (IPWD_RV_ERROR); } - - /* Check if device is valid ethernet device */ - h_pcap = pcap_open_live (variable, BUFSIZ, 0, 0, errbuf); - if (h_pcap == NULL) - { - ipwd_message (IPWD_MSG_TYPE_ERROR, "IPwatchD is unable to work with interface \"%s\"", variable); - return (IPWD_RV_ERROR); - } - if (pcap_datalink (h_pcap) != DLT_EN10MB) + /* Check if device is valid ethernet device */ + if (ipwd_check_dev (variable) != IPWD_RV_SUCCESS) { - ipwd_message (IPWD_MSG_TYPE_ERROR, "Device \"%s\" is not valid ethernet interface", variable); return (IPWD_RV_ERROR); } - pcap_close (h_pcap); - /* Check mode value */ if ((strcasecmp (value, "active") != 0) && (strcasecmp (value, "passive") != 0)) { diff --git a/src/devinfo.c b/src/devinfo.c index 7d44eda..f7755e2 100644 --- a/src/devinfo.c +++ b/src/devinfo.c @@ -132,12 +132,8 @@ int ipwd_fill_devices (void) int ifaces_num = 0; int i = 0; - pcap_t *h_pcap = NULL; - char errbuf[PCAP_ERRBUF_SIZE]; - memset (ifaces_buf, 0, sizeof (ifaces_buf)); memset (&ifc, 0, sizeof (ifc)); - memset (errbuf, 0, PCAP_ERRBUF_SIZE); /* Verify that devices structure is empty and configuration mode is automatic */ if ((devices.dev != NULL) || (devices.devnum != 0) || (config.mode != IPWD_CONFIGURATION_MODE_AUTOMATIC)) @@ -191,22 +187,11 @@ int ipwd_fill_devices (void) } /* Check if device is valid ethernet device */ - h_pcap = pcap_open_live (iface->ifr_name, BUFSIZ, 0, 0, errbuf); - if (h_pcap == NULL) - { - ipwd_message (IPWD_MSG_TYPE_ERROR, "IPwatchD is unable to work with device \"%s\"", iface->ifr_name); - continue; - } - - if (pcap_datalink (h_pcap) != DLT_EN10MB) + if (ipwd_check_dev (iface->ifr_name) != IPWD_RV_SUCCESS) { - ipwd_message (IPWD_MSG_TYPE_ERROR, "Device \"%s\" is not valid ethernet device", iface->ifr_name); - pcap_close (h_pcap); continue; } - pcap_close (h_pcap); - /* Put read values into devices structure */ if ((devices.dev = (IPWD_S_DEV *) realloc (devices.dev, (devices.devnum + 1) * sizeof (IPWD_S_DEV))) == NULL) { @@ -236,3 +221,34 @@ int ipwd_fill_devices (void) } + +//! Check if device is valid ethernet device +/*! + * \param p_dev Name of the device (i.e. eth0) + * \return IPWD_RV_SUCCESS if successful IPWD_RV_ERROR otherwise + */ +int ipwd_check_dev (const char* p_dev) +{ + pcap_t *h_pcap = NULL; + char errbuf[PCAP_ERRBUF_SIZE]; + + memset (errbuf, 0, PCAP_ERRBUF_SIZE); + + h_pcap = pcap_open_live (p_dev, BUFSIZ, 0, 0, errbuf); + if (h_pcap == NULL) + { + ipwd_message (IPWD_MSG_TYPE_ERROR, "IPwatchD is unable to work with interface \"%s\"", p_dev); + return (IPWD_RV_ERROR); + } + + if (pcap_datalink (h_pcap) != DLT_EN10MB) + { + ipwd_message (IPWD_MSG_TYPE_ERROR, "Device \"%s\" is not valid ethernet interface", p_dev); + return (IPWD_RV_ERROR); + } + + pcap_close (h_pcap); + + return IPWD_RV_SUCCESS; + +} diff --git a/src/ipwatchd.h b/src/ipwatchd.h index 9caac3c..b49bf7b 100644 --- a/src/ipwatchd.h +++ b/src/ipwatchd.h @@ -190,6 +190,7 @@ int ipwd_check_pidfile (void); /* devinfo.c */ int ipwd_devinfo (const char *p_dev, char *p_ip, char *p_mac); int ipwd_fill_devices (void); +int ipwd_check_dev (const char *p_dev); /* genarp.c */ int ipwd_genarp (const char *dev, const char *p_sip, const char *p_smac, const char *p_dip, const char *p_dmac, int opcode);