NetCore.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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* alloc,
  35. struct EventBase* base,
  36. struct Random* rand,
  37. struct Log* log)
  38. {
  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_memcpyConst(myAddress->key, ca->publicKey, 32);
  48. Address_getPrefix(myAddress);
  49. // lower half
  50. struct SwitchCore* switchCore = nc->switchCore = SwitchCore_new(log, alloc, base);
  51. struct SwitchAdapter* switchAdapter = nc->switchAdapter = SwitchAdapter_new(alloc, log);
  52. Iface_plumb(&switchAdapter->switchIf, switchCore->routerIf);
  53. struct ControlHandler* controlHandler = nc->controlHandler =
  54. ControlHandler_new(alloc, log, ee, ca->publicKey);
  55. Iface_plumb(&controlHandler->coreIf, &switchAdapter->controlIf);
  56. struct SwitchPinger* sp = nc->sp = SwitchPinger_new(base, rand, log, myAddress, alloc);
  57. Iface_plumb(&controlHandler->switchPingerIf, &sp->controlHandlerIf);
  58. nc->ifController = InterfaceController_new(ca, switchCore, log, base, sp, rand, alloc, ee);
  59. // session manager
  60. struct SessionManager* sm = nc->sm = SessionManager_new(alloc, base, ca, rand, log, ee);
  61. Iface_plumb(&switchAdapter->sessionManagerIf, &sm->switchIf);
  62. // upper half
  63. struct ConverterV15* v15conv = ConverterV15_new(alloc, log, sm, myAddress->ip6.bytes);
  64. Iface_plumb(&v15conv->sessionManagerIf, &sm->insideIf);
  65. struct UpperDistributor* upper = nc->upper = UpperDistributor_new(alloc, log, ee);
  66. Iface_plumb(&v15conv->upperDistributorIf, &upper->sessionManagerIf);
  67. struct TUNAdapter* tunAdapt = nc->tunAdapt = TUNAdapter_new(alloc, log, myAddress->ip6.bytes);
  68. Iface_plumb(&tunAdapt->upperDistributorIf, &upper->tunAdapterIf);
  69. return nc;
  70. }