Last active
March 20, 2021 03:20
-
-
Save qdk0901/a502fba8264edbb89da501dc8bda032a to your computer and use it in GitHub Desktop.
get ip from mac address
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
#include <string> | |
#include <iostream> | |
extern "C" { | |
#include <netlink/cli/utils.h> | |
#include <netlink/cli/neigh.h> | |
#include <netlink/cli/link.h> | |
} | |
std::string get_ip_from_mac(std::string mac) | |
{ | |
struct nl_sock *sock; | |
struct rtnl_neigh *neigh; | |
struct nl_cache *neigh_cache; | |
struct nl_dump_params params; | |
char buf[128] = {0}; | |
sock = nl_cli_alloc_socket(); | |
nl_cli_connect(sock, NETLINK_ROUTE); | |
neigh_cache = nl_cli_neigh_alloc_cache(sock); | |
neigh = nl_cli_neigh_alloc(); | |
nl_cli_neigh_parse_lladdr(neigh, (char*)mac.c_str()); | |
nl_cache_foreach_filter(neigh_cache, OBJ_CAST(neigh), [](struct nl_object *obj, void *arg) { | |
struct rtnl_addr *addr = (struct rtnl_addr *) obj; | |
char* buf = (char*)arg; | |
nl_addr2str(rtnl_addr_get_local(addr), buf, 128); | |
}, buf); | |
rtnl_neigh_put(neigh); | |
nl_cache_put(neigh_cache); | |
nl_socket_free(sock); | |
return buf; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
std::string ip = get_ip_from_mac("3e:85:e3:01:ea:87"); | |
if (ip == "") | |
std::cout << "not found\n" << std::endl; | |
else | |
std::cout << ip << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment