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