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