NetworkConfig.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 &d,bool includeLegacy) const
  23. {
  24. Buffer<ZT_DICTIONARY_MAX_SIZE> 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. case ZT_NETWORK_RULE_MATCH_TCP_RELATIVE_SEQUENCE_NUMBER_RANGE:
  229. tmp.append((uint8_t)8);
  230. tmp.append((uint32_t)rules[i].v.tcpseq[0]);
  231. tmp.append((uint32_t)rules[i].v.tcpseq[1]);
  232. break;
  233. }
  234. }
  235. if (tmp.size()) {
  236. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_RULES,tmp)) return false;
  237. }
  238. return true;
  239. }
  240. bool NetworkConfig::fromDictionary(const Dictionary &d)
  241. {
  242. try {
  243. Buffer<ZT_DICTIONARY_MAX_SIZE> tmp;
  244. char tmp2[ZT_DICTIONARY_MAX_SIZE];
  245. memset(this,0,sizeof(NetworkConfig));
  246. // Fields that are always present, new or old
  247. this->networkId = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,0);
  248. if (!this->networkId)
  249. return false;
  250. this->timestamp = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,0);
  251. this->revision = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REVISION,0);
  252. this->issuedTo = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,0);
  253. if (!this->issuedTo)
  254. return false;
  255. this->multicastLimit = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,0);
  256. d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name,sizeof(this->name));
  257. if (d.getUI(ZT_NETWORKCONFIG_DICT_KEY_VERSION,0) < 6) {
  258. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  259. // Decode legacy fields if version is old
  260. if (d.getB(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING_OLD))
  261. this->flags |= ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING;
  262. if (d.getB(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD))
  263. this->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST;
  264. this->type = (d.getB(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD,true)) ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC;
  265. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD,tmp2,sizeof(tmp2)) > 0) {
  266. char *saveptr = (char *)0;
  267. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  268. if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) break;
  269. InetAddress ip(f);
  270. if (!ip.isNetwork())
  271. this->staticIps[this->staticIpCount++] = ip;
  272. }
  273. }
  274. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD,tmp2,sizeof(tmp2)) > 0) {
  275. char *saveptr = (char *)0;
  276. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  277. if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) break;
  278. InetAddress ip(f);
  279. if (!ip.isNetwork())
  280. this->staticIps[this->staticIpCount++] = ip;
  281. }
  282. }
  283. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD,tmp2,sizeof(tmp2)) > 0) {
  284. this->com.fromString(tmp2);
  285. }
  286. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD,tmp2,sizeof(tmp2)) > 0) {
  287. char *saveptr = (char *)0;
  288. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  289. unsigned int et = Utils::hexStrToUInt(f) & 0xffff;
  290. if ((this->ruleCount + 2) > ZT_MAX_NETWORK_RULES) break;
  291. if (et > 0) {
  292. this->rules[this->ruleCount].t = (uint8_t)ZT_NETWORK_RULE_MATCH_ETHERTYPE;
  293. this->rules[this->ruleCount].v.etherType = (uint16_t)et;
  294. ++this->ruleCount;
  295. }
  296. this->rules[this->ruleCount++].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  297. }
  298. } else {
  299. this->rules[0].t = ZT_NETWORK_RULE_ACTION_ACCEPT;
  300. this->ruleCount = 1;
  301. }
  302. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD,tmp2,sizeof(tmp2)) > 0) {
  303. char *saveptr = (char *)0;
  304. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  305. this->addSpecialist(Address(f),ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE);
  306. }
  307. }
  308. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_RELAYS_OLD,tmp2,sizeof(tmp2)) > 0) {
  309. char *saveptr = (char *)0;
  310. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  311. char tmp3[256];
  312. Utils::scopy(tmp3,sizeof(tmp3),f);
  313. InetAddress phy;
  314. char *semi = tmp3;
  315. while (*semi) {
  316. if (*semi == ';') {
  317. *semi = (char)0;
  318. ++semi;
  319. phy = InetAddress(semi);
  320. } else ++semi;
  321. }
  322. Address zt(tmp3);
  323. this->addSpecialist(zt,ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY);
  324. if ((phy)&&(this->pinnedCount < ZT_MAX_NETWORK_PINNED)) {
  325. this->pinned[this->pinnedCount].zt = zt;
  326. this->pinned[this->pinnedCount].phy = phy;
  327. ++this->pinnedCount;
  328. }
  329. }
  330. }
  331. #else
  332. return false;
  333. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  334. } else {
  335. // Otherwise we can use the new fields
  336. this->flags = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,0);
  337. this->type = (ZT_VirtualNetworkType)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)ZT_NETWORK_TYPE_PRIVATE);
  338. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_COM,tmp)) {
  339. this->com.deserialize(tmp,0);
  340. }
  341. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS,tmp)) {
  342. unsigned int p = 0;
  343. while (((p + 8) <= tmp.size())&&(specialistCount < ZT_MAX_NETWORK_SPECIALISTS)) {
  344. this->specialists[this->specialistCount++] = tmp.at<uint64_t>(p);
  345. p += 8;
  346. }
  347. }
  348. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ROUTES,tmp)) {
  349. unsigned int p = 0;
  350. while ((p < tmp.size())&&(routeCount < ZT_MAX_NETWORK_ROUTES)) {
  351. p += reinterpret_cast<InetAddress *>(&(this->routes[this->routeCount].target))->deserialize(tmp,p);
  352. p += reinterpret_cast<InetAddress *>(&(this->routes[this->routeCount].via))->deserialize(tmp,p);
  353. this->routes[this->routeCount].flags = tmp.at<uint16_t>(p); p += 2;
  354. this->routes[this->routeCount].metric = tmp.at<uint16_t>(p); p += 2;
  355. ++this->routeCount;
  356. }
  357. }
  358. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS,tmp)) {
  359. unsigned int p = 0;
  360. while ((p < tmp.size())&&(staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)) {
  361. p += this->staticIps[this->staticIpCount++].deserialize(tmp,p);
  362. }
  363. }
  364. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_PINNED,tmp)) {
  365. unsigned int p = 0;
  366. while ((p < tmp.size())&&(pinnedCount < ZT_MAX_NETWORK_PINNED)) {
  367. this->pinned[this->pinnedCount].zt.setTo(tmp.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  368. p += this->pinned[this->pinnedCount].phy.deserialize(tmp,p);
  369. ++this->pinnedCount;
  370. }
  371. }
  372. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_RULES,tmp)) {
  373. unsigned int p = 0;
  374. while ((p < tmp.size())&&(ruleCount < ZT_MAX_NETWORK_RULES)) {
  375. rules[ruleCount].t = (uint8_t)tmp[p++];
  376. unsigned int fieldLen = (unsigned int)tmp[p++];
  377. switch((ZT_VirtualNetworkRuleType)(rules[ruleCount].t & 0x7f)) {
  378. default:
  379. break;
  380. case ZT_NETWORK_RULE_ACTION_TEE:
  381. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  382. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  383. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  384. rules[ruleCount].v.zt = Address(tmp.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH).toInt();
  385. break;
  386. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  387. rules[ruleCount].v.vlanId = tmp.at<uint16_t>(p);
  388. break;
  389. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  390. rules[ruleCount].v.vlanPcp = (uint8_t)tmp[p];
  391. break;
  392. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  393. rules[ruleCount].v.vlanDei = (uint8_t)tmp[p];
  394. break;
  395. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  396. rules[ruleCount].v.etherType = tmp.at<uint16_t>(p);
  397. break;
  398. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  399. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  400. memcpy(rules[ruleCount].v.mac,tmp.field(p,6),6);
  401. break;
  402. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  403. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  404. memcpy(&(rules[ruleCount].v.ipv4.ip),tmp.field(p,4),4);
  405. rules[ruleCount].v.ipv4.mask = (uint8_t)tmp[p + 4];
  406. break;
  407. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  408. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  409. memcpy(rules[ruleCount].v.ipv6.ip,tmp.field(p,16),16);
  410. rules[ruleCount].v.ipv6.mask = (uint8_t)tmp[p + 16];
  411. break;
  412. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  413. rules[ruleCount].v.ipTos = (uint8_t)tmp[p];
  414. break;
  415. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  416. rules[ruleCount].v.ipProtocol = (uint8_t)tmp[p];
  417. break;
  418. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  419. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  420. rules[ruleCount].v.port[0] = tmp.at<uint16_t>(p);
  421. rules[ruleCount].v.port[1] = tmp.at<uint16_t>(p + 2);
  422. break;
  423. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  424. rules[ruleCount].v.characteristics = tmp.at<uint64_t>(p);
  425. break;
  426. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  427. rules[ruleCount].v.frameSize[0] = tmp.at<uint16_t>(p);
  428. rules[ruleCount].v.frameSize[0] = tmp.at<uint16_t>(p + 2);
  429. break;
  430. case ZT_NETWORK_RULE_MATCH_TCP_RELATIVE_SEQUENCE_NUMBER_RANGE:
  431. rules[ruleCount].v.tcpseq[0] = tmp.at<uint32_t>(p);
  432. rules[ruleCount].v.tcpseq[1] = tmp.at<uint32_t>(p + 4);
  433. break;
  434. }
  435. p += fieldLen;
  436. ++ruleCount;
  437. }
  438. }
  439. }
  440. /*
  441. printf("~~~\n%s\n~~~\n",d.data());
  442. dump();
  443. printf("~~~\n");
  444. */
  445. return true;
  446. } catch ( ... ) {
  447. return false;
  448. }
  449. }
  450. } // namespace ZeroTier