NetworkConfig.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #include <stdint.h>
  19. #include "NetworkConfig.hpp"
  20. #include "Utils.hpp"
  21. namespace ZeroTier {
  22. bool NetworkConfig::toDictionary(Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d,bool includeLegacy) const
  23. {
  24. Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY> tmp;
  25. d.clear();
  26. // Try to put the more human-readable fields first
  27. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_VERSION,(uint64_t)ZT_NETWORKCONFIG_VERSION)) return false;
  28. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,this->networkId)) return false;
  29. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,this->timestamp)) return false;
  30. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_REVISION,this->revision)) return false;
  31. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,this->issuedTo)) return false;
  32. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,this->flags)) return false;
  33. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,(uint64_t)this->multicastLimit)) return false;
  34. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)this->type)) return false;
  35. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name)) return false;
  36. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  37. if (includeLegacy) {
  38. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING_OLD,this->allowPassiveBridging())) return false;
  39. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD,this->enableBroadcast())) return false;
  40. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD,this->isPrivate())) return false;
  41. std::string v4s;
  42. for(unsigned int i=0;i<staticIpCount;++i) {
  43. if (this->staticIps[i].ss_family == AF_INET) {
  44. if (v4s.length() > 0)
  45. v4s.push_back(',');
  46. v4s.append(this->staticIps[i].toString());
  47. }
  48. }
  49. if (v4s.length() > 0) {
  50. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD,v4s.c_str())) return false;
  51. }
  52. std::string v6s;
  53. for(unsigned int i=0;i<staticIpCount;++i) {
  54. if (this->staticIps[i].ss_family == AF_INET6) {
  55. if (v6s.length() > 0)
  56. v6s.push_back(',');
  57. v6s.append(this->staticIps[i].toString());
  58. }
  59. }
  60. if (v6s.length() > 0) {
  61. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD,v6s.c_str())) return false;
  62. }
  63. std::string ets;
  64. unsigned int et = 0;
  65. ZT_VirtualNetworkRuleType lastrt = ZT_NETWORK_RULE_ACTION_ACCEPT;
  66. for(unsigned int i=0;i<ruleCount;++i) {
  67. ZT_VirtualNetworkRuleType rt = (ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f);
  68. if (rt == ZT_NETWORK_RULE_MATCH_ETHERTYPE) {
  69. et = rules[i].v.etherType;
  70. } else if (rt == ZT_NETWORK_RULE_ACTION_ACCEPT) {
  71. if (((int)lastrt < 32)||(lastrt == ZT_NETWORK_RULE_MATCH_ETHERTYPE)) {
  72. if (ets.length() > 0)
  73. ets.push_back(',');
  74. char tmp[16];
  75. Utils::snprintf(tmp,sizeof(tmp),"%x",et);
  76. ets.append(tmp);
  77. }
  78. et = 0;
  79. }
  80. lastrt = rt;
  81. }
  82. if (ets.length() > 0) {
  83. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD,ets.c_str())) return false;
  84. }
  85. if (this->com) {
  86. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD,this->com.toString().c_str())) return false;
  87. }
  88. std::string ab;
  89. for(unsigned int i=0;i<this->specialistCount;++i) {
  90. if ((this->specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0) {
  91. if (ab.length() > 0)
  92. ab.push_back(',');
  93. ab.append(Address(this->specialists[i]).toString().c_str());
  94. }
  95. }
  96. if (ab.length() > 0) {
  97. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD,ab.c_str())) return false;
  98. }
  99. std::vector<Relay> rvec(this->relays());
  100. std::string rl;
  101. for(std::vector<Relay>::const_iterator i(rvec.begin());i!=rvec.end();++i) {
  102. if (rl.length() > 0)
  103. rl.push_back(',');
  104. rl.append(i->address.toString());
  105. if (i->phy4) {
  106. rl.push_back(';');
  107. rl.append(i->phy4.toString());
  108. } else if (i->phy6) {
  109. rl.push_back(';');
  110. rl.append(i->phy6.toString());
  111. }
  112. }
  113. if (rl.length() > 0) {
  114. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_RELAYS_OLD,rl.c_str())) return false;
  115. }
  116. }
  117. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  118. // Then add binary blobs
  119. if (this->com) {
  120. tmp.clear();
  121. this->com.serialize(tmp);
  122. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_COM,tmp)) return false;
  123. }
  124. tmp.clear();
  125. for(unsigned int i=0;i<this->specialistCount;++i) {
  126. tmp.append((uint64_t)this->specialists[i]);
  127. }
  128. if (tmp.size()) {
  129. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS,tmp)) return false;
  130. }
  131. tmp.clear();
  132. for(unsigned int i=0;i<this->routeCount;++i) {
  133. reinterpret_cast<const InetAddress *>(&(this->routes[i].target))->serialize(tmp);
  134. reinterpret_cast<const InetAddress *>(&(this->routes[i].via))->serialize(tmp);
  135. tmp.append((uint16_t)this->routes[i].flags);
  136. tmp.append((uint16_t)this->routes[i].metric);
  137. }
  138. if (tmp.size()) {
  139. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ROUTES,tmp)) return false;
  140. }
  141. tmp.clear();
  142. for(unsigned int i=0;i<this->staticIpCount;++i) {
  143. this->staticIps[i].serialize(tmp);
  144. }
  145. if (tmp.size()) {
  146. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS,tmp)) return false;
  147. }
  148. tmp.clear();
  149. for(unsigned int i=0;i<this->pinnedCount;++i) {
  150. this->pinned[i].zt.appendTo(tmp);
  151. this->pinned[i].phy.serialize(tmp);
  152. }
  153. if (tmp.size()) {
  154. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_PINNED,tmp)) return false;
  155. }
  156. tmp.clear();
  157. for(unsigned int i=0;i<this->ruleCount;++i) {
  158. tmp.append((uint8_t)rules[i].t);
  159. switch((ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f)) {
  160. //case ZT_NETWORK_RULE_ACTION_DROP:
  161. //case ZT_NETWORK_RULE_ACTION_ACCEPT:
  162. default:
  163. tmp.append((uint8_t)0);
  164. break;
  165. case ZT_NETWORK_RULE_ACTION_TEE:
  166. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  167. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  168. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  169. tmp.append((uint8_t)5);
  170. Address(rules[i].v.zt).appendTo(tmp);
  171. break;
  172. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  173. tmp.append((uint8_t)2);
  174. tmp.append((uint16_t)rules[i].v.vlanId);
  175. break;
  176. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  177. tmp.append((uint8_t)1);
  178. tmp.append((uint8_t)rules[i].v.vlanPcp);
  179. break;
  180. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  181. tmp.append((uint8_t)1);
  182. tmp.append((uint8_t)rules[i].v.vlanDei);
  183. break;
  184. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  185. tmp.append((uint8_t)2);
  186. tmp.append((uint16_t)rules[i].v.etherType);
  187. break;
  188. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  189. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  190. tmp.append((uint8_t)6);
  191. tmp.append(rules[i].v.mac,6);
  192. break;
  193. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  194. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  195. tmp.append((uint8_t)5);
  196. tmp.append(&(rules[i].v.ipv4.ip),4);
  197. tmp.append((uint8_t)rules[i].v.ipv4.mask);
  198. break;
  199. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  200. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  201. tmp.append((uint8_t)17);
  202. tmp.append(rules[i].v.ipv6.ip,16);
  203. tmp.append((uint8_t)rules[i].v.ipv6.mask);
  204. break;
  205. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  206. tmp.append((uint8_t)1);
  207. tmp.append((uint8_t)rules[i].v.ipTos);
  208. break;
  209. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  210. tmp.append((uint8_t)1);
  211. tmp.append((uint8_t)rules[i].v.ipProtocol);
  212. break;
  213. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  214. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  215. tmp.append((uint8_t)4);
  216. tmp.append((uint16_t)rules[i].v.port[0]);
  217. tmp.append((uint16_t)rules[i].v.port[1]);
  218. break;
  219. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  220. tmp.append((uint8_t)8);
  221. tmp.append((uint64_t)rules[i].v.characteristics);
  222. break;
  223. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  224. tmp.append((uint8_t)4);
  225. tmp.append((uint16_t)rules[i].v.frameSize[0]);
  226. tmp.append((uint16_t)rules[i].v.frameSize[1]);
  227. break;
  228. }
  229. }
  230. if (tmp.size()) {
  231. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_RULES,tmp)) return false;
  232. }
  233. return true;
  234. }
  235. bool NetworkConfig::fromDictionary(const Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d)
  236. {
  237. try {
  238. Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY> tmp;
  239. char tmp2[ZT_NETWORKCONFIG_DICT_CAPACITY];
  240. memset(this,0,sizeof(NetworkConfig));
  241. // Fields that are always present, new or old
  242. this->networkId = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,0);
  243. if (!this->networkId)
  244. return false;
  245. this->timestamp = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,0);
  246. this->revision = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REVISION,0);
  247. this->issuedTo = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,0);
  248. if (!this->issuedTo)
  249. return false;
  250. this->multicastLimit = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,0);
  251. d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name,sizeof(this->name));
  252. if (d.getUI(ZT_NETWORKCONFIG_DICT_KEY_VERSION,0) < 6) {
  253. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  254. // Decode legacy fields if version is old
  255. if (d.getB(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING_OLD))
  256. this->flags |= ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING;
  257. if (d.getB(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD))
  258. this->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST;
  259. this->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION; // always enable for old-style netconf
  260. this->type = (d.getB(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD,true)) ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC;
  261. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD,tmp2,sizeof(tmp2)) > 0) {
  262. char *saveptr = (char *)0;
  263. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  264. if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) break;
  265. InetAddress ip(f);
  266. if (!ip.isNetwork())
  267. this->staticIps[this->staticIpCount++] = ip;
  268. }
  269. }
  270. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD,tmp2,sizeof(tmp2)) > 0) {
  271. char *saveptr = (char *)0;
  272. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  273. if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) break;
  274. InetAddress ip(f);
  275. if (!ip.isNetwork())
  276. this->staticIps[this->staticIpCount++] = ip;
  277. }
  278. }
  279. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD,tmp2,sizeof(tmp2)) > 0) {
  280. this->com.fromString(tmp2);
  281. }
  282. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD,tmp2,sizeof(tmp2)) > 0) {
  283. char *saveptr = (char *)0;
  284. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  285. unsigned int et = Utils::hexStrToUInt(f) & 0xffff;
  286. if ((this->ruleCount + 2) > ZT_MAX_NETWORK_RULES) break;
  287. if (et > 0) {
  288. this->rules[this->ruleCount].t = (uint8_t)ZT_NETWORK_RULE_MATCH_ETHERTYPE;
  289. this->rules[this->ruleCount].v.etherType = (uint16_t)et;
  290. ++this->ruleCount;
  291. }
  292. this->rules[this->ruleCount++].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  293. }
  294. } else {
  295. this->rules[0].t = ZT_NETWORK_RULE_ACTION_ACCEPT;
  296. this->ruleCount = 1;
  297. }
  298. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD,tmp2,sizeof(tmp2)) > 0) {
  299. char *saveptr = (char *)0;
  300. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  301. this->addSpecialist(Address(f),ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE);
  302. }
  303. }
  304. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_RELAYS_OLD,tmp2,sizeof(tmp2)) > 0) {
  305. char *saveptr = (char *)0;
  306. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  307. char tmp3[256];
  308. Utils::scopy(tmp3,sizeof(tmp3),f);
  309. InetAddress phy;
  310. char *semi = tmp3;
  311. while (*semi) {
  312. if (*semi == ';') {
  313. *semi = (char)0;
  314. ++semi;
  315. phy = InetAddress(semi);
  316. } else ++semi;
  317. }
  318. Address zt(tmp3);
  319. this->addSpecialist(zt,ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY);
  320. if ((phy)&&(this->pinnedCount < ZT_MAX_NETWORK_PINNED)) {
  321. this->pinned[this->pinnedCount].zt = zt;
  322. this->pinned[this->pinnedCount].phy = phy;
  323. ++this->pinnedCount;
  324. }
  325. }
  326. }
  327. #else
  328. return false;
  329. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  330. } else {
  331. // Otherwise we can use the new fields
  332. this->flags = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,0);
  333. this->type = (ZT_VirtualNetworkType)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)ZT_NETWORK_TYPE_PRIVATE);
  334. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_COM,tmp)) {
  335. this->com.deserialize(tmp,0);
  336. }
  337. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS,tmp)) {
  338. unsigned int p = 0;
  339. while (((p + 8) <= tmp.size())&&(specialistCount < ZT_MAX_NETWORK_SPECIALISTS)) {
  340. this->specialists[this->specialistCount++] = tmp.at<uint64_t>(p);
  341. p += 8;
  342. }
  343. }
  344. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ROUTES,tmp)) {
  345. unsigned int p = 0;
  346. while ((p < tmp.size())&&(routeCount < ZT_MAX_NETWORK_ROUTES)) {
  347. p += reinterpret_cast<InetAddress *>(&(this->routes[this->routeCount].target))->deserialize(tmp,p);
  348. p += reinterpret_cast<InetAddress *>(&(this->routes[this->routeCount].via))->deserialize(tmp,p);
  349. this->routes[this->routeCount].flags = tmp.at<uint16_t>(p); p += 2;
  350. this->routes[this->routeCount].metric = tmp.at<uint16_t>(p); p += 2;
  351. ++this->routeCount;
  352. }
  353. }
  354. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS,tmp)) {
  355. unsigned int p = 0;
  356. while ((p < tmp.size())&&(staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)) {
  357. p += this->staticIps[this->staticIpCount++].deserialize(tmp,p);
  358. }
  359. }
  360. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_PINNED,tmp)) {
  361. unsigned int p = 0;
  362. while ((p < tmp.size())&&(pinnedCount < ZT_MAX_NETWORK_PINNED)) {
  363. this->pinned[this->pinnedCount].zt.setTo(tmp.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  364. p += this->pinned[this->pinnedCount].phy.deserialize(tmp,p);
  365. ++this->pinnedCount;
  366. }
  367. }
  368. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_RULES,tmp)) {
  369. unsigned int p = 0;
  370. while ((p < tmp.size())&&(ruleCount < ZT_MAX_NETWORK_RULES)) {
  371. rules[ruleCount].t = (uint8_t)tmp[p++];
  372. unsigned int fieldLen = (unsigned int)tmp[p++];
  373. switch((ZT_VirtualNetworkRuleType)(rules[ruleCount].t & 0x7f)) {
  374. default:
  375. break;
  376. case ZT_NETWORK_RULE_ACTION_TEE:
  377. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  378. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  379. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  380. rules[ruleCount].v.zt = Address(tmp.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH).toInt();
  381. break;
  382. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  383. rules[ruleCount].v.vlanId = tmp.at<uint16_t>(p);
  384. break;
  385. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  386. rules[ruleCount].v.vlanPcp = (uint8_t)tmp[p];
  387. break;
  388. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  389. rules[ruleCount].v.vlanDei = (uint8_t)tmp[p];
  390. break;
  391. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  392. rules[ruleCount].v.etherType = tmp.at<uint16_t>(p);
  393. break;
  394. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  395. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  396. memcpy(rules[ruleCount].v.mac,tmp.field(p,6),6);
  397. break;
  398. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  399. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  400. memcpy(&(rules[ruleCount].v.ipv4.ip),tmp.field(p,4),4);
  401. rules[ruleCount].v.ipv4.mask = (uint8_t)tmp[p + 4];
  402. break;
  403. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  404. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  405. memcpy(rules[ruleCount].v.ipv6.ip,tmp.field(p,16),16);
  406. rules[ruleCount].v.ipv6.mask = (uint8_t)tmp[p + 16];
  407. break;
  408. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  409. rules[ruleCount].v.ipTos = (uint8_t)tmp[p];
  410. break;
  411. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  412. rules[ruleCount].v.ipProtocol = (uint8_t)tmp[p];
  413. break;
  414. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  415. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  416. rules[ruleCount].v.port[0] = tmp.at<uint16_t>(p);
  417. rules[ruleCount].v.port[1] = tmp.at<uint16_t>(p + 2);
  418. break;
  419. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  420. rules[ruleCount].v.characteristics = tmp.at<uint64_t>(p);
  421. break;
  422. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  423. rules[ruleCount].v.frameSize[0] = tmp.at<uint16_t>(p);
  424. rules[ruleCount].v.frameSize[0] = tmp.at<uint16_t>(p + 2);
  425. break;
  426. }
  427. p += fieldLen;
  428. ++ruleCount;
  429. }
  430. }
  431. }
  432. //printf("~~~\n%s\n~~~\n",d.data());
  433. //dump();
  434. //printf("~~~\n");
  435. return true;
  436. } catch ( ... ) {
  437. return false;
  438. }
  439. }
  440. } // namespace ZeroTier