Skip to content

Conversation

@zs39
Copy link
Contributor

@zs39 zs39 commented Dec 23, 2025

Summary

Add an API that can detect whether a specified network interface card (NIC) has an IP conflict.

Impact

The added function has no impact on existing functionality.

Testing

Set up two SIM locally. First, assign an IP address to the first SIM. Then, change the MAC address of the second SIM. Finally, execute the command "hello" on the second SIM (as shown in the code below). This should detect a conflict.

/****************************************************************************
 * apps/examples/hello/hello_main.c
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * 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 <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>

#include "netutils/netlib.h"

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

/****************************************************************************
 * hello_main
 ****************************************************************************/

int main(int argc, FAR char *argv[])
{
#ifdef CONFIG_NET_ARP_ACD
#ifdef CONFIG_NET_IPv4
  int ret;
  const char *ifname = "eth0";
  struct in_addr ipaddr;
  struct in_addr netmask;

  printf("Hello, World!!\n");
  printf("\n=== IP Conflict Detection Test ===\n");

  /* Configure IP address to 10.0.1.2 */
  inet_aton("10.0.1.2", &ipaddr);
  inet_aton("255.255.255.0", &netmask);

  printf("Setting IP address to 10.0.1.2...\n");
  ret = netlib_set_ipv4addr(ifname, &ipaddr);
  if (ret < 0)
    {
      printf("ERROR: Failed to set IP address: %d\n", ret);
      return ret;
    }

  ret = netlib_set_ipv4netmask(ifname, &netmask);
  if (ret < 0)
    {
      printf("WARNING: Failed to set netmask: %d\n", ret);
    }

  /* Wait for ARP ACD detection */
  printf("Waiting for conflict detection (3 seconds)...\n");
  sleep(3);

  /* Check conflict status */
  ret = netlib_check_ifconflict(ifname);
  if (ret < 0)
    {
      printf("ERROR: netlib_check_ifconflict failed: %d\n", ret);
      return ret;
    }
  else if (ret == 1)
    {
      printf("RESULT: IP conflict detected! ✓\n");
    }
  else
    {
      printf("RESULT: No IP conflict detected\n");
    }

#else
  printf("Hello, World!!\n");
  printf("CONFIG_NET_IPv4 is not enabled.\n");
#endif
#else
  printf("Hello, World!!\n");
  printf("CONFIG_NET_ARP_ACD is not enabled.\n");
#endif

  return 0;
}

The execution results are as follows

nsh> hello
Hello, World!!

=== IP Conflict Detection Test ===
Setting IP address to 10.0.1.2...
Waiting for conflict detection (3 seconds)...
RESULT: IP conflict detected! ✓
nsh> 

Supports checking for IP conflicts on a NIC.

Signed-off-by: meijian <meijian@xiaomi.com>
Copy link
Contributor

@acassis acassis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zs39 nice work! Please copy these Summary instruction to a Documentation, otherwise it will be another hidden feature of NuttX

@zs39
Copy link
Contributor Author

zs39 commented Dec 24, 2025

@zs39 nice work! Please copy these Summary instruction to a Documentation, otherwise it will be another hidden feature of NuttX

apache/nuttx#17657 @acassis
Documentation added here

@zs39 zs39 requested a review from acassis December 24, 2025 06:09
@xiaoxiang781216 xiaoxiang781216 dismissed acassis’s stale review December 25, 2025 01:44

document already provided

@xiaoxiang781216 xiaoxiang781216 merged commit 4b3070c into apache:master Dec 25, 2025
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants