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