zs39 opened a new pull request, #3266:
URL: https://github.com/apache/nuttx-apps/pull/3266
## 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;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]