Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_if_api.c Source File

lwip_if_api.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Interface Identification APIs from:
00004  *              RFC 3493: Basic Socket Interface Extensions for IPv6
00005  *                  Section 4: Interface Identification
00006  *
00007  * @defgroup if_api Interface Identification API
00008  * @ingroup socket
00009  */
00010 
00011 /*
00012  * Copyright (c) 2017 Joel Cunningham, Garmin International, Inc. <joel.cunningham@garmin.com>
00013  * All rights reserved.
00014  *
00015  * Redistribution and use in source and binary forms, with or without modification,
00016  * are permitted provided that the following conditions are met:
00017  *
00018  * 1. Redistributions of source code must retain the above copyright notice,
00019  *    this list of conditions and the following disclaimer.
00020  * 2. Redistributions in binary form must reproduce the above copyright notice,
00021  *    this list of conditions and the following disclaimer in the documentation
00022  *    and/or other materials provided with the distribution.
00023  * 3. The name of the author may not be used to endorse or promote products
00024  *    derived from this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00027  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00028  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00029  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00031  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00034  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00035  * OF SUCH DAMAGE.
00036  *
00037  * This file is part of the lwIP TCP/IP stack.
00038  *
00039  * Author: Joel Cunningham <joel.cunningham@me.com>
00040  *
00041  */
00042 #include "lwip/opt.h"
00043 
00044 #if LWIP_SOCKET
00045 
00046 #include "lwip/errno.h"
00047 #include "lwip/if_api.h"
00048 #include "lwip/netifapi.h"
00049 #include "lwip/priv/sockets_priv.h"
00050 
00051 /**
00052  * @ingroup if_api
00053  * Maps an interface index to its corresponding name.
00054  * @param ifindex interface index
00055  * @param ifname shall point to a buffer of at least {IF_NAMESIZE} bytes
00056  * @return If ifindex is an interface index, then the function shall return the
00057  * value supplied in ifname, which points to a buffer now containing the interface name.
00058  * Otherwise, the function shall return a NULL pointer.
00059  */
00060 char *
00061 lwip_if_indextoname(unsigned int ifindex, char *ifname)
00062 {
00063 #if LWIP_NETIF_API
00064   if (ifindex <= 0xff) {
00065     err_t err = netifapi_netif_index_to_name((u8_t)ifindex, ifname);
00066     if (!err && ifname[0] != '\0') {
00067       return ifname;
00068     }
00069   }
00070 #else /* LWIP_NETIF_API */
00071   LWIP_UNUSED_ARG(ifindex);
00072   LWIP_UNUSED_ARG(ifname);
00073 #endif /* LWIP_NETIF_API */
00074   set_errno(ENXIO);
00075   return NULL;
00076 }
00077 
00078 /**
00079  * @ingroup if_api
00080  * Returs the interface index corresponding to name ifname.
00081  * @param ifname Interface name
00082  * @return The corresponding index if ifname is the name of an interface;
00083  * otherwise, zero.
00084  */
00085 unsigned int
00086 lwip_if_nametoindex(const char *ifname)
00087 {
00088 #if LWIP_NETIF_API
00089   err_t err;
00090   u8_t idx;
00091 
00092   err = netifapi_netif_name_to_index(ifname, &idx);
00093   if (!err) {
00094     return idx;
00095   }
00096 #else /* LWIP_NETIF_API */
00097   LWIP_UNUSED_ARG(ifname);
00098 #endif /* LWIP_NETIF_API */
00099   return 0; /* invalid index */
00100 }
00101 
00102 #endif /* LWIP_SOCKET */