1
0

NetCore.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "crypto/CryptoAuth.h"
  17. #include "memory/Allocator.h"
  18. #include "switch/SwitchCore.h"
  19. #include "net/NetCore.h"
  20. #include "util/log/Log.h"
  21. #include "util/events/EventBase.h"
  22. #include "net/SwitchPinger.h"
  23. #include "net/ControlHandler.h"
  24. #include "net/InterfaceController.h"
  25. #include "interface/Iface.h"
  26. #include "tunnel/IpTunnel.h"
  27. #include "net/EventEmitter.h"
  28. #include "net/SessionManager.h"
  29. #include "net/UpperDistributor.h"
  30. #include "net/TUNAdapter.h"
  31. #include "util/version/Version.h"
  32. struct NetCore* NetCore_new(uint8_t* privateKey,
  33. struct Allocator* allocator,
  34. struct EventBase* base,
  35. struct Random* rand,
  36. struct Log* log)
  37. {
  38. struct Allocator* alloc = Allocator_child(allocator);
  39. struct NetCore* nc = Allocator_calloc(alloc, sizeof(struct NetCore), 1);
  40. nc->alloc = alloc;
  41. nc->base = base;
  42. nc->rand = rand;
  43. nc->log = log;
  44. struct CryptoAuth* ca = nc->ca = CryptoAuth_new(alloc, privateKey, base, log, rand);
  45. struct EventEmitter* ee = nc->ee = EventEmitter_new(alloc, log, ca->publicKey);
  46. struct Address* myAddress = nc->myAddress = Allocator_calloc(alloc, sizeof(struct Address), 1);
  47. Bits_memcpy(myAddress->key, ca->publicKey, 32);
  48. Address_getPrefix(myAddress);
  49. myAddress->protocolVersion = Version_CURRENT_PROTOCOL;
  50. myAddress->path = 1;
  51. struct SwitchCore* switchCore = nc->switchCore = SwitchCore_new(log, alloc, base);
  52. struct SessionManager* sm = nc->sm = SessionManager_new(alloc, base, ca, rand, log, ee);
  53. Iface_plumb(switchCore->routerIf, &sm->switchIf);
  54. struct UpperDistributor* upper = nc->upper = UpperDistributor_new(alloc, log, ee, myAddress);
  55. Iface_plumb(&sm->insideIf, &upper->sessionManagerIf);
  56. struct ControlHandler* controlHandler = nc->controlHandler =
  57. ControlHandler_new(alloc, log, ee, ca->publicKey);
  58. Iface_plumb(&controlHandler->coreIf, &upper->controlHandlerIf);
  59. struct SwitchPinger* sp = nc->sp = SwitchPinger_new(base, rand, log, myAddress, alloc);
  60. Iface_plumb(&controlHandler->switchPingerIf, &sp->controlHandlerIf);
  61. nc->ifController = InterfaceController_new(ca, switchCore, log, base, sp, rand, alloc, ee);
  62. struct TUNAdapter* tunAdapt = nc->tunAdapt = TUNAdapter_new(alloc, log, myAddress->ip6.bytes);
  63. Iface_plumb(&tunAdapt->upperDistributorIf, &upper->tunAdapterIf);
  64. return nc;
  65. }