Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions src/config.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* IPwatchD - IP conflict detection tool for Linux
* Copyright (C) 2007-2018 Jaroslav Imrich <jariq(at)jariq(dot)sk>
*
*
* 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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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))
{
Expand Down
48 changes: 32 additions & 16 deletions src/devinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;

}
1 change: 1 addition & 0 deletions src/ipwatchd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down