Topology.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 "Constants.hpp"
  19. #include "Topology.hpp"
  20. #include "RuntimeEnvironment.hpp"
  21. #include "Node.hpp"
  22. #include "Network.hpp"
  23. #include "NetworkConfig.hpp"
  24. #include "Buffer.hpp"
  25. namespace ZeroTier {
  26. // 2015-11-16 -- The Fabulous Four (should have named them after Beatles!)
  27. //#define ZT_DEFAULT_WORLD_LENGTH 494
  28. //static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {0x01,0x00,0x00,0x00,0x00,0x08,0xea,0xc9,0x0a,0x00,0x00,0x01,0x51,0x11,0x70,0xb2,0xfb,0xb8,0xb3,0x88,0xa4,0x69,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7d,0x85,0xaf,0xe6,0x61,0x1f,0xb4,0x3f,0xf0,0xb7,0x41,0x26,0xd9,0x0a,0x6e,0x00,0x01,0x04,0x80,0xc7,0xc5,0xd9,0x27,0x09};
  29. // 2015-11-20 -- Alice and Bob are live, and we're now IPv6 dual-stack!
  30. //#define ZT_DEFAULT_WORLD_LENGTH 792
  31. //static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {0x01,0x00,0x00,0x00,0x00,0x08,0xea,0xc9,0x0a,0x00,0x00,0x01,0x51,0x26,0x6f,0x7c,0x8a,0xb8,0xb3,0x88,0xa4,0x69,0x22,0x14,0x91,0xaa,0x9a,0xcd,0x66,0xcc,0x76,0x4c,0xde,0xfd,0x56,0x03,0x9f,0x10,0x67,0xae,0x15,0xe6,0x9c,0x6f,0xb4,0x2d,0x7b,0x55,0x33,0x0e,0x3f,0xda,0xac,0x52,0x9c,0x07,0x92,0xfd,0x73,0x40,0xa6,0xaa,0x21,0xab,0xa8,0xa4,0x89,0xfd,0xae,0xa4,0x4a,0x39,0xbf,0x2d,0x00,0x65,0x9a,0xc9,0xc8,0x18,0xeb,0xe8,0x0a,0xf5,0xbc,0xf8,0x3d,0x97,0xcd,0xc3,0xf8,0xe2,0x41,0x16,0x42,0x0f,0xc7,0x76,0x8e,0x07,0xf3,0x7e,0x9e,0x7d,0x1b,0xb3,0x23,0x21,0x79,0xce,0xb9,0xd0,0xcb,0xb5,0x94,0x7b,0x89,0x21,0x57,0x72,0xf6,0x70,0xa1,0xdd,0x67,0x38,0xcf,0x45,0x45,0xc2,0x8d,0x46,0xec,0x00,0x2c,0xe0,0x2a,0x63,0x3f,0x63,0x8d,0x33,0x08,0x51,0x07,0x77,0x81,0x5b,0x32,0x49,0xae,0x87,0x89,0xcf,0x31,0xaa,0x41,0xf1,0x52,0x97,0xdc,0xa2,0x55,0xe1,0x4a,0x6e,0x3c,0x04,0xf0,0x4f,0x8a,0x0e,0xe9,0xca,0xec,0x24,0x30,0x04,0x9d,0x21,0x90,0x39,0xf3,0x00,0x01,0xf0,0x92,0x2a,0x98,0xe3,0xb3,0x4e,0xbc,0xbf,0xf3,0x33,0x26,0x9d,0xc2,0x65,0xd7,0xa0,0x20,0xaa,0xb6,0x9d,0x72,0xbe,0x4d,0x4a,0xcc,0x9c,0x8c,0x92,0x94,0x78,0x57,0x71,0x25,0x6c,0xd1,0xd9,0x42,0xa9,0x0d,0x1b,0xd1,0xd2,0xdc,0xa3,0xea,0x84,0xef,0x7d,0x85,0xaf,0xe6,0x61,0x1f,0xb4,0x3f,0xf0,0xb7,0x41,0x26,0xd9,0x0a,0x6e,0x00,0x0c,0x04,0xbc,0xa6,0x5e,0xb1,0x27,0x09,0x06,0x2a,0x03,0xb0,0xc0,0x00,0x02,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0x7d,0x00,0x01,0x27,0x09,0x04,0x9a,0x42,0xc5,0x21,0x27,0x09,0x06,0x2c,0x0f,0xf8,0x50,0x01,0x54,0x01,0x97,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x27,0x09,0x04,0x9f,0xcb,0x61,0xab,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x08,0x00,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,0x54,0x60,0x01,0x27,0x09,0x04,0xa9,0x39,0x8f,0x68,0x27,0x09,0x06,0x26,0x07,0xf0,0xd0,0x1d,0x01,0x00,0x57,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x27,0x09,0x04,0x6b,0xaa,0xc5,0x0e,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x00,0x01,0x00,0x20,0x00,0x00,0x00,0x00,0x02,0x00,0xe0,0x01,0x27,0x09,0x04,0x80,0xc7,0xc5,0xd9,0x27,0x09,0x06,0x24,0x00,0x61,0x80,0x00,0x00,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0xb7,0x40,0x01,0x27,0x09,0x88,0x41,0x40,0x8a,0x2e,0x00,0xbb,0x1d,0x31,0xf2,0xc3,0x23,0xe2,0x64,0xe9,0xe6,0x41,0x72,0xc1,0xa7,0x4f,0x77,0x89,0x95,0x55,0xed,0x10,0x75,0x1c,0xd5,0x6e,0x86,0x40,0x5c,0xde,0x11,0x8d,0x02,0xdf,0xfe,0x55,0x5d,0x46,0x2c,0xcf,0x6a,0x85,0xb5,0x63,0x1c,0x12,0x35,0x0c,0x8d,0x5d,0xc4,0x09,0xba,0x10,0xb9,0x02,0x5d,0x0f,0x44,0x5c,0xf4,0x49,0xd9,0x2b,0x1c,0x00,0x0c,0x04,0x2d,0x20,0xc6,0x82,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x64,0x00,0x81,0xc3,0x54,0x00,0x00,0xff,0xfe,0x18,0x1d,0x61,0x27,0x09,0x04,0x2e,0x65,0xa0,0xf9,0x27,0x09,0x06,0x2a,0x03,0xb0,0xc0,0x00,0x03,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0x6a,0x30,0x01,0x27,0x09,0x04,0x6b,0xbf,0x2e,0xd2,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x68,0x00,0x83,0xa4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x64,0x27,0x09,0x04,0x2d,0x20,0xf6,0xb3,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x58,0x00,0x8b,0xf8,0x54,0x00,0x00,0xff,0xfe,0x15,0xb3,0x9a,0x27,0x09,0x04,0x2d,0x20,0xf8,0x57,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x70,0x00,0x9b,0xc9,0x54,0x00,0x00,0xff,0xfe,0x15,0xc4,0xf5,0x27,0x09,0x04,0x9f,0xcb,0x02,0x9a,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x0c,0xad,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0x26,0x70,0x01,0x27,0x09,0x7e,0x19,0x87,0x6a,0xba,0x00,0x2a,0x6e,0x2b,0x23,0x18,0x93,0x0f,0x60,0xeb,0x09,0x7f,0x70,0xd0,0xf4,0xb0,0x28,0xb2,0xcd,0x6d,0x3d,0x0c,0x63,0xc0,0x14,0xb9,0x03,0x9f,0xf3,0x53,0x90,0xe4,0x11,0x81,0xf2,0x16,0xfb,0x2e,0x6f,0xa8,0xd9,0x5c,0x1e,0xe9,0x66,0x71,0x56,0x41,0x19,0x05,0xc3,0xdc,0xcf,0xea,0x78,0xd8,0xc6,0xdf,0xaf,0xba,0x68,0x81,0x70,0xb3,0xfa,0x00,0x01,0x04,0xc6,0xc7,0x61,0xdc,0x27,0x09,0x8a,0xcf,0x05,0x9f,0xe3,0x00,0x48,0x2f,0x6e,0xe5,0xdf,0xe9,0x02,0x31,0x9b,0x41,0x9d,0xe5,0xbd,0xc7,0x65,0x20,0x9c,0x0e,0xcd,0xa3,0x8c,0x4d,0x6e,0x4f,0xcf,0x0d,0x33,0x65,0x83,0x98,0xb4,0x52,0x7d,0xcd,0x22,0xf9,0x31,0x12,0xfb,0x9b,0xef,0xd0,0x2f,0xd7,0x8b,0xf7,0x26,0x1b,0x33,0x3f,0xc1,0x05,0xd1,0x92,0xa6,0x23,0xca,0x9e,0x50,0xfc,0x60,0xb3,0x74,0xa5,0x00,0x01,0x04,0xa2,0xf3,0x4d,0x6f,0x27,0x09};
  32. // 2015-12-17 -- Old New York root is dead, old SF still alive
  33. //#define ZT_DEFAULT_WORLD_LENGTH 732
  34. //static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {0x01,0x00,0x00,0x00,0x00,0x08,0xea,0xc9,0x0a,0x00,0x00,0x01,0x51,0xb1,0x7e,0x39,0x9d,0xb8,0xb3,0x88,0xa4,0x69,0x22,0x14,0x91,0xaa,0x9a,0xcd,0x66,0xcc,0x76,0x4c,0xde,0xfd,0x56,0x03,0x9f,0x10,0x67,0xae,0x15,0xe6,0x9c,0x6f,0xb4,0x2d,0x7b,0x55,0x33,0x0e,0x3f,0xda,0xac,0x52,0x9c,0x07,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21,0x27,0x09,0x06,0x2c,0x0f,0xf8,0x50,0x01,0x54,0x01,0x97,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x27,0x09,0x04,0x9f,0xcb,0x61,0xab,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x08,0x00,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,0x54,0x60,0x01,0x27,0x09,0x04,0xa9,0x39,0x8f,0x68,0x27,0x09,0x06,0x26,0x07,0xf0,0xd0,0x1d,0x01,0x00,0x57,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x27,0x09,0x04,0x6b,0xaa,0xc5,0x0e,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x00,0x01,0x00,0x20,0x00,0x00,0x00,0x00,0x02,0x00,0xe0,0x01,0x27,0x09,0x04,0x80,0xc7,0xc5,0xd9,0x27,0x09,0x06,0x24,0x00,0x61,0x80,0x00,0x00,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0xb7,0x40,0x01,0x27,0x09,0x88,0x41,0x40,0x8a,0x2e,0x00,0xbb,0x1d,0x31,0xf2,0xc3,0x23,0xe2,0x64,0xe9,0xe6,0x41,0x72,0xc1,0xa7,0x4f,0x77,0x89,0x95,0x55,0xed,0x10,0x75,0x1c,0xd5,0x6e,0x86,0x40,0x5c,0xde,0x11,0x8d,0x02,0xdf,0xfe,0x55,0x5d,0x46,0x2c,0xcf,0x6a,0x85,0xb5,0x63,0x1c,0x12,0x35,0x0c,0x8d,0x5d,0xc4,0x09,0xba,0x10,0xb9,0x02,0x5d,0x0f,0x44,0x5c,0xf4,0x49,0xd9,0x2b,0x1c,0x00,0x0c,0x04,0x2d,0x20,0xc6,0x82,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x64,0x00,0x81,0xc3,0x54,0x00,0x00,0xff,0xfe,0x18,0x1d,0x61,0x27,0x09,0x04,0x2e,0x65,0xa0,0xf9,0x27,0x09,0x06,0x2a,0x03,0xb0,0xc0,0x00,0x03,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0x6a,0x30,0x01,0x27,0x09,0x04,0x6b,0xbf,0x2e,0xd2,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x68,0x00,0x83,0xa4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x64,0x27,0x09,0x04,0x2d,0x20,0xf6,0xb3,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x58,0x00,0x8b,0xf8,0x54,0x00,0x00,0xff,0xfe,0x15,0xb3,0x9a,0x27,0x09,0x04,0x2d,0x20,0xf8,0x57,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x70,0x00,0x9b,0xc9,0x54,0x00,0x00,0xff,0xfe,0x15,0xc4,0xf5,0x27,0x09,0x04,0x9f,0xcb,0x02,0x9a,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x0c,0xad,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0x26,0x70,0x01,0x27,0x09,0x7e,0x19,0x87,0x6a,0xba,0x00,0x2a,0x6e,0x2b,0x23,0x18,0x93,0x0f,0x60,0xeb,0x09,0x7f,0x70,0xd0,0xf4,0xb0,0x28,0xb2,0xcd,0x6d,0x3d,0x0c,0x63,0xc0,0x14,0xb9,0x03,0x9f,0xf3,0x53,0x90,0xe4,0x11,0x81,0xf2,0x16,0xfb,0x2e,0x6f,0xa8,0xd9,0x5c,0x1e,0xe9,0x66,0x71,0x56,0x41,0x19,0x05,0xc3,0xdc,0xcf,0xea,0x78,0xd8,0xc6,0xdf,0xaf,0xba,0x68,0x81,0x70,0xb3,0xfa,0x00,0x02,0x04,0xc6,0xc7,0x61,0xdc,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x00,0x01,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0xc5,0xf0,0x01,0x27,0x09};
  35. // 2016-01-13 -- Old San Francisco 1.0.1 root is dead, now we're just on Alice and Bob!
  36. #define ZT_DEFAULT_WORLD_LENGTH 634
  37. static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {0x01,0x00,0x00,0x00,0x00,0x08,0xea,0xc9,0x0a,0x00,0x00,0x01,0x52,0x3c,0x32,0x50,0x1a,0xb8,0xb3,0x88,0xa4,0x69,0x22,0x14,0x91,0xaa,0x9a,0xcd,0x66,0xcc,0x76,0x4c,0xde,0xfd,0x56,0x03,0x9f,0x10,0x67,0xae,0x15,0xe6,0x9c,0x6f,0xb4,0x2d,0x7b,0x55,0x33,0x0e,0x3f,0xda,0xac,0x52,0x9c,0x07,0x92,0xfd,0x73,0x40,0xa6,0xaa,0x21,0xab,0xa8,0xa4,0x89,0xfd,0xae,0xa4,0x4a,0x39,0xbf,0x2d,0x00,0x65,0x9a,0xc9,0xc8,0x18,0xeb,0x4a,0xf7,0x86,0xa8,0x40,0xd6,0x52,0xea,0xae,0x9e,0x7a,0xbf,0x4c,0x97,0x66,0xab,0x2d,0x6f,0xaf,0xc9,0x2b,0x3a,0xff,0xed,0xd6,0x30,0x3e,0xc4,0x6a,0x65,0xf2,0xbd,0x83,0x52,0xf5,0x40,0xe9,0xcc,0x0d,0x6e,0x89,0x3f,0x9a,0xa0,0xb8,0xdf,0x42,0xd2,0x2f,0x84,0xe6,0x03,0x26,0x0f,0xa8,0xe3,0xcc,0x05,0x05,0x03,0xef,0x12,0x80,0x0d,0xce,0x3e,0xb6,0x58,0x3b,0x1f,0xa8,0xad,0xc7,0x25,0xf9,0x43,0x71,0xa7,0x5c,0x9a,0xc7,0xe1,0xa3,0xb8,0x88,0xd0,0x71,0x6c,0x94,0x99,0x73,0x41,0x0b,0x1b,0x48,0x84,0x02,0x9d,0x21,0x90,0x39,0xf3,0x00,0x01,0xf0,0x92,0x2a,0x98,0xe3,0xb3,0x4e,0xbc,0xbf,0xf3,0x33,0x26,0x9d,0xc2,0x65,0xd7,0xa0,0x20,0xaa,0xb6,0x9d,0x72,0xbe,0x4d,0x4a,0xcc,0x9c,0x8c,0x92,0x94,0x78,0x57,0x71,0x25,0x6c,0xd1,0xd9,0x42,0xa9,0x0d,0x1b,0xd1,0xd2,0xdc,0xa3,0xea,0x84,0xef,0x7d,0x85,0xaf,0xe6,0x61,0x1f,0xb4,0x3f,0xf0,0xb7,0x41,0x26,0xd9,0x0a,0x6e,0x00,0x0c,0x04,0xbc,0xa6,0x5e,0xb1,0x27,0x09,0x06,0x2a,0x03,0xb0,0xc0,0x00,0x02,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0x7d,0x00,0x01,0x27,0x09,0x04,0x9a,0x42,0xc5,0x21,0x27,0x09,0x06,0x2c,0x0f,0xf8,0x50,0x01,0x54,0x01,0x97,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x27,0x09,0x04,0x9f,0xcb,0x61,0xab,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x08,0x00,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,0x54,0x60,0x01,0x27,0x09,0x04,0xa9,0x39,0x8f,0x68,0x27,0x09,0x06,0x26,0x07,0xf0,0xd0,0x1d,0x01,0x00,0x57,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x27,0x09,0x04,0x6b,0xaa,0xc5,0x0e,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x00,0x01,0x00,0x20,0x00,0x00,0x00,0x00,0x02,0x00,0xe0,0x01,0x27,0x09,0x04,0x80,0xc7,0xc5,0xd9,0x27,0x09,0x06,0x24,0x00,0x61,0x80,0x00,0x00,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0xb7,0x40,0x01,0x27,0x09,0x88,0x41,0x40,0x8a,0x2e,0x00,0xbb,0x1d,0x31,0xf2,0xc3,0x23,0xe2,0x64,0xe9,0xe6,0x41,0x72,0xc1,0xa7,0x4f,0x77,0x89,0x95,0x55,0xed,0x10,0x75,0x1c,0xd5,0x6e,0x86,0x40,0x5c,0xde,0x11,0x8d,0x02,0xdf,0xfe,0x55,0x5d,0x46,0x2c,0xcf,0x6a,0x85,0xb5,0x63,0x1c,0x12,0x35,0x0c,0x8d,0x5d,0xc4,0x09,0xba,0x10,0xb9,0x02,0x5d,0x0f,0x44,0x5c,0xf4,0x49,0xd9,0x2b,0x1c,0x00,0x0c,0x04,0x2d,0x20,0xc6,0x82,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x64,0x00,0x81,0xc3,0x54,0x00,0x00,0xff,0xfe,0x18,0x1d,0x61,0x27,0x09,0x04,0x2e,0x65,0xa0,0xf9,0x27,0x09,0x06,0x2a,0x03,0xb0,0xc0,0x00,0x03,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0x6a,0x30,0x01,0x27,0x09,0x04,0x6b,0xbf,0x2e,0xd2,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x68,0x00,0x83,0xa4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x64,0x27,0x09,0x04,0x2d,0x20,0xf6,0xb3,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x58,0x00,0x8b,0xf8,0x54,0x00,0x00,0xff,0xfe,0x15,0xb3,0x9a,0x27,0x09,0x04,0x2d,0x20,0xf8,0x57,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x70,0x00,0x9b,0xc9,0x54,0x00,0x00,0xff,0xfe,0x15,0xc4,0xf5,0x27,0x09,0x04,0x9f,0xcb,0x02,0x9a,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x0c,0xad,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0x26,0x70,0x01,0x27,0x09};
  38. Topology::Topology(const RuntimeEnvironment *renv) :
  39. RR(renv),
  40. _amRoot(false)
  41. {
  42. std::string alls(RR->node->dataStoreGet("peers.save"));
  43. const uint8_t *all = reinterpret_cast<const uint8_t *>(alls.data());
  44. RR->node->dataStoreDelete("peers.save");
  45. Buffer<ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE> *deserializeBuf = new Buffer<ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE>();
  46. unsigned int ptr = 0;
  47. while ((ptr + 4) < alls.size()) {
  48. try {
  49. const unsigned int reclen = ( // each Peer serialized record is prefixed by a record length
  50. ((((unsigned int)all[ptr]) & 0xff) << 24) |
  51. ((((unsigned int)all[ptr + 1]) & 0xff) << 16) |
  52. ((((unsigned int)all[ptr + 2]) & 0xff) << 8) |
  53. (((unsigned int)all[ptr + 3]) & 0xff)
  54. );
  55. unsigned int pos = 0;
  56. deserializeBuf->copyFrom(all + ptr,reclen + 4);
  57. SharedPtr<Peer> p(Peer::deserializeNew(RR,RR->identity,*deserializeBuf,pos));
  58. ptr += pos;
  59. if (!p)
  60. break; // stop if invalid records
  61. if (p->address() != RR->identity.address())
  62. _peers.set(p->address(),p);
  63. } catch ( ... ) {
  64. break; // stop if invalid records
  65. }
  66. }
  67. delete deserializeBuf;
  68. clean(RR->node->now());
  69. std::string dsWorld(RR->node->dataStoreGet("world"));
  70. World cachedWorld;
  71. if (dsWorld.length() > 0) {
  72. try {
  73. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> dswtmp(dsWorld.data(),(unsigned int)dsWorld.length());
  74. cachedWorld.deserialize(dswtmp,0);
  75. } catch ( ... ) {
  76. cachedWorld = World(); // clear if cached world is invalid
  77. }
  78. }
  79. World defaultWorld;
  80. {
  81. Buffer<ZT_DEFAULT_WORLD_LENGTH> wtmp(ZT_DEFAULT_WORLD,ZT_DEFAULT_WORLD_LENGTH);
  82. defaultWorld.deserialize(wtmp,0); // throws on error, which would indicate a bad static variable up top
  83. }
  84. if (cachedWorld.shouldBeReplacedBy(defaultWorld,false)) {
  85. _setWorld(defaultWorld);
  86. if (dsWorld.length() > 0)
  87. RR->node->dataStoreDelete("world");
  88. } else _setWorld(cachedWorld);
  89. }
  90. Topology::~Topology()
  91. {
  92. Buffer<ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE> *pbuf = 0;
  93. try {
  94. pbuf = new Buffer<ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE>();
  95. std::string all;
  96. Address *a = (Address *)0;
  97. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  98. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  99. while (i.next(a,p)) {
  100. if (std::find(_rootAddresses.begin(),_rootAddresses.end(),*a) == _rootAddresses.end()) {
  101. pbuf->clear();
  102. try {
  103. (*p)->serialize(*pbuf);
  104. try {
  105. all.append((const char *)pbuf->data(),pbuf->size());
  106. } catch ( ... ) {
  107. return; // out of memory? just skip
  108. }
  109. } catch ( ... ) {} // peer too big? shouldn't happen, but it so skip
  110. }
  111. }
  112. RR->node->dataStorePut("peers.save",all,true);
  113. delete pbuf;
  114. } catch ( ... ) {
  115. delete pbuf;
  116. }
  117. }
  118. SharedPtr<Peer> Topology::addPeer(const SharedPtr<Peer> &peer)
  119. {
  120. #ifdef ZT_TRACE
  121. if ((!peer)||(peer->address() == RR->identity.address())) {
  122. if (!peer)
  123. fprintf(stderr,"FATAL BUG: addPeer() caught attempt to add NULL peer" ZT_EOL_S);
  124. else fprintf(stderr,"FATAL BUG: addPeer() caught attempt to add peer for self" ZT_EOL_S);
  125. abort();
  126. }
  127. #endif
  128. SharedPtr<Peer> np;
  129. {
  130. Mutex::Lock _l(_lock);
  131. SharedPtr<Peer> &hp = _peers[peer->address()];
  132. if (!hp)
  133. hp = peer;
  134. np = hp;
  135. }
  136. np->use(RR->node->now());
  137. saveIdentity(np->identity());
  138. return np;
  139. }
  140. SharedPtr<Peer> Topology::getPeer(const Address &zta)
  141. {
  142. if (zta == RR->identity.address()) {
  143. TRACE("BUG: ignored attempt to getPeer() for self, returned NULL");
  144. return SharedPtr<Peer>();
  145. }
  146. {
  147. Mutex::Lock _l(_lock);
  148. const SharedPtr<Peer> *const ap = _peers.get(zta);
  149. if (ap) {
  150. (*ap)->use(RR->node->now());
  151. return *ap;
  152. }
  153. }
  154. try {
  155. Identity id(_getIdentity(zta));
  156. if (id) {
  157. SharedPtr<Peer> np(new Peer(RR,RR->identity,id));
  158. {
  159. Mutex::Lock _l(_lock);
  160. SharedPtr<Peer> &ap = _peers[zta];
  161. if (!ap)
  162. ap.swap(np);
  163. ap->use(RR->node->now());
  164. return ap;
  165. }
  166. }
  167. } catch ( ... ) {
  168. fprintf(stderr,"EXCEPTION in getPeer() part 2\n");
  169. abort();
  170. } // invalid identity on disk?
  171. return SharedPtr<Peer>();
  172. }
  173. Identity Topology::getIdentity(const Address &zta)
  174. {
  175. {
  176. Mutex::Lock _l(_lock);
  177. const SharedPtr<Peer> *const ap = _peers.get(zta);
  178. if (ap)
  179. return (*ap)->identity();
  180. }
  181. return _getIdentity(zta);
  182. }
  183. void Topology::saveIdentity(const Identity &id)
  184. {
  185. if (id) {
  186. char p[128];
  187. Utils::snprintf(p,sizeof(p),"iddb.d/%.10llx",(unsigned long long)id.address().toInt());
  188. RR->node->dataStorePut(p,id.toString(false),false);
  189. }
  190. }
  191. SharedPtr<Peer> Topology::getBestRoot(const Address *avoid,unsigned int avoidCount,bool strictAvoid)
  192. {
  193. const uint64_t now = RR->node->now();
  194. Mutex::Lock _l(_lock);
  195. if (_amRoot) {
  196. /* If I am a root server, the "best" root server is the one whose address
  197. * is numerically greater than mine (with wrap at top of list). This
  198. * causes packets searching for a route to pretty much literally
  199. * circumnavigate the globe rather than bouncing between just two. */
  200. for(unsigned long p=0;p<_rootAddresses.size();++p) {
  201. if (_rootAddresses[p] == RR->identity.address()) {
  202. for(unsigned long q=1;q<_rootAddresses.size();++q) {
  203. const SharedPtr<Peer> *const nextsn = _peers.get(_rootAddresses[(p + q) % _rootAddresses.size()]);
  204. if ((nextsn)&&((*nextsn)->hasActiveDirectPath(now))) {
  205. (*nextsn)->use(now);
  206. return *nextsn;
  207. }
  208. }
  209. break;
  210. }
  211. }
  212. } else {
  213. /* If I am not a root server, the best root server is the active one with
  214. * the lowest quality score. (lower == better) */
  215. unsigned int bestQualityOverall = ~((unsigned int)0);
  216. unsigned int bestQualityNotAvoid = ~((unsigned int)0);
  217. const SharedPtr<Peer> *bestOverall = (const SharedPtr<Peer> *)0;
  218. const SharedPtr<Peer> *bestNotAvoid = (const SharedPtr<Peer> *)0;
  219. for(std::vector< SharedPtr<Peer> >::const_iterator r(_rootPeers.begin());r!=_rootPeers.end();++r) {
  220. bool avoiding = false;
  221. for(unsigned int i=0;i<avoidCount;++i) {
  222. if (avoid[i] == (*r)->address()) {
  223. avoiding = true;
  224. break;
  225. }
  226. }
  227. const unsigned int q = (*r)->relayQuality(now);
  228. if (q <= bestQualityOverall) {
  229. bestQualityOverall = q;
  230. bestOverall = &(*r);
  231. }
  232. if ((!avoiding)&&(q <= bestQualityNotAvoid)) {
  233. bestQualityNotAvoid = q;
  234. bestNotAvoid = &(*r);
  235. }
  236. }
  237. if (bestNotAvoid) {
  238. (*bestNotAvoid)->use(now);
  239. return *bestNotAvoid;
  240. } else if ((!strictAvoid)&&(bestOverall)) {
  241. (*bestOverall)->use(now);
  242. return *bestOverall;
  243. }
  244. }
  245. return SharedPtr<Peer>();
  246. }
  247. bool Topology::isUpstream(const Identity &id) const
  248. {
  249. if (isRoot(id))
  250. return true;
  251. std::vector< SharedPtr<Network> > nws(RR->node->allNetworks());
  252. for(std::vector< SharedPtr<Network> >::const_iterator nw(nws.begin());nw!=nws.end();++nw) {
  253. SharedPtr<NetworkConfig> nc((*nw)->config2());
  254. if (nc) {
  255. for(std::vector< std::pair<Address,InetAddress> >::const_iterator r(nc->relays().begin());r!=nc->relays().end();++r) {
  256. if (r->first == id.address())
  257. return true;
  258. }
  259. }
  260. }
  261. return false;
  262. }
  263. bool Topology::worldUpdateIfValid(const World &newWorld)
  264. {
  265. Mutex::Lock _l(_lock);
  266. if (_world.shouldBeReplacedBy(newWorld,true)) {
  267. _setWorld(newWorld);
  268. try {
  269. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> dswtmp;
  270. newWorld.serialize(dswtmp,false);
  271. RR->node->dataStorePut("world",dswtmp.data(),dswtmp.size(),false);
  272. } catch ( ... ) {
  273. RR->node->dataStoreDelete("world");
  274. }
  275. return true;
  276. }
  277. return false;
  278. }
  279. void Topology::clean(uint64_t now)
  280. {
  281. Mutex::Lock _l(_lock);
  282. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  283. Address *a = (Address *)0;
  284. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  285. while (i.next(a,p)) {
  286. if (((now - (*p)->lastUsed()) >= ZT_PEER_IN_MEMORY_EXPIRATION)&&(std::find(_rootAddresses.begin(),_rootAddresses.end(),*a) == _rootAddresses.end())) {
  287. _peers.erase(*a);
  288. } else {
  289. (*p)->clean(now);
  290. }
  291. }
  292. }
  293. Identity Topology::_getIdentity(const Address &zta)
  294. {
  295. char p[128];
  296. Utils::snprintf(p,sizeof(p),"iddb.d/%.10llx",(unsigned long long)zta.toInt());
  297. std::string ids(RR->node->dataStoreGet(p));
  298. if (ids.length() > 0) {
  299. try {
  300. return Identity(ids);
  301. } catch ( ... ) {} // ignore invalid IDs
  302. }
  303. return Identity();
  304. }
  305. void Topology::_setWorld(const World &newWorld)
  306. {
  307. // assumed _lock is locked (or in constructor)
  308. _world = newWorld;
  309. _amRoot = false;
  310. _rootAddresses.clear();
  311. _rootPeers.clear();
  312. for(std::vector<World::Root>::const_iterator r(_world.roots().begin());r!=_world.roots().end();++r) {
  313. _rootAddresses.push_back(r->identity.address());
  314. if (r->identity.address() == RR->identity.address()) {
  315. _amRoot = true;
  316. } else {
  317. SharedPtr<Peer> *rp = _peers.get(r->identity.address());
  318. if (rp) {
  319. _rootPeers.push_back(*rp);
  320. } else {
  321. SharedPtr<Peer> newrp(new Peer(RR,RR->identity,r->identity));
  322. _peers.set(r->identity.address(),newrp);
  323. _rootPeers.push_back(newrp);
  324. }
  325. }
  326. }
  327. }
  328. } // namespace ZeroTier