NetCore.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/Ca.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. EventBase_t* base,
  35. struct Random* rand,
  36. struct Log* log,
  37. bool enableNoise)
  38. {
  39. struct Allocator* alloc = Allocator_child(allocator);
  40. struct NetCore* nc = Allocator_calloc(alloc, sizeof(struct NetCore), 1);
  41. nc->alloc = alloc;
  42. nc->base = base;
  43. nc->rand = rand;
  44. nc->log = log;
  45. Ca_t* ca = nc->ca = Ca_new(alloc, privateKey, base, log, rand);
  46. uint8_t ourPubKey[32];
  47. Ca_getPubKey(ca, ourPubKey);
  48. struct EventEmitter* ee = nc->ee = EventEmitter_new(alloc, log, ourPubKey);
  49. struct Address* myAddress = nc->myAddress = Allocator_calloc(alloc, sizeof(struct Address), 1);
  50. Bits_memcpy(myAddress->key, ourPubKey, 32);
  51. Address_getPrefix(myAddress);
  52. myAddress->protocolVersion = Version_CURRENT_PROTOCOL;
  53. myAddress->path = 1;
  54. struct SwitchCore* switchCore = nc->switchCore = SwitchCore_new(log, alloc, base);
  55. struct SessionManager* sm = nc->sm = SessionManager_new(alloc, base, ca, rand, log, ee);
  56. Iface_plumb(switchCore->routerIf, &sm->switchIf);
  57. struct UpperDistributor* upper = nc->upper = UpperDistributor_new(alloc, log, ee, myAddress);
  58. Iface_plumb(&sm->insideIf, &upper->sessionManagerIf);
  59. struct SwitchPinger* sp = nc->sp = SwitchPinger_new(base, rand, log, myAddress, alloc);
  60. struct InterfaceController* ifc = nc->ifController =
  61. InterfaceController_new(
  62. ca, switchCore, log, base, sp, rand, alloc, ee, enableNoise);
  63. struct ControlHandler* controlHandler = nc->controlHandler =
  64. ControlHandler_new(alloc, log, ee, ourPubKey, ifc);
  65. Iface_plumb(&controlHandler->coreIf, &upper->controlHandlerIf);
  66. Iface_plumb(&controlHandler->switchPingerIf, &sp->controlHandlerIf);
  67. struct TUNAdapter* tunAdapt = nc->tunAdapt = TUNAdapter_new(alloc, log, myAddress->ip6.bytes);
  68. Iface_plumb(&tunAdapt->upperDistributorIf, &upper->tunAdapterIf);
  69. return nc;
  70. }