mirror of
git://git.musl-libc.org/musl
synced 2025-03-06 20:48:29 +01:00
implement if_indextoname and if_nametoindex functions
This commit is contained in:
parent
ef8b4b1aa6
commit
92b2eb8d03
2 changed files with 36 additions and 0 deletions
18
src/network/if_indextoname.c
Normal file
18
src/network/if_indextoname.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
#define _GNU_SOURCE
|
||||
#include <net/if.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <string.h>
|
||||
#include "syscall.h"
|
||||
|
||||
char *if_indextoname(unsigned index, char *name)
|
||||
{
|
||||
struct ifreq ifr;
|
||||
int fd, r;
|
||||
|
||||
if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) return 0;
|
||||
ifr.ifr_ifindex = index;
|
||||
r = ioctl(fd, SIOCGIFNAME, &ifr);
|
||||
__syscall(SYS_close, fd);
|
||||
return r < 0 ? 0 : strncpy(name, ifr.ifr_name, IF_NAMESIZE);
|
||||
}
|
18
src/network/if_nametoindex.c
Normal file
18
src/network/if_nametoindex.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
#define _GNU_SOURCE
|
||||
#include <net/if.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <string.h>
|
||||
#include "syscall.h"
|
||||
|
||||
unsigned if_nametoindex(const char *name)
|
||||
{
|
||||
struct ifreq ifr;
|
||||
int fd, r;
|
||||
|
||||
if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) return -1;
|
||||
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
|
||||
r = ioctl(fd, SIOCGIFINDEX, &ifr);
|
||||
__syscall(SYS_close, fd);
|
||||
return r < 0 ? r : ifr.ifr_ifindex;
|
||||
}
|
Loading…
Add table
Reference in a new issue