1
0

NetCore.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <http://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/SwitchAdapter.h"
  30. #include "net/ConverterV15.h"
  31. #include "net/UpperDistributor.h"
  32. #include "net/TUNAdapter.h"
  33. struct NetCore* NetCore_new(uint8_t* privateKey,
  34. struct Allocator* allocator,
  35. struct EventBase* base,
  36. struct Random* rand,
  37. struct Log* log)
  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. struct CryptoAuth* ca = nc->ca = CryptoAuth_new(alloc, privateKey, base, log, rand);
  46. struct EventEmitter* ee = nc->ee = EventEmitter_new(alloc, log, ca->publicKey);
  47. struct Address* myAddress = nc->myAddress = Allocator_calloc(alloc, sizeof(struct Address), 1);
  48. Bits_memcpyConst(myAddress->key, ca->publicKey, 32);
  49. Address_getPrefix(myAddress);
  50. // lower half
  51. struct SwitchCore* switchCore = nc->switchCore = SwitchCore_new(log, alloc, base);
  52. struct SwitchAdapter* switchAdapter = nc->switchAdapter = SwitchAdapter_new(alloc, log);
  53. Iface_plumb(&switchAdapter->switchIf, switchCore->routerIf);
  54. struct ControlHandler* controlHandler = nc->controlHandler =
  55. ControlHandler_new(alloc, log, ee, ca->publicKey);
  56. Iface_plumb(&controlHandler->coreIf, &switchAdapter->controlIf);
  57. struct SwitchPinger* sp = nc->sp = SwitchPinger_new(base, rand, log, myAddress, alloc);
  58. Iface_plumb(&controlHandler->switchPingerIf, &sp->controlHandlerIf);
  59. nc->ifController = InterfaceController_new(ca, switchCore, log, base, sp, rand, alloc, ee);
  60. // session manager
  61. struct SessionManager* sm = nc->sm = SessionManager_new(alloc, base, ca, rand, log, ee);
  62. Iface_plumb(&switchAdapter->sessionManagerIf, &sm->switchIf);
  63. // upper half
  64. struct ConverterV15* v15conv = ConverterV15_new(alloc, log, sm, myAddress->ip6.bytes);
  65. Iface_plumb(&v15conv->sessionManagerIf, &sm->insideIf);
  66. struct UpperDistributor* upper = nc->upper = UpperDistributor_new(alloc, log, ee);
  67. Iface_plumb(&v15conv->upperDistributorIf, &upper->sessionManagerIf);
  68. struct TUNAdapter* tunAdapt = nc->tunAdapt = TUNAdapter_new(alloc, log, myAddress->ip6.bytes);
  69. Iface_plumb(&tunAdapt->upperDistributorIf, &upper->tunAdapterIf);
  70. return nc;
  71. }