Topology.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #include "Switch.hpp"
  26. namespace ZeroTier {
  27. // 2015-11-16 -- The Fabulous Four (should have named them after Beatles!)
  28. //#define ZT_DEFAULT_WORLD_LENGTH 494
  29. //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,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,0x80,0x31,0xa4,0x65,0x95,0x45,0x06,0x1c,0xfb,0xc2,0x4e,0x5d,0xe7,0x0a,0x40,0x7a,0x97,0xce,0x36,0xa2,0x3d,0x05,0xca,0x87,0xc7,0x59,0x27,0x5c,0x8b,0x0d,0x4c,0xb4,0xbb,0x26,0x2f,0x77,0x17,0x5e,0xb7,0x4d,0xb8,0xd3,0xb4,0xe9,0x23,0x5d,0xcc,0xa2,0x71,0xa8,0xdf,0xf1,0x23,0xa3,0xb2,0x66,0x74,0xea,0xe5,0xdc,0x8d,0xef,0xd3,0x0a,0xa9,0xac,0xcb,0xda,0x93,0xbd,0x6c,0xcd,0x43,0x1d,0xa7,0x98,0x6a,0xde,0x70,0xc0,0xc6,0x1c,0xaf,0xf0,0xfd,0x7f,0x8a,0xb9,0x76,0x13,0xe1,0xde,0x4f,0xf3,0xd6,0x13,0x04,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,0xa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xcc,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,0x01,0x04,0x80,0xc7,0xc5,0xd9,0x27,0x09};
  30. // 2015-11-20 -- Alice and Bob are live, and we're now IPv6 dual-stack!
  31. //#define ZT_DEFAULT_WORLD_LENGTH 792
  32. //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};
  33. // 2015-12-17 -- Old New York root is dead, old SF still alive
  34. //#define ZT_DEFAULT_WORLD_LENGTH 732
  35. //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,0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,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,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};
  36. // 2016-01-13 -- Old San Francisco 1.0.1 root is dead, now we're just on Alice and Bob!
  37. #define ZT_DEFAULT_WORLD_LENGTH 634
  38. 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};
  39. Topology::Topology(const RuntimeEnvironment *renv) :
  40. RR(renv),
  41. _trustedPathCount(0),
  42. _amRoot(false)
  43. {
  44. try {
  45. World cachedPlanet;
  46. std::string buf(RR->node->dataStoreGet("planet"));
  47. if (buf.length() > 0) {
  48. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> dswtmp(buf.data(),(unsigned int)buf.length());
  49. cachedPlanet.deserialize(dswtmp,0);
  50. }
  51. addWorld(cachedPlanet);
  52. } catch ( ... ) {}
  53. World defaultPlanet;
  54. {
  55. Buffer<ZT_DEFAULT_WORLD_LENGTH> wtmp(ZT_DEFAULT_WORLD,ZT_DEFAULT_WORLD_LENGTH);
  56. defaultPlanet.deserialize(wtmp,0); // throws on error, which would indicate a bad static variable up top
  57. }
  58. addWorld(defaultPlanet);
  59. }
  60. SharedPtr<Peer> Topology::addPeer(const SharedPtr<Peer> &peer)
  61. {
  62. #ifdef ZT_TRACE
  63. if ((!peer)||(peer->address() == RR->identity.address())) {
  64. if (!peer)
  65. fprintf(stderr,"FATAL BUG: addPeer() caught attempt to add NULL peer" ZT_EOL_S);
  66. else fprintf(stderr,"FATAL BUG: addPeer() caught attempt to add peer for self" ZT_EOL_S);
  67. abort();
  68. }
  69. #endif
  70. SharedPtr<Peer> np;
  71. {
  72. Mutex::Lock _l(_peers_m);
  73. SharedPtr<Peer> &hp = _peers[peer->address()];
  74. if (!hp)
  75. hp = peer;
  76. np = hp;
  77. }
  78. saveIdentity(np->identity());
  79. return np;
  80. }
  81. SharedPtr<Peer> Topology::getPeer(const Address &zta)
  82. {
  83. if (zta == RR->identity.address()) {
  84. TRACE("BUG: ignored attempt to getPeer() for self, returned NULL");
  85. return SharedPtr<Peer>();
  86. }
  87. {
  88. Mutex::Lock _l(_peers_m);
  89. const SharedPtr<Peer> *const ap = _peers.get(zta);
  90. if (ap)
  91. return *ap;
  92. }
  93. try {
  94. Identity id(_getIdentity(zta));
  95. if (id) {
  96. SharedPtr<Peer> np(new Peer(RR,RR->identity,id));
  97. {
  98. Mutex::Lock _l(_peers_m);
  99. SharedPtr<Peer> &ap = _peers[zta];
  100. if (!ap)
  101. ap.swap(np);
  102. return ap;
  103. }
  104. }
  105. } catch ( ... ) {} // invalid identity on disk?
  106. return SharedPtr<Peer>();
  107. }
  108. Identity Topology::getIdentity(const Address &zta)
  109. {
  110. if (zta == RR->identity.address()) {
  111. return RR->identity;
  112. } else {
  113. Mutex::Lock _l(_peers_m);
  114. const SharedPtr<Peer> *const ap = _peers.get(zta);
  115. if (ap)
  116. return (*ap)->identity();
  117. }
  118. return _getIdentity(zta);
  119. }
  120. void Topology::saveIdentity(const Identity &id)
  121. {
  122. if (id) {
  123. char p[128];
  124. Utils::snprintf(p,sizeof(p),"iddb.d/%.10llx",(unsigned long long)id.address().toInt());
  125. RR->node->dataStorePut(p,id.toString(false),false);
  126. }
  127. }
  128. SharedPtr<Peer> Topology::getUpstreamPeer(const Address *avoid,unsigned int avoidCount,bool strictAvoid)
  129. {
  130. const uint64_t now = RR->node->now();
  131. Mutex::Lock _l1(_peers_m);
  132. Mutex::Lock _l2(_upstreams_m);
  133. if (_amRoot) {
  134. /* If I am a root, pick another root that isn't mine and that
  135. * has a numerically greater ID. This causes packets to roam
  136. * around the top rather than bouncing between just two. */
  137. for(unsigned long p=0;p<_upstreamAddresses.size();++p) {
  138. if (_upstreamAddresses[p] == RR->identity.address()) {
  139. for(unsigned long q=1;q<_upstreamAddresses.size();++q) {
  140. const SharedPtr<Peer> *const nextsn = _peers.get(_upstreamAddresses[(p + q) % _upstreamAddresses.size()]);
  141. if ((nextsn)&&((*nextsn)->hasActiveDirectPath(now)))
  142. return *nextsn;
  143. }
  144. break;
  145. }
  146. }
  147. } else {
  148. /* Otherwise pick the bestest looking upstream */
  149. unsigned int bestQualityOverall = ~((unsigned int)0);
  150. unsigned int bestQualityNotAvoid = ~((unsigned int)0);
  151. const SharedPtr<Peer> *bestOverall = (const SharedPtr<Peer> *)0;
  152. const SharedPtr<Peer> *bestNotAvoid = (const SharedPtr<Peer> *)0;
  153. for(std::vector<Address>::const_iterator a(_upstreamAddresses.begin());a!=_upstreamAddresses.end();++a) {
  154. const SharedPtr<Peer> *p = _peers.get(*a);
  155. if (p) {
  156. bool avoiding = false;
  157. for(unsigned int i=0;i<avoidCount;++i) {
  158. if (avoid[i] == (*p)->address()) {
  159. avoiding = true;
  160. break;
  161. }
  162. }
  163. const unsigned int q = (*p)->relayQuality(now);
  164. if (q <= bestQualityOverall) {
  165. bestQualityOverall = q;
  166. bestOverall = &(*p);
  167. }
  168. if ((!avoiding)&&(q <= bestQualityNotAvoid)) {
  169. bestQualityNotAvoid = q;
  170. bestNotAvoid = &(*p);
  171. }
  172. }
  173. }
  174. if (bestNotAvoid) {
  175. return *bestNotAvoid;
  176. } else if ((!strictAvoid)&&(bestOverall)) {
  177. return *bestOverall;
  178. }
  179. }
  180. return SharedPtr<Peer>();
  181. }
  182. bool Topology::isUpstream(const Identity &id) const
  183. {
  184. Mutex::Lock _l(_upstreams_m);
  185. return (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),id.address()) != _upstreamAddresses.end());
  186. }
  187. ZT_PeerRole Topology::role(const Address &ztaddr) const
  188. {
  189. Mutex::Lock _l(_upstreams_m);
  190. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),ztaddr) != _upstreamAddresses.end()) {
  191. for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) {
  192. if (i->identity.address() == ztaddr)
  193. return ZT_PEER_ROLE_ROOT;
  194. }
  195. return ZT_PEER_ROLE_UPSTREAM;
  196. }
  197. return ZT_PEER_ROLE_LEAF;
  198. }
  199. bool Topology::isProhibitedEndpoint(const Address &ztaddr,const InetAddress &ipaddr) const
  200. {
  201. Mutex::Lock _l(_upstreams_m);
  202. // For roots the only permitted addresses are those defined. This adds just a little
  203. // bit of extra security against spoofing, replaying, etc.
  204. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),ztaddr) != _upstreamAddresses.end()) {
  205. for(std::vector<World::Root>::const_iterator r(_planet.roots().begin());r!=_planet.roots().end();++r) {
  206. if (r->identity.address() == ztaddr) {
  207. if (r->stableEndpoints.size() == 0)
  208. return false; // no stable endpoints specified, so allow dynamic paths
  209. for(std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin());e!=r->stableEndpoints.end();++e) {
  210. if (ipaddr.ipsEqual(*e))
  211. return false;
  212. }
  213. }
  214. }
  215. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  216. for(std::vector<World::Root>::const_iterator r(m->roots().begin());r!=m->roots().end();++r) {
  217. if (r->identity.address() == ztaddr) {
  218. if (r->stableEndpoints.size() == 0)
  219. return false; // no stable endpoints specified, so allow dynamic paths
  220. for(std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin());e!=r->stableEndpoints.end();++e) {
  221. if (ipaddr.ipsEqual(*e))
  222. return false;
  223. }
  224. }
  225. }
  226. }
  227. return true;
  228. }
  229. return false;
  230. }
  231. bool Topology::addWorld(const World &newWorld)
  232. {
  233. if ((newWorld.type() != World::TYPE_PLANET)&&(newWorld.type() != World::TYPE_MOON))
  234. return false;
  235. Mutex::Lock _l1(_upstreams_m);
  236. Mutex::Lock _l2(_peers_m);
  237. World *existing = (World *)0;
  238. switch(newWorld.type()) {
  239. case World::TYPE_PLANET:
  240. existing = &_planet;
  241. break;
  242. case World::TYPE_MOON:
  243. for(std::vector< World >::iterator m(_moons.begin());m!=_moons.end();++m) {
  244. if (m->id() == newWorld.id()) {
  245. existing = &(*m);
  246. break;
  247. }
  248. }
  249. break;
  250. default:
  251. return false;
  252. }
  253. if (existing) {
  254. if (existing->shouldBeReplacedBy(newWorld))
  255. *existing = newWorld;
  256. else return false;
  257. } else if ((newWorld.type() == World::TYPE_MOON)&&(std::find(_contactingMoons.begin(),_contactingMoons.end(),Address(newWorld.id() >> 24)) != _contactingMoons.end())) {
  258. _moons.push_back(newWorld);
  259. existing = &(_moons.back());
  260. std::vector<Address> cm;
  261. for(std::vector<Address>::const_iterator m(_contactingMoons.begin());m!=_contactingMoons.end();++m) {
  262. if (m->toInt() != ((existing->id() >> 24) & 0xffffffffffULL))
  263. cm.push_back(*m);
  264. }
  265. _contactingMoons.swap(cm);
  266. } else return false;
  267. char savePath[64];
  268. if (existing->type() == World::TYPE_MOON)
  269. Utils::snprintf(savePath,sizeof(savePath),"moons.d/%.16llx.moon",existing->id());
  270. else Utils::scopy(savePath,sizeof(savePath),"planet");
  271. try {
  272. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> dswtmp;
  273. existing->serialize(dswtmp,false);
  274. RR->node->dataStorePut(savePath,dswtmp.data(),dswtmp.size(),false);
  275. } catch ( ... ) {
  276. RR->node->dataStoreDelete(savePath);
  277. }
  278. _memoizeUpstreams();
  279. return true;
  280. }
  281. void Topology::addMoon(const uint64_t id)
  282. {
  283. {
  284. const Address a(id >> 24);
  285. Mutex::Lock _l(_upstreams_m);
  286. if (std::find(_contactingMoons.begin(),_contactingMoons.end(),a) == _contactingMoons.end())
  287. _contactingMoons.push_back(a);
  288. }
  289. char savePath[64];
  290. Utils::snprintf(savePath,sizeof(savePath),"moons.d/%.16llx.moon",id);
  291. try {
  292. std::string moonBin(RR->node->dataStoreGet(savePath));
  293. if (moonBin.length() > 1) {
  294. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> wtmp(moonBin.data(),(unsigned int)moonBin.length());
  295. World w;
  296. w.deserialize(wtmp);
  297. if (w.type() == World::TYPE_MOON) {
  298. addWorld(w);
  299. return;
  300. }
  301. }
  302. } catch ( ... ) {}
  303. RR->node->dataStorePut(savePath,"\0",1,false); // persist that we want to be a member
  304. }
  305. void Topology::removeMoon(const uint64_t id)
  306. {
  307. Mutex::Lock _l1(_upstreams_m);
  308. Mutex::Lock _l2(_peers_m);
  309. std::vector<World> nm;
  310. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  311. if (m->id() != id) {
  312. nm.push_back(*m);
  313. } else {
  314. char savePath[64];
  315. Utils::snprintf(savePath,sizeof(savePath),"moons.d/%.16llx.moon",id);
  316. RR->node->dataStoreDelete(savePath);
  317. }
  318. }
  319. _moons.swap(nm);
  320. std::vector<Address> cm;
  321. for(std::vector<Address>::const_iterator m(_contactingMoons.begin());m!=_contactingMoons.end();++m) {
  322. if (m->toInt() != ((id >> 24) & 0xffffffffffULL))
  323. cm.push_back(*m);
  324. }
  325. _contactingMoons.swap(cm);
  326. _memoizeUpstreams();
  327. }
  328. void Topology::clean(uint64_t now)
  329. {
  330. {
  331. Mutex::Lock _l1(_peers_m);
  332. Mutex::Lock _l2(_upstreams_m);
  333. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  334. Address *a = (Address *)0;
  335. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  336. while (i.next(a,p)) {
  337. if ( (!(*p)->isAlive(now)) && (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),*a) == _upstreamAddresses.end()) )
  338. _peers.erase(*a);
  339. }
  340. }
  341. {
  342. Mutex::Lock _l(_paths_m);
  343. Hashtable< Path::HashKey,SharedPtr<Path> >::Iterator i(_paths);
  344. Path::HashKey *k = (Path::HashKey *)0;
  345. SharedPtr<Path> *p = (SharedPtr<Path> *)0;
  346. while (i.next(k,p)) {
  347. if (p->reclaimIfWeak())
  348. _paths.erase(*k);
  349. }
  350. }
  351. }
  352. Identity Topology::_getIdentity(const Address &zta)
  353. {
  354. char p[128];
  355. Utils::snprintf(p,sizeof(p),"iddb.d/%.10llx",(unsigned long long)zta.toInt());
  356. std::string ids(RR->node->dataStoreGet(p));
  357. if (ids.length() > 0) {
  358. try {
  359. return Identity(ids);
  360. } catch ( ... ) {} // ignore invalid IDs
  361. }
  362. return Identity();
  363. }
  364. void Topology::_memoizeUpstreams()
  365. {
  366. // assumes _upstreams_m and _peers_m are locked
  367. _upstreamAddresses.clear();
  368. _amRoot = false;
  369. for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) {
  370. if (i->identity == RR->identity) {
  371. _amRoot = true;
  372. } else if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),i->identity.address()) == _upstreamAddresses.end()) {
  373. _upstreamAddresses.push_back(i->identity.address());
  374. SharedPtr<Peer> &hp = _peers[i->identity.address()];
  375. if (!hp) {
  376. hp = new Peer(RR,RR->identity,i->identity);
  377. saveIdentity(i->identity);
  378. }
  379. }
  380. }
  381. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  382. for(std::vector<World::Root>::const_iterator i(m->roots().begin());i!=m->roots().end();++i) {
  383. if (i->identity == RR->identity) {
  384. _amRoot = true;
  385. } else if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),i->identity.address()) == _upstreamAddresses.end()) {
  386. _upstreamAddresses.push_back(i->identity.address());
  387. SharedPtr<Peer> &hp = _peers[i->identity.address()];
  388. if (!hp) {
  389. hp = new Peer(RR,RR->identity,i->identity);
  390. saveIdentity(i->identity);
  391. }
  392. }
  393. }
  394. }
  395. std::sort(_upstreamAddresses.begin(),_upstreamAddresses.end());
  396. _cor.clear();
  397. for(std::vector<Address>::const_iterator a(_upstreamAddresses.begin());a!=_upstreamAddresses.end();++a) {
  398. if (!_cor.addRepresentative(*a))
  399. break;
  400. }
  401. _cor.sign(RR->identity,RR->node->now());
  402. }
  403. } // namespace ZeroTier