arpmonitor.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include <errno.h>
  7. #include <sys/types.h>
  8. #include <asm/types.h>
  9. #include <arpa/inet.h>
  10. #include <sys/socket.h>
  11. #include <linux/netlink.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/route.h>
  14. #include <sys/stat.h>
  15. #include <dirent.h>
  16. #include <sys/ioctl.h>
  17. #include <netinet/in.h>
  18. //#include <libconfig.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <syslog.h>
  23. #include <fcntl.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/socket.h>
  26. #include <sys/ioctl.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <string.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/if_arp.h>
  32. #include <linux/sockios.h>
  33. #include <stdbool.h>
  34. #define BIT_IP(x) ((unsigned char*)&(x))
  35. #define STR_IP(x) BIT_IP(x)[0], BIT_IP(x)[1], BIT_IP(x)[2], BIT_IP(x)[3]
  36. #define STR_IPH(x) BIT_IP(x)[3], BIT_IP(x)[2], BIT_IP(x)[1], BIT_IP(x)[0]
  37. #define FMT_IP "%d.%d.%d.%d"
  38. #ifndef NDA_RTA
  39. #define NDA_RTA(r) \
  40. ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
  41. #endif
  42. #ifndef NDA_PAYLOAD
  43. #define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ndmsg))
  44. #endif
  45. void arp_parse_rattr(struct rtattr **tb, int max, struct rtattr *rta, int len)
  46. {
  47. memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
  48. while (RTA_OK(rta, len)) {
  49. if ((rta->rta_type <= max) && (!tb[rta->rta_type]))
  50. tb[rta->rta_type] = rta;
  51. rta = RTA_NEXT(rta,len);
  52. }
  53. if (len)
  54. fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
  55. return;
  56. }
  57. const char *addr_to_mac(unsigned char *addr, int alen, char *buf, ssize_t blen)
  58. {
  59. int i;
  60. int l;
  61. l = 0;
  62. for (i = 0; i < alen; i++) {
  63. if (i==0) {
  64. snprintf(buf+l, blen, "%02x", addr[i]);
  65. blen -= 2;
  66. l += 2;
  67. } else {
  68. snprintf(buf+l, blen, ":%02x", addr[i]);
  69. blen -= 3;
  70. l += 3;
  71. }
  72. }
  73. return buf;
  74. }
  75. static void read_netlink_message(int fd)
  76. {
  77. char buff[128 * 1024] = {0};
  78. char *buf[1024] = {0};
  79. struct nlmsghdr *nh;
  80. struct ndmsg *ndmsg;
  81. int read_size;
  82. int len = 0;
  83. struct rtattr *tb[NDA_MAX + 1];
  84. read_size = read(fd, buff, sizeof(buff));
  85. if (read_size < 0) {
  86. return;
  87. }
  88. for (nh = (struct nlmsghdr*)buff; NLMSG_OK(nh, read_size); nh = NLMSG_NEXT(nh, read_size))
  89. {
  90. switch (nh->nlmsg_type)
  91. {
  92. case RTM_NEWNEIGH:
  93. ndmsg = NLMSG_DATA(nh);
  94. if (ndmsg->ndm_family == AF_INET)
  95. {
  96. printf("add new arp cache ");
  97. len = nh->nlmsg_len - NLMSG_SPACE(sizeof(struct ndmsg));
  98. arp_parse_rattr(tb, NDA_MAX, NDA_RTA(ndmsg), len);
  99. uint32_t ipaddr = htonl(*((uint32_t *)RTA_DATA(tb[NDA_DST])));
  100. if (tb[NDA_DST] && tb[NDA_LLADDR])
  101. {
  102. printf("ip: %d.%d.%d.%d ", (ipaddr >> 24) & 0xFF, (ipaddr >> 16) & 0xFF, (ipaddr >> 8) & 0xFF, ipaddr & 0xFF);
  103. unsigned char *macaddr = RTA_DATA(tb[NDA_LLADDR]);
  104. int maclen = RTA_PAYLOAD(tb[NDA_LLADDR]);
  105. printf("mac: %x:%x:%x:%x:%x:%x\n", macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]);
  106. }
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. return;
  114. }
  115. int main(void)
  116. {
  117. int fd;
  118. struct sockaddr_nl sa;
  119. fd_set rd_set;
  120. int ret;
  121. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  122. bzero(&sa, sizeof(sa));
  123. sa.nl_family = AF_NETLINK;
  124. sa.nl_groups = RTM_NEWNEIGH | RTM_DELNEIGH;
  125. if ((bind(fd, (struct sockaddr *)&sa, sizeof(sa))) != 0) {
  126. perror("bind");
  127. return -1;
  128. }
  129. while (1) {
  130. FD_ZERO(&rd_set);
  131. FD_SET(fd, &rd_set);
  132. ret = select(fd + 1, &rd_set, NULL, NULL, NULL);
  133. if (ret < 0) {
  134. printf("select error\n");
  135. break;
  136. } else if (ret > 0) {
  137. if (FD_ISSET(fd, &rd_set)) {
  138. read_netlink_message(fd);
  139. }
  140. }
  141. }
  142. close(fd);
  143. return 0;
  144. }