Advertisement
Guest User

Untitled

a guest
Mar 30th, 2018
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <memory.h>
  4. #include <net/if.h>
  5. #include <arpa/inet.h>
  6. #include <sys/socket.h>
  7. #include <linux/rtnetlink.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10.  
  11. char buf[2000];
  12.  
  13. struct nl_req {
  14. struct nlmsghdr hdr;
  15. struct rtmsg msg;
  16. };
  17.  
  18. int main(void)
  19. {
  20. int rtnl_sock = -1;
  21. if ((rtnl_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0)
  22. return -1;
  23.  
  24. struct sockaddr_nl local;
  25. memset(&local, 0, sizeof(local));
  26. local.nl_family = AF_NETLINK;
  27. local.nl_pid = getpid();
  28.  
  29. if (bind(rtnl_sock, (struct sockaddr *)&local, sizeof(local)) == -1)
  30. {
  31. perror("socket binding");
  32. return -1;
  33. }
  34.  
  35. struct nl_req nlReq;
  36. memset(&nlReq, 0, sizeof(nlReq));
  37.  
  38. /* Fill message */
  39. nlReq.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  40. nlReq.hdr.nlmsg_type = RTM_GETROUTE;
  41. nlReq.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
  42. nlReq.hdr.nlmsg_seq = 1;
  43. nlReq.hdr.nlmsg_pid = getpid();
  44.  
  45. nlReq.msg.rtm_family = AF_INET;
  46. nlReq.msg.rtm_type = RTN_UNICAST;
  47. nlReq.msg.rtm_table = RT_TABLE_MAIN;
  48. nlReq.msg.rtm_protocol = RTPROT_UNSPEC;
  49.  
  50. /* Send message */
  51. send(rtnl_sock, &nlReq, sizeof(nlReq), 0);
  52.  
  53. int packet = 1;
  54. while(1)
  55. {
  56. int len = recv(rtnl_sock, buf, sizeof(buf), MSG_DONTWAIT);
  57. if (len == -1)
  58. {
  59. perror("recv");
  60. return -1;
  61. }
  62.  
  63. struct nlmsghdr *hdr;
  64. hdr = (struct nlmsghdr *)buf;
  65.  
  66. printf("\Packet recieved: --%d--\n"\
  67. "--------------------------------------\n"\
  68. "Read from socket %d bytes\n", packet, len);
  69.  
  70. char data_buff[32];
  71. int i = 0;
  72.  
  73. for (; NLMSG_OK(hdr, len); NLMSG_NEXT(hdr, len)) {
  74.  
  75. if (hdr->nlmsg_type == NLMSG_DONE)
  76. {
  77. printf("\nGot NLMSG_DONE\n; nlmsg seq is: %d", hdr->nlmsg_type);
  78. break;
  79. }
  80.  
  81. if (hdr->nlmsg_type == NLMSG_ERROR)
  82. {
  83. printf("\nGot NLMSG_ERROR\n; nlmsg seq is: %d", hdr->nlmsg_type);
  84. break;
  85. }
  86.  
  87. printf("\n%d) header---------\nnlmsg type is: %d\n\n", i, hdr->nlmsg_type);
  88.  
  89. struct rtmsg *route_entry = (struct rtmsg *)NLMSG_DATA(hdr);
  90. struct rtattr *route_attr = (struct rtattr *)RTM_RTA(route_entry);
  91. int route_attr_len = RTM_PAYLOAD(hdr);
  92.  
  93. printf("\tntmsghdr.nlmsg_pid: 0x%x\n", hdr->nlmsg_pid);
  94. printf("\tntmsghdr.flags: 0x%x\n", hdr->nlmsg_flags);
  95. printf("\trtmsg.rtm_scope: 0x%x\n", route_entry->rtm_scope);
  96. printf("\trtmsg.rtm_flags: 0x%x\n", route_entry->rtm_flags);
  97. printf("\trtmsg.rtm_tos: 0x%x\n", route_entry->rtm_tos);
  98. printf("\trtmsg.rtm_src_len: 0x%x\n", route_entry->rtm_src_len);
  99. printf("\trtmsg.rtm_dst_len: 0x%x\n", route_entry->rtm_dst_len);
  100. printf("\trtmsg.rtm_table: 0x%x\n", route_entry->rtm_table);
  101. printf("\trtmsg.rtm_family: 0x%x\n", route_entry->rtm_family);
  102.  
  103. int j = 0;
  104.  
  105. for (; RTA_OK(route_attr, route_attr_len); route_attr = RTA_NEXT(route_attr, route_attr_len))
  106. {
  107. char *ptr = (char *)RTA_DATA(route_attr);
  108. switch(route_attr->rta_type) {
  109. case RTA_GATEWAY:
  110. inet_ntop(AF_INET, RTA_DATA(route_attr), data_buff, sizeof(data_buff));
  111. printf("\t%d entry)) Gateway address is: %s\n", j, data_buff);
  112. break;
  113. case RTA_PRIORITY:
  114. printf("\t%d entry)) Priority is: 0x%x\n", j, *ptr);
  115. break;
  116. case RTA_TABLE:
  117. inet_ntop(AF_INET, RTA_DATA(route_attr), data_buff, sizeof(data_buff));
  118. printf("\t%d entry)) Table ID is: %s\n", j, data_buff);
  119. break;
  120. case RTA_OIF:
  121. printf("\t%d entry)) Ouput IF index: 0x%x\n", j, *ptr);
  122. break;
  123. case RTA_DST:
  124. inet_ntop(AF_INET, RTA_DATA(route_attr), data_buff, sizeof(data_buff));
  125. printf("\t%d entry)) Destination is: %s\n", j, data_buff);
  126. break;
  127. case RTA_PREF:
  128. printf("\t%d entry)) Preference is: 0x%x\n", j, *ptr);
  129. break;
  130. case RTA_CACHEINFO:
  131. printf("\t%d entry)) Cache info is: 0x%x\n", j, *ptr);
  132. break;
  133. default:
  134. printf("\t%d entry)) rta_type == %d; value is: %x\n", j, route_attr->rta_type, *ptr);
  135. break;
  136. }
  137. j++;
  138. }
  139. i++;
  140. }
  141. printf("\nPacket ends here\n--------------------------------------\n\n\n");
  142. packet++;
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement