#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "extern.h"
#include "watch_err.h"

#if USE_SYSLOG
#include <syslog.h>
#endif

#define NETDEV_LINE_LEN	128

int check_iface(struct list *dev)
{
    FILE *file = fopen ("/proc/net/dev", "r");
    
    if (file == NULL) {
        int err = errno;
        
#if USE_SYSLOG
        syslog(LOG_ERR, "cannot open /proc/net/dev (errno = %d = '%m')", err);
#else                           /* USE_SYSLOG */
        perror(progname);
#endif                          /* USE_SYSLOG */

        if (softboot)
		return (err);

	return(ENOERR);
    }

    /* read the fil line by line */
    while (!feof(file)) {
	char line[NETDEV_LINE_LEN];
        
        if (fgets(line, CONFIG_LINE_LEN, file) == NULL) {
		if (!ferror(file))
		        break;
	        else {
	        	int err = errno;
#if USE_SYSLOG
		        syslog(LOG_ERR, "cannot read /proc/net/dev (errno = %d = '%m')", err);
#else                           /* USE_SYSLOG */
		        perror(progname);
#endif                          /* USE_SYSLOG */

			fclose(file);
		        if (softboot)
				return (err);

			return(ENOERR);
	        }
	} else {
		int i = 0;
			
		for (; line[i] == ' ' || line[i] == '\t'; i++);
		if (strncmp(line + i, dev->name, strlen(dev->name)) == 0) {
			int bytes = atoi(line + i + strlen(dev->name) + 1);
			
#if USE_SYSLOG
			/* do verbose logging */
			if (verbose)
		            syslog(LOG_INFO, "device %s received %d bytes", dev->name, bytes);
#endif   

			if (dev->parameter.iface.bytes == bytes) {
#if USE_SYSLOG
		            	syslog(LOG_INFO, "device %s did not receive anything since last check", dev->name);
#endif   

				return (ENETUNREACH);
			}
			else
				dev->parameter.iface.bytes = bytes;
		}
        }
    }

    if (fclose(file) != 0) {
        int err = errno;
        
#if USE_SYSLOG
        syslog(LOG_ERR, "cannot close /proc/net/dev (errno = %d = '%m')", err);
#else                           /* USE_SYSLOG */
        perror(progname);
#endif                          /* USE_SYSLOG */

        if (softboot)
		return (err);

	return(ENOERR);
    }

    return (ENOERR);
}
