source: nscp/include/nsca/nsca_packet.hpp @ f7a074d

0.4.00.4.10.4.2
Last change on this file since f7a074d was f7a074d, checked in by Michael Medin <michael@…>, 19 months ago
  • Had some "vacation" so no updates for a while
  • A lot of fixes to the NSCA parts (now 100% compatible with old settings file)
  • Added option to read sections (to plugin API)
  • Fixed issues in settings wrapper so child paths are also mapped (not just keys)
  • Many fixes related to NRPE/NSCA/*
  • "RC quality" expect RC within the week (only need to fix default config file somehow)
  • Upgraded all unit test to use the simplified API
  • Fixed a some unicode issues in PythonScript module
  • Improved threading a bit
  • Fixed CheckSystem (service check)
  • Fixed duplicate keys (when replacing) in old settings client
  • Property mode set to 100644
File size: 7.3 KB
Line 
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#include <unicode_char.hpp>
11#include <utils.h>
12
13namespace 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 {
64                std::wstring msg_;
65        public:
66                nsca_exception(std::wstring msg) : msg_(msg) {}
67                std::wstring what() { return msg_;}
68        };
69
70        class length {
71        public:
72                typedef unsigned int size_type;
73                static const short host_length = 64;
74                static const short desc_length = 128;
75        public:
76                static size_type payload_length_;
77                static void set_payload_length(size_type length) {
78                        payload_length_ = length;
79                }
80                static size_type get_packet_length() {
81                        return get_packet_length(payload_length_);
82                }
83                static size_type get_packet_length(size_type output_length) {
84                        return sizeof(nsca::data::data_packet)+output_length*sizeof(char)+host_length*sizeof(char)+desc_length*sizeof(char);
85                }
86                static size_type get_payload_length() {
87                        return payload_length_;
88                }
89                static size_type get_payload_length(size_type packet_length) {
90                        return (packet_length- (host_length*sizeof(char)+desc_length*sizeof(char)+sizeof(nsca::data::data_packet)) )/sizeof(char);
91                }
92                class iv {
93                public:
94                        static const unsigned int payload_length_ = nsca::data::transmitted_iuv_size;
95                        /*
96                        static void set_payload_length(size_type length) {
97                                payload_length_ = length;
98                        }
99                        */
100                        static size_type get_packet_length() {
101                                return get_packet_length(payload_length_);
102                        }
103                        static size_type get_packet_length(size_type output_length) {
104                                return sizeof(nsca::data::iv_packet);
105                        }
106                        static size_type get_payload_length() {
107                                return payload_length_;
108                        }
109                        static size_type get_payload_length(size_type packet_length) {
110                                return payload_length_;
111                        }
112                };
113        };
114
115        class packet {
116        public:
117                std::string service;
118                std::string result;
119                std::string host;
120                unsigned int code;
121                unsigned int time;
122                unsigned int payload_length_;
123        public:
124                packet(std::string _host, unsigned int payload_length = 512, int time_delta = 0) : host(_host), payload_length_(payload_length) {
125                        boost::posix_time::ptime now = boost::posix_time::second_clock::local_time()
126                                 + boost::posix_time::seconds(time_delta);
127                        boost::posix_time::ptime time_t_epoch(boost::gregorian::date(1970,1,1));
128                        boost::posix_time::time_duration diff = now - time_t_epoch;
129                        time = diff.total_seconds();
130                }
131                packet(unsigned int payload_length) : payload_length_(payload_length) {}
132                packet() : payload_length_(nsca::length::get_payload_length()) {}
133
134                std::string to_string() const {
135                        return "service: " + service + ", " +
136                                "code: " + boost::lexical_cast<std::string>(code) + ", " +
137                                "time: " + boost::lexical_cast<std::string>(time) + ", " +
138                                "result: " + result;
139                }
140
141                void parse_data(const char* buffer, unsigned int buffer_len) {
142                        char *tmp = new char[buffer_len];
143                        memcpy(tmp, buffer, buffer_len);
144                        nsca::data::data_packet *data = reinterpret_cast<nsca::data::data_packet*>(tmp);
145                        //packet_version=swap_bytes::ntoh<int16_t>(data->packet_version);
146                        time=swap_bytes::ntoh<u_int32_t>(data->timestamp);
147                        code = swap_bytes::ntoh<int16_t>(data->return_code);
148                        //data->crc32_value= swap_bytes::hton<u_int32_t>(0);
149
150                        host = data->get_host_ptr();
151                        service= data->get_desc_ptr(nsca::length::host_length);
152                        result= data->get_result_ptr(nsca::length::host_length, nsca::length::desc_length);
153
154                        unsigned int crc32 = swap_bytes::ntoh<u_int32_t>(data->crc32_value);
155                        data->crc32_value = 0;
156                        unsigned int calculated_crc32=calculate_crc32(tmp, buffer_len);
157                        delete [] tmp;
158                        if (crc32 != calculated_crc32)
159                                throw nsca::nsca_exception(_T("Invalid crc: ") + strEx::itos(crc32) + _T(" != ") + strEx::itos(calculated_crc32));
160                }
161
162                void get_buffer(std::string &buffer) const {
163                        // FIXME: This is crap and needs rewriting. No std::string and beetter zero handling...
164                        if (service.length() >= nsca::length::desc_length)
165                                throw nsca::nsca_exception(_T("Description field to long"));
166                        if (host.length() >= nsca::length::host_length)
167                                throw nsca::nsca_exception(_T("Host field to long"));
168                        if (result.length() >= get_payload_length())
169                                throw nsca::nsca_exception(_T("Result field to long"));
170                        if (buffer.size() < get_packet_length())
171                                throw nsca::nsca_exception(_T("Buffer is to short"));
172
173                        nsca::data::data_packet *data = reinterpret_cast<nsca::data::data_packet*>(&*buffer.begin());
174
175                        data->packet_version=swap_bytes::hton<int16_t>(nsca::data::version3);
176                        data->timestamp=swap_bytes::hton<u_int32_t>(time);
177                        data->return_code = swap_bytes::hton<int16_t>(code);
178                        data->crc32_value= swap_bytes::hton<u_int32_t>(0);
179
180                        memset(data->get_host_ptr(), 0, host.size()+1);
181                        host.copy(data->get_host_ptr(), host.size());
182                        memset(data->get_desc_ptr(nsca::length::host_length), 0, service.size()+1);
183                        service.copy(data->get_desc_ptr(nsca::length::host_length), service.size());
184                        memset(data->get_result_ptr(nsca::length::host_length, nsca::length::desc_length), 0, result.size()+1);
185                        result.copy(data->get_result_ptr(nsca::length::host_length, nsca::length::desc_length), result.size());
186
187                        unsigned int calculated_crc32=calculate_crc32(buffer.c_str(),buffer.size());
188                        data->crc32_value=swap_bytes::hton<u_int32_t>(calculated_crc32);
189                }
190                std::string get_buffer() const {
191                        std::string buffer;
192                        get_buffer(buffer);
193                        return buffer;
194                }
195                unsigned int get_packet_length() const { return nsca::length::get_packet_length(payload_length_); }
196                unsigned int get_payload_length() const { return payload_length_; }
197        };
198
199        class iv_packet {
200                std::string iv;
201        public:
202                iv_packet(std::string iv) : iv(iv) {
203                }
204                std::string get_buffer() const {
205                        nsca::data::iv_packet data;
206                        strncpy(data.iv, iv.c_str(), iv.size());
207                        char *src = reinterpret_cast<char*>(&data);
208                        std::string buffer(src, nsca::length::iv::get_packet_length());
209                        return buffer;
210                }
211//              iv_packet(char* buf, unsigned int len) {
212//
213//              }
214
215        };
216}
Note: See TracBrowser for help on using the repository browser.