/****************************************************************************
 * apps/netutils/netlib/netlib_checkifconflict.c
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.  The
 * ASF licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <debug.h>

#include "netutils/netlib.h"

#ifdef CONFIG_NET_ARP_ACD

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/* Determines the size of an intermediate buffer that must be large enough
 * to handle the longest line generated by this logic.
 */

#define PROCFS_LINELEN   80
#define PROCFS_NET_PATH "/proc/net/"

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: netlib_check_ifconflict
 *
 * Description:
 *   Check the network driver ip conflict
 *
 * Parameters:
 *   ifname   The name of the interface to use
 *
 * Return:
 *   0 on no conflict; a nagtive on failure; 1 on conflict
 *
 ****************************************************************************/

int netlib_check_ifconflict(FAR const char *ifname)
{
  int conflict = 0;
  char path[32];
  char line[PROCFS_LINELEN];
  FAR FILE *stream;

  if (ifname == NULL)
    {
      fprintf(stderr, "ERROR: ifname is NULL \n");
      return -EINVAL;
    }

  snprintf(path, sizeof(path), "%s%s", PROCFS_NET_PATH, ifname);
  ninfo("get dev statistics from %s \n", path);

  stream = fopen(path, "r");
  if (stream == NULL)
    {
      fprintf(stderr, "ERROR: Failed to open path:%s \n", path);
      return -errno;
    }

  while (fgets(line, sizeof(line), stream) != NULL)
    {
      if (strstr(line, "conflict!") != NULL)
        {
          conflict = 1;
          break;
        }
    }

  fclose(stream);
  return conflict;
}

#endif /* CONFIG_NET_ARP_ACD */