xorp

cli_node.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2011 XORP, Inc and Others
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 // $XORP: xorp/cli/cli_node.hh,v 1.35 2008/10/02 21:56:29 bms Exp $
00021 
00022 
00023 #ifndef __CLI_CLI_NODE_HH__
00024 #define __CLI_CLI_NODE_HH__
00025 
00026 
00027 //
00028 // CLI node definition.
00029 //
00030 
00031 
00032 
00033 
00034 #include "libxorp/xorpfd.hh"
00035 #include "libxorp/eventloop.hh"
00036 #include "libproto/proto_node.hh"
00037 #include "cli_command.hh"
00038 
00039 
00040 //
00041 // Constants definitions
00042 //
00043 
00044 //
00045 // Structures/classes, typedefs and macros
00046 //
00047 
00048 class EventLoop;
00049 class CliClient;
00050 class CliPipe;
00051 class IPvXNet;
00052 
00059 class CliNode : public ProtoNode<Vif> {
00060 public:
00072     CliNode(int init_family, xorp_module_id init_module_id,
00073         EventLoop& init_eventloop);
00074 
00078     virtual ~CliNode();
00079     
00085     int     start();
00086     
00092     int     stop();
00093 
00099     void    enable();
00100     
00107     void    disable();
00108 
00117     void    set_cli_port(unsigned short v) { _cli_port = htons(v); }
00118     
00128     void    add_enable_cli_access_from_subnet(const IPvXNet& subnet_addr);
00129     
00138     int     delete_enable_cli_access_from_subnet(const IPvXNet& subnet_addr);
00139     
00149     void    add_disable_cli_access_from_subnet(const IPvXNet& subnet_addr);
00150     
00159     int     delete_disable_cli_access_from_subnet(const IPvXNet& subnet_addr);
00160     
00166     CliCommand  *cli_command_root() { return (&_cli_command_root); }
00167     
00177     static int  xlog_output(void *obj, xlog_level_t level, const char *msg);
00178     
00186     CliClient   *find_cli_by_term_name(const string& term_name) const;
00187     
00195     CliClient   *find_cli_by_session_id(uint32_t session_id) const;
00196     
00203     list<CliClient *>& client_list() { return (_client_list); }
00204     
00223     int add_cli_command(
00224     // Input values,
00225     const string&   processor_name,
00226     const string&   command_name,
00227     const string&   command_help,
00228     const bool& is_command_cd,
00229     const string&   command_cd_prompt,
00230     const bool& is_command_processor,
00231     // Output values,
00232     string&     error_msg);
00233 
00243     int delete_cli_command(
00244     // Input values,
00245     const string&   processor_name,
00246     const string&   command_name,
00247     // Output values,
00248     string&     error_msg);
00249 
00259     void recv_process_command_output(const string *processor_name,
00260                      const string *cli_term_name,
00261                      const uint32_t *cli_session_id,
00262                      const string *command_output);
00263     
00264     //
00265     // Protocol message and kernel signal send/recv: not used by the CLI.
00266     //
00270     int proto_recv(const string&    , // if_name,
00271            const string&    , // vif_name,
00272            const IPvX&      , // src_address,
00273            const IPvX&      , // dst_address,
00274            uint8_t      , // ip_protocol,
00275            int32_t      , // ip_ttl,
00276            int32_t      , // ip_tos,
00277            bool         , // ip_router_alert,
00278            bool         , // ip_internet_control,
00279            const vector<uint8_t>& , // payload,
00280            string&        // error_msg
00281     ) { assert (false); return (XORP_ERROR); }
00282 
00286     int proto_send(const string&    , // if_name,
00287            const string&    , // vif_name,
00288            const IPvX&      , // src_address,
00289            const IPvX&      , // dst_address,
00290            uint8_t      , // ip_protocol,
00291            int32_t      , // ip_ttl,
00292            int32_t      , // ip_tos,
00293            bool         , // ip_router_alert,
00294            bool         , // ip_internet_control,
00295            const uint8_t*   , // sndbuf,
00296            size_t       , // sndlen,
00297            string&        // error_msg
00298     ) { assert (false); return (XORP_ERROR); }
00299 
00303     int signal_message_recv(const string&   , // src_module_instance_name,
00304                 int         , // message_type,
00305                 uint32_t        , // vif_index,
00306                 const IPvX&     , // src,
00307                 const IPvX&     , // dst,
00308                 const uint8_t * , // rcvbuf,
00309                 size_t        // rcvlen
00310     ) { assert (false); return (XORP_ERROR); }
00314     int signal_message_send(const string&   , // dst_module_instance_name,
00315                 int         , // message_type,
00316                 uint32_t        , // vif_index,
00317                 const IPvX&     , // src,
00318                 const IPvX&     , // dst,
00319                 const uint8_t * , // sndbuf,
00320                 size_t        // sndlen
00321     ) { assert (false); return (XORP_ERROR); }
00322     
00323     typedef XorpCallback6<void,
00324     const string&,      // target
00325     const string&,      // server_name
00326     const string&,      // cli_term_name
00327     uint32_t,       // cli_session_id
00328     const vector<string>&,  // command_global_name
00329     const vector<string>&   // argv
00330     >::RefPtr SenderProcessCallback;
00331     
00337     void set_send_process_command_callback(const SenderProcessCallback& v) {
00338     _send_process_command_callback = v;
00339     }
00340     
00356     CliClient *add_client(XorpFd input_fd, XorpFd output_fd, bool is_network,
00357               const string& startup_cli_prompt, string& error_msg);
00358 
00368     int remove_client(CliClient *cli_client, string& error_msg);
00369 
00370     typedef XorpCallback1<void,
00371     CliClient*      // CLI client to delete
00372     >::RefPtr CliClientDeleteCallback;
00373     
00379     void set_cli_client_delete_callback(const CliClientDeleteCallback& v) {
00380     _cli_client_delete_callback = v;
00381     }
00382 
00383     //
00384     // Debug-related methods
00385     //
00386     
00394     bool    is_log_trace() const { return (_is_log_trace); }
00395     
00403     void    set_log_trace(bool is_enabled) { _is_log_trace = is_enabled; }
00404     
00405 private:
00406     friend class CliClient;
00407     
00408     //
00409     // Internal CLI commands
00410     //
00411     int     add_internal_cli_commands(string& error_msg);
00412     int     cli_show_log(const string& server_name,
00413                  const string& cli_term_name,
00414                  uint32_t cli_session_id,
00415                  const vector<string>& command_global_name,
00416                  const vector<string>& argv);
00417     int     cli_show_log_user(const string& server_name,
00418                   const string& cli_term_name,
00419                   uint32_t cli_session_id,
00420                   const vector<string>& command_global_name,
00421                   const vector<string>& argv);
00422     int     cli_set_log_output_cli(const string& server_name,
00423                        const string& cli_term_name,
00424                        uint32_t cli_session_id,
00425                        const vector<string>& command_global_name,
00426                        const vector<string>& argv);
00427     int     cli_set_log_output_file(const string& server_name,
00428                     const string& cli_term_name,
00429                     uint32_t cli_session_id,
00430                     const vector<string>& command_global_name,
00431                     const vector<string>& argv);
00432     int     cli_set_log_output_remove_cli(const string& server_name,
00433                           const string& cli_term_name,
00434                           uint32_t cli_session_id,
00435                           const vector<string>& command_global_name,
00436                           const vector<string>& argv);
00437     int     cli_set_log_output_remove_file(const string& server_name,
00438                            const string& cli_term_name,
00439                            uint32_t cli_session_id,
00440                            const vector<string>& command_global_name,
00441                            const vector<string>& argv);
00442     
00443     int send_process_command(const string& server_name,
00444                  const string& cli_term_name,
00445                  const uint32_t cli_session_id,
00446                  const vector<string>& command_global_name,
00447                  const vector<string>& argv);
00448     
00449     XorpFd  sock_serv_open();
00450     int     sock_serv_close();
00451     CliClient   *add_connection(XorpFd input_fd, XorpFd output_fd,
00452                 bool is_network,
00453                 const string& startup_cli_prompt,
00454                 string& error_msg);
00455     int     delete_connection(CliClient *cli_client, string& error_msg);
00456     void    accept_connection(XorpFd fd, IoEventType type);
00457     
00458     bool    is_allow_cli_access(const IPvX& ipvx) const;
00459     
00460     XorpFd      _cli_socket;    // The CLI listening socket
00461     unsigned short  _cli_port;  // The CLI port (network-order) to
00462                     //   listen on for new connections.
00463     list<CliClient *>   _client_list;   // The list with the CLI clients
00464     uint32_t        _next_session_id; // Used to assign unique session IDs.
00465     string      _startup_cli_prompt; // The CLI prompt on startup
00466     
00467     CliCommand      _cli_command_root; // The root of the CLI commands
00468     
00469     SenderProcessCallback _send_process_command_callback;
00470     
00471     // The callback pethod that is invoked whenever a CliClient is deleted
00472     CliClientDeleteCallback _cli_client_delete_callback;
00473     
00474     list<IPvXNet>   _enable_cli_access_subnet_list;
00475     list<IPvXNet>   _disable_cli_access_subnet_list;
00476 
00477     //
00478     // Debug and test-related state
00479     //
00480     bool    _is_log_trace;      // If true, enable XLOG_TRACE()
00481 };
00482 
00483 
00484 //
00485 // Global variables
00486 //
00487 
00488 
00489 //
00490 // Global functions prototypes
00491 //
00492 
00493 #endif // __CLI_CLI_NODE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations