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