Network.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // Network.swift
  3. // ZeroTier One
  4. //
  5. // Created by Grant Limberg on 5/17/16.
  6. // Copyright © 2016 ZeroTier, Inc. All rights reserved.
  7. //
  8. import Cocoa
  9. enum NetworkStatus : Int, CustomStringConvertible {
  10. case REQUESTING_CONFIGURATION
  11. case OK
  12. case ACCESS_DENIED
  13. case NOT_FOUND
  14. case PORT_ERROR
  15. case CLIENT_TOO_OLD
  16. var description: String {
  17. switch self {
  18. case .REQUESTING_CONFIGURATION: return "REQUESTING_CONFIGURATION"
  19. case .OK: return "OK"
  20. case .ACCESS_DENIED: return "ACCESS_DENIED"
  21. case .NOT_FOUND: return "NOT_FOUND"
  22. case .PORT_ERROR: return "PORT_ERROR"
  23. case .CLIENT_TOO_OLD: return "CLIENT_TOO_OLD"
  24. }
  25. }
  26. }
  27. enum NetworkType: Int, CustomStringConvertible {
  28. case PUBLIC
  29. case PRIVATE
  30. var description: String {
  31. switch self {
  32. case .PUBLIC: return "PUBLIC"
  33. case .PRIVATE: return "PRIVATE"
  34. }
  35. }
  36. }
  37. struct PropertyKeys {
  38. static let addressesKey = "addresses"
  39. static let bridgeKey = "bridge"
  40. static let broadcastKey = "broadcast"
  41. static let dhcpKey = "dhcp"
  42. static let macKey = "mac"
  43. static let mtuKey = "mtu"
  44. static let multicastKey = "multicast"
  45. static let nameKey = "name"
  46. static let netconfKey = "netconf"
  47. static let nwidKey = "nwid"
  48. static let portNameKey = "port"
  49. static let portErrorKey = "portError"
  50. static let statusKey = "status"
  51. static let typeKey = "type"
  52. }
  53. class Network: NSObject, NSCoding {
  54. var assignedAddresses: [String] = [String]()
  55. var bridge: Bool = false
  56. var broadcastEnabled: Bool = false
  57. var dhcp: Bool = false
  58. var mac: String = ""
  59. var mtu: Int = 0
  60. var name: String = ""
  61. var netconfRevision: Int = 232
  62. var nwid: UInt64 = 0
  63. var portDeviceName: String = ""
  64. var portError: Int = 0
  65. var status: NetworkStatus = .REQUESTING_CONFIGURATION
  66. var type: NetworkType = .PRIVATE
  67. var connected: Bool = false // NOT PERSISTED. Set to true if loaded via JSON
  68. init(jsonData: [String: AnyObject]) {
  69. super.init()
  70. if let aa = jsonData["assignedAddresses"] as? [String] {
  71. for a in aa {
  72. assignedAddresses.append(a)
  73. }
  74. }
  75. if let b = jsonData["bridge"] as? NSNumber {
  76. bridge = b.boolValue
  77. }
  78. if let b = jsonData["broadcastEnabled"] as? NSNumber {
  79. broadcastEnabled = b.boolValue
  80. }
  81. if let d = jsonData["dhcp"] as? NSNumber {
  82. dhcp = d.boolValue
  83. }
  84. if let m = jsonData["mac"] as? String {
  85. mac = m
  86. }
  87. if let m = jsonData["mtu"] as? NSNumber {
  88. mtu = m.integerValue
  89. }
  90. if let n = jsonData["name"] as? String {
  91. name = n
  92. }
  93. if let n = jsonData["netconfRevision"] as? NSNumber {
  94. netconfRevision = n.integerValue
  95. }
  96. if let n = UInt64((jsonData["nwid"] as! String), radix: 16) {
  97. nwid = n
  98. }
  99. if let p = jsonData["portDeviceName"] as? String {
  100. portDeviceName = p
  101. }
  102. if let p = jsonData["portError"] as? NSNumber {
  103. portError = p.integerValue
  104. }
  105. if let statusStr = jsonData["status"] as? String {
  106. switch statusStr {
  107. case "REQUESTING_CONFIGURATION":
  108. status = .REQUESTING_CONFIGURATION
  109. case "OK":
  110. status = .OK
  111. case "ACCESS_DENIED":
  112. status = .ACCESS_DENIED
  113. case "NOT_FOUND":
  114. status = .NOT_FOUND
  115. case "PORT_ERROR":
  116. status = .PORT_ERROR
  117. case "CLIENT_TOO_OLD":
  118. status = .CLIENT_TOO_OLD
  119. default:
  120. break
  121. }
  122. }
  123. if let typeStr = jsonData["type"] as? String {
  124. switch typeStr {
  125. case "PRIVATE":
  126. type = .PRIVATE
  127. case "PUBLIC":
  128. type = .PUBLIC
  129. default:
  130. break
  131. }
  132. }
  133. // if it's being initialized via JSON, it's connected
  134. connected = true
  135. }
  136. required init?(coder aDecoder: NSCoder) {
  137. if aDecoder.containsValueForKey(PropertyKeys.addressesKey) {
  138. self.assignedAddresses = aDecoder.decodeObjectForKey(PropertyKeys.addressesKey) as! [String]
  139. }
  140. if aDecoder.containsValueForKey(PropertyKeys.bridgeKey) {
  141. self.bridge = aDecoder.decodeBoolForKey(PropertyKeys.bridgeKey)
  142. }
  143. if aDecoder.containsValueForKey(PropertyKeys.broadcastKey) {
  144. self.broadcastEnabled = aDecoder.decodeBoolForKey(PropertyKeys.broadcastKey)
  145. }
  146. if aDecoder.containsValueForKey(PropertyKeys.dhcpKey) {
  147. self.dhcp = aDecoder.decodeBoolForKey(PropertyKeys.dhcpKey)
  148. }
  149. if aDecoder.containsValueForKey(PropertyKeys.macKey) {
  150. self.mac = aDecoder.decodeObjectForKey(PropertyKeys.macKey) as! String
  151. }
  152. if aDecoder.containsValueForKey(PropertyKeys.mtuKey) {
  153. self.mtu = aDecoder.decodeIntegerForKey(PropertyKeys.mtuKey)
  154. }
  155. if aDecoder.containsValueForKey(PropertyKeys.nameKey) {
  156. self.name = aDecoder.decodeObjectForKey(PropertyKeys.nameKey) as! String
  157. }
  158. if aDecoder.containsValueForKey(PropertyKeys.netconfKey) {
  159. self.netconfRevision = aDecoder.decodeIntegerForKey(PropertyKeys.netconfKey)
  160. }
  161. if aDecoder.containsValueForKey(PropertyKeys.nwidKey) {
  162. self.nwid = (aDecoder.decodeObjectForKey(PropertyKeys.nwidKey) as! NSNumber).unsignedLongLongValue
  163. }
  164. if aDecoder.containsValueForKey(PropertyKeys.portNameKey) {
  165. self.portDeviceName = aDecoder.decodeObjectForKey(PropertyKeys.portNameKey) as! String
  166. }
  167. if aDecoder.containsValueForKey(PropertyKeys.portErrorKey) {
  168. self.portError = aDecoder.decodeIntegerForKey(PropertyKeys.portErrorKey)
  169. }
  170. if aDecoder.containsValueForKey(PropertyKeys.statusKey) {
  171. self.status = NetworkStatus(rawValue: aDecoder.decodeIntegerForKey(PropertyKeys.statusKey))!
  172. }
  173. if aDecoder.containsValueForKey(PropertyKeys.typeKey) {
  174. self.type = NetworkType(rawValue: aDecoder.decodeIntegerForKey(PropertyKeys.typeKey))!
  175. }
  176. }
  177. func encodeWithCoder(aCoder: NSCoder) {
  178. aCoder.encodeObject(self.assignedAddresses, forKey: PropertyKeys.addressesKey)
  179. aCoder.encodeBool(self.bridge, forKey: PropertyKeys.bridgeKey)
  180. aCoder.encodeBool(self.broadcastEnabled, forKey: PropertyKeys.broadcastKey)
  181. aCoder.encodeBool(self.dhcp, forKey: PropertyKeys.dhcpKey)
  182. aCoder.encodeObject(self.mac, forKey: PropertyKeys.macKey)
  183. aCoder.encodeInteger(self.mtu, forKey: PropertyKeys.mtuKey)
  184. aCoder.encodeObject(self.name, forKey: PropertyKeys.nameKey)
  185. aCoder.encodeInteger(self.netconfRevision, forKey: PropertyKeys.netconfKey)
  186. aCoder.encodeObject(NSNumber(unsignedLongLong: self.nwid), forKey: PropertyKeys.nwidKey)
  187. aCoder.encodeObject(self.portDeviceName, forKey: PropertyKeys.portNameKey)
  188. aCoder.encodeInteger(self.portError, forKey: PropertyKeys.portErrorKey)
  189. aCoder.encodeInteger(self.status.rawValue, forKey: PropertyKeys.statusKey)
  190. aCoder.encodeInteger(self.type.rawValue, forKey: PropertyKeys.typeKey)
  191. }
  192. }