mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-01 20:29:00 +00:00
apps/net: add if network conntivity check API
Perform connectivity testing on the specified network interface card. Signed-off-by: meijian <meijian@xiaomi.com>
This commit is contained in:
parent
10eae1dc6b
commit
c61fb846d7
4 changed files with 93 additions and 2 deletions
|
|
@ -515,6 +515,8 @@ int netlib_check_ifconflict(FAR const char *ifname);
|
|||
|
||||
#ifdef CONFIG_NETUTILS_PING
|
||||
int netlib_check_ipconnectivity(FAR const char *ip, int timeout, int retry);
|
||||
int netlib_check_ifconnectivity(FAR const char *ifname,
|
||||
int timeout, int retry);
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_MM_IOB
|
||||
|
|
|
|||
|
|
@ -158,7 +158,7 @@ if(CONFIG_NETUTILS_NETLIB)
|
|||
endif()
|
||||
|
||||
if(CONFIG_NETUTILS_PING)
|
||||
list(APPEND SRCS netlib_checkipconnectivity.c)
|
||||
list(APPEND SRCS netlib_checkipconnectivity.c netlib_checkifconnectivity.c)
|
||||
endif()
|
||||
|
||||
target_sources(apps PRIVATE ${SRCS})
|
||||
|
|
|
|||
|
|
@ -157,7 +157,7 @@ CSRCS += netlib_getiobinfo.c
|
|||
endif
|
||||
|
||||
ifeq ($(CONFIG_NETUTILS_PING),y)
|
||||
CSRCS += netlib_checkipconnectivity.c
|
||||
CSRCS += netlib_checkipconnectivity.c netlib_checkifconnectivity.c
|
||||
endif
|
||||
|
||||
include $(APPDIR)/Application.mk
|
||||
|
|
|
|||
89
netutils/netlib/netlib_checkifconnectivity.c
Normal file
89
netutils/netlib/netlib_checkifconnectivity.c
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
/****************************************************************************
|
||||
* apps/netutils/netlib/netlib_checkifconnectivity.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 <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <nuttx/net/ip.h>
|
||||
|
||||
#include "netutils/netlib.h"
|
||||
|
||||
#ifdef CONFIG_NETUTILS_PING
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: netlib_check_ifconnectivity
|
||||
*
|
||||
* Description:
|
||||
* Check network interface connectivity by pinging the gateway
|
||||
*
|
||||
* Parameters:
|
||||
* ifname The name of the interface to use
|
||||
* timeout The timeout of ping
|
||||
* retry The retry times of ping
|
||||
*
|
||||
* Return:
|
||||
* nums of gateway reply of ping; a nagtive on failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int netlib_check_ifconnectivity(FAR const char *ifname,
|
||||
int timeout, int retry)
|
||||
{
|
||||
int ret;
|
||||
char destip[INET_ADDRSTRLEN];
|
||||
struct in_addr addr;
|
||||
|
||||
ret = netlib_get_dripv4addr(ifname, &addr);
|
||||
if (ret < 0)
|
||||
{
|
||||
nerr("ERROR: failed to get gateway ip %s\n", ifname);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (net_ipv4addr_cmp(&addr, INADDR_ANY))
|
||||
{
|
||||
nerr("ERROR: failed to get gateway ip %s\n", ifname);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
inet_ntop(AF_INET, &addr, destip, sizeof(destip));
|
||||
ninfo("ping gateway %s \n", destip);
|
||||
|
||||
return netlib_check_ipconnectivity(destip, timeout, retry);
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NETUTILS_PING */
|
||||
Loading…
Add table
Add a link
Reference in a new issue