Changeset 7515d00 in nscp
- Timestamp:
- 09/11/11 21:52:15 (20 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 76d1076
- Parents:
- a78a985
- Files:
-
- 9 added
- 31 edited
-
CMakeLists.txt (modified) (1 diff)
-
build.cmake (modified) (1 diff)
-
build/cmake/FindZeroMQ.cmake (added)
-
changelog (modified) (1 diff)
-
include/client/command_line_parser.cpp (added)
-
include/client/command_line_parser.hpp (added)
-
include/net/net.hpp (modified) (3 diffs)
-
include/nscapi/targets.cpp (added)
-
include/nscapi/targets.hpp (added)
-
include/nscp/client/socket.hpp (modified) (2 diffs)
-
include/nscp/handler.cpp (added)
-
include/nscp/handler.hpp (added)
-
include/nscp/packet.hpp (modified) (11 diffs)
-
include/nscp/server/connection.cpp (modified) (3 diffs)
-
include/nscp/server/connection.hpp (modified) (3 diffs)
-
include/nscp/server/handler.hpp (modified) (1 diff)
-
include/nscp/server/parser.hpp (modified) (1 diff)
-
include/nscp/server/server.hpp (modified) (3 diffs)
-
include/nscp/server/ssl_connection.cpp (modified) (1 diff)
-
include/nscp/server/ssl_connection.hpp (modified) (1 diff)
-
include/nscp/server/tcp_connection.cpp (modified) (1 diff)
-
include/nscp/server/tcp_connection.hpp (modified) (1 diff)
-
include/settings/Settings.h (modified) (2 diffs)
-
include/settings/client/settings_client.hpp (modified) (1 diff)
-
include/settings/client/settings_proxy.hpp (modified) (1 diff)
-
include/settings/client/targets.hpp (added)
-
include/strEx.h (modified) (4 diffs)
-
include/zmsg.hpp (added)
-
libs/plugin_api/CMakeLists.txt (modified) (2 diffs)
-
libs/protobuf/ipc.proto (modified) (1 diff)
-
modules/NSCPClient/CMakeLists.txt (modified) (2 diffs)
-
modules/NSCPClient/NSCPClient.cpp (modified) (10 diffs)
-
modules/NSCPClient/NSCPClient.h (modified) (3 diffs)
-
modules/NSCPServer/CMakeLists.txt (modified) (2 diffs)
-
modules/NSCPServer/NSCPServer.cpp (modified) (1 diff)
-
modules/NSCPServer/handler_impl.cpp (modified) (1 diff)
-
modules/NSCPServer/handler_impl.hpp (modified) (2 diffs)
-
service/CMakeLists.txt (modified) (1 diff)
-
version.hpp (modified) (1 diff)
-
version.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
CMakeLists.txt
rafd42f1 r7515d00 99 99 MESSAGE(STATUS "Found python (lib) in: ${PYTHON_LIBRARY}") 100 100 ENDIF (PYTHONLIBSEX_FOUND) 101 102 103 FIND_PACKAGE(ZeroMQ) 104 IF (ZEROMQ_FOUND) 105 MESSAGE(STATUS "Found zeromq (lib) in: ${ZEROMQ_LIBRARY}") 106 ENDIF (ZEROMQ_FOUND) 101 107 102 108 -
build.cmake
r04ef932 r7515d00 25 25 26 26 SET(INC_OPENSSL_INCLUDEDIR "${INC_NSCP_INCLUDEDIR}") 27 28 SET(ZEROMQ_INCLUDE_DIR "D:/source/libraries/zeromq-2.1.9/include") 27 29 28 30 SET(INC_PROTOBUF_DIR "D:/source/libraries/protobuf-2.4.0a") -
changelog
ra78a985 r7515d00 6 6 * Fix RtlStringFromGUID problem on NT4 7 7 8 9 2011-09-11 MickeM 10 * Massive overhaul here and there 11 * Added new helper for handling "targets" (so they are the same) 12 * Extracted "command line handling" so all clients will work the same 13 * Extracted command processor to a common class to make all clients work the same 14 * Added initial zeromq stuff (nothing usable) 15 * Added an implementation layer for NSCP protocol parsing (so zeromq stuff can reuse it) 16 8 17 2011-09-05 MickeM 9 18 * Cleaned up some dependencies (on config.h) -
include/net/net.hpp
rb7d17f8 r7515d00 9 9 std::string path; 10 10 std::string query; 11 unsigned int port; 11 12 std::string to_string() { 12 return protocol + "://" + host + "/" + path; 13 std::stringstream ss; 14 ss << protocol << "://" << host << ":" << port << path; 15 return ss.str(); 13 16 } 14 17 }; … … 18 21 std::wstring path; 19 22 std::wstring query; 23 unsigned int port; 20 24 std::wstring to_string() { 21 return protocol + _T("://") + host + _T("/") + path; 25 std::wstringstream ss; 26 ss << protocol << _T("://") << host << _T(":") << port << path; 27 return ss.str(); 22 28 } 23 29 inline url get_url() { … … 27 33 r.path = utf8::cvt<std::string>(path); 28 34 r.query = utf8::cvt<std::string>(query); 35 r.port = port; 29 36 return r; 30 37 } 31 38 32 39 }; 33 inline wurl parse(const std::wstring& url_s ) {40 inline wurl parse(const std::wstring& url_s, unsigned int default_port = 80) { 34 41 wurl ret; 35 42 const std::wstring prot_end(_T("://")); 36 43 std::wstring::const_iterator prot_i = std::search(url_s.begin(), url_s.end(), prot_end.begin(), prot_end.end()); 37 ret.protocol.reserve(std::distance(url_s.begin(), prot_i)); 38 std::transform(url_s.begin(), prot_i, std::back_inserter(ret.protocol), std::ptr_fun<int,int>(std::tolower)); // protocol is icase 39 if( prot_i == url_s.end() ) 40 return ret; 41 std::advance(prot_i, prot_end.length()); 42 std::wstring::const_iterator path_i = std::find(prot_i, url_s.end(), L'/'); 44 if (prot_i != url_s.end()) { 45 ret.protocol.reserve(std::distance(url_s.begin(), prot_i)); 46 std::transform(url_s.begin(), prot_i, std::back_inserter(ret.protocol), std::ptr_fun<int,int>(std::tolower)); // protocol is icase 47 std::advance(prot_i, prot_end.length()); 48 } else { 49 ret.protocol = _T(""); 50 prot_i = url_s.begin(); 51 } 52 std::wstring k(_T("/:")); 53 std::wstring::const_iterator path_i = std::find_first_of(prot_i, url_s.end(), k.begin(), k.end()); 43 54 ret.host.reserve(std::distance(prot_i, path_i)); 44 55 std::transform(prot_i, path_i, std::back_inserter(ret.host), std::ptr_fun<int,int>(std::tolower)); // host is icase 56 if ((path_i != url_s.end()) && (*path_i == L':')) { 57 std::wstring::const_iterator port_b = path_i; ++port_b; 58 path_i = std::find(path_i, url_s.end(), L'/'); 59 ret.port = boost::lexical_cast<unsigned int>(std::wstring(port_b, path_i)); 60 } else { 61 ret.port = default_port; 62 } 45 63 std::wstring::const_iterator query_i = std::find(path_i, url_s.end(), L'?'); 46 64 ret.path.assign(path_i, query_i); -
include/nscp/client/socket.hpp
r8840f09 r7515d00 63 63 BOOST_FOREACH(const nscp::packet &chunk, chunks) { 64 64 std::wcout << _T(">>>") << chunk.signature.to_wstring() << std::endl; 65 if (!writer->write_and_wait(*socket_, get_socket(), boost::asio::buffer(chunk. to_buffer()))) {65 if (!writer->write_and_wait(*socket_, get_socket(), boost::asio::buffer(chunk.write_string()))) { 66 66 std::cout << "FaILED TO SEND DATA..." << std::endl; 67 67 return; … … 140 140 BOOST_FOREACH(const nscp::packet &chunk, chunks) { 141 141 std::wcout << _T(">>>") << chunk.signature.to_wstring() << std::endl; 142 if (!writer->write_and_wait(*ssl_socket_, get_socket(), boost::asio::buffer(chunk. to_buffer()))) {142 if (!writer->write_and_wait(*ssl_socket_, get_socket(), boost::asio::buffer(chunk.write_string()))) { 143 143 std::cout << "FaILED TO SEND DATA..." << std::endl; 144 144 return; -
include/nscp/packet.hpp
r60e6c18 r7515d00 49 49 static const short exec_response = 21; 50 50 51 static const int nscp_magic_number = 12345; 51 52 static const short error = 100; 52 53 … … 57 58 58 59 int16_t header_type; 59 u _int32_theader_length;60 unsigned long long header_length; 60 61 61 62 int16_t payload_type; 62 u _int32_tpayload_length;63 unsigned long long payload_length; 63 64 64 65 u_int32_t additional_packet_count; 65 66 signature_packet() {} 66 u_int32_t magic_number; 67 68 signature_packet() : magic_number(nscp_magic_number) {} 67 69 signature_packet(const signature_packet &other) 68 70 : version(other.version) … … 71 73 , payload_type(other.payload_type) 72 74 , payload_length(other.payload_length) 75 , magic_number(other.magic_number) 73 76 , additional_packet_count(other.additional_packet_count) 74 77 {} … … 80 83 payload_length = other.payload_length; 81 84 additional_packet_count = other.additional_packet_count; 82 85 magic_number = other.magic_number; 83 86 return *this; 87 } 88 89 bool validate() const { 90 return magic_number == nscp_magic_number; 84 91 } 85 92 … … 87 94 std::wstringstream ss; 88 95 ss << _T("version: ") << version 96 << _T(", magic: ") << magic_number 89 97 << _T(", header: ") << header_type 90 98 << _T(", ") << header_length … … 92 100 << _T(", ") << payload_length 93 101 << _T(", count: ") << additional_packet_count ; 102 return ss.str(); 103 } 104 std::string to_string() const { 105 std::stringstream ss; 106 ss << "version: " << version 107 << ", magic: " << magic_number 108 << ", header: " << header_type 109 << ", " << header_length 110 << ", payload: " << payload_type 111 << ", " << payload_length 112 << ", count: " << additional_packet_count ; 94 113 return ss.str(); 95 114 } … … 137 156 } 138 157 139 std::string to_buffer() const { 140 std::string ret = write_signature(); 158 ////////////////////////////////////////////////////////////////////////// 159 // Write to string 160 std::string write_string() const { 161 std::string ret; 162 write_signature(ret); 163 write_header(ret); 164 write_payload(ret); 165 return ret; 166 } 167 void write_signature(std::string &buffer) const { 168 // @todo: Optimize this away once this is working 169 char * tmpbuffer = new char[length::get_signature_size()+1]; 170 nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(tmpbuffer); 171 *tmp = signature; 172 buffer.append(tmpbuffer, length::get_signature_size()); 173 delete [] tmpbuffer; 174 } 175 inline void write_header(std::string &buffer) const { 141 176 if (!header.empty()) 142 ret.insert(ret.end(), header.begin(), header.end()); 177 buffer.insert(buffer.end(), header.begin(), header.end()); 178 } 179 inline void write_payload(std::string &buffer) const { 143 180 if (!payload.empty()) 144 ret.insert(ret.end(), payload.begin(), payload.end()); 145 return ret; 146 } 181 buffer.insert(buffer.end(), payload.begin(), payload.end()); 182 } 183 184 ////////////////////////////////////////////////////////////////////////// 185 // Read from vector (string?) 147 186 void read_signature(std::vector<char> &buf) { 148 assert(buf.size() >= sizeof(nscp::data::signature_packet)); 187 assert(buf.size() >= nscp::length::get_signature_size()); 188 // @todo: Optimize this away once this is working 149 189 nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(&(*buf.begin())); 150 190 signature = *tmp; … … 152 192 signature.payload_length = tmp->payload_length; 153 193 } 194 void read_signature(std::string::iterator begin, std::string::iterator end) { 195 assert(end-begin >= nscp::length::get_signature_size()); 196 // @todo: Optimize this away once this is working 197 nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(&(*begin)); 198 signature = *tmp; 199 signature.payload_type = tmp->payload_type; 200 signature.payload_length = tmp->payload_length; 201 } 202 void nibble_signature(std::string &buf) { 203 assert(buf.size() >= nscp::length::get_signature_size()); 204 // @todo: Optimize this away once this is working 205 nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(&(*buf.begin())); 206 signature = *tmp; 207 signature.payload_type = tmp->payload_type; 208 signature.payload_length = tmp->payload_length; 209 buf.erase(buf.begin(), buf.begin()+nscp::length::get_signature_size()); 210 } 211 void read_header(std::vector<char> &buf) { 212 header = std::string(buf.begin(), buf.end()); 213 } 214 void read_header(std::string::iterator begin, std::string::iterator end) { 215 header = std::string(begin, end); 216 } 217 void nibble_header(std::string &buf) { 218 assert(buf.size() >= nscp::length::get_header_size(signature)); 219 header = std::string(buf.begin(), buf.begin()+nscp::length::get_header_size(signature)); 220 buf.erase(buf.begin(), buf.begin()+nscp::length::get_header_size(signature)); 221 } 154 222 void read_payload(std::vector<char> &buf) { 155 223 payload = std::string(buf.begin(), buf.end()); 156 224 } 157 std::string write_signature() const { 158 char * buffer = new char[sizeof(nscp::data::signature_packet)+1]; 159 nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(buffer); 160 *tmp = signature; 161 std::string str_buf(buffer, sizeof(nscp::data::signature_packet)); 162 delete [] buffer; 163 return str_buf; 164 } 165 166 static packet build_envelope_request(unsigned long additionl_packets) { 225 void read_payload(std::string::iterator begin, std::string::iterator end) { 226 payload = std::string(begin, end); 227 } 228 void nibble_payload(std::string &buf) { 229 assert(buf.size() >= nscp::length::get_payload_size(signature)); 230 payload = std::string(buf.begin(), buf.begin()+nscp::length::get_payload_size(signature)); 231 buf.erase(buf.begin(), buf.begin()+nscp::length::get_payload_size(signature)); 232 } 233 void read_all(std::string &buffer) { 234 std::string::iterator begin = buffer.begin(); 235 std::string::iterator end = begin+length::get_signature_size(); 236 read_signature(begin, end); 237 begin = end; 238 end = begin+length::get_header_size(signature); 239 read_header(begin, end); 240 begin = end; 241 end = begin+length::get_payload_size(signature); 242 read_payload(begin, end); 243 } 244 245 std::wstring to_wstring() const { 246 return signature.to_wstring(); 247 } 248 std::string to_string() const { 249 return signature.to_string(); 250 } 251 252 }; 253 254 struct factory { 255 256 257 static nscp::data::signature_packet create_simple_sig(int payload_type, std::string::size_type size) { 258 nscp::data::signature_packet signature; 259 signature.header_length = 0; 260 signature.header_type = 0; 261 262 signature.additional_packet_count = 0; 263 signature.version = nscp::data::version_1; 264 265 signature.payload_length = size; 266 signature.payload_type = payload_type; 267 return signature; 268 269 } 270 static packet create_payload(unsigned long payload_type, std::string buffer, unsigned long additional_packets = 0) { 271 nscp::data::signature_packet signature; 272 signature.header_length = 0; 273 signature.header_type = 0; 274 275 signature.additional_packet_count = additional_packets; 276 signature.version = nscp::data::version_1; 277 278 signature.payload_length = buffer.size(); 279 signature.payload_type = payload_type; 280 281 return packet(signature, "", buffer); 282 } 283 284 static packet create_query_response(std::string buffer) { 285 return create_payload(nscp::data::command_response, buffer); 286 } 287 288 static packet create_envelope_request(unsigned long additionl_packets) { 167 289 nscp::data::signature_packet signature; 168 290 signature.header_length = 0; … … 184 306 } 185 307 186 static packet build_envelope_response(unsigned long additionl_packets) {308 static packet create_envelope_response(unsigned long additionl_packets) { 187 309 nscp::data::signature_packet signature; 188 310 signature.header_length = 0; … … 204 326 } 205 327 206 static nscp::data::signature_packet create_simple_sig(int payload_type, std::string::size_type size) { 207 nscp::data::signature_packet signature; 208 signature.header_length = 0; 209 signature.header_type = 0; 210 211 signature.additional_packet_count = 0; 212 signature.version = nscp::data::version_1; 213 214 signature.payload_length = size; 215 signature.payload_type = payload_type; 216 return signature; 217 218 } 219 static packet create_payload(unsigned long payload_type, std::string buffer, unsigned long additional_packets = 0) { 220 nscp::data::signature_packet signature; 221 signature.header_length = 0; 222 signature.header_type = 0; 223 224 signature.additional_packet_count = additional_packets; 225 signature.version = nscp::data::version_1; 226 227 signature.payload_length = buffer.size(); 228 signature.payload_type = payload_type; 229 230 return packet(signature, "", buffer); 231 } 232 std::wstring to_wstring() { 233 return signature.to_wstring(); 234 } 235 static nscp::packet create_error(std::wstring msg) { 328 static packet create_error(std::wstring msg) { 236 329 nscp::data::signature_packet signature; 237 330 signature.header_length = 0; … … 253 346 return packet(signature, "", buffer); 254 347 } 255 bool is_envelope_request() {256 return signature.payload_type == nscp::data::envelope_request; 257 }258 bool is_envelope_response() {259 return signature.payload_type == nscp::data::envelope_response;260 } 261 bool is_query_request() {262 return signature.payload_type == nscp::data::command_request;263 } 264 bool is_query_response() {265 return signature.payload_type == nscp::data::command_response;266 } 267 bool is_exec_request() {268 return signature.payload_type == nscp::data::exec_request;269 } 270 bool is_exec_response() {271 return signature.payload_type == nscp::data::exec_response;272 } 273 bool is_submit_message() {274 return signature.payload_type == nscp::data::command_response;275 } 276 bool is_error() {277 return signature.payload_type == nscp::data::error;278 } 279 static nscp::packet create_query_response(std::string buffer) {280 return create_payload(nscp::data::command_response, buffer);348 }; 349 350 struct checks { 351 static bool is_envelope_request(const nscp::packet &packet) { 352 return packet.signature.payload_type == nscp::data::envelope_request; 353 } 354 static bool is_envelope_response(const nscp::packet &packet) { 355 return packet.signature.payload_type == nscp::data::envelope_response; 356 } 357 static bool is_query_request(const nscp::packet &packet) { 358 return packet.signature.payload_type == nscp::data::command_request; 359 } 360 static bool is_query_response(const nscp::packet &packet) { 361 return packet.signature.payload_type == nscp::data::command_response; 362 } 363 static bool is_exec_request(const nscp::packet &packet) { 364 return packet.signature.payload_type == nscp::data::exec_request; 365 } 366 static bool is_exec_response(const nscp::packet &packet) { 367 return packet.signature.payload_type == nscp::data::exec_response; 368 } 369 static bool is_submit_message(const nscp::packet &packet) { 370 return packet.signature.payload_type == nscp::data::command_response; 371 } 372 static bool is_error(const nscp::packet &packet) { 373 return packet.signature.payload_type == nscp::data::error; 281 374 } 282 375 }; -
include/nscp/server/connection.cpp
r8840f09 r7515d00 15 15 namespace server { 16 16 17 connection::connection(boost::asio::io_service& io_service, boost::shared_ptr<nscp::server:: handler> handler)17 connection::connection(boost::asio::io_service& io_service, boost::shared_ptr<nscp::server::server_handler> handler) 18 18 : strand_(io_service) 19 19 , handler_(handler) … … 24 24 connection::~connection() {} 25 25 26 connection* factories::create(boost::asio::io_service& io_service, boost::asio::ssl::context &context, boost::shared_ptr<nscp::server:: handler> handler, bool use_ssl) {26 connection* factories::create(boost::asio::io_service& io_service, boost::asio::ssl::context &context, boost::shared_ptr<nscp::server::server_handler> handler, bool use_ssl) { 27 27 if (use_ssl) 28 28 return create_ssl(io_service, context, handler); 29 29 return create_tcp(io_service, handler); 30 30 } 31 connection* factories::create_tcp(boost::asio::io_service& io_service, boost::shared_ptr<nscp::server:: handler> handler) {31 connection* factories::create_tcp(boost::asio::io_service& io_service, boost::shared_ptr<nscp::server::server_handler> handler) { 32 32 return new tcp_connection(io_service, handler); 33 33 } 34 connection* factories::create_ssl(boost::asio::io_service& io_service, boost::asio::ssl::context &context, boost::shared_ptr<nscp::server:: handler> handler) {34 connection* factories::create_ssl(boost::asio::io_service& io_service, boost::asio::ssl::context &context, boost::shared_ptr<nscp::server::server_handler> handler) { 35 35 return new ssl_connection(io_service, context, handler); 36 36 } … … 88 88 } else { 89 89 unsigned int count = outbound_queue_.size(); 90 outbound_queue_.push_front(nscp:: packet::build_envelope_response(count));90 outbound_queue_.push_front(nscp::factory::create_envelope_response(count)); 91 91 BOOST_FOREACH(nscp::packet &chunk, outbound_queue_) { 92 92 chunk.signature.additional_packet_count = count--; 93 std::string s = chunk. to_buffer();93 std::string s = chunk.write_string(); 94 94 handler_->log_debug(__FILE__, __LINE__, _T(">>>") + chunk.signature.to_wstring()); 95 95 response_buffers_.push_back(buf(s)); -
include/nscp/server/connection.hpp
rb38e845 r7515d00 32 32 33 33 /// Construct a connection with the given io_service. 34 explicit connection(boost::asio::io_service& io_service, boost::shared_ptr<nscp::server:: handler> handler);34 explicit connection(boost::asio::io_service& io_service, boost::shared_ptr<nscp::server::server_handler> handler); 35 35 36 36 /// Get the socket associated with the connection. … … 71 71 72 72 /// The handler used to process the incoming request. 73 boost::shared_ptr<nscp::server:: handler> handler_;73 boost::shared_ptr<nscp::server::server_handler> handler_; 74 74 75 75 nscp::data::signature_packet sig; … … 95 95 class factories { 96 96 public: 97 static connection* create(boost::asio::io_service& io_service, boost::asio::ssl::context &context, boost::shared_ptr<nscp::server:: handler> handler, bool use_ssl);98 static connection* create_tcp(boost::asio::io_service& io_service, boost::shared_ptr<nscp::server:: handler> handler);99 static connection* create_ssl(boost::asio::io_service& io_service, boost::asio::ssl::context &context, boost::shared_ptr<nscp::server:: handler> handler);97 static connection* create(boost::asio::io_service& io_service, boost::asio::ssl::context &context, boost::shared_ptr<nscp::server::server_handler> handler, bool use_ssl); 98 static connection* create_tcp(boost::asio::io_service& io_service, boost::shared_ptr<nscp::server::server_handler> handler); 99 static connection* create_ssl(boost::asio::io_service& io_service, boost::asio::ssl::context &context, boost::shared_ptr<nscp::server::server_handler> handler); 100 100 }; 101 101 -
include/nscp/server/handler.hpp
rb38e845 r7515d00 6 6 namespace nscp { 7 7 namespace server { 8 class handler { 9 public: 10 handler() {} 11 handler(const handler &other) {} 12 handler& operator= (const handler &other) { 8 class server_handler { 9 private: 10 server_handler(const server_handler &other) {} 11 server_handler& operator= (const server_handler &other) { 13 12 return *this; 14 13 } 15 virtual std::list<nscp::packet> process(nscp::packet &packet) = 0; 14 public: 15 server_handler() {} 16 virtual std::list<nscp::packet> process(const nscp::packet &packet) = 0; 17 virtual std::list<nscp::packet> process_all(const std::list<nscp::packet> &packet) = 0; 16 18 17 19 virtual void log_debug(std::string file, int line, std::wstring msg) = 0; -
include/nscp/server/parser.hpp
rb38e845 r7515d00 12 12 class parser : public boost::noncopyable { 13 13 std::vector<char> buffer_; 14 boost::shared_ptr<nscp::server:: handler> handler_;14 boost::shared_ptr<nscp::server::server_handler> handler_; 15 15 public: 16 parser(boost::shared_ptr<nscp::server:: handler> handler) : handler_(handler) {}16 parser(boost::shared_ptr<nscp::server::server_handler> handler) : handler_(handler) {} 17 17 18 18 typedef boost::function<boost::tuple<bool, char*>(parser*, char*, char*)> digest_function; -
include/nscp/server/server.hpp
r438998b r7515d00 40 40 public: 41 41 struct connection_info : public socket_helpers::connection_info { 42 connection_info(boost::shared_ptr<nscp::server:: handler> request_handler) : request_handler(request_handler) {}42 connection_info(boost::shared_ptr<nscp::server::server_handler> request_handler) : request_handler(request_handler) {} 43 43 connection_info(const connection_info &other) 44 44 : socket_helpers::connection_info(other) … … 56 56 bool allow_args; 57 57 bool allow_nasty; 58 boost::shared_ptr<nscp::server:: handler> request_handler;58 boost::shared_ptr<nscp::server::server_handler> request_handler; 59 59 }; 60 60 … … 93 93 94 94 /// The handler for all incoming requests. 95 boost::shared_ptr<nscp::server:: handler> request_handler_;95 boost::shared_ptr<nscp::server::server_handler> request_handler_; 96 96 97 97 boost::asio::ssl::context context_; -
include/nscp/server/ssl_connection.cpp
r0b8df3e r7515d00 11 11 namespace server { 12 12 13 ssl_connection::ssl_connection(boost::asio::io_service& io_service, boost::asio::ssl::context &context, boost::shared_ptr<nscp::server:: handler> handler)13 ssl_connection::ssl_connection(boost::asio::io_service& io_service, boost::asio::ssl::context &context, boost::shared_ptr<nscp::server::server_handler> handler) 14 14 : connection(io_service, handler) 15 15 , socket_(io_service, context) -
include/nscp/server/ssl_connection.hpp
r438998b r7515d00 20 20 public: 21 21 22 ssl_connection(boost::asio::io_service& io_service, boost::asio::ssl::context &context, boost::shared_ptr<nscp::server:: handler> handler);22 ssl_connection(boost::asio::io_service& io_service, boost::asio::ssl::context &context, boost::shared_ptr<nscp::server::server_handler> handler); 23 23 24 24 /// Get the socket associated with the connection. -
include/nscp/server/tcp_connection.cpp
r0b8df3e r7515d00 11 11 namespace server { 12 12 13 tcp_connection::tcp_connection(boost::asio::io_service& io_service, boost::shared_ptr<nscp::server:: handler> handler)13 tcp_connection::tcp_connection(boost::asio::io_service& io_service, boost::shared_ptr<nscp::server::server_handler> handler) 14 14 : connection(io_service, handler) 15 15 , socket_(io_service) -
include/nscp/server/tcp_connection.hpp
r438998b r7515d00 19 19 public: 20 20 /// Construct a connection with the given io_service. 21 explicit tcp_connection(boost::asio::io_service& io_service, boost::shared_ptr<nscp::server:: handler> handler);21 explicit tcp_connection(boost::asio::io_service& io_service, boost::shared_ptr<nscp::server::server_handler> handler); 22 22 23 23 /// Get the socket associated with the connection. -
include/settings/Settings.h
re11d494 r7515d00 31 31 #include <boost/regex.hpp> 32 32 #include <strEx.h> 33 #include <net/net.hpp> 33 34 #define BUFF_LEN 4096 34 35 … … 1302 1303 path_cache_type path_cache_; 1303 1304 std::wstring context_; 1304 net:: url url_;1305 net::wurl url_; 1305 1306 1306 1307 //SettingsInterfaceImpl() : core_(NULL) {} -
include/settings/client/settings_client.hpp
r4b1e6fe r7515d00 453 453 return settings_paths_easy_init(get_path(path), owner_); 454 454 } 455 inline std::wstring get_path(std::wstring path ) {455 inline std::wstring get_path(std::wstring path = _T("")) { 456 456 if (path.empty()) 457 457 return _T("/") + alias_; -
include/settings/client/settings_proxy.hpp
rc760fc9 r7515d00 1 1 #pragma once 2 2 #include <settings/settings_core.hpp> 3 #include <settings/settings_handler_impl.hpp> 3 4 #include <settings/client/settings_client.hpp> 4 5 #include <nscapi/nscapi_core_wrapper.hpp> -
include/strEx.h
r438998b r7515d00 60 60 #include <locale> 61 61 62 63 namespace utf8 { 64 /** Converts a std::wstring into a std::string with UTF-8 encoding. */ 65 template<typename StringT> 66 StringT cvt(std::wstring const & string); 67 68 /** Converts a std::String with UTF-8 encoding into a std::wstring. */ 69 template<typename StringT> 70 StringT cvt(std::string const & string ); 71 72 /** Nop specialization for std::string. */ 73 template <> 74 inline std::string cvt(std::string const & string) { 75 return string; 76 } 77 78 /** Nop specialization for std::wstring. */ 79 template<> 80 inline std::wstring cvt(std::wstring const & rc_string) { 81 return rc_string; 82 } 83 84 template<> 85 inline std::string cvt(std::wstring const & str) { 86 #ifdef WIN32 87 // figure out how many narrow characters we are going to get 88 int nChars = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()), NULL, 0, NULL, NULL); 89 if (nChars == 0) 90 return ""; 91 92 // convert the wide string to a narrow string 93 // nb: slightly naughty to write directly into the string like this 94 std::string buf; 95 buf.resize(nChars); 96 WideCharToMultiByte(CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()), const_cast<char*>(buf.c_str()), nChars, NULL, NULL); 97 return buf; 98 #else 99 size_t wideSize = sizeof(wchar_t)*str.length(); 100 size_t outbytesLeft = wideSize+sizeof(char); //We cannot know how many wide character there is yet 101 102 //Copy the instring 103 char *inString = (char*)new wchar_t[str.length()+1]; 104 memcpy(inString, str.c_str(), wideSize+sizeof(wchar_t)); 105 106 //Create buffer for output 107 char *outString = new char[outbytesLeft]; 108 memset(outString, 0, sizeof(char)*(outbytesLeft)); 109 110 char *inPointer = inString; 111 char *outPointer = outString; 112 113 iconv_t convDesc = iconv_open("UTF-8", "WCHAR_T"); 114 iconv(convDesc, &inPointer, &wideSize, &outPointer, &outbytesLeft); 115 iconv_close(convDesc); 116 117 std::string retval(outString); 118 119 //Cleanup 120 delete[] inString; 121 delete[] outString; 122 123 return retval; 124 #endif 125 } 126 127 template<> 128 inline std::wstring cvt(std::string const & str) { 129 #ifdef WIN32 130 // figure out how many wide characters we are going to get 131 int nChars = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()), NULL, 0); 132 if (nChars == 0) 133 return L""; 134 135 // convert the narrow string to a wide string 136 // nb: slightly naughty to write directly into the string like this 137 std::wstring buf; 138 buf.resize(nChars); 139 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()), const_cast<wchar_t*>(buf.c_str()), nChars); 140 return buf; 141 #else 142 size_t utf8Length = str.length(); 143 size_t outbytesLeft = utf8Length*sizeof(wchar_t); 144 145 //Copy the instring 146 char *inString = new char[str.length()+1]; 147 strcpy(inString, str.c_str()); 148 149 //Create buffer for output 150 char *outString = (char*)new wchar_t[utf8Length+1]; 151 memset(outString, 0, sizeof(wchar_t)*(utf8Length+1)); 152 153 char *inPointer = inString; 154 char *outPointer = outString; 155 156 iconv_t convDesc = iconv_open("WCHAR_T", "UTF-8"); 157 iconv(convDesc, &inPointer, &utf8Length, &outPointer, &outbytesLeft); 158 iconv_close(convDesc); 159 160 std::wstring retval( (wchar_t *)outString ); 161 162 //Cleanup 163 delete[] inString; 164 delete[] outString; 165 166 return retval; 167 #endif 168 } 169 } 170 62 171 namespace boost 63 172 { 64 173 template<> 65 inline std::wstring lexical_cast<std::wstring, std::string>(const std::string& arg) 66 { 67 std::wstring result; 68 std::locale loc; 69 for(unsigned int i= 0; i < arg.size(); ++i) 70 { 71 result += std::use_facet<std::ctype<wchar_t> >(loc).widen(arg[i]); 72 } 73 return result; 174 inline std::wstring lexical_cast<std::wstring, std::string>(const std::string& arg) { 175 return utf8::cvt<std::wstring>(arg); 74 176 } 75 177 76 178 template<> 77 inline std::string lexical_cast<std::string, std::wstring>(const std::wstring& arg) 78 { 79 std::string result; 80 std::locale loc; 81 for(unsigned int i= 0; i < arg.size(); ++i) 82 result += std::use_facet<std::ctype<wchar_t> >(loc).narrow(arg[i], 0); 83 return result; 179 inline std::string lexical_cast<std::string, std::wstring>(const std::wstring& arg) { 180 return utf8::cvt<std::string>(arg); 84 181 } 85 182 } … … 163 260 } 164 261 inline std::string wstring_to_string( const std::wstring& str ) { 165 return boost::lexical_cast<std::string>(str);262 return utf8::cvt<std::string>(str); 166 263 } 167 264 inline std::wstring string_to_wstring( const std::string& str ) { 168 return boost::lexical_cast<std::wstring>(str);265 return utf8::cvt<std::wstring>(str); 169 266 } 170 267 … … 849 946 } 850 947 } 948 template <typename T> std::string to_string(const std::string& arg) { 949 return arg; 950 } 951 template <typename T> std::string to_string(const std::wstring& arg) { 952 return utf8::cvt<std::string>(arg); 953 } 851 954 template <typename T> std::wstring to_wstring(const T& arg) { 852 955 try { … … 857 960 } 858 961 } 962 template <typename T> std::wstring to_wstring(const std::wstring& arg) { 963 return arg; 964 } 965 template <typename T> std::wstring to_wstring(const std::string& arg) { 966 return utf8::cvt<std::wstring>(arg); 967 } 859 968 } 860 969 } 861 /* 862 #ifdef __GNUC__ 863 size_t Wcslen(const wchar_t*w) 864 { 865 size_t size=0; 866 while (*w++) 867 size++; 868 return size; 869 } 870 #endif 871 #ifdef WIN32 872 #define Wcslen wcslen 873 #endif 874 */ 875 namespace utf8 { 876 /** Converts a std::wstring into a std::string with UTF-8 encoding. */ 877 template<typename StringT> 878 StringT cvt(std::wstring const & string); 879 880 /** Converts a std::String with UTF-8 encoding into a std::wstring. */ 881 template<typename StringT> 882 StringT cvt(std::string const & string ); 883 884 /** Nop specialization for std::string. */ 885 template <> 886 inline std::string cvt(std::string const & string) { 887 return string; 888 } 889 890 /** Nop specialization for std::wstring. */ 891 template<> 892 inline std::wstring cvt(std::wstring const & rc_string) { 893 return rc_string; 894 } 895 896 template<> 897 inline std::string cvt(std::wstring const & str) { 898 #ifdef WIN32 899 // figure out how many narrow characters we are going to get 900 int nChars = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()), NULL, 0, NULL, NULL); 901 if (nChars == 0) 902 return ""; 903 904 // convert the wide string to a narrow string 905 // nb: slightly naughty to write directly into the string like this 906 std::string buf; 907 buf.resize(nChars); 908 WideCharToMultiByte(CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()), const_cast<char*>(buf.c_str()), nChars, NULL, NULL); 909 return buf; 910 #else 911 size_t wideSize = sizeof(wchar_t)*str.length(); 912 size_t outbytesLeft = wideSize+sizeof(char); //We cannot know how many wide character there is yet 913 914 //Copy the instring 915 char *inString = (char*)new wchar_t[str.length()+1]; 916 memcpy(inString, str.c_str(), wideSize+sizeof(wchar_t)); 917 918 //Create buffer for output 919 char *outString = new char[outbytesLeft]; 920 memset(outString, 0, sizeof(char)*(outbytesLeft)); 921 922 char *inPointer = inString; 923 char *outPointer = outString; 924 925 iconv_t convDesc = iconv_open("UTF-8", "WCHAR_T"); 926 iconv(convDesc, &inPointer, &wideSize, &outPointer, &outbytesLeft); 927 iconv_close(convDesc); 928 929 std::string retval(outString); 930 931 //Cleanup 932 delete[] inString; 933 delete[] outString; 934 935 return retval; 936 #endif 937 } 938 939 template<> 940 inline std::wstring cvt(std::string const & str) { 941 #ifdef WIN32 942 // figure out how many wide characters we are going to get 943 int nChars = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()), NULL, 0); 944 if (nChars == 0) 945 return L""; 946 947 // convert the narrow string to a wide string 948 // nb: slightly naughty to write directly into the string like this 949 std::wstring buf; 950 buf.resize(nChars); 951 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()), const_cast<wchar_t*>(buf.c_str()), nChars); 952 return buf; 953 #else 954 size_t utf8Length = str.length(); 955 size_t outbytesLeft = utf8Length*sizeof(wchar_t); 956 957 //Copy the instring 958 char *inString = new char[str.length()+1]; 959 strcpy(inString, str.c_str()); 960 961 //Create buffer for output 962 char *outString = (char*)new wchar_t[utf8Length+1]; 963 memset(outString, 0, sizeof(wchar_t)*(utf8Length+1)); 964 965 char *inPointer = inString; 966 char *outPointer = outString; 967 968 iconv_t convDesc = iconv_open("WCHAR_T", "UTF-8"); 969 iconv(convDesc, &inPointer, &utf8Length, &outPointer, &outbytesLeft); 970 iconv_close(convDesc); 971 972 std::wstring retval( (wchar_t *)outString ); 973 974 //Cleanup 975 delete[] inString; 976 delete[] outString; 977 978 return retval; 979 #endif 980 } 981 } 970 -
libs/plugin_api/CMakeLists.txt
r3bdaf18 r7515d00 16 16 ${NSCP_INCLUDEDIR}/nscapi/nscapi_core_wrapper.cpp 17 17 ${NSCP_INCLUDEDIR}/settings/client/settings_client.cpp 18 ${NSCP_INCLUDEDIR}/nscapi/targets.cpp 18 19 ) 19 20 … … 31 32 ${NSCP_INCLUDEDIR}/nscapi/settings_proxy.hpp 32 33 ${NSCP_INCLUDEDIR}/nscapi/macros.hpp 34 ${NSCP_INCLUDEDIR}/nscapi/targets.hpp 33 35 34 36 ) -
libs/protobuf/ipc.proto
rb38e845 r7515d00 13 13 repeated string slaves = 2; 14 14 15 }; 16 17 message Signature { 18 required int32 version = 1; 19 required int32 header_type = 2; 20 required int32 payload_type = 3; 21 required int32 additional_packet_count = 4; 15 22 }; 16 23 -
modules/NSCPClient/CMakeLists.txt
r438998b r7515d00 9 9 "${TARGET}.cpp" 10 10 ${NSCP_INCLUDEDIR}/nscp/packet.cpp 11 ${NSCP_INCLUDEDIR}/nscp/handler.cpp 11 12 ${NSCP_INCLUDEDIR}/socket/socket_helpers.cpp 13 ${NSCP_INCLUDEDIR}/client/command_line_parser.cpp 12 14 13 15 ${NSCP_DEF_PLUGIN_CPP} … … 22 24 "${TARGET}.def" 23 25 ${NSCP_INCLUDEDIR}/nscp/packet.hpp 26 ${NSCP_INCLUDEDIR}/nscp/handler.hpp 24 27 ${NSCP_INCLUDEDIR}/nscp/client/socket.hpp 25 28 ${NSCP_INCLUDEDIR}/swap_bytes.hpp 26 29 ${NSCP_INCLUDEDIR}/socket/socket_helpers.hpp 30 ${NSCP_INCLUDEDIR}/client/command_line_parser.hpp 27 31 28 32 ${NSCP_DEF_PLUGIN_HPP} -
modules/NSCPClient/NSCPClient.cpp
ra44cb15 r7515d00 27 27 #include <config.h> 28 28 #include <strEx.h> 29 #include <net/net.hpp> 29 30 #include <nscp/client/socket.hpp> 30 31 … … 36 37 namespace sh = nscapi::settings_helper; 37 38 38 NSCPClient::NSCPClient() : buffer_length_(0){39 NSCPClient::NSCPClient() { 39 40 } 40 41 … … 53 54 register_command(_T("query_nscp"), _T("Submit a query to a remote host via NSCP")); 54 55 register_command(_T("submit_nscp"), _T("Submit a query to a remote host via NSCP")); 55 //"/settings/NSCP/client/handlers" 56 register_command(_T("exec_nscp"), _T("Execute remote command on a remote host via NSCP")); 57 56 58 sh::settings_registry settings(get_settings_proxy()); 57 59 settings.set_alias(_T("NSCP"), alias, _T("client")); 60 target_path = settings.alias().get_settings_path(_T("targets")); 58 61 59 62 settings.alias().add_path_to_settings() … … 62 65 _T("CLIENT HANDLER SECTION"), _T("")) 63 66 64 (_T(" servers"), sh::fun_values_path(boost::bind(&NSCPClient::add_server, this, _1, _2)),65 _T("REMOTE SERVERDEFINITIONS"), _T(""))67 (_T("targets"), sh::fun_values_path(boost::bind(&NSCPClient::add_target, this, _1, _2)), 68 _T("REMOTE TARGET DEFINITIONS"), _T("")) 66 69 67 70 ; 68 71 /* 69 72 settings.alias().add_key_to_settings() 70 73 … … 73 76 74 77 ; 75 78 */ 76 79 77 80 settings.register_all(); 78 81 settings.notify(); 82 83 NSC_LOG_ERROR_STD(_T("Targets: ") + targets.to_wstring()); 79 84 80 85 } catch (...) { … … 94 99 } 95 100 96 void NSCPClient::add_common_options(po::options_description &desc, nscp_connection_data &command_data) { 97 desc.add_options() 98 ("host,H", po::wvalue<std::wstring>(&command_data.host), "The address of the host running the NSCP daemon") 99 ("port,p", po::value<int>(&command_data.port), "The port on which the daemon is running (default=5668)") 100 ("timeout,t", po::value<int>(&command_data.timeout), "Number of seconds before connection times out (default=10)") 101 ("no-ssl,n", po::value<bool>(&command_data.no_ssl)->zero_tokens()->default_value(false), "Do not initial an ssl handshake with the server, talk in plain text.") 102 ("query,q", po::bool_switch(&command_data.query), "Force query mode (only useful when this is not obvious)") 103 ("submit,s", po::bool_switch(&command_data.submit), "Force submit mode (only useful when this is not obvious)") 104 ; 105 } 106 void NSCPClient::add_query_options(po::options_description &desc, nscp_connection_data &command_data) { 107 desc.add_options() 108 ("command,c", po::wvalue<std::wstring>(&command_data.command), "The name of the query that the remote daemon should run") 109 ("arguments,a", po::wvalue<std::vector<std::wstring> >(&command_data.arguments), "list of arguments") 110 ; 111 } 112 void NSCPClient::add_submit_options(po::options_description &desc, nscp_connection_data &command_data) { 113 desc.add_options() 114 ("command,c", po::wvalue<std::wstring>(&command_data.command), "The name of the command that the remote daemon should run") 115 ("message,m", po::wvalue<std::wstring>(&command_data.message), "Message") 116 ("result,r", po::value<unsigned int>(&command_data.result), "Result code") 117 ; 118 } 119 void NSCPClient::add_exec_options(po::options_description &desc, nscp_connection_data &command_data) { 120 desc.add_options() 121 ("command,c", po::wvalue<std::wstring>(&command_data.command), "The name of the command that the remote daemon should run") 122 ("arguments,a", po::wvalue<std::vector<std::wstring> >(&command_data.arguments), "list of arguments") 123 ; 124 } 125 126 void NSCPClient::add_server(std::wstring key, std::wstring args) { 127 } 128 129 void NSCPClient::add_command(std::wstring key, std::wstring args) { 130 try { 131 132 NSCPClient::nscp_connection_data command_data; 133 boost::program_options::variables_map vm; 134 135 po::options_description common("Common options"); 136 add_common_options(common, command_data); 137 po::options_description query("Query options"); 138 add_query_options(query, command_data); 139 po::options_description submit("Submit options"); 140 add_submit_options(submit, command_data); 141 142 po::positional_options_description p; 143 p.add("arguments", -1); 144 145 std::vector<std::wstring> list; 146 //explicit escaped_list_separator(Char e = '\\', Char c = ',',Char q = '\"') 147 boost::escaped_list_separator<wchar_t> sep(L'\\', L' ', L'\"'); 148 typedef boost::tokenizer<boost::escaped_list_separator<wchar_t>,std::wstring::const_iterator, std::wstring > tokenizer_t; 149 tokenizer_t tok(args, sep); 150 for(tokenizer_t::iterator beg=tok.begin(); beg!=tok.end();++beg){ 151 list.push_back(*beg); 152 } 153 154 po::options_description desc("Availible options"); 155 desc.add(common).add(query).add(submit); 156 po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>(list).options(desc).positional(p).run(); 157 po::store(parsed, vm); 158 po::notify(vm); 159 160 NSC_DEBUG_MSG_STD(_T("Added NSCP Client: ") + key.c_str() + _T(" = ") + command_data.toString()); 161 commands[key.c_str()] = command_data; 162 163 register_command(key.c_str(), command_data.toString()); 164 101 void NSCPClient::add_target(std::wstring key, std::wstring arg) { 102 try { 103 targets.add(get_settings_proxy(), target_path , key, arg); 104 } catch (...) { 105 NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); 106 } 107 } 108 109 void NSCPClient::add_command(std::wstring name, std::wstring args) { 110 try { 111 client::configuration config; 112 std::wstring cmd = setup(config, name); 113 std::wstring key = commands.add_command(config, name, args); 114 if (!key.empty()) 115 register_command(key.c_str(), _T("Custom command for: ") + name); 165 116 } catch (boost::program_options::validation_error &e) { 166 NSC_LOG_ERROR_STD(_T("Could not parse: ") + key.c_str() + strEx::string_to_wstring(e.what()));117 NSC_LOG_ERROR_STD(_T("Could not parse: ") + name + to_wstring(e.what())); 167 118 } catch (...) { 168 NSC_LOG_ERROR_STD(_T("Could not parse: ") + key.c_str());119 NSC_LOG_ERROR_STD(_T("Could not parse: ") + name); 169 120 } 170 121 } … … 181 132 } 182 133 183 184 std::list<std::string> collect_result(std::list<std::string> payloads, int &ret) { 185 std::list<std::string> result; 186 ret = NSCAPI::returnOK; 187 BOOST_FOREACH(std::string p, payloads) { 188 Plugin::QueryResponseMessage message; 189 message.ParseFromString(p); 190 for (int i=0;i<message.payload_size();i++) { 191 const Plugin::QueryResponseMessage::Response &payload = message.payload(i); 192 ret = nscapi::plugin_helper::maxState(ret, nscapi::functions::gbp_to_nagios_status(payload.result())); 193 std::string line = payload.message(); 194 195 // @todo: Add performance data parsing here! 196 //nscapi::functions::parse_performance_data(payload, perf); 197 //if (!payload.perf().empty()) 198 // line += _T("|") + payload; 199 result.push_back(line); 200 } 201 } 202 return result; 203 } 204 134 void NSCPClient::add_local_options(po::options_description &desc, nscp_connection_data &command_data) { 135 desc.add_options() 136 ("no-ssl,n", po::value<bool>(&command_data.no_ssl)->zero_tokens()->default_value(false), "Do not initial an ssl handshake with the server, talk in plain text.") 137 ("cert,c", po::value<std::wstring>(&command_data.cert)->default_value(cert_), "Certificate to use.") 138 ; 139 } 140 141 std::wstring NSCPClient::setup(client::configuration config, const std::wstring &command) { 142 clp_handler_impl *handler = new clp_handler_impl(this); 143 add_local_options(config.local, handler->local_data); 144 std::wstring cmd = command; 145 if (command.length() > 5 && command.substr(0,5) == _T("nscp_")) 146 cmd = command.substr(5); 147 if (command.length() > 5 && command.substr(command.length()-5) == _T("_nscp")) 148 cmd = command.substr(0, command.length()-5); 149 config.handler = client::configuration::handler_type(handler); 150 return cmd; 151 } 205 152 206 153 NSCAPI::nagiosReturn NSCPClient::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) { 207 154 if (command == _T("query_nscp")) { 208 return query_nscp(arguments, message, perf); 155 client::configuration config; 156 std::wstring cmd = setup(config, command); 157 return client::command_line_parser::query(config, cmd, arguments, message, perf); 209 158 } 210 159 if (command == _T("submit_nscp")) { 211 return query_nscp(arguments, message, perf); 212 } 213 command_list::const_iterator cit = commands.find(command); 214 if (cit == commands.end()) 215 return NSCAPI::returnIgnored; 216 217 std::string buffer; 218 nscapi::functions::create_simple_query_request((*cit).second.command, arguments, buffer); 219 std::list<std::string> payloads = execute_nscp_command((*cit).second, buffer); 220 int ret = NSCAPI::returnUNKNOWN; 221 std::list<std::string> result = collect_result(payloads, ret); 222 BOOST_FOREACH(std::string p, result) { 223 message += utf8::cvt<std::wstring>(p) + _T("\n"); 224 } 225 return ret; 226 } 227 228 229 NSCAPI::nagiosReturn NSCPClient::query_nscp(std::list<std::wstring> &arguments, std::wstring &message, std::wstring perf) { 230 try { 231 NSCPClient::nscp_connection_data command_data; 232 boost::program_options::variables_map vm; 233 234 po::options_description common("Common options"); 235 add_common_options(common, command_data); 236 po::options_description query("Query options"); 237 add_query_options(query, command_data); 238 239 po::options_description desc("Allowed options"); 240 desc.add(common).add(query); 241 242 std::vector<std::wstring> vargs(arguments.begin(), arguments.end()); 243 po::positional_options_description p; 244 p.add("arguments", -1); 245 po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>(vargs).options(desc).positional(p).run(); 246 po::store(parsed, vm); 247 po::notify(vm); 248 249 250 std::string buffer; 251 nscapi::functions::create_simple_query_request(command_data.command, arguments, buffer); 252 std::list<std::string> payloads = execute_nscp_command(command_data, buffer); 253 int ret = NSCAPI::returnUNKNOWN; 254 std::list<std::string> result = collect_result(payloads, ret); 255 BOOST_FOREACH(std::string p, result) { 256 message += utf8::cvt<std::wstring>(p) + _T("\n"); 257 } 258 return ret; 259 } catch (boost::program_options::validation_error &e) { 260 message = _T("Error: ") + utf8::cvt<std::wstring>(e.what()); 261 return NSCAPI::returnUNKNOWN; 262 } catch (...) { 263 message = _T("Unknown exception parsing command line"); 264 return NSCAPI::returnUNKNOWN; 265 } 266 return NSCAPI::returnUNKNOWN; 267 } 268 269 bool NSCPClient::submit_nscp(std::list<std::wstring> &arguments, std::wstring &result) { 270 try { 271 NSCPClient::nscp_connection_data command_data; 272 boost::program_options::variables_map vm; 273 274 po::options_description common("Common options"); 275 add_common_options(common, command_data); 276 po::options_description submit("Submit options"); 277 add_submit_options(submit, command_data); 278 279 po::options_description desc("Allowed options"); 280 desc.add(common).add(submit); 281 282 std::vector<std::wstring> vargs(arguments.begin(), arguments.end()); 283 po::positional_options_description p; 284 p.add("arguments", -1); 285 po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>(vargs).options(desc).positional(p).run(); 286 po::store(parsed, vm); 287 po::notify(vm); 288 289 290 std::string buffer; 291 nscapi::functions::create_simple_query_response(command_data.command, command_data.result, command_data.message, _T(""), buffer); 292 std::list<std::string> errors = submit_nscp_command(command_data, buffer); 293 294 BOOST_FOREACH(std::string e, errors) { 295 result += utf8::cvt<std::wstring>(e) + _T("\n"); 296 } 297 } catch (boost::program_options::validation_error &e) { 298 result = _T("Error: ") + utf8::cvt<std::wstring>(e.what()); 299 return false; 300 } catch (...) { 301 result = _T("Unknown exception parsing command line"); 302 return false; 303 } 304 return true; 160 client::configuration config; 161 std::wstring cmd = setup(config, command); 162 std::list<std::string> errors = client::command_line_parser::submit(config, cmd, arguments); 163 BOOST_FOREACH(std::string p, errors) { 164 NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(p)); 165 } 166 return errors.empty()?NSCAPI::returnOK:NSCAPI::returnCRIT; 167 } 168 if (command == _T("exec_nscp")) { 169 client::configuration config; 170 std::wstring cmd = setup(config, command); 171 return client::command_line_parser::exec(config, cmd, arguments, message); 172 } 173 return commands.exec_simple(target, command, arguments, message, perf); 174 return NSCAPI::returnIgnored; 305 175 } 306 176 307 177 308 178 int NSCPClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 309 NSCPClient::nscp_connection_data command_data; 310 if (command == _T("help")) { 311 po::options_description common("Common options"); 312 add_common_options(common, command_data); 313 po::options_description query("Query NSCP options"); 314 add_query_options(query, command_data); 315 po::options_description submit("Submit NSCP options"); 316 add_submit_options(submit, command_data); 317 po::options_description exec("Execute NSCP options"); 318 add_exec_options(exec, command_data); 319 po::options_description desc("Allowed options"); 320 desc.add(common).add(query).add(submit); 321 322 std::stringstream ss; 323 ss << "NSCPClient Command line syntax for command: query_nscp and submit_nscp" << std::endl;; 324 ss << desc; 325 result = utf8::cvt<std::wstring>(ss.str()); 326 return NSCAPI::returnOK; 327 } else if (command == _T("query_nscp")) { 328 boost::program_options::variables_map vm; 329 330 po::options_description common("Common options"); 331 add_common_options(common, command_data); 332 po::options_description query("Query NSCP options"); 333 add_query_options(query, command_data); 334 po::options_description desc("Allowed options"); 335 desc.add(common).add(query); 336 337 std::vector<std::wstring> vargs(arguments.begin(), arguments.end()); 338 po::positional_options_description p; 339 p.add("arguments", -1); 340 po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>(vargs).options(desc).positional(p).run(); 341 po::store(parsed, vm); 342 po::notify(vm); 343 344 std::string buffer; 345 nscapi::functions::create_simple_query_request(command_data.command, command_data.arguments, buffer); 346 std::list<std::string> payloads = execute_nscp_query(command_data, buffer); 347 int ret = NSCAPI::returnUNKNOWN; 348 std::list<std::string> strings = collect_result(payloads, ret); 349 BOOST_FOREACH(std::string p, strings) { 350 result += utf8::cvt<std::wstring>(p) + _T("\n"); 351 } 352 return ret; 353 } else if (command == _T("exec_nscp")) { 354 boost::program_options::variables_map vm; 355 356 po::options_description common("Common options"); 357 add_common_options(common, command_data); 358 po::options_description query("Query NSCP options"); 359 add_exec_options(query, command_data); 360 po::options_description desc("Allowed options"); 361 desc.add(common).add(query); 362 363 std::vector<std::wstring> vargs(arguments.begin(), arguments.end()); 364 po::positional_options_description p; 365 p.add("arguments", -1); 366 po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>(vargs).options(desc).positional(p).run(); 367 po::store(parsed, vm); 368 po::notify(vm); 369 370 std::string buffer; 371 nscapi::functions::create_simple_exec_request(command_data.command, command_data.arguments, buffer); 372 std::list<std::string> payloads = execute_nscp_command(command_data, buffer); 373 int ret = NSCAPI::returnUNKNOWN; 374 std::list<std::string> strings = collect_result(payloads, ret); 375 BOOST_FOREACH(std::string p, strings) { 376 result += utf8::cvt<std::wstring>(p) + _T("\n"); 377 } 378 return ret; 379 } else if (command == _T("submit_nscp")) { 380 boost::program_options::variables_map vm; 381 382 po::options_description common("Common options"); 383 add_common_options(common, command_data); 384 po::options_description submit("Submit NSCP options"); 385 add_submit_options(submit, command_data); 386 po::options_description desc("Allowed options"); 387 desc.add(common).add(submit); 388 389 std::vector<std::wstring> vargs(arguments.begin(), arguments.end()); 390 po::positional_options_description p; 391 p.add("arguments", -1); 392 po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>(vargs).options(desc).positional(p).run(); 393 po::store(parsed, vm); 394 po::notify(vm); 395 396 std::string buffer; 397 nscapi::functions::create_simple_query_response(command_data.command, command_data.result, command_data.message, _T(""), buffer); 398 std::list<std::string> errors = submit_nscp_command(command_data, buffer); 399 BOOST_FOREACH(std::string p, errors) { 400 result += utf8::cvt<std::wstring>(p) + _T("\n"); 401 } 402 return NSCAPI::returnOK; 403 } 404 return NSCAPI::returnIgnored; 405 } 406 std::list<std::string> NSCPClient::execute_nscp_command(nscp_connection_data con, std::string buffer) { 407 std::list<std::string> result; 179 client::configuration config; 180 std::wstring cmd = setup(config, command); 181 return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 182 } 183 184 ////////////////////////////////////////////////////////////////////////// 185 // Parser implementations 186 int NSCPClient::clp_handler_impl::query(client::configuration::data_type data, std::string request, std::string &reply) { 187 NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 408 188 try { 409 189 std::list<nscp::packet> chunks; 410 chunks.push_back(nscp:: packet::build_envelope_request(1));411 chunks.push_back(nscp:: packet::create_payload(nscp::data::command_request, buffer, 0));412 chunks = send(con, chunks);190 chunks.push_back(nscp::factory::create_envelope_request(1)); 191 chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request, 0)); 192 chunks = instance->send(data, local_data, chunks); 413 193 BOOST_FOREACH(nscp::packet &chunk, chunks) { 414 if ( chunk.is_query_response()) {415 re sult.push_back(chunk.payload);416 } else if ( chunk.is_error()) {194 if (nscp::checks::is_query_response(chunk)) { 195 reply = chunk.payload; 196 } else if (nscp::checks::is_error(chunk)) { 417 197 NSCPIPC::ErrorMessage message; 418 198 message.ParseFromString(chunk.payload); … … 420 200 NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 421 201 } 202 ret = NSCAPI::returnUNKNOWN; 422 203 } else { 423 204 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 205 ret = NSCAPI::returnUNKNOWN; 424 206 } 425 //NSC_DEBUG_MSG_STD(_T("Found chunk: ") + utf8::cvt<std::wstring>(strEx::format_buffer(chunk.payload.c_str(), chunk.payload.size()))); 426 } 427 return result; 207 } 208 return ret; 428 209 } catch (std::exception &e) { 429 210 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 430 return result;431 } 432 } 433 std::list<std::string> NSCPClient:: execute_nscp_query(nscp_connection_data con, std::string buffer) {211 return NSCAPI::returnUNKNOWN; 212 } 213 } 214 std::list<std::string> NSCPClient::clp_handler_impl::submit(client::configuration::data_type data, std::string request) { 434 215 std::list<std::string> result; 435 216 try { 436 217 std::list<nscp::packet> chunks; 437 chunks.push_back(nscp::packet::build_envelope_request(1)); 438 chunks.push_back(nscp::packet::create_payload(nscp::data::exec_request, buffer, 0)); 439 chunks = send(con, chunks); 218 chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request, 0)); 219 chunks = instance->send(data, local_data, chunks); 440 220 BOOST_FOREACH(nscp::packet &chunk, chunks) { 441 if ( chunk.is_exec_response()) {221 if (nscp::checks::is_query_response(chunk)) { 442 222 result.push_back(chunk.payload); 443 } else if (chunk.is_error()) { 444 NSCPIPC::ErrorMessage message; 445 message.ParseFromString(chunk.payload); 446 for (int i=0;i<message.error_size();i++) { 447 NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 448 } 449 } else { 450 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 451 } 452 //NSC_DEBUG_MSG_STD(_T("Found chunk: ") + utf8::cvt<std::wstring>(strEx::format_buffer(chunk.payload.c_str(), chunk.payload.size()))); 453 } 454 return result; 455 } catch (std::exception &e) { 456 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 457 return result; 458 } 459 } 460 461 std::list<std::string> NSCPClient::submit_nscp_command(nscp_connection_data con, std::string buffer) { 462 std::list<std::string> result; 463 try { 464 std::list<nscp::packet> chunks; 465 chunks.push_back(nscp::packet::create_payload(nscp::data::command_response, buffer, 0)); 466 chunks = send(con, chunks); 467 BOOST_FOREACH(nscp::packet &chunk, chunks) { 468 if (chunk.is_query_response()) { 469 result.push_back(chunk.payload); 470 } else if (chunk.is_error()) { 223 } else if (nscp::checks::is_error(chunk)) { 471 224 NSCPIPC::ErrorMessage message; 472 225 message.ParseFromString(chunk.payload); … … 476 229 } else { 477 230 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 231 result.push_back("Invalid payload"); 478 232 } 479 //NSC_DEBUG_MSG_STD(_T("Found chunk: ") + utf8::cvt<std::wstring>(strEx::format_buffer(chunk.payload.c_str(), chunk.payload.size())));480 233 } 481 234 return result; 482 235 } catch (std::exception &e) { 236 result.push_back(e.what()); 483 237 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 484 238 return result; 485 239 } 486 240 } 487 488 std::list<nscp::packet> NSCPClient::send(nscp_connection_data &con, std::list<nscp::packet> &chunks) { 489 chunks.push_front(nscp::packet::build_envelope_request(1)); 241 int NSCPClient::clp_handler_impl::exec(client::configuration::data_type data, std::string request, std::string &reply) { 242 int ret = NSCAPI::returnOK; 243 try { 244 std::list<nscp::packet> chunks; 245 chunks.push_back(nscp::factory::create_envelope_request(1)); 246 chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request, 0)); 247 chunks = instance->send(data, local_data, chunks); 248 BOOST_FOREACH(nscp::packet &chunk, chunks) { 249 if (nscp::checks::is_exec_response(chunk)) { 250 reply = chunk.payload; 251 } else if (nscp::checks::is_error(chunk)) { 252 NSCPIPC::ErrorMessage message; 253 message.ParseFromString(chunk.payload); 254 for (int i=0;i<message.error_size();i++) { 255 NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 256 ret = NSCAPI::returnUNKNOWN; 257 } 258 } else { 259 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 260 ret = NSCAPI::returnUNKNOWN; 261 } 262 } 263 return ret; 264 } catch (std::exception &e) { 265 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 266 return NSCAPI::returnUNKNOWN; 267 } 268 } 269 270 ////////////////////////////////////////////////////////////////////////// 271 // Socket interface 272 std::list<nscp::packet> NSCPClient::send(client::configuration::data_type generic_data, nscp_connection_data &data, std::list<nscp::packet> &chunks) { 273 chunks.push_front(nscp::factory::create_envelope_request(1)); 490 274 std::list<nscp::packet> tmp, result; 491 if (!con.no_ssl) { 275 std::wstring host = generic_data->host; 276 if (host.empty() && !generic_data->target.empty()) { 277 nscapi::target_handler::optarget t = targets.find_target(generic_data->target); 278 if (t) 279 host = (*t).host; 280 } 281 net::wurl url = net::parse(host, 5666); 282 if (!data.no_ssl) { 492 283 #ifdef USE_SSL 493 tmp = send_ssl( con.host, con.port, con.timeout, chunks);284 tmp = send_ssl(url.host, url.port, data.cert, generic_data->timeout, chunks); 494 285 #else 495 286 NSC_LOG_ERROR_STD(_T("SSL not avalible (not compiled with USE_SSL)")); 496 result.push_back(nscp:: packet::create_error(_T("SSL support not available (compiled without USE_SSL)!")));287 result.push_back(nscp::factory::create_error(_T("SSL support not available (compiled without USE_SSL)!"))); 497 288 #endif 498 289 } else { 499 tmp = send_nossl( con.host, con.port, con.timeout, chunks);290 tmp = send_nossl(url.host, url.port, generic_data->timeout, chunks); 500 291 } 501 292 BOOST_FOREACH(nscp::packet &p, tmp) { 502 if ( p.is_envelope_response()) {293 if (nscp::checks::is_envelope_response(p)) { 503 294 std::cout << "Got envelope" << std::endl; 504 295 } else { … … 510 301 511 302 #ifdef USE_SSL 512 std::list<nscp::packet> NSCPClient::send_ssl(std::wstring host, int port, int timeout, const std::list<nscp::packet> &chunks) {303 std::list<nscp::packet> NSCPClient::send_ssl(std::wstring host, int port, std::wstring cert, int timeout, const std::list<nscp::packet> &chunks) { 513 304 NSC_DEBUG_MSG_STD(_T("Connecting to: ") + host + _T(":") + strEx::itos(port)); 514 305 boost::asio::io_service io_service; 515 306 boost::asio::ssl::context ctx(io_service, boost::asio::ssl::context::sslv23); 516 307 SSL_CTX_set_cipher_list(ctx.impl(), "ADH"); 517 ctx.use_tmp_dh_file(to_string(cert _));308 ctx.use_tmp_dh_file(to_string(cert)); 518 309 ctx.set_verify_mode(boost::asio::ssl::context::verify_none); 519 310 nscp::client::ssl_socket socket(io_service, ctx, host, port); -
modules/NSCPClient/NSCPClient.h
ra14aa07 r7515d00 26 26 #include <map> 27 27 #include <nscp/packet.hpp> 28 28 #include <client/command_line_parser.hpp> 29 #include <settings/client/targets.hpp> 30 #include <nscapi/targets.hpp> 29 31 30 32 class NSCPClient : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 31 33 private: 32 struct nscp_connection_data { 33 std::wstring host; 34 std::wstring command; 35 std::wstring command_line; 36 std::wstring message; 37 unsigned int result; 38 bool submit; 39 bool query; 40 std::vector<std::wstring> arguments; 41 int port; 42 int timeout; 34 std::wstring cert_; 35 nscapi::target_handler targets; 36 std::wstring target_path; 37 struct nscp_connection_data : public client::nscp_cli_data { 43 38 bool no_ssl; 44 nscp_connection_data() 45 : host(_T("127.0.0.1")), 46 port(5668), 47 timeout(10), 48 no_ssl(false), 49 submit(false), 50 query(false) 51 {} 52 std::wstring toString() { 39 std::wstring cert; 40 nscp_connection_data() : no_ssl(false) {} 41 std::wstring to_wstring() { 53 42 std::wstringstream ss; 54 ss << _T("host: ") << host; 55 ss << _T(", port: ") << port; 56 ss << _T(", timeout: ") << timeout; 43 ss << _T(", cert: ") << cert; 57 44 ss << _T(", no_ssl: ") << no_ssl; 58 ss << _T(", command: ") << command;59 ss << _T(", message: ") << message;60 ss << _T(", result: ") << result;61 int i=0;62 BOOST_FOREACH(std::wstring a, arguments) {63 ss << _T(", argument[") << i++ << _T("]: ") << a;64 }65 45 return ss.str(); 66 46 } 67 47 }; 68 typedef std::map<std::wstring, nscp_connection_data> command_list; 69 command_list commands; 70 unsigned int buffer_length_; 71 std::wstring cert_; 48 49 struct clp_handler_impl : public client::clp_handler { 50 51 NSCPClient *instance; 52 clp_handler_impl(NSCPClient *instance) : instance(instance) {} 53 nscp_connection_data local_data; 54 55 int query(client::configuration::data_type data, std::string request, std::string &reply); 56 std::list<std::string> submit(client::configuration::data_type data, std::string request); 57 int exec(client::configuration::data_type data, std::string request, std::string &reply); 58 }; 72 59 73 60 public: … … 109 96 std::list<std::string> execute_nscp_command(nscp_connection_data con, std::string buffer); 110 97 std::list<std::string> execute_nscp_query(nscp_connection_data con, std::string buffer); 111 std::list<nscp::packet> send( nscp_connection_data &con, std::list<nscp::packet> &chunks);98 std::list<nscp::packet> send(client::configuration::data_type generic_data, nscp_connection_data &data, std::list<nscp::packet> &chunks); 112 99 std::list<nscp::packet> send_nossl(std::wstring host, int port, int timeout, const std::list<nscp::packet> &chunks); 113 std::list<nscp::packet> send_ssl(std::wstring host, int port, int timeout, const std::list<nscp::packet> &chunks); 114 void add_common_options(po::options_description &desc, nscp_connection_data &command_data); 115 void add_query_options(po::options_description &desc, nscp_connection_data &command_data); 116 void add_submit_options(po::options_description &desc, nscp_connection_data &command_data); 117 void add_exec_options(po::options_description &desc, nscp_connection_data &command_data); 100 std::list<nscp::packet> send_ssl(std::wstring host, int port, std::wstring cert, int timeout, const std::list<nscp::packet> &chunks); 101 void add_local_options(po::options_description &desc, nscp_connection_data &command_data); 118 102 119 103 NSCAPI::nagiosReturn query_nscp(std::list<std::wstring> &arguments, std::wstring &message, std::wstring perf); … … 121 105 122 106 private: 107 client::command_manager commands; 123 108 void add_command(std::wstring key, std::wstring args); 124 void add_server(std::wstring key, std::wstring args); 109 void add_target(std::wstring key, std::wstring args); 110 std::wstring setup(client::configuration config, const std::wstring &command); 125 111 126 112 }; -
modules/NSCPServer/CMakeLists.txt
r438998b r7515d00 18 18 ${NSCP_INCLUDEDIR}/nscp/packet.cpp 19 19 ${NSCP_INCLUDEDIR}/socket/socket_helpers.cpp 20 ${NSCP_INCLUDEDIR}/nscp/handler.cpp 20 21 21 22 ${NSCP_DEF_PLUGIN_CPP} … … 37 38 ${NSCP_INCLUDEDIR}/nscp/server/parser.hpp 38 39 ${NSCP_INCLUDEDIR}/nscp/packet.hpp 40 ${NSCP_INCLUDEDIR}/nscp/handler.hpp 39 41 ${NSCP_INCLUDEDIR}/swap_bytes.hpp 40 42 ${NSCP_INCLUDEDIR}/socket/socket_helpers.hpp -
modules/NSCPServer/NSCPServer.cpp
r81e420c r7515d00 32 32 namespace sh = nscapi::settings_helper; 33 33 34 NSCPListener::NSCPListener() : info_(boost::shared_ptr<nscp::server:: handler>(new handler_impl(1024))) {34 NSCPListener::NSCPListener() : info_(boost::shared_ptr<nscp::server::server_handler>(new handler_impl(1024))) { 35 35 } 36 36 NSCPListener::~NSCPListener() {} -
modules/NSCPServer/handler_impl.cpp
r8840f09 r7515d00 7 7 #include "handler_impl.hpp" 8 8 9 std::list<nscp::packet> handler_impl::process(nscp::packet &packet) { 10 std::list<nscp::packet> result; 9 NSCAPI::nagiosReturn handler_impl::handle_query_request(const std::string &request, Plugin::QueryRequestMessage &msg, std::string &reply) { 10 Plugin::Common::Header hdr; 11 hdr.CopyFrom(msg.header()); 11 12 12 Plugin::Common::Header hdr; 13 Plugin::QueryResponseMessage response; 14 // @todo: swap data in the dhear (ie. sender /recipent) 15 response.mutable_header()->CopyFrom(hdr); 13 16 14 if (packet.is_query_request()) { 15 Plugin::QueryRequestMessage msg; 16 msg.ParseFromString(packet.payload); 17 hdr.CopyFrom(msg.header()); 17 // @todo: Make split optional 18 for (int i=0;i<msg.payload_size();i++) { 19 const Plugin::QueryRequestMessage_Request &payload = msg.payload(i); 20 std::string outBuffer; 21 std::wstring command = utf8::cvt<std::wstring>(payload.command()); 18 22 19 // @todo: Make split optional 20 // @todo: Make this return ONE response not multiple responses 21 22 for (int i=0;i<msg.payload_size();i++) { 23 const Plugin::QueryRequestMessage_Request &payload = msg.payload(i); 24 std::string outBuffer; 25 std::wstring command = utf8::cvt<std::wstring>(payload.command()); 26 27 if (command.empty() || command == _T("_NSCP_CHECK")) { 28 nscapi::functions::create_simple_query_response(_T("_NSCP_CHECK"), NSCAPI::returnOK, _T("I (") + nscapi::plugin_singleton->get_core()->getApplicationVersionString() + _T(") seem to be doing fine..."), _T(""), outBuffer); 29 } else if (!allowArgs_ && payload.arguments_size() > 0) { 30 nscapi::functions::create_simple_query_response_unknown(command, _T("Arguments not allowed for command: ") + command, _T(""), outBuffer); 31 } else { 32 bool ok = true; 33 if (!allowNasty_) { 34 for (int j=0;j<payload.arguments_size();j++) { 35 if (payload.arguments(j).find_first_of(NASTY_METACHARS) != std::wstring::npos) { 36 ok = false; 37 break; 38 } 23 if (command.empty() || command == _T("_NSCP_CHECK")) { 24 nscapi::functions::create_simple_query_response(_T("_NSCP_CHECK"), NSCAPI::returnOK, _T("I (") + nscapi::plugin_singleton->get_core()->getApplicationVersionString() + _T(") seem to be doing fine..."), _T(""), outBuffer); 25 } else if (!allowArgs_ && payload.arguments_size() > 0) { 26 nscapi::functions::create_simple_query_response_unknown(command, _T("Arguments not allowed for command: ") + command, _T(""), outBuffer); 27 } else { 28 bool ok = true; 29 if (!allowNasty_) { 30 for (int j=0;j<payload.arguments_size();j++) { 31 if (payload.arguments(j).find_first_of(NASTY_METACHARS) != std::wstring::npos) { 32 ok = false; 33 break; 39 34 } 40 35 } 41 if (ok) { 42 std::string tmpBuffer; 43 Plugin::QueryRequestMessage tmp; 44 tmp.mutable_header()->CopyFrom(hdr); 45 tmp.add_payload()->CopyFrom(payload); 46 tmp.SerializeToString(&tmpBuffer); 47 NSCAPI::nagiosReturn returncode = nscapi::plugin_singleton->get_core()->query(command, tmpBuffer, outBuffer); 48 if (returncode == NSCAPI::returnIgnored) { 49 nscapi::functions::create_simple_query_response_unknown(command, _T("Command was not found: ") + command, _T(""), outBuffer); 50 } 51 } else { 52 nscapi::functions::create_simple_query_response_unknown(command, _T("Nasty arguments not allowed for command: ") + command, _T(""), outBuffer); 36 } 37 if (ok) { 38 std::string tmpBuffer; 39 Plugin::QueryRequestMessage tmp; 40 tmp.mutable_header()->CopyFrom(hdr); 41 tmp.add_payload()->CopyFrom(payload); 42 tmp.SerializeToString(&tmpBuffer); 43 NSCAPI::nagiosReturn returncode = nscapi::plugin_singleton->get_core()->query(command, tmpBuffer, outBuffer); 44 if (returncode == NSCAPI::returnIgnored) { 45 nscapi::functions::create_simple_query_response_unknown(command, _T("Command was not found: ") + command, _T(""), outBuffer); 53 46 } 47 } else { 48 nscapi::functions::create_simple_query_response_unknown(command, _T("Nasty arguments not allowed for command: ") + command, _T(""), outBuffer); 54 49 } 55 result.push_back(nscp::packet::create_query_response(outBuffer)); 50 Plugin::QueryResponseMessage tmpResponse; 51 tmpResponse.ParseFromString(outBuffer); 52 for (int i=0;i<tmpResponse.payload_size();i++) { 53 response.add_payload()->CopyFrom(tmpResponse.payload(i)); 54 } 56 55 } 57 } else if (packet.is_query_response()) { 58 59 // @todo handle submission here 60 61 } else { 62 NSC_LOG_ERROR(_T("Unknown packet: ") + packet.to_wstring()); 63 result.push_back(create_error(_T("Unknown packet: ") + packet.to_wstring())); 56 response.SerializeToString(&reply); 64 57 } 65 return result; 58 // @todo: fixme this should probably be an aggregate right? 59 return NSCAPI::isSuccess; 66 60 } 67 61 -
modules/NSCPServer/handler_impl.hpp
rb38e845 r7515d00 2 2 3 3 #include <nscp/packet.hpp> 4 #include <nscp/ server/handler.hpp>4 #include <nscp/handler.hpp> 5 5 #include <boost/tuple/tuple.hpp> 6 6 7 class handler_impl : p ublic nscp::server::handler, private boost::noncopyable{7 class handler_impl : private boost::noncopyable, public nscp::handler { 8 8 unsigned int payload_length_; 9 9 bool allowArgs_; … … 13 13 handler_impl(unsigned int payload_length) : payload_length_(payload_length), noPerfData_(false), allowNasty_(false), allowArgs_(false) {} 14 14 15 unsigned int get_payload_length() { 16 return payload_length_; 17 } 18 void set_payload_length(unsigned int payload) { 19 payload_length_ = payload; 20 } 21 22 std::list<nscp::packet> process(nscp::packet &buffer); 15 NSCAPI::nagiosReturn handle_query_request(const std::string &request, Plugin::QueryRequestMessage &msg, std::string &reply); 23 16 24 17 nscp::packet create_error(std::wstring msg) { 25 return nscp:: packet::create_error(msg);18 return nscp::factory::create_error(msg); 26 19 } 27 20 -
service/CMakeLists.txt
r81e420c r7515d00 99 99 settings_manager 100 100 ) 101 SET_TARGET_PROPERTIES(${TARGET} PROPERTIES FOLDER "core")102 101 #IF(WIN32) 103 102 # SET_TARGET_PROPERTIES(${TARGET} PROPERTIES LINK_FLAGS "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\" /SUBSYSTEM:WINDOWS") 104 103 #ENDIF(WIN32) 104 #INSTALL(TARGETS ${TARGET} RUNTIME DESTINATION .) 105 105 106 SOURCE_GROUP("Common Files" REGULAR_EXPRESSION .*include/.*) 107 SOURCE_GROUP("Parser" REGULAR_EXPRESSION .*include/parser/.*) 106 #INSTALL(CODE "SET(PDB_FULL_PATH ${BUILD_TARGET_EXE_PATH}/${TARGET}.pdb)") 107 #INSTALL(CODE "FILE(INSTALL DESTINATION \${CMAKE_INSTALL_PREFIX} TYPE EXECUTABLE FILES \${PDB_FULL_PATH})") 108 108 109 SOURCE_GROUP("NSCP API" REGULAR_EXPRESSION .*include/nscapi/.*) 109 110 SOURCE_GROUP("Settings" REGULAR_EXPRESSION .*include/settings/.*) 111 SOURCE_GROUP("Common Files" REGULAR_EXPRESSION .*include/.*) 110 112 111 #SOURCE_GROUP("NSCP API/Sources" FILES ${NSCP_DEF_PLUGIN_CPP}) 112 #SOURCE_GROUP("Settings" REGULAR_EXPRESSION .*settings.*) 113 114 INSTALL(TARGETS ${TARGET} RUNTIME DESTINATION .) 115 116 # First, build the full name of the EXE. 117 INSTALL(CODE "SET(PDB_FULL_PATH ${BUILD_TARGET_EXE_PATH}/${TARGET}.pdb)") 118 INSTALL(CODE "FILE(INSTALL DESTINATION \${CMAKE_INSTALL_PREFIX} TYPE EXECUTABLE FILES \${PDB_FULL_PATH})") 119 #INSTALL(CODE "MESSAGE(STATUS ----->\${CMAKE_INSTALL_PREFIX})") 120 #INSTALL(CODE "MESSAGE(STATUS ----->\${CMAKE_INSTALL_CONFIG_NAME})") 121 #INSTALL(CODE "MESSAGE(STATUS ----->${CMAKE_CURRENT_BINARY_DIR})") 122 #INSTALL(CODE "MESSAGE(STATUS ----->\${PDB_FULL_PATH})") 123 # Then, replace .exe with .pdb. 124 #INSTALL(CODE "STRING(REPLACE .exe .pdb PDB_FULL_PATH \${PDB_FULL_PATH})") 125 # Finally, get it installed. 113 SET_TARGET_PROPERTIES(${TARGET} PROPERTIES FOLDER "core") -
version.hpp
ra78a985 r7515d00 1 1 #ifndef VERSION_HPP 2 2 #define VERSION_HPP 3 #define PRODUCTVER 0,4,0,9 64 #define STRPRODUCTVER "0,4,0,9 6"5 #define STRPRODUCTDATE "2011-09-0 5"3 #define PRODUCTVER 0,4,0,98 4 #define STRPRODUCTVER "0,4,0,98" 5 #define STRPRODUCTDATE "2011-09-06" 6 6 #endif // VERSION_HPP -
version.txt
ra78a985 r7515d00 1 1 version=0.4.0 2 build=9 63 date=2011-09-0 52 build=98 3 date=2011-09-06
Note: See TracChangeset
for help on using the changeset viewer.








