• 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 #include "lwip/opt.h"
43 
44 #if LWIP_SOCKET
45 
46 #include "lwip/errno.h"
47 #include "lwip/if_api.h"
48 #include "lwip/netifapi.h"
49 #include "lwip/priv/sockets_priv.h"
50 
51 /**
52  * @ingroup if_api
53  * Maps an interface index to its corresponding name.
54  * @param ifindex interface index
55  * @param ifname shall point to a buffer of at least {IF_NAMESIZE} bytes
56  * @return If ifindex is an interface index, then the function shall return the
57  * value supplied in ifname, which points to a buffer now containing the interface name.
58  * Otherwise, the function shall return a NULL pointer.
59  */
60 char *
lwip_if_indextoname(unsigned int ifindex,char * ifname)61 lwip_if_indextoname(unsigned int ifindex, char *ifname)
62 {
63 #if LWIP_NETIF_API
64   if (ifindex <= 0xff) {
65     err_t err = netifapi_netif_index_to_name((u8_t)ifindex, ifname);
66     if (!err && ifname[0] != '\0') {
67       return ifname;
68     }
69   }
70 #else /* LWIP_NETIF_API */
71   LWIP_UNUSED_ARG(ifindex);
72   LWIP_UNUSED_ARG(ifname);
73 #endif /* LWIP_NETIF_API */
74   set_errno(ENXIO);
75   return NULL;
76 }
77 
78 /**
79  * @ingroup if_api
80  * Returs the interface index corresponding to name ifname.
81  * @param ifname Interface name
82  * @return The corresponding index if ifname is the name of an interface;
83  * otherwise, zero.
84  */
85 unsigned int
lwip_if_nametoindex(const char * ifname)86 lwip_if_nametoindex(const char *ifname)
87 {
88 #if LWIP_NETIF_API
89   err_t err;
90   u8_t idx;
91 
92   err = netifapi_netif_name_to_index(ifname, &idx);
93   if (!err) {
94     return idx;
95   }
96 #else /* LWIP_NETIF_API */
97   LWIP_UNUSED_ARG(ifname);
98 #endif /* LWIP_NETIF_API */
99   return 0; /* invalid index */
100 }
101 
102 #endif /* LWIP_SOCKET */
103