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 | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 16 | #include "face/tcp-factory.hpp" |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 17 | |
| 18 | #ifdef HAVE_UNIX_SOCKETS |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 19 | #include "face/unix-stream-factory.hpp" |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 20 | #endif |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 21 | |
| 22 | namespace nfd { |
| 23 | |
| 24 | NFD_LOG_INIT("Main"); |
| 25 | |
| 26 | struct ProgramOptions |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 27 | { |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 28 | struct TcpOutgoing |
| 29 | { |
| 30 | TcpOutgoing(std::pair<std::string, std::string> endpoint) |
| 31 | : m_endpoint(endpoint) |
| 32 | { |
| 33 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 34 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 35 | std::pair<std::string, std::string> m_endpoint; |
| 36 | std::vector<Name> m_prefixes; |
| 37 | }; |
| 38 | |
| 39 | bool m_showUsage; |
| 40 | std::pair<std::string, std::string> m_tcpListen; |
| 41 | std::vector<TcpOutgoing> m_tcpOutgoings; |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 42 | std::string m_unixListen; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 43 | std::string m_config; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 46 | static ProgramOptions g_options; |
| 47 | static Forwarder* g_forwarder; |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 48 | static FibManager* g_fibManager; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 49 | static FaceManager* g_faceManager; |
Alexander Afanasyev | 472acbe | 2014-02-26 19:30:44 -0800 | [diff] [blame] | 50 | static LocalControlHeaderManager* g_localControlHeaderManager; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 51 | static StrategyChoiceManager* g_strategyChoiceManager; |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 52 | static TcpFactory* g_tcpFactory; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 53 | static shared_ptr<TcpChannel> g_tcpChannel; |
| 54 | static shared_ptr<InternalFace> g_internalFace; |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 55 | |
| 56 | #ifdef HAVE_UNIX_SOCKETS |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 57 | static UnixStreamFactory* g_unixFactory; |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 58 | static shared_ptr<UnixStreamChannel> g_unixChannel; |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 59 | #endif |
| 60 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 61 | |
| 62 | void |
| 63 | usage(char* programName) |
| 64 | { |
| 65 | printf( |
| 66 | "%s --help\n\tshow this help and exit\n" |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 67 | "%s [--tcp-listen \"0.0.0.0:6363\"] " |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 68 | #ifdef HAVE_UNIX_SOCKETS |
| 69 | "[--unix-listen \"/var/run/nfd.sock\"] " |
| 70 | #endif |
| 71 | "[--tcp-connect \"192.0.2.1:6363\" " |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 72 | "[--prefix </example>]] " |
| 73 | "[--config /path/to/nfd.conf]\n" |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 74 | "\trun forwarding daemon\n" |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 75 | "\t--tcp-listen <ip:port>: listen on IP and port\n" |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 76 | #ifdef HAVE_UNIX_SOCKETS |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 77 | "\t--unix-listen <unix-socket-path>: listen on Unix socket\n" |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 78 | #endif |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 79 | "\t--tcp-connect <ip:port>: connect to IP and port (can occur multiple times)\n" |
| 80 | "\t--prefix <NDN name>: add this face as nexthop to FIB entry " |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 81 | "(must appear after --tcp-connect, can occur multiple times)\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 82 | "\t--config <configuration file>]: path to configuration file\n" |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 83 | "\n", |
| 84 | programName, programName |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | inline std::pair<std::string, std::string> |
| 89 | parseIpPortPair(const std::string& s) |
| 90 | { |
| 91 | size_t pos = s.rfind(":"); |
| 92 | if (pos == std::string::npos) { |
| 93 | throw std::invalid_argument("ip:port"); |
| 94 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 95 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 96 | return std::make_pair(s.substr(0, pos), s.substr(pos+1)); |
| 97 | } |
| 98 | |
| 99 | bool |
| 100 | parseCommandLine(int argc, char** argv) |
| 101 | { |
| 102 | g_options.m_showUsage = false; |
| 103 | g_options.m_tcpListen = std::make_pair("0.0.0.0", "6363"); |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 104 | g_options.m_unixListen = "/var/run/nfd.sock"; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 105 | g_options.m_tcpOutgoings.clear(); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 106 | g_options.m_config = DEFAULT_CONFIG_FILE; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 107 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 108 | while (1) { |
| 109 | int option_index = 0; |
| 110 | static ::option long_options[] = { |
| 111 | { "help" , no_argument , 0, 0 }, |
| 112 | { "tcp-listen" , required_argument, 0, 0 }, |
| 113 | { "tcp-connect" , required_argument, 0, 0 }, |
| 114 | { "prefix" , required_argument, 0, 0 }, |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 115 | { "unix-listen" , required_argument, 0, 0 }, |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 116 | { "config" , required_argument, 0, 0 }, |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 117 | { 0 , 0 , 0, 0 } |
| 118 | }; |
| 119 | int c = getopt_long_only(argc, argv, "", long_options, &option_index); |
| 120 | if (c == -1) break; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 121 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 122 | switch (c) { |
| 123 | case 0: |
| 124 | switch (option_index) { |
| 125 | case 0://help |
| 126 | g_options.m_showUsage = true; |
| 127 | break; |
| 128 | case 1://tcp-listen |
| 129 | g_options.m_tcpListen = parseIpPortPair(::optarg); |
| 130 | break; |
| 131 | case 2://tcp-connect |
| 132 | g_options.m_tcpOutgoings.push_back(parseIpPortPair(::optarg)); |
| 133 | break; |
| 134 | case 3://prefix |
| 135 | if (g_options.m_tcpOutgoings.empty()) { |
| 136 | return false; |
| 137 | } |
| 138 | g_options.m_tcpOutgoings.back().m_prefixes.push_back(Name(::optarg)); |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 139 | break; |
| 140 | case 4://unix-listen |
| 141 | g_options.m_unixListen = ::optarg; |
| 142 | break; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 143 | case 5://config |
| 144 | g_options.m_config = ::optarg; |
| 145 | break; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 146 | } |
| 147 | break; |
| 148 | } |
| 149 | } |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 150 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | onFaceFail(shared_ptr<Face> face, const std::string& reason) |
| 156 | { |
| 157 | g_forwarder->removeFace(face); |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | onFaceEstablish(shared_ptr<Face> newFace, std::vector<Name>* prefixes) |
| 162 | { |
| 163 | g_forwarder->addFace(newFace); |
| 164 | newFace->onFail += bind(&onFaceFail, newFace, _1); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 165 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 166 | // add nexthop on prefixes |
| 167 | if (prefixes != 0) { |
| 168 | Fib& fib = g_forwarder->getFib(); |
| 169 | for (std::vector<Name>::iterator it = prefixes->begin(); |
| 170 | it != prefixes->end(); ++it) { |
| 171 | std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult = |
| 172 | fib.insert(*it); |
| 173 | shared_ptr<fib::Entry> fibEntry = fibInsertResult.first; |
| 174 | fibEntry->addNextHop(newFace, 0); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | onFaceError(const std::string& reason) |
| 181 | { |
| 182 | throw std::runtime_error(reason); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | initializeTcp() |
| 187 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 188 | g_tcpFactory = new TcpFactory(); |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 189 | g_tcpChannel = g_tcpFactory->createChannel(g_options.m_tcpListen.first, |
| 190 | g_options.m_tcpListen.second); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 191 | g_tcpChannel->listen( |
| 192 | bind(&onFaceEstablish, _1, static_cast<std::vector<Name>*>(0)), |
| 193 | &onFaceError); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 194 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 195 | for (std::vector<ProgramOptions::TcpOutgoing>::iterator it = |
| 196 | g_options.m_tcpOutgoings.begin(); |
| 197 | it != g_options.m_tcpOutgoings.end(); ++it) { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 198 | g_tcpChannel->connect(it->m_endpoint.first, it->m_endpoint.second, |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 199 | bind(&onFaceEstablish, _1, &(it->m_prefixes)), &onFaceError); |
| 200 | } |
| 201 | } |
| 202 | |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 203 | #ifdef HAVE_UNIX_SOCKETS |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 204 | void |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 205 | initializeUnix() |
| 206 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 207 | g_unixFactory = new UnixStreamFactory(); |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 208 | g_unixChannel = g_unixFactory->createChannel(g_options.m_unixListen); |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 209 | |
| 210 | g_unixChannel->listen( |
| 211 | bind(&onFaceEstablish, _1, static_cast<std::vector<Name>*>(0)), |
| 212 | &onFaceError); |
| 213 | } |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 214 | #endif |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 215 | |
| 216 | void |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 217 | initializeMgmt() |
| 218 | { |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 219 | ConfigFile config; |
| 220 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 221 | g_internalFace = make_shared<InternalFace>(); |
| 222 | g_forwarder->addFace(g_internalFace); |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 223 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 224 | g_internalFace->getValidator().setConfigFile(config); |
| 225 | |
| 226 | |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 227 | g_fibManager = new FibManager(g_forwarder->getFib(), |
| 228 | bind(&Forwarder::getFace, g_forwarder, _1), |
| 229 | g_internalFace); |
| 230 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 231 | g_faceManager = new FaceManager(g_forwarder->getFaceTable(), g_internalFace); |
| 232 | |
Alexander Afanasyev | 472acbe | 2014-02-26 19:30:44 -0800 | [diff] [blame] | 233 | g_localControlHeaderManager = |
| 234 | new LocalControlHeaderManager(bind(&Forwarder::getFace, g_forwarder, _1), |
| 235 | g_internalFace); |
| 236 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 237 | g_strategyChoiceManager = new StrategyChoiceManager(g_forwarder->getStrategyChoice(), |
| 238 | g_internalFace); |
| 239 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 240 | /// \todo add face manager section handler |
| 241 | |
| 242 | /// \todo add parsing back when there is an official default config file |
| 243 | // config.parse(g_options.m_config); |
| 244 | |
Alexander Afanasyev | 472acbe | 2014-02-26 19:30:44 -0800 | [diff] [blame] | 245 | shared_ptr<fib::Entry> entry = g_forwarder->getFib().insert("/localhost/nfd").first; |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 246 | entry->addNextHop(g_internalFace, 0); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | int |
| 250 | main(int argc, char** argv) |
| 251 | { |
| 252 | bool isCommandLineValid = parseCommandLine(argc, argv); |
| 253 | if (!isCommandLineValid) { |
| 254 | usage(argv[0]); |
| 255 | return 1; |
| 256 | } |
| 257 | if (g_options.m_showUsage) { |
| 258 | usage(argv[0]); |
| 259 | return 0; |
| 260 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 261 | |
| 262 | g_forwarder = new Forwarder(); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 263 | initializeTcp(); |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 264 | #ifdef HAVE_UNIX_SOCKETS |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 265 | initializeUnix(); |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 266 | #endif |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 267 | initializeMgmt(); |
Alexander Afanasyev | 062dfb4 | 2014-02-16 22:10:53 -0800 | [diff] [blame] | 268 | |
| 269 | /// \todo Add signal processing to gracefully terminate the app |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 270 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 271 | try { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 272 | getGlobalIoService().run(); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 273 | } catch(std::exception& ex) { |
| 274 | NFD_LOG_ERROR(ex.what()); |
| 275 | return 1; |
| 276 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 277 | |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 278 | return 0; |
| 279 | } |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 280 | |
| 281 | } // namespace nfd |
| 282 | |
| 283 | int |
| 284 | main(int argc, char** argv) |
| 285 | { |
| 286 | return nfd::main(argc, argv); |
| 287 | } |