-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement if_nametoindex & if_indextoname
- Loading branch information
1 parent
9336f16
commit 9be1b21
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <net/if.h> | ||
#include <sys/socket.h> | ||
#include <sys/ioctl.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <errno.h> | ||
|
||
__typeof__(if_indextoname) __if_indextoname; | ||
|
||
char * | ||
__if_indextoname(unsigned short index, char name[IF_NAMESIZE]) | ||
{ | ||
struct ifreq ifr; | ||
int fd, r; | ||
|
||
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ | ||
__set_errno (ENXIO); | ||
return NULL; | ||
} | ||
ifr.ifr_ifindex = index; | ||
r = ioctl(fd, SIOCGIFNAME_ETH, &ifr); | ||
close(fd); | ||
if(!r){ | ||
return strncpy(name, ifr.ifr_name, IF_NAMESIZE); | ||
} | ||
__set_errno (ENXIO); | ||
return NULL; | ||
} | ||
weak_alias (__if_indextoname, if_indextoname) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <net/if.h> | ||
#include <sys/socket.h> | ||
#include <sys/ioctl.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <errno.h> | ||
|
||
__typeof__(if_nametoindex) __if_nametoindex; | ||
|
||
unsigned short | ||
if_nametoindex(const char *name) | ||
{ | ||
struct ifreq ifr; | ||
int fd, r; | ||
|
||
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { | ||
__set_errno (ENODEV); | ||
return 0; | ||
} | ||
strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name); | ||
r = ioctl(fd, SIOCGIFINDEX, &ifr); | ||
close(fd); | ||
if(!r){ | ||
return ifr.ifr_ifindex; | ||
} | ||
__set_errno (ENODEV); | ||
return 0; | ||
} | ||
weak_alias (__if_nametoindex, if_nametoindex) |