Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 7 | #include <getopt.h> |
| 8 | #include "core/logger.hpp" |
| 9 | #include "fw/forwarder.hpp" |
| 10 | #include "mgmt/internal-face.hpp" |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 11 | #include "mgmt/fib-manager.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 12 | #include "mgmt/face-manager.hpp" |
Alexander Afanasyev | 472acbe | 2014-02-26 19:30:44 -0800 | [diff] [blame] | 13 | #include "mgmt/local-control-header-manager.hpp" |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 14 | #include "mgmt/strategy-choice-manager.hpp" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 15 | #include "mgmt/config-file.hpp" |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 16 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 17 | #include <boost/filesystem.hpp> |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 18 | |
| 19 | namespace nfd { |
| 20 | |
| 21 | NFD_LOG_INIT("Main"); |
| 22 | |
| 23 | struct ProgramOptions |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 24 | { |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 25 | bool m_showUsage; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 26 | std::string m_config; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 29 | static ProgramOptions g_options; |
| 30 | static Forwarder* g_forwarder; |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 31 | static FibManager* g_fibManager; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 32 | static FaceManager* g_faceManager; |
Alexander Afanasyev | 472acbe | 2014-02-26 19:30:44 -0800 | [diff] [blame] | 33 | static LocalControlHeaderManager* g_localControlHeaderManager; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 34 | static StrategyChoiceManager* g_strategyChoiceManager; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 35 | static shared_ptr<InternalFace> g_internalFace; |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 36 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 37 | |
| 38 | void |
| 39 | usage(char* programName) |
| 40 | { |
| 41 | printf( |
| 42 | "%s --help\n\tshow this help and exit\n" |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 43 | "%s " |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 44 | "[--config /path/to/nfd.conf]\n" |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 45 | "\trun forwarding daemon\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 46 | "\t--config <configuration file>]: path to configuration file\n" |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 47 | "\n", |
| 48 | programName, programName |
| 49 | ); |
| 50 | } |
| 51 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 52 | bool |
| 53 | parseCommandLine(int argc, char** argv) |
| 54 | { |
| 55 | g_options.m_showUsage = false; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 56 | g_options.m_config = DEFAULT_CONFIG_FILE; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 57 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 58 | while (1) { |
| 59 | int option_index = 0; |
| 60 | static ::option long_options[] = { |
| 61 | { "help" , no_argument , 0, 0 }, |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 62 | { "config" , required_argument, 0, 0 }, |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 63 | { 0 , 0 , 0, 0 } |
| 64 | }; |
| 65 | int c = getopt_long_only(argc, argv, "", long_options, &option_index); |
| 66 | if (c == -1) break; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 67 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 68 | switch (c) { |
| 69 | case 0: |
| 70 | switch (option_index) { |
| 71 | case 0://help |
| 72 | g_options.m_showUsage = true; |
| 73 | break; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 74 | case 1://config |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 75 | g_options.m_config = ::optarg; |
| 76 | break; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 77 | } |
| 78 | break; |
| 79 | } |
| 80 | } |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 81 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
| 85 | void |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 86 | initializeMgmt() |
| 87 | { |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 88 | ConfigFile config; |
| 89 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 90 | g_internalFace = make_shared<InternalFace>(); |
| 91 | g_forwarder->addFace(g_internalFace); |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 92 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 93 | g_internalFace->getValidator().setConfigFile(config); |
| 94 | |
| 95 | |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 96 | g_fibManager = new FibManager(g_forwarder->getFib(), |
| 97 | bind(&Forwarder::getFace, g_forwarder, _1), |
| 98 | g_internalFace); |
| 99 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 100 | g_faceManager = new FaceManager(g_forwarder->getFaceTable(), g_internalFace); |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 101 | g_faceManager->setConfigFile(config); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 102 | |
Alexander Afanasyev | 472acbe | 2014-02-26 19:30:44 -0800 | [diff] [blame] | 103 | g_localControlHeaderManager = |
| 104 | new LocalControlHeaderManager(bind(&Forwarder::getFace, g_forwarder, _1), |
| 105 | g_internalFace); |
| 106 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 107 | g_strategyChoiceManager = new StrategyChoiceManager(g_forwarder->getStrategyChoice(), |
| 108 | g_internalFace); |
| 109 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 110 | config.parse(g_options.m_config, true); |
| 111 | config.parse(g_options.m_config, false); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 112 | |
Alexander Afanasyev | 472acbe | 2014-02-26 19:30:44 -0800 | [diff] [blame] | 113 | shared_ptr<fib::Entry> entry = g_forwarder->getFib().insert("/localhost/nfd").first; |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 114 | entry->addNextHop(g_internalFace, 0); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | int |
| 118 | main(int argc, char** argv) |
| 119 | { |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 120 | try { |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 121 | bool isCommandLineValid = parseCommandLine(argc, argv); |
| 122 | if (!isCommandLineValid) { |
| 123 | usage(argv[0]); |
| 124 | return 1; |
| 125 | } |
| 126 | if (g_options.m_showUsage) { |
| 127 | usage(argv[0]); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | g_forwarder = new Forwarder(); |
| 132 | initializeMgmt(); |
| 133 | |
| 134 | /// \todo Add signal processing to gracefully terminate the app |
| 135 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 136 | getGlobalIoService().run(); |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 137 | // } catch(ConfigFile::Error& error) { |
| 138 | // NFD_LOG_ERROR("Error: " << error.what()); |
| 139 | // NFD_LOG_ERROR("You should either specify --config option or copy sample configuration into " |
| 140 | // << DEFAULT_CONFIG_FILE); |
| 141 | } catch(boost::filesystem::filesystem_error& error) { |
| 142 | if (error.code() == boost::system::errc::permission_denied) { |
| 143 | NFD_LOG_ERROR("Error: Permissions denied for " << error.path1()); |
| 144 | NFD_LOG_ERROR(argv[0] << " should be run as superuser"); |
| 145 | } else { |
| 146 | NFD_LOG_ERROR("Error: " << error.what()); |
| 147 | } |
| 148 | } catch(std::exception& exception) { |
| 149 | NFD_LOG_ERROR("Error: " << exception.what()); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 150 | return 1; |
| 151 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 152 | |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 153 | return 0; |
| 154 | } |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 155 | |
| 156 | } // namespace nfd |
| 157 | |
| 158 | int |
| 159 | main(int argc, char** argv) |
| 160 | { |
| 161 | return nfd::main(argc, argv); |
| 162 | } |