VirtualNetworkConfig.java 11 KB

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