VirtualNetworkConfig.java 8.6 KB

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