MacOsIPInterfaceProperties.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "il2cpp-config.h"
  2. #include "MacOsIPInterfaceProperties.h"
  3. #if IL2CPP_TARGET_OSX || IL2CPP_TARGET_IOS
  4. #include "il2cpp-class-internals.h"
  5. #include "gc/GarbageCollector.h"
  6. #include "gc/WriteBarrier.h"
  7. #include "utils/Memory.h"
  8. #include "utils/StringUtils.h"
  9. #include "vm/Array.h"
  10. #include "vm/Class.h"
  11. #include "vm/Path.h"
  12. #include "vm/String.h"
  13. #include "vm/Exception.h"
  14. #include "utils/Memory.h"
  15. #include "utils/StringUtils.h"
  16. #include <sys/socket.h>
  17. #include <net/if.h>
  18. #include <net/if_dl.h>
  19. #if IL2CPP_TARGET_OSX
  20. #include <net/route.h>
  21. #else
  22. // Borrowed necessary defines and structs from the macOS route.h for use on iOS/tvOS
  23. /*
  24. * These numbers are used by reliable protocols for determining
  25. * retransmission behavior and are included in the routing structure.
  26. */
  27. struct rt_metrics
  28. {
  29. u_int32_t rmx_locks; /* Kernel leaves these values alone */
  30. u_int32_t rmx_mtu; /* MTU for this path */
  31. u_int32_t rmx_hopcount; /* max hops expected */
  32. int32_t rmx_expire; /* lifetime for route, e.g. redirect */
  33. u_int32_t rmx_recvpipe; /* inbound delay-bandwidth product */
  34. u_int32_t rmx_sendpipe; /* outbound delay-bandwidth product */
  35. u_int32_t rmx_ssthresh; /* outbound gateway buffer limit */
  36. u_int32_t rmx_rtt; /* estimated round trip time */
  37. u_int32_t rmx_rttvar; /* estimated rtt variance */
  38. u_int32_t rmx_pksent; /* packets sent using this route */
  39. u_int32_t rmx_state; /* route state */
  40. u_int32_t rmx_filler[3]; /* will be used for T/TCP later */
  41. };
  42. /*
  43. * Structures for routing messages.
  44. */
  45. struct rt_msghdr
  46. {
  47. u_short rtm_msglen; /* to skip over non-understood messages */
  48. u_char rtm_version; /* future binary compatibility */
  49. u_char rtm_type; /* message type */
  50. u_short rtm_index; /* index for associated ifp */
  51. int rtm_flags; /* flags, incl. kern & message, e.g. DONE */
  52. int rtm_addrs; /* bitmask identifying sockaddrs in msg */
  53. pid_t rtm_pid; /* identify sender */
  54. int rtm_seq; /* for sender to identify action */
  55. int rtm_errno; /* why failed */
  56. int rtm_use; /* from rtentry */
  57. u_int32_t rtm_inits; /* which metrics we are initializing */
  58. struct rt_metrics rtm_rmx; /* metrics themselves */
  59. };
  60. #define RTA_GATEWAY 0x2 /* gateway sockaddr present */
  61. #define RTM_VERSION 5 /* Up the ante and ignore older versions */
  62. #endif
  63. #include <netinet/in.h>
  64. #include <sys/param.h>
  65. #include <sys/sysctl.h>
  66. #include <stdlib.h>
  67. #include <string.h>
  68. static in_addr_t gateway_from_rtm(struct rt_msghdr *rtm)
  69. {
  70. struct sockaddr *gw;
  71. unsigned int l;
  72. struct sockaddr *addr = (struct sockaddr *)(rtm + 1);
  73. l = roundup(addr->sa_len, sizeof(long));
  74. gw = (struct sockaddr *)((char *)addr + l);
  75. if (rtm->rtm_addrs & RTA_GATEWAY)
  76. {
  77. if (gw->sa_family == AF_INET)
  78. {
  79. struct sockaddr_in *sockin = (struct sockaddr_in *)gw;
  80. return (sockin->sin_addr.s_addr);
  81. }
  82. }
  83. return 0;
  84. }
  85. #endif // IL2CPP_TARGET_OSX || IL2CPP_TARGET_IOS
  86. namespace il2cpp
  87. {
  88. namespace icalls
  89. {
  90. namespace System
  91. {
  92. namespace System
  93. {
  94. namespace Net
  95. {
  96. namespace NetworkInformation
  97. {
  98. bool MacOsIPInterfaceProperties::ParseRouteInfo_icall(Il2CppString* iface, Il2CppArray** gw_addr_list)
  99. {
  100. #if IL2CPP_TARGET_OSX || IL2CPP_TARGET_IOS
  101. size_t needed;
  102. in_addr_t in;
  103. int mib[6];
  104. int num_gws = 0;
  105. int gwnum = 0;
  106. unsigned int ifindex = 0;
  107. char *buf;
  108. char *next;
  109. char *lim;
  110. struct rt_msghdr *rtm;
  111. std::string ifacename = il2cpp::utils::StringUtils::Utf16ToUtf8(utils::StringUtils::GetChars(iface));
  112. if ((ifindex = if_nametoindex(ifacename.c_str())) == 0)
  113. return false;
  114. // MIB array defining data to read from sysctl
  115. mib[0] = CTL_NET; // Networking
  116. mib[1] = PF_ROUTE; // Routing messages
  117. mib[2] = 0; // Protocol number (always zero)
  118. mib[3] = AF_INET; // Address family (IPv4)
  119. mib[4] = NET_RT_DUMP; // Dump routing table
  120. mib[5] = 0; //
  121. // First sysctl call with oldp set to NULL to determine size of available data
  122. if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
  123. return false;
  124. // Allocate suffcient memory for available data based on the previous sysctl call
  125. if ((buf = (char*)IL2CPP_MALLOC(needed)) == NULL)
  126. return false;
  127. // Second sysctl call to retrieve data into appropriately sized buffer
  128. if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
  129. {
  130. IL2CPP_FREE(buf);
  131. return false;
  132. }
  133. lim = buf + needed;
  134. for (next = buf; next < lim; next += rtm->rtm_msglen)
  135. {
  136. rtm = (struct rt_msghdr *)next;
  137. if (rtm->rtm_version != RTM_VERSION)
  138. continue;
  139. if (rtm->rtm_index != ifindex)
  140. continue;
  141. if ((in = gateway_from_rtm(rtm)) == 0)
  142. continue;
  143. num_gws++;
  144. }
  145. *gw_addr_list = (Il2CppArray*)vm::Array::New(il2cpp_defaults.string_class, num_gws);
  146. for (next = buf; next < lim; next += rtm->rtm_msglen)
  147. {
  148. rtm = (struct rt_msghdr *)next;
  149. if (rtm->rtm_version != RTM_VERSION)
  150. continue;
  151. if (rtm->rtm_index != ifindex)
  152. continue;
  153. if ((in = gateway_from_rtm(rtm)) == 0)
  154. continue;
  155. char addr[16], *ptr;
  156. int len;
  157. ptr = (char *)&in;
  158. len = snprintf(addr, sizeof(addr), "%u.%u.%u.%u",
  159. (unsigned char)ptr[0],
  160. (unsigned char)ptr[1],
  161. (unsigned char)ptr[2],
  162. (unsigned char)ptr[3]);
  163. if ((len >= sizeof(addr)) || (len < 0))
  164. continue;
  165. il2cpp_array_setref(*gw_addr_list, gwnum, il2cpp::vm::String::New(addr));
  166. gwnum++;
  167. }
  168. IL2CPP_FREE(buf);
  169. return true;
  170. #else
  171. IL2CPP_NOT_IMPLEMENTED_ICALL(MacOsIPInterfaceProperties::ParseRouteInfo_icall);
  172. IL2CPP_UNREACHABLE;
  173. return false;
  174. #endif
  175. }
  176. } // namespace NetworkInformation
  177. } // namespace Net
  178. } // namespace System
  179. } // namespace System
  180. } // namespace icalls
  181. } // namespace il2cpp