Task #14314: Add interface name/index APIs

This commit adds the following sets of interface name/index APIs:

Interface Identification APIs from RFC 3493:
  * lwip_if_nametoindex (COMPAT macro if_nametoindex)
  * lwip_if_indextoname (COMPAT macro if_indextoname)

netifapi:
  * netifapi_netif_name_to_index
  * netifapi_netif_index_to_name

netif:
  * netif_name_to_index
  * netif_index_to_name
  * netif_num_to_index
  * netif_index_to_num
This commit is contained in:
Joel Cunningham
2017-01-19 21:38:46 -06:00
parent 9c80a66253
commit 1b20e664bf
8 changed files with 328 additions and 0 deletions

54
src/include/lwip/if.h Normal file
View File

@@ -0,0 +1,54 @@
/**
* @file
* Interface Identification APIs from:
* RFC 3493: Basic Socket Interface Extensions for IPv6
* Section 4: Interface Identification
*/
/*
* Copyright (c) 2017 Joel Cunningham <joel.cunningham@me.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Joel Cunningham <joel.cunningham@me.com>
*
*/
#ifndef LWIP_HDR_IF_H
#define LWIP_HDR_IF_H
#include "lwip/opt.h"
#define IF_NAMESIZE 6 /* 2 chars, 3 nums, 1 \0 */
char * lwip_if_indextoname(unsigned ifindex, char *ifname);
unsigned int lwip_if_nametoindex(const char *ifname);
#if LWIP_COMPAT_SOCKETS
#define if_indextoname(ifindex, ifname) lwip_if_indextoname(ifindex,ifname)
#define if_nametoindex(ifname) lwip_if_nametoindex(ifname)
#endif /* LWIP_COMPAT_SOCKETS */
#endif /* LWIP_HDR_IF_H */

View File

@@ -490,6 +490,14 @@ err_t netif_add_ip6_address(struct netif *netif, const ip6_addr_t *ip6addr, s8_t
#define NETIF_SET_HWADDRHINT(netif, hint)
#endif /* LWIP_NETIF_HWADDRHINT */
/* @ingroup netif */
u8_t netif_name_to_index(const char *name);
char * netif_index_to_name(u8_t index, char *name);
/* Interface indexes always start at 1 per RFC 3493, section 4, num starts at 0 */
#define netif_num_to_index(netif) ((netif)->num + 1)
#define netif_index_to_num(index) ((index) - 1)
#ifdef __cplusplus
}
#endif

View File

@@ -40,6 +40,7 @@
#include "lwip/netif.h"
#include "lwip/dhcp.h"
#include "lwip/autoip.h"
#include "lwip/if.h"
#include "lwip/priv/tcpip_priv.h"
#ifdef __cplusplus
@@ -73,6 +74,14 @@ struct netifapi_msg {
netifapi_void_fn voidfunc;
netifapi_errt_fn errtfunc;
} common;
struct {
#if LWIP_MPU_COMPATIBLE
char name[IF_NAMESIZE];
#else /* LWIP_MPU_COMPATIBLE */
char *name;
#endif /* LWIP_MPU_COMPATIBLE */
u8_t index;
} ifs;
} msg;
};
@@ -92,6 +101,11 @@ err_t netifapi_netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr,
err_t netifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc,
netifapi_errt_fn errtfunc);
/** @ingroup netifapi_netif */
err_t netifapi_netif_name_to_index(const char *name, u8_t *index);
/** @ingroup netifapi_netif */
err_t netifapi_netif_index_to_name(u8_t index, char *name);
/** @ingroup netifapi_netif */
#define netifapi_netif_remove(n) netifapi_netif_common(n, netif_remove, NULL)
/** @ingroup netifapi_netif */

View File

@@ -0,0 +1,33 @@
/**
* @file
* This file is a posix wrapper for lwip/if.h.
*/
/*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
*/
#include "lwip/if.h"