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