diff options
author | Ian Moffett <ian@osmora.org> | 2024-03-07 17:28:00 -0500 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2024-03-07 17:28:32 -0500 |
commit | bd5969fc876a10b18613302db7087ef3c40f18e1 (patch) | |
tree | 7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/sysdeps/managarm/generic/net.cpp | |
parent | a95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff) |
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/sysdeps/managarm/generic/net.cpp')
-rw-r--r-- | lib/mlibc/sysdeps/managarm/generic/net.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/mlibc/sysdeps/managarm/generic/net.cpp b/lib/mlibc/sysdeps/managarm/generic/net.cpp new file mode 100644 index 0000000..63f2d0c --- /dev/null +++ b/lib/mlibc/sysdeps/managarm/generic/net.cpp @@ -0,0 +1,57 @@ +#include <mlibc/all-sysdeps.hpp> +#include <sys/ioctl.h> +#include <net/if.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> + +namespace mlibc { + +int sys_if_indextoname(unsigned int index, char *name) { + int fd = 0; + int r = sys_socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, AF_UNSPEC, &fd); + + if(r) + return r; + + struct ifreq ifr; + ifr.ifr_ifindex = index; + + int res = 0; + int ret = sys_ioctl(fd, SIOCGIFNAME, &ifr, &res); + close(fd); + + if(ret) { + if(ret == ENODEV) + return ENXIO; + return ret; + } + + strncpy(name, ifr.ifr_name, IF_NAMESIZE); + + return 0; +} + +int sys_if_nametoindex(const char *name, unsigned int *ret) { + int fd = 0; + int r = sys_socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, AF_UNSPEC, &fd); + + if(r) + return r; + + struct ifreq ifr; + strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); + + int res = 0; + r = sys_ioctl(fd, SIOCGIFINDEX, &ifr, &res); + close(fd); + + if(r) + return r; + + *ret = ifr.ifr_ifindex; + + return 0; +} + +} //namespace mlibc |