luasandbox  1.4.0
Generic Lua sandbox for dynamic data analysis
heka_message.h
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /** Heka message representation @file */
8 
9 #ifndef luasandbox_util_heka_message_h_
10 #define luasandbox_util_heka_message_h_
11 
12 #include <stdbool.h>
13 #include <stddef.h>
14 
15 #include "input_buffer.h"
16 #include "output_buffer.h"
17 #include "string.h"
18 #include "util.h"
19 
20 #define LSB_UUID_SIZE 16
21 #define LSB_UUID_STR_SIZE 36
22 #define LSB_HDR_FRAME_SIZE 3
23 #define LSB_MIN_HDR_SIZE 14
24 #define LSB_MAX_HDR_SIZE (255 + LSB_HDR_FRAME_SIZE)
25 
26 #define LSB_UUID "Uuid"
27 #define LSB_TIMESTAMP "Timestamp"
28 #define LSB_TYPE "Type"
29 #define LSB_LOGGER "Logger"
30 #define LSB_SEVERITY "Severity"
31 #define LSB_PAYLOAD "Payload"
32 #define LSB_ENV_VERSION "EnvVersion"
33 #define LSB_PID "Pid"
34 #define LSB_HOSTNAME "Hostname"
35 #define LSB_FIELDS "Fields"
36 #define LSB_RAW "raw"
37 #define LSB_FRAMED "framed"
38 #define LSB_SIZE "size"
39 
40 typedef enum {
52 
53 typedef enum {
62 } lsb_pb_field;
63 
64 typedef enum {
71 
72 typedef struct lsb_heka_field
73 {
79 
80 typedef struct lsb_heka_message
81 {
89 
91 
92  long long timestamp;
93  int severity;
94  int pid;
98 
99 typedef enum {
104 } lsb_read_type;
105 
106 typedef struct {
107  union
108  {
110  double d;
111  } u;
114 
115 #ifdef __cplusplus
116 extern "C"
117 {
118 #endif
119 
120 /**
121  * Zero the structure and allocate memory for at least 'size' fields
122  *
123  * @param m Heka message structure
124  * @param num_fields Preallocated number of fields (must be >0)
125  *
126  * @return lsb_err_value NULL on success error message on failure
127  *
128  */
130 lsb_init_heka_message(lsb_heka_message *m, int num_fields);
131 
132 /**
133  * Frees the memory allocated for the message fields
134  *
135  * @param m Heka message structure
136  *
137  */
139 
140 /**
141  * Resets the message headers and fields zeroing the allocated memory
142  *
143  * @param m Heka message structure
144  *
145  */
147 
148 /**
149  * Locates a framed Heka message in an input buffer
150  *
151  * @param m Heka message structure
152  * @param ib Input buffer
153  * @param decode True if the framed message should be protobuf decoded
154  * @param discarded_bytes Used to track stream corruption
155  * @param logger Logger structure (can be set to NULL to disable logging)
156  *
157  * @return bool True on success
158  */
160  lsb_input_buffer *ib,
161  bool decode,
162  size_t *discarded_bytes,
163  lsb_logger *logger);
164 
165 /**
166  * Decodes an array of bytes into a Heka message. The message structure is
167  * cleared before decoding.
168  *
169  * @param m Heka message structure
170  * @param buf Protobuf array
171  * @param len Length of the protobuf array
172  * @param logger Logger structure (can be set to NULL to disable logging)
173  *
174  * @return bool True on success
175  *
176  */
178  const char *buf,
179  size_t len,
180  lsb_logger *logger);
181 
182 /**
183  * Reads a dynamic field from the Heka message
184  *
185  * @param m Heka meassage structure
186  * @param name Field name
187  * @param fi Field index
188  * @param ai Array index into the field
189  * @param val Value structure to be populated by the read
190  *
191  * @return bool True on success
192  */
195  int fi,
196  int ai,
197  lsb_read_value *val);
198 
199 /**
200  * Writes a binary UUID to the output buffer
201  *
202  * @param ob Pointer to the output data buffer.
203  * @param uuid Uuid string to output (can be NULL, binary, human readable UUID)
204  * @param len Length of UUID (16 or 36 anything else will cause a new UUID to be
205  * created).
206  *
207  * @return lsb_err_value NULL on success error message on failure
208  */
210 lsb_write_heka_uuid(lsb_output_buffer *ob, const char *uuid, size_t len);
211 
212 /**
213  * Writes the Heka framing header to the specified buffer.
214  *
215  * @param buf Buffer to write the header to must be at least LSB_MIN_HDR_SIZE
216  * size.
217  * @param len Length of the message to encode into the header
218  *
219  * @return LSB_UTIL_EXPORT size_t
220  */
221 LSB_UTIL_EXPORT size_t lsb_write_heka_header(char *buf, size_t len);
222 
223 #ifdef __cplusplus
224 }
225 #endif
226 
227 #endif
LSB_UTIL_EXPORT void lsb_clear_heka_message(lsb_heka_message *m)
Resets the message headers and fields zeroing the allocated memory.
const char * lsb_err_value
Definition: error.h:15
Heka message string.
lsb_pb_value_types
Definition: heka_message.h:64
lsb_const_string payload
Definition: heka_message.h:86
struct lsb_heka_field lsb_heka_field
lsb_const_string name
Definition: heka_message.h:74
lsb_heka_field * fields
Definition: heka_message.h:90
LSB_UTIL_EXPORT void lsb_free_heka_message(lsb_heka_message *m)
Frees the memory allocated for the message fields.
LSB_UTIL_EXPORT bool lsb_read_heka_field(const lsb_heka_message *m, lsb_const_string *name, int fi, int ai, lsb_read_value *val)
Reads a dynamic field from the Heka message.
lsb_const_string env_version
Definition: heka_message.h:87
lsb_const_string representation
Definition: heka_message.h:75
lsb_const_string hostname
Definition: heka_message.h:88
Data stream output buffer.
lsb_read_type type
Definition: heka_message.h:112
Shared types and structures.
#define LSB_UTIL_EXPORT
Definition: util.h:28
lsb_pb_field
Definition: heka_message.h:53
long long timestamp
Definition: heka_message.h:92
lsb_const_string uuid
Definition: heka_message.h:83
lsb_const_string s
Definition: heka_message.h:109
lsb_const_string type
Definition: heka_message.h:84
LSB_UTIL_EXPORT bool lsb_decode_heka_message(lsb_heka_message *m, const char *buf, size_t len, lsb_logger *logger)
Decodes an array of bytes into a Heka message.
lsb_pb_value_types value_type
Definition: heka_message.h:77
lsb_const_string logger
Definition: heka_message.h:85
lsb_pb_message
Definition: heka_message.h:40
LSB_UTIL_EXPORT size_t lsb_write_heka_header(char *buf, size_t len)
Writes the Heka framing header to the specified buffer.
lsb_const_string raw
Definition: heka_message.h:82
struct lsb_heka_message lsb_heka_message
Data stream input buffer.
LSB_UTIL_EXPORT bool lsb_find_heka_message(lsb_heka_message *m, lsb_input_buffer *ib, bool decode, size_t *discarded_bytes, lsb_logger *logger)
Locates a framed Heka message in an input buffer.
lsb_const_string value
Definition: heka_message.h:76
LSB_UTIL_EXPORT lsb_err_value lsb_write_heka_uuid(lsb_output_buffer *ob, const char *uuid, size_t len)
Writes a binary UUID to the output buffer.
lsb_read_type
Definition: heka_message.h:99
LSB_UTIL_EXPORT lsb_err_value lsb_init_heka_message(lsb_heka_message *m, int num_fields)
Zero the structure and allocate memory for at least &#39;size&#39; fields.