| 1 | #pragma once
|
|---|
| 2 |
|
|---|
| 3 | #include <boost/date_time.hpp>
|
|---|
| 4 | #include <boost/lexical_cast.hpp>
|
|---|
| 5 |
|
|---|
| 6 | #include <types.hpp>
|
|---|
| 7 | #include <swap_bytes.hpp>
|
|---|
| 8 | #include <nsca/nsca_enrypt.hpp>
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 | namespace nsca {
|
|---|
| 12 | //#define NSCA_MAX_PLUGINOUTPUT_LENGTH 512
|
|---|
| 13 | //#define NSCA_MAX_PASSWORD_LENGTH 512
|
|---|
| 14 | class data {
|
|---|
| 15 | public:
|
|---|
| 16 | static const short transmitted_iuv_size = 128;
|
|---|
| 17 | static const short version3 = 3;
|
|---|
| 18 |
|
|---|
| 19 | typedef struct data_packet : public boost::noncopyable {
|
|---|
| 20 | int16_t packet_version;
|
|---|
| 21 | u_int32_t crc32_value;
|
|---|
| 22 | u_int32_t timestamp;
|
|---|
| 23 | int16_t return_code;
|
|---|
| 24 | char data[];
|
|---|
| 25 | /*
|
|---|
| 26 | char host_name[];
|
|---|
| 27 | char svc_description[];
|
|---|
| 28 | char plugin_output[];
|
|---|
| 29 | */
|
|---|
| 30 | //data_packet_struct() : packet_version(NSCA_PACKET_VERSION_3) {}
|
|---|
| 31 |
|
|---|
| 32 | char* get_host_ptr() {
|
|---|
| 33 | return &data[0];
|
|---|
| 34 | }
|
|---|
| 35 | char* get_desc_ptr(unsigned int host_len) {
|
|---|
| 36 | return &data[host_len];
|
|---|
| 37 | }
|
|---|
| 38 | char* get_result_ptr(unsigned int host_len, unsigned int desc_len) {
|
|---|
| 39 | return &data[host_len+desc_len];
|
|---|
| 40 | }
|
|---|
| 41 | } data_packet;
|
|---|
| 42 |
|
|---|
| 43 | /* initialization packet containing IV and timestamp */
|
|---|
| 44 | typedef struct iv_packet {
|
|---|
| 45 | char iv[transmitted_iuv_size];
|
|---|
| 46 | u_int32_t timestamp;
|
|---|
| 47 | } init_packet;
|
|---|
| 48 |
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | /* data packet containing service check results */
|
|---|
| 52 | class nsca_exception {
|
|---|
| 53 | std::wstring msg_;
|
|---|
| 54 | public:
|
|---|
| 55 | nsca_exception(std::wstring msg) : msg_(msg) {}
|
|---|
| 56 | std::wstring what() { return msg_;}
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | class length {
|
|---|
| 60 | public:
|
|---|
| 61 | typedef unsigned int size_type;
|
|---|
| 62 | static const short host_length = 64;
|
|---|
| 63 | static const short desc_length = 128;
|
|---|
| 64 | public:
|
|---|
| 65 | class data {
|
|---|
| 66 | public:
|
|---|
| 67 | static size_type payload_length_;
|
|---|
| 68 | static void set_payload_length(size_type length) {
|
|---|
| 69 | payload_length_ = length;
|
|---|
| 70 | }
|
|---|
| 71 | static size_type get_packet_length() {
|
|---|
| 72 | return get_packet_length(payload_length_);
|
|---|
| 73 | }
|
|---|
| 74 | static size_type get_packet_length(size_type output_length) {
|
|---|
| 75 | return sizeof(nsca::data::data_packet)+output_length*sizeof(char)+host_length*sizeof(char)+desc_length*sizeof(char);
|
|---|
| 76 | }
|
|---|
| 77 | static size_type get_payload_length() {
|
|---|
| 78 | return payload_length_;
|
|---|
| 79 | }
|
|---|
| 80 | static size_type get_payload_length(size_type packet_length) {
|
|---|
| 81 | return (packet_length- (host_length*sizeof(char)+desc_length*sizeof(char)+sizeof(nsca::data::data_packet)) )/sizeof(char);
|
|---|
| 82 | }
|
|---|
| 83 | };
|
|---|
| 84 | class iv {
|
|---|
| 85 | public:
|
|---|
| 86 | static const unsigned int payload_length_ = nsca::data::transmitted_iuv_size;
|
|---|
| 87 | /*
|
|---|
| 88 | static void set_payload_length(size_type length) {
|
|---|
| 89 | payload_length_ = length;
|
|---|
| 90 | }
|
|---|
| 91 | */
|
|---|
| 92 | static size_type get_packet_length() {
|
|---|
| 93 | return get_packet_length(payload_length_);
|
|---|
| 94 | }
|
|---|
| 95 | static size_type get_packet_length(size_type output_length) {
|
|---|
| 96 | return sizeof(nsca::data::iv_packet);
|
|---|
| 97 | }
|
|---|
| 98 | static size_type get_payload_length() {
|
|---|
| 99 | return payload_length_;
|
|---|
| 100 | }
|
|---|
| 101 | static size_type get_payload_length(size_type packet_length) {
|
|---|
| 102 | return payload_length_;
|
|---|
| 103 | }
|
|---|
| 104 | };
|
|---|
| 105 | };
|
|---|
| 106 |
|
|---|
| 107 | class packet {
|
|---|
| 108 | public:
|
|---|
| 109 | std::string service;
|
|---|
| 110 | std::string result;
|
|---|
| 111 | std::string host;
|
|---|
| 112 | unsigned int code;
|
|---|
| 113 | unsigned int time;
|
|---|
| 114 | unsigned int payload_length_;
|
|---|
| 115 | public:
|
|---|
| 116 | packet(std::string _host, unsigned int payload_length = 512, int time_delta = 0) : host(_host), payload_length_(payload_length) {
|
|---|
| 117 | boost::posix_time::ptime now = boost::posix_time::second_clock::local_time()
|
|---|
| 118 | + boost::posix_time::seconds(time_delta);
|
|---|
| 119 | boost::posix_time::ptime time_t_epoch(boost::gregorian::date(1970,1,1));
|
|---|
| 120 | boost::posix_time::time_duration diff = now - time_t_epoch;
|
|---|
| 121 | time = diff.total_seconds();
|
|---|
| 122 | }
|
|---|
| 123 | packet() : payload_length_(nsca::length::data::get_payload_length())
|
|---|
| 124 | {
|
|---|
| 125 | }
|
|---|
| 126 | packet(unsigned char* buffer, unsigned int buffer_len, unsigned int payload_length) : payload_length_(payload_length) {
|
|---|
| 127 | // TODO: Parse here
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | std::string toString() const {
|
|---|
| 131 | return "service: " + service + ", " +
|
|---|
| 132 | "code: " + boost::lexical_cast<std::string>(code) + ", " +
|
|---|
| 133 | "time: " + boost::lexical_cast<std::string>(time) + ", " +
|
|---|
| 134 | "result: " + result;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | void get_buffer(std::string &buffer) const {
|
|---|
| 138 | // FIXME: This is crap and needs rewriting. No std::string and beetter zero handling...
|
|---|
| 139 | if (service.length() >= nsca::length::desc_length)
|
|---|
| 140 | throw nsca::nsca_exception(_T("Description field to long"));
|
|---|
| 141 | if (host.length() >= nsca::length::host_length)
|
|---|
| 142 | throw nsca::nsca_exception(_T("Host field to long"));
|
|---|
| 143 | if (result.length() >= get_payload_length())
|
|---|
| 144 | throw nsca::nsca_exception(_T("Result field to long"));
|
|---|
| 145 | if (buffer.size() < get_packet_length())
|
|---|
| 146 | throw nsca::nsca_exception(_T("Buffer is to short"));
|
|---|
| 147 |
|
|---|
| 148 | nsca::data::data_packet *data = reinterpret_cast<nsca::data::data_packet*>(&*buffer.begin());
|
|---|
| 149 |
|
|---|
| 150 | data->packet_version=swap_bytes::hton<int16_t>(nsca::data::version3);
|
|---|
| 151 | data->timestamp=swap_bytes::hton<u_int32_t>(time);
|
|---|
| 152 | data->return_code = swap_bytes::hton<int16_t>(code);
|
|---|
| 153 | data->crc32_value= swap_bytes::hton<u_int32_t>(0);
|
|---|
| 154 |
|
|---|
| 155 | memset(data->get_host_ptr(), 0, host.size()+1);
|
|---|
| 156 | host.copy(data->get_host_ptr(), host.size());
|
|---|
| 157 | memset(data->get_desc_ptr(nsca::length::host_length), 0, service.size()+1);
|
|---|
| 158 | service.copy(data->get_desc_ptr(nsca::length::host_length), service.size());
|
|---|
| 159 | memset(data->get_result_ptr(nsca::length::host_length, nsca::length::desc_length), 0, result.size()+1);
|
|---|
| 160 | result.copy(data->get_result_ptr(nsca::length::host_length, nsca::length::desc_length), result.size());
|
|---|
| 161 |
|
|---|
| 162 | unsigned int calculated_crc32=calculate_crc32(buffer.c_str(),buffer.size());
|
|---|
| 163 | data->crc32_value=swap_bytes::hton<u_int32_t>(calculated_crc32);
|
|---|
| 164 | }
|
|---|
| 165 | std::string get_buffer() const {
|
|---|
| 166 | std::string buffer;
|
|---|
| 167 | get_buffer(buffer);
|
|---|
| 168 | return buffer;
|
|---|
| 169 | }
|
|---|
| 170 | unsigned int get_packet_length() const { return nsca::length::data::get_packet_length(payload_length_); }
|
|---|
| 171 | unsigned int get_payload_length() const { return payload_length_; }
|
|---|
| 172 | };
|
|---|
| 173 |
|
|---|
| 174 | class iv_packet {
|
|---|
| 175 | public:
|
|---|
| 176 | iv_packet() {
|
|---|
| 177 | }
|
|---|
| 178 | iv_packet(char* buf, unsigned int len) {
|
|---|
| 179 |
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | };
|
|---|
| 183 | }
|
|---|