1195972f6Sopenharmony_ci/**
2195972f6Sopenharmony_ci * @file
3195972f6Sopenharmony_ci * Interface Identification APIs from:
4195972f6Sopenharmony_ci *              RFC 3493: Basic Socket Interface Extensions for IPv6
5195972f6Sopenharmony_ci *                  Section 4: Interface Identification
6195972f6Sopenharmony_ci *
7195972f6Sopenharmony_ci * @defgroup if_api Interface Identification API
8195972f6Sopenharmony_ci * @ingroup socket
9195972f6Sopenharmony_ci */
10195972f6Sopenharmony_ci
11195972f6Sopenharmony_ci/*
12195972f6Sopenharmony_ci * Copyright (c) 2017 Joel Cunningham, Garmin International, Inc. <joel.cunningham@garmin.com>
13195972f6Sopenharmony_ci * All rights reserved.
14195972f6Sopenharmony_ci *
15195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
16195972f6Sopenharmony_ci * are permitted provided that the following conditions are met:
17195972f6Sopenharmony_ci *
18195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice,
19195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer.
20195972f6Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice,
21195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer in the documentation
22195972f6Sopenharmony_ci *    and/or other materials provided with the distribution.
23195972f6Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote products
24195972f6Sopenharmony_ci *    derived from this software without specific prior written permission.
25195972f6Sopenharmony_ci *
26195972f6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
27195972f6Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28195972f6Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
29195972f6Sopenharmony_ci * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30195972f6Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31195972f6Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32195972f6Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33195972f6Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34195972f6Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35195972f6Sopenharmony_ci * OF SUCH DAMAGE.
36195972f6Sopenharmony_ci *
37195972f6Sopenharmony_ci * This file is part of the lwIP TCP/IP stack.
38195972f6Sopenharmony_ci *
39195972f6Sopenharmony_ci * Author: Joel Cunningham <joel.cunningham@me.com>
40195972f6Sopenharmony_ci *
41195972f6Sopenharmony_ci */
42195972f6Sopenharmony_ci#include "lwip/opt.h"
43195972f6Sopenharmony_ci
44195972f6Sopenharmony_ci#if LWIP_SOCKET
45195972f6Sopenharmony_ci
46195972f6Sopenharmony_ci#include "lwip/errno.h"
47195972f6Sopenharmony_ci#include "lwip/if_api.h"
48195972f6Sopenharmony_ci#include "lwip/netifapi.h"
49195972f6Sopenharmony_ci#include "lwip/priv/sockets_priv.h"
50195972f6Sopenharmony_ci
51195972f6Sopenharmony_ci/**
52195972f6Sopenharmony_ci * @ingroup if_api
53195972f6Sopenharmony_ci * Maps an interface index to its corresponding name.
54195972f6Sopenharmony_ci * @param ifindex interface index
55195972f6Sopenharmony_ci * @param ifname shall point to a buffer of at least {IF_NAMESIZE} bytes
56195972f6Sopenharmony_ci * @return If ifindex is an interface index, then the function shall return the
57195972f6Sopenharmony_ci * value supplied in ifname, which points to a buffer now containing the interface name.
58195972f6Sopenharmony_ci * Otherwise, the function shall return a NULL pointer.
59195972f6Sopenharmony_ci */
60195972f6Sopenharmony_cichar *
61195972f6Sopenharmony_cilwip_if_indextoname(unsigned int ifindex, char *ifname)
62195972f6Sopenharmony_ci{
63195972f6Sopenharmony_ci#if LWIP_NETIF_API
64195972f6Sopenharmony_ci  if (ifindex <= 0xff) {
65195972f6Sopenharmony_ci    err_t err = netifapi_netif_index_to_name((u8_t)ifindex, ifname);
66195972f6Sopenharmony_ci    if (!err && ifname[0] != '\0') {
67195972f6Sopenharmony_ci      return ifname;
68195972f6Sopenharmony_ci    }
69195972f6Sopenharmony_ci  }
70195972f6Sopenharmony_ci#else /* LWIP_NETIF_API */
71195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(ifindex);
72195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(ifname);
73195972f6Sopenharmony_ci#endif /* LWIP_NETIF_API */
74195972f6Sopenharmony_ci  set_errno(ENXIO);
75195972f6Sopenharmony_ci  return NULL;
76195972f6Sopenharmony_ci}
77195972f6Sopenharmony_ci
78195972f6Sopenharmony_ci/**
79195972f6Sopenharmony_ci * @ingroup if_api
80195972f6Sopenharmony_ci * Returs the interface index corresponding to name ifname.
81195972f6Sopenharmony_ci * @param ifname Interface name
82195972f6Sopenharmony_ci * @return The corresponding index if ifname is the name of an interface;
83195972f6Sopenharmony_ci * otherwise, zero.
84195972f6Sopenharmony_ci */
85195972f6Sopenharmony_ciunsigned int
86195972f6Sopenharmony_cilwip_if_nametoindex(const char *ifname)
87195972f6Sopenharmony_ci{
88195972f6Sopenharmony_ci#if LWIP_NETIF_API
89195972f6Sopenharmony_ci  err_t err;
90195972f6Sopenharmony_ci  u8_t idx;
91195972f6Sopenharmony_ci
92195972f6Sopenharmony_ci  err = netifapi_netif_name_to_index(ifname, &idx);
93195972f6Sopenharmony_ci  if (!err) {
94195972f6Sopenharmony_ci    return idx;
95195972f6Sopenharmony_ci  }
96195972f6Sopenharmony_ci#else /* LWIP_NETIF_API */
97195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(ifname);
98195972f6Sopenharmony_ci#endif /* LWIP_NETIF_API */
99195972f6Sopenharmony_ci  return 0; /* invalid index */
100195972f6Sopenharmony_ci}
101195972f6Sopenharmony_ci
102195972f6Sopenharmony_ci#endif /* LWIP_SOCKET */
103