VirtualNetworkConfig.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. package com.zerotier.sdk;
  28. import android.util.Log;
  29. import com.zerotier.sdk.util.StringUtils;
  30. import java.net.InetSocketAddress;
  31. import java.util.ArrayList;
  32. import java.util.Arrays;
  33. import java.util.Collections;
  34. /**
  35. * Virtual network configuration
  36. *
  37. * Defined in ZeroTierOne.h as ZT_VirtualNetworkConfig
  38. */
  39. public class VirtualNetworkConfig implements Comparable<VirtualNetworkConfig> {
  40. private final static String TAG = "VirtualNetworkConfig";
  41. public static final int MAX_MULTICAST_SUBSCRIPTIONS = 4096;
  42. public static final int ZT_MAX_ZT_ASSIGNED_ADDRESSES = 16;
  43. private final long nwid;
  44. private final long mac;
  45. private final String name;
  46. private final VirtualNetworkStatus status;
  47. private final VirtualNetworkType type;
  48. private final int mtu;
  49. private final boolean dhcp;
  50. private final boolean bridge;
  51. private final boolean broadcastEnabled;
  52. //
  53. // ANDROID-56: temporarily remove parameters to prevent crashing
  54. //
  55. // private final int portError;
  56. //
  57. // private final long netconfRevision;
  58. private final InetSocketAddress[] assignedAddresses;
  59. private final VirtualNetworkRoute[] routes;
  60. private final VirtualNetworkDNS dns;
  61. public VirtualNetworkConfig(long nwid, long mac, String name, VirtualNetworkStatus status, VirtualNetworkType type, int mtu, boolean dhcp, boolean bridge, boolean broadcastEnabled, InetSocketAddress[] assignedAddresses, VirtualNetworkRoute[] routes, VirtualNetworkDNS dns) {
  62. this.nwid = nwid;
  63. this.mac = mac;
  64. this.name = name;
  65. this.status = status;
  66. this.type = type;
  67. if (mtu < 0) {
  68. throw new RuntimeException("mtu < 0: " + mtu);
  69. }
  70. this.mtu = mtu;
  71. this.dhcp = dhcp;
  72. this.bridge = bridge;
  73. this.broadcastEnabled = broadcastEnabled;
  74. // this.portError = portError;
  75. // if (netconfRevision < 0) {
  76. // throw new RuntimeException("netconfRevision < 0: " + netconfRevision);
  77. // }
  78. // this.netconfRevision = netconfRevision;
  79. this.assignedAddresses = assignedAddresses;
  80. this.routes = routes;
  81. this.dns = dns;
  82. }
  83. @Override
  84. public String toString() {
  85. return "VirtualNetworkConfig(" + StringUtils.networkIdToString(nwid) + ", " + StringUtils.macAddressToString(mac) + ", " + name + ", " + status + ", " + type + ", " + mtu + ", " + dhcp + ", " + bridge + ", " + broadcastEnabled + ", " + Arrays.toString(assignedAddresses) + ", " + Arrays.toString(routes) + ", " + dns + ")";
  86. }
  87. @Override
  88. public boolean equals(Object o) {
  89. if (!(o instanceof VirtualNetworkConfig)) {
  90. return false;
  91. }
  92. VirtualNetworkConfig cfg = (VirtualNetworkConfig) o;
  93. if (this.nwid != cfg.nwid) {
  94. Log.i(TAG, "NetworkID Changed. Old: " + StringUtils.networkIdToString(this.nwid) + " (" + this.nwid + "), " +
  95. "New: " + StringUtils.networkIdToString(cfg.nwid) + " (" + cfg.nwid + ")");
  96. return false;
  97. }
  98. if (this.mac != cfg.mac) {
  99. Log.i(TAG, "MAC Changed. Old: " + StringUtils.macAddressToString(this.mac) + ", New: " + StringUtils.macAddressToString(cfg.mac));
  100. return false;
  101. }
  102. if (!this.name.equals(cfg.name)) {
  103. Log.i(TAG, "Name Changed. Old: " + this.name + ", New: " + cfg.name);
  104. return false;
  105. }
  106. if (this.status != cfg.status) {
  107. Log.i(TAG, "Status Changed. Old: " + this.status + ", New: " + cfg.status);
  108. return false;
  109. }
  110. if (this.type != cfg.type) {
  111. Log.i(TAG, "Type changed. Old " + this.type + ", New: " + cfg.type);
  112. return false;
  113. }
  114. if (this.mtu != cfg.mtu) {
  115. Log.i(TAG, "MTU Changed. Old: " + this.mtu + ", New: " + cfg.mtu);
  116. return false;
  117. }
  118. if (this.dhcp != cfg.dhcp) {
  119. Log.i(TAG, "DHCP Flag Changed. Old: " + this.dhcp + ", New: " + cfg.dhcp);
  120. return false;
  121. }
  122. if (this.bridge != cfg.bridge) {
  123. Log.i(TAG, "Bridge Flag Changed. Old: " + this.bridge + ", New: " + cfg.bridge);
  124. return false;
  125. }
  126. if (this.broadcastEnabled != cfg.broadcastEnabled) {
  127. Log.i(TAG, "Broadcast Flag Changed. Old: "+ this.broadcastEnabled + ", New: " + cfg.broadcastEnabled);
  128. return false;
  129. }
  130. // if (this.portError != cfg.portError) {
  131. // Log.i(TAG, "Port Error Changed. Old: " + this.portError + ", New: " + cfg.portError);
  132. //
  133. // return false;
  134. // }
  135. //
  136. // if (this.netconfRevision != cfg.netconfRevision) {
  137. // Log.i(TAG, "NetConfRevision Changed. Old: " + this.netconfRevision + ", New: " + cfg.netconfRevision);
  138. //
  139. // return false;
  140. // }
  141. if (!Arrays.equals(assignedAddresses, cfg.assignedAddresses)) {
  142. ArrayList<String> aaCurrent = new ArrayList<>();
  143. ArrayList<String> aaNew = new ArrayList<>();
  144. for (InetSocketAddress s : assignedAddresses) {
  145. aaCurrent.add(s.toString());
  146. }
  147. for (InetSocketAddress s : cfg.assignedAddresses) {
  148. aaNew.add(s.toString());
  149. }
  150. Collections.sort(aaCurrent);
  151. Collections.sort(aaNew);
  152. Log.i(TAG, "Assigned Addresses Changed");
  153. Log.i(TAG, "Old:");
  154. for (String s : aaCurrent) {
  155. Log.i(TAG, " " + s);
  156. }
  157. Log.i(TAG, "");
  158. Log.i(TAG, "New:");
  159. for (String s : aaNew) {
  160. Log.i(TAG, " " +s);
  161. }
  162. Log.i(TAG, "");
  163. return false;
  164. }
  165. if (!Arrays.equals(routes, cfg.routes)) {
  166. ArrayList<String> rCurrent = new ArrayList<>();
  167. ArrayList<String> rNew = new ArrayList<>();
  168. for (VirtualNetworkRoute r : routes) {
  169. rCurrent.add(r.toString());
  170. }
  171. for (VirtualNetworkRoute r : cfg.routes) {
  172. rNew.add(r.toString());
  173. }
  174. Collections.sort(rCurrent);
  175. Collections.sort(rNew);
  176. Log.i(TAG, "Managed Routes Changed");
  177. Log.i(TAG, "Old:");
  178. for (String s : rCurrent) {
  179. Log.i(TAG, " " + s);
  180. }
  181. Log.i(TAG, "");
  182. Log.i(TAG, "New:");
  183. for (String s : rNew) {
  184. Log.i(TAG, " " + s);
  185. }
  186. Log.i(TAG, "");
  187. return false;
  188. }
  189. boolean dnsEquals;
  190. if (this.dns == null) {
  191. //noinspection RedundantIfStatement
  192. if (cfg.dns == null) {
  193. dnsEquals = true;
  194. } else {
  195. dnsEquals = false;
  196. }
  197. } else {
  198. if (cfg.dns == null) {
  199. dnsEquals = false;
  200. } else {
  201. dnsEquals = this.dns.equals(cfg.dns);
  202. }
  203. }
  204. if (!dnsEquals) {
  205. return false;
  206. }
  207. return true;
  208. }
  209. @Override
  210. public int compareTo(VirtualNetworkConfig cfg) {
  211. return Long.compare(this.nwid, cfg.nwid);
  212. }
  213. @Override
  214. public int hashCode() {
  215. int result = 17;
  216. result = 37 * result + (int) (nwid ^ (nwid >>> 32));
  217. result = 37 * result + (int) (mac ^ (mac >>> 32));
  218. result = 37 * result + name.hashCode();
  219. result = 37 * result + status.hashCode();
  220. result = 37 * result + type.hashCode();
  221. result = 37 * result + mtu;
  222. result = 37 * result + (dhcp ? 1 : 0);
  223. result = 37 * result + (bridge ? 1 : 0);
  224. result = 37 * result + (broadcastEnabled ? 1 : 0);
  225. // result = 37 * result + portError;
  226. // result = 37 * result + (int) (netconfRevision ^ (netconfRevision >>> 32));
  227. result = 37 * result + Arrays.hashCode(assignedAddresses);
  228. result = 37 * result + Arrays.hashCode(routes);
  229. result = 37 * result + (dns == null ? 0 : dns.hashCode());
  230. return result;
  231. }
  232. /**
  233. * 64-bit ZeroTier network ID
  234. */
  235. public long getNwid() {
  236. return nwid;
  237. }
  238. /**
  239. * Ethernet MAC (48 bits) that should be assigned to port
  240. */
  241. public long getMac() {
  242. return mac;
  243. }
  244. /**
  245. * Network name (from network configuration master)
  246. */
  247. public String getName() {
  248. return name;
  249. }
  250. /**
  251. * Network configuration request status
  252. */
  253. public VirtualNetworkStatus getStatus() {
  254. return status;
  255. }
  256. /**
  257. * Network type
  258. */
  259. public VirtualNetworkType getType() {
  260. return type;
  261. }
  262. /**
  263. * Maximum interface MTU
  264. */
  265. public int getMtu() {
  266. return mtu;
  267. }
  268. /**
  269. * If the network this port belongs to indicates DHCP availability
  270. *
  271. * <p>This is a suggestion. The underlying implementation is free to ignore it
  272. * for security or other reasons. This is simply a netconf parameter that
  273. * means 'DHCP is available on this network.'</p>
  274. */
  275. public boolean isDhcp() {
  276. return dhcp;
  277. }
  278. /**
  279. * If this port is allowed to bridge to other networks
  280. *
  281. * <p>This is informational. If this is false, bridged packets will simply
  282. * be dropped and bridging won't work.</p>
  283. */
  284. public boolean isBridge() {
  285. return bridge;
  286. }
  287. /**
  288. * If true, this network supports and allows broadcast (ff:ff:ff:ff:ff:ff) traffic
  289. */
  290. public boolean isBroadcastEnabled() {
  291. return broadcastEnabled;
  292. }
  293. /**
  294. * If the network is in PORT_ERROR state, this is the error most recently returned by the port config callback
  295. */
  296. // public int getPortError() {
  297. // return portError;
  298. // }
  299. /**
  300. * Network config revision as reported by netconf master
  301. *
  302. * <p>If this is zero, it means we're still waiting for our netconf.</p>
  303. */
  304. // public long getNetconfRevision() {
  305. // return netconfRevision;
  306. // }
  307. /**
  308. * ZeroTier-assigned addresses (in {@link InetSocketAddress} objects)
  309. *
  310. * For IP, the port number of the sockaddr_XX structure contains the number
  311. * of bits in the address netmask. Only the IP address and port are used.
  312. * Other fields like interface number can be ignored.
  313. *
  314. * This is only used for ZeroTier-managed address assignments sent by the
  315. * virtual network's configuration master.
  316. */
  317. public InetSocketAddress[] getAssignedAddresses() {
  318. return assignedAddresses;
  319. }
  320. /**
  321. * ZeroTier-assigned routes (in {@link VirtualNetworkRoute} objects)
  322. */
  323. public VirtualNetworkRoute[] getRoutes() {
  324. return routes;
  325. }
  326. /**
  327. * Network specific DNS configuration
  328. */
  329. public VirtualNetworkDNS getDns() {
  330. return dns;
  331. }
  332. }