2018-06-25 20:41:28 +02:00
|
|
|
/****************************************************************************
|
2021-12-19 08:49:28 +01:00
|
|
|
* libs/libc/net/lib_indextoname.c
|
2018-06-25 20:41:28 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2018-06-25 20:41:28 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-06-25 20:41:28 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2018-06-25 20:41:28 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <net/if.h>
|
2021-12-19 08:49:28 +01:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2018-06-25 20:41:28 +02:00
|
|
|
|
2021-12-19 08:49:28 +01:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2018-06-25 20:41:28 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: if_indextoname
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The if_indextoname() function maps an interface index to its
|
|
|
|
* corresponding name.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* ifname - Points to a buffer of at least IF_NAMESIZE bytes.
|
|
|
|
* if_indextoname() will place in this buffer the name of the
|
|
|
|
* interface with index ifindex.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* If ifindex is an interface index, then the function will return the
|
|
|
|
* value supplied by ifname. Otherwise, the function returns a NULL pointer
|
|
|
|
* and sets errno to indicate the error.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR char *if_indextoname(unsigned int ifindex, FAR char *ifname)
|
|
|
|
{
|
2021-12-19 08:49:28 +01:00
|
|
|
int sockfd = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, NET_SOCK_PROTOCOL);
|
|
|
|
if (sockfd >= 0)
|
2018-06-25 20:41:28 +02:00
|
|
|
{
|
2021-12-19 08:49:28 +01:00
|
|
|
struct ifreq req;
|
|
|
|
req.ifr_ifindex = ifindex;
|
|
|
|
if (ioctl(sockfd, SIOCGIFNAME, (unsigned long)&req) >= 0)
|
|
|
|
{
|
|
|
|
strncpy(ifname, req.ifr_name, IF_NAMESIZE);
|
|
|
|
close(sockfd);
|
|
|
|
return ifname;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sockfd);
|
2018-06-25 20:41:28 +02:00
|
|
|
}
|
|
|
|
|
2021-12-19 08:49:28 +01:00
|
|
|
return NULL;
|
2018-06-25 20:41:28 +02:00
|
|
|
}
|