diff options
Diffstat (limited to 'src/utils/socket_utils.cpp')
-rw-r--r-- | src/utils/socket_utils.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/utils/socket_utils.cpp b/src/utils/socket_utils.cpp index 54b806c3..0bc1e606 100644 --- a/src/utils/socket_utils.cpp +++ b/src/utils/socket_utils.cpp @@ -28,6 +28,11 @@ #include "utils/socket_utils.hpp" +#if __linux__ +#include <linux/netlink.h> +#include <linux/rtnetlink.h> +#endif + #include "common/code_utils.hpp" int SocketWithCloseExec(int aDomain, int aType, int aProtocol, SocketBlockOption aBlockOption) @@ -55,3 +60,30 @@ exit: return fd; } + +#if __linux__ +int CreateNetLinkRouteSocket(uint32_t aNlGroups) +{ + int sock; + int rval = 0; + struct sockaddr_nl addr; + + sock = SocketWithCloseExec(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE, kSocketBlock); + VerifyOrExit(sock != -1); + + memset(&addr, 0, sizeof(addr)); + addr.nl_family = AF_NETLINK; + addr.nl_groups = aNlGroups; // e.g. RTMGRP_LINK | RTMGRP_IPV6_IFADDR; + + rval = bind(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)); + +exit: + if (rval != 0) + { + close(sock); + sock = -1; + } + + return sock; +} +#endif |