BSDRoutingTable.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <sys/param.h>
  33. #include <sys/sysctl.h>
  34. #include <sys/socket.h>
  35. #include <netinet/in.h>
  36. #include <arpa/inet.h>
  37. #include <net/route.h>
  38. #include <net/if.h>
  39. #include <net/if_dl.h>
  40. #include <ifaddrs.h>
  41. #include <algorithm>
  42. #include <utility>
  43. #include "Constants.hpp"
  44. #include "BSDRoutingTable.hpp"
  45. // All I wanted was the bloody rounting table. I didn't expect the Spanish inquisition.
  46. namespace ZeroTier {
  47. BSDRoutingTable::BSDRoutingTable()
  48. {
  49. }
  50. BSDRoutingTable::~BSDRoutingTable()
  51. {
  52. }
  53. std::vector<RoutingTable::Entry> BSDRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const
  54. {
  55. std::vector<RoutingTable::Entry> entries;
  56. int mib[6];
  57. size_t needed;
  58. mib[0] = CTL_NET;
  59. mib[1] = PF_ROUTE;
  60. mib[2] = 0;
  61. mib[3] = 0;
  62. mib[4] = NET_RT_DUMP;
  63. mib[5] = 0;
  64. if (!sysctl(mib,6,NULL,&needed,NULL,0)) {
  65. if (needed <= 0)
  66. return entries;
  67. char *buf = (char *)::malloc(needed);
  68. if (buf) {
  69. if (!sysctl(mib,6,buf,&needed,NULL,0)) {
  70. struct rt_msghdr *rtm;
  71. for(char *next=buf,*end=buf+needed;next<end;) {
  72. rtm = (struct rt_msghdr *)next;
  73. char *saptr = (char *)(rtm + 1);
  74. char *saend = next + rtm->rtm_msglen;
  75. if (((rtm->rtm_flags & RTF_LLINFO) == 0)&&((rtm->rtm_flags & RTF_HOST) == 0)&&((rtm->rtm_flags & RTF_UP) != 0)&&((rtm->rtm_flags & RTF_MULTICAST) == 0)) {
  76. RoutingTable::Entry e;
  77. e.deviceIndex = -9999; // unset
  78. int which = 0;
  79. while (saptr < saend) {
  80. struct sockaddr *sa = (struct sockaddr *)saptr;
  81. unsigned int salen = sa->sa_len;
  82. if (!salen)
  83. break;
  84. // Skip missing fields in rtm_addrs bit field
  85. while ((rtm->rtm_addrs & 1) == 0) {
  86. rtm->rtm_addrs >>= 1;
  87. ++which;
  88. if (which > 6)
  89. break;
  90. }
  91. if (which > 6)
  92. break;
  93. rtm->rtm_addrs >>= 1;
  94. switch(which++) {
  95. case 0:
  96. //printf("RTA_DST\n");
  97. if (sa->sa_family == AF_INET6) {
  98. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
  99. // Nobody expects the Spanish inquisition!
  100. if ((sin6->sin6_addr.s6_addr[0] == 0xfe)&&((sin6->sin6_addr.s6_addr[1] & 0xc0) == 0x80)) {
  101. // Our chief weapon is... in-band signaling!
  102. unsigned int interfaceIndex = ((((unsigned int)sin6->sin6_addr.s6_addr[2]) << 8) & 0xff) | (((unsigned int)sin6->sin6_addr.s6_addr[3]) & 0xff);
  103. sin6->sin6_addr.s6_addr[2] = 0;
  104. sin6->sin6_addr.s6_addr[3] = 0;
  105. if (!sin6->sin6_scope_id)
  106. sin6->sin6_scope_id = interfaceIndex;
  107. }
  108. }
  109. e.destination.set(sa);
  110. break;
  111. case 1:
  112. //printf("RTA_GATEWAY\n");
  113. switch(sa->sa_family) {
  114. case AF_LINK:
  115. e.deviceIndex = (int)((const struct sockaddr_dl *)sa)->sdl_index;
  116. break;
  117. case AF_INET:
  118. case AF_INET6:
  119. e.gateway.set(sa);
  120. break;
  121. }
  122. break;
  123. case 2: {
  124. if (e.destination.isV6()) {
  125. salen = sizeof(struct sockaddr_in6); // Confess!
  126. unsigned int bits = 0;
  127. for(int i=0;i<16;++i) {
  128. unsigned char c = (unsigned char)((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[i];
  129. if (c == 0xff)
  130. bits += 8;
  131. else break;
  132. /* must they be multiples of 8? Most of the BSD source I can find says yes..?
  133. else {
  134. while ((c & 0x80) == 0x80) {
  135. ++bits;
  136. c <<= 1;
  137. }
  138. break;
  139. }
  140. */
  141. }
  142. e.destination.setPort(bits);
  143. } else {
  144. salen = sizeof(struct sockaddr_in); // Confess!
  145. e.destination.setPort((unsigned int)Utils::countBits((uint32_t)((const struct sockaddr_in *)sa)->sin_addr.s_addr));
  146. }
  147. //printf("RTA_NETMASK\n");
  148. } break;
  149. /*
  150. case 3:
  151. //printf("RTA_GENMASK\n");
  152. break;
  153. case 4:
  154. //printf("RTA_IFP\n");
  155. break;
  156. case 5:
  157. //printf("RTA_IFA\n");
  158. break;
  159. case 6:
  160. //printf("RTA_AUTHOR\n");
  161. break;
  162. */
  163. }
  164. saptr += salen;
  165. }
  166. e.metric = (int)rtm->rtm_rmx.rmx_hopcount;
  167. if (((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback()))))
  168. entries.push_back(e);
  169. }
  170. next = saend;
  171. }
  172. }
  173. ::free(buf);
  174. }
  175. }
  176. for(std::vector<ZeroTier::RoutingTable::Entry>::iterator e1(entries.begin());e1!=entries.end();++e1) {
  177. if ((!e1->device[0])&&(e1->deviceIndex >= 0))
  178. if_indextoname(e1->deviceIndex,e1->device);
  179. }
  180. for(std::vector<ZeroTier::RoutingTable::Entry>::iterator e1(entries.begin());e1!=entries.end();++e1) {
  181. if ((!e1->device[0])&&(e1->gateway)) {
  182. int bestMetric = 9999999;
  183. for(std::vector<ZeroTier::RoutingTable::Entry>::iterator e2(entries.begin());e2!=entries.end();++e2) {
  184. if ((e1->gateway.within(e2->destination))&&(e2->metric <= bestMetric)) {
  185. bestMetric = e2->metric;
  186. Utils::scopy(e1->device,sizeof(e1->device),e2->device);
  187. }
  188. }
  189. }
  190. }
  191. std::sort(entries.begin(),entries.end());
  192. return entries;
  193. }
  194. bool BSDRoutingTable::set(const RoutingTable::Entry &re)
  195. {
  196. return true;
  197. }
  198. } // namespace ZeroTier
  199. // Enable and build to test routing table interface
  200. ///*
  201. int main(int argc,char **argv)
  202. {
  203. ZeroTier::BSDRoutingTable rt;
  204. std::vector<ZeroTier::RoutingTable::Entry> ents(rt.get());
  205. for(std::vector<ZeroTier::RoutingTable::Entry>::iterator e(ents.begin());e!=ents.end();++e)
  206. printf("%s\n",e->toString().c_str());
  207. return 0;
  208. }
  209. //*/