main.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "address.h"
  2. #include "hardware_address.h"
  3. #include "link.h"
  4. #include "vector.h"
  5. #include <cassert>
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <libmnl/libmnl.h>
  9. #include <linux/if_addr.h>
  10. #include <linux/netlink.h>
  11. #include <linux/rtnetlink.h> // struct ifinfomsg
  12. // https://netfilter.org/projects/libmnl/doxygen/html/modules.html
  13. void mnl_recv_run_cb_all(const mnl_socket *nl, void *buf, size_t bufsiz,
  14. mnl_cb_t cb_data, void *data) {
  15. unsigned int nlpid = mnl_socket_get_portid(nl);
  16. ssize_t numbytes = mnl_socket_recvfrom(nl, buf, bufsiz);
  17. while (numbytes > 0) {
  18. if (mnl_cb_run(buf, numbytes, 0, nlpid, cb_data, data) <= MNL_CB_STOP) {
  19. break;
  20. }
  21. numbytes = mnl_socket_recvfrom(nl, buf, bufsiz);
  22. }
  23. assert(numbytes != -1);
  24. }
  25. int link_cb(const nlmsghdr *nlh, void *data) {
  26. vector<Link> *links = (vector<Link> *)data;
  27. links->emplace_back((const ifinfomsg *)mnl_nlmsg_get_payload(nlh));
  28. assert(links->back().index == links->size());
  29. mnl_attr_parse(nlh, sizeof(ifinfomsg), Link::mnl_attr_cb, &links->back());
  30. return MNL_CB_OK;
  31. }
  32. void mnl_read_links(const mnl_socket *nl, vector<Link> *links) {
  33. uint8_t msgbuf[MNL_SOCKET_BUFFER_SIZE];
  34. nlmsghdr *nlh = mnl_nlmsg_put_header(msgbuf);
  35. nlh->nlmsg_type = RTM_GETLINK;
  36. nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
  37. mnl_nlmsg_put_extra_header(nlh, sizeof(rtgenmsg));
  38. assert(mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) > 0);
  39. mnl_recv_run_cb_all(nl, msgbuf, sizeof(msgbuf), link_cb, links);
  40. }
  41. static int address_cb(const struct nlmsghdr *nlh, void *data) {
  42. vector<Link> *links = (vector<Link> *)data;
  43. Address addr((const ifaddrmsg *)mnl_nlmsg_get_payload(nlh));
  44. mnl_attr_parse(nlh, sizeof(ifaddrmsg), Address::mnl_attr_cb, &addr);
  45. links->at(addr.ifindex - 1).addresses.push_back(addr);
  46. return MNL_CB_OK;
  47. }
  48. void mnl_add_addresses(const mnl_socket *nl, vector<Link> *links) {
  49. char msgbuf[MNL_SOCKET_BUFFER_SIZE];
  50. nlmsghdr *nlh = mnl_nlmsg_put_header(msgbuf);
  51. nlh->nlmsg_type = RTM_GETADDR;
  52. nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
  53. rtgenmsg *rt = (rtgenmsg *)mnl_nlmsg_put_extra_header(nlh, sizeof(rtgenmsg));
  54. rt->rtgen_family = AF_UNSPEC;
  55. assert(mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) > 0);
  56. mnl_recv_run_cb_all(nl, msgbuf, sizeof(msgbuf), address_cb, links);
  57. }
  58. int main(int argc, char *argv[]) {
  59. mnl_socket *nl = mnl_socket_open(NETLINK_ROUTE);
  60. assert(nl);
  61. assert(mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) == 0);
  62. vector<Link> links;
  63. mnl_read_links(nl, &links);
  64. mnl_add_addresses(nl, &links);
  65. std::cout << "links: ";
  66. links.write_yaml(std::cout);
  67. std::cout << std::endl;
  68. mnl_socket_close(nl);
  69. return EXIT_SUCCESS;
  70. }