• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * Interface Identification APIs from:
4  *              RFC 3493: Basic Socket Interface Extensions for IPv6
5  *                  Section 4: Interface Identification
6  *
7  * @defgroup if_api Interface Identification API
8  * @ingroup socket
9  */
10 
11 /*
12  * Copyright (c) 2017 Joel Cunningham, Garmin International, Inc. <joel.cunningham@garmin.com>
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without modification,
16  * are permitted provided that the following conditions are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright notice,
19  *    this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright notice,
21  *    this list of conditions and the following disclaimer in the documentation
22  *    and/or other materials provided with the distribution.
23  * 3. The name of the author may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
29  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * This file is part of the lwIP TCP/IP stack.
38  *
39  * Author: Joel Cunningham <joel.cunningham@me.com>
40  *
41  */
42  /*
43 * This file contains the implementation for RFC 2553/3493 section Section 4: Interface Identification
44 * The following functions are implemented
45 *  lwip_if_indextoname
46 *  lwip_if_nametoindex
47 *  lwip_if_nameindex
48 *  lwip_if_freenameindex
49 *  @note: when passing the ifname pointer to the function lwip_if_indextoname:
50 *  The ifname argument must point to a buffer of at least IF_NAMESIZE
51 *  bytes into which the interface name corresponding to the specified
52 *  index is returned.
53 *  IF_NAMESIZE is same as IFNAMSIZ and is of 16 bytes including null
54 *  The lwip_if_nameindex API can only support max 10 interface indexes. The index starts
55 *  from 1 and ends at 10.
56 *
57 */
58 #include "lwip/opt.h"
59 
60 #if LWIP_SOCKET
61 
62 #include "lwip/errno.h"
63 #include "lwip/if_api.h"
64 #include "lwip/netifapi.h"
65 #include "lwip/priv/sockets_priv.h"
66 
67 /*
68  * @ingroup if_api
69  * Maps an interface index to its corresponding name.
70  * @param ifindex interface index
71  * @param ifname shall point to a buffer of at least {IF_NAMESIZE} bytes
72  * @return If ifindex is an interface index, then the function shall return the
73  * value supplied in ifname, which points to a buffer now containing the interface name.
74  * Otherwise, the function shall return a NULL pointer.
75  */
76 char *
lwip_if_indextoname(unsigned int ifindex,char * ifname)77 lwip_if_indextoname(unsigned int ifindex, char *ifname)
78 {
79 #if LWIP_NETIF_API
80   if (ifindex <= 0xff) {
81     LWIP_ERROR("lwip_if_indextoname:ifname is NULL", (ifname != NULL), set_errno(EFAULT); return NULL);
82     err_t err = netifapi_netif_index_to_name((u8_t)ifindex, ifname);
83     if (!err && (ifname[0] != '\0')) {
84       return ifname;
85     }
86   }
87 #else /* LWIP_NETIF_API */
88   LWIP_UNUSED_ARG(ifindex);
89   LWIP_UNUSED_ARG(ifname);
90 #endif /* LWIP_NETIF_API */
91   set_errno(ENXIO);
92   return NULL;
93 }
94 
95 /*
96  * @ingroup if_api
97  * Returs the interface index corresponding to name ifname.
98  * @param ifname Interface name
99  * @return The corresponding index if ifname is the name of an interface;
100  * otherwise, zero.
101  */
102 unsigned int
lwip_if_nametoindex(const char * ifname)103 lwip_if_nametoindex(const char *ifname)
104 {
105 #if LWIP_NETIF_API
106   err_t err;
107   u8_t idx;
108   LWIP_ERROR("lwip_if_nametoindex:ifname is NULL", (ifname != NULL), set_errno(EFAULT); return 0);
109   err = netifapi_netif_name_to_index(ifname, &idx);
110   if (!err) {
111     return idx;
112   }
113 #else /* LWIP_NETIF_API */
114   LWIP_UNUSED_ARG(ifname);
115 #endif /* LWIP_NETIF_API */
116   set_errno(ENODEV);
117   return 0; /* invalid index */
118 }
119 
120 /*
121  * This function returns an array of if_nameindex structures, one  structure per interface.
122  *
123  * @param input None
124  * @return if_nameindex*  The array of if_nameindex structures, one structure per interface
125  *
126  */
lwip_if_nameindex(void)127 struct if_nameindex *lwip_if_nameindex(void)
128 {
129 #if LWIP_NETIF_API
130   err_t err;
131   struct if_nameindex *if_list = NULL;
132   err = netifapi_netif_nameindex_all((void *)&if_list);
133   if ((err == ERR_OK) && (if_list != NULL)) {
134     return if_list;
135   }
136 #endif
137   set_errno(ENOBUFS);
138   return NULL;
139 }
140 
141 /*
142  * This function frees the dynamic memory that was allocated by   lwip_if_nameindex().
143  *
144  * @param if_nameindex* The pointer to if_nameindex structure
145  * @return None
146  *
147  */
lwip_if_freenameindex(struct if_nameindex * ptr)148 void lwip_if_freenameindex(struct if_nameindex *ptr)
149 {
150 #if LWIP_NETIF_API
151   if (ptr == NULL) {
152     return;
153   }
154   mem_free(ptr);
155 #else
156   LWIP_UNUSED_ARG(ptr);
157 #endif
158 }
159 
160 #endif /* LWIP_SOCKET */
161