1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #ifndef ZABBIX_ZJSON_H
21 #define ZABBIX_ZJSON_H
22 
23 #include <stdarg.h>
24 
25 #define ZBX_PROTO_TAG_CLOCK		"clock"
26 #define ZBX_PROTO_TAG_NS		"ns"
27 #define ZBX_PROTO_TAG_DATA		"data"
28 #define ZBX_PROTO_TAG_REGEXP		"regexp"
29 #define ZBX_PROTO_TAG_DELAY		"delay"
30 #define ZBX_PROTO_TAG_DRULE		"drule"
31 #define ZBX_PROTO_TAG_DCHECK		"dcheck"
32 #define ZBX_PROTO_TAG_HOST		"host"
33 #define ZBX_PROTO_TAG_HOST_METADATA	"host_metadata"
34 #define ZBX_PROTO_TAG_INFO		"info"
35 #define ZBX_PROTO_TAG_IP		"ip"
36 #define ZBX_PROTO_TAG_DNS		"dns"
37 #define ZBX_PROTO_TAG_CONN		"conn"
38 #define ZBX_PROTO_TAG_KEY		"key"
39 #define ZBX_PROTO_TAG_KEY_ORIG		"key_orig"
40 #define ZBX_PROTO_TAG_KEYS		"keys"
41 #define ZBX_PROTO_TAG_LASTLOGSIZE	"lastlogsize"
42 #define ZBX_PROTO_TAG_MTIME		"mtime"
43 #define ZBX_PROTO_TAG_LOGTIMESTAMP	"timestamp"
44 #define ZBX_PROTO_TAG_LOGSOURCE		"source"
45 #define ZBX_PROTO_TAG_LOGSEVERITY	"severity"
46 #define ZBX_PROTO_TAG_LOGEVENTID	"eventid"
47 #define ZBX_PROTO_TAG_PORT		"port"
48 #define ZBX_PROTO_TAG_PROXY		"proxy"
49 #define ZBX_PROTO_TAG_REQUEST		"request"
50 #define ZBX_PROTO_TAG_RESPONSE		"response"
51 #define ZBX_PROTO_TAG_STATUS		"status"
52 #define ZBX_PROTO_TAG_STATE		"state"
53 #define ZBX_PROTO_TAG_TYPE		"type"
54 #define ZBX_PROTO_TAG_LIMIT		"limit"
55 #define ZBX_PROTO_TAG_VALUE		"value"
56 #define ZBX_PROTO_TAG_SCRIPTID		"scriptid"
57 #define ZBX_PROTO_TAG_HOSTID		"hostid"
58 #define ZBX_PROTO_TAG_AVAILABLE		"available"
59 #define ZBX_PROTO_TAG_SNMP_AVAILABLE	"snmp_available"
60 #define ZBX_PROTO_TAG_IPMI_AVAILABLE	"ipmi_available"
61 #define ZBX_PROTO_TAG_JMX_AVAILABLE	"jmx_available"
62 #define ZBX_PROTO_TAG_ERROR		"error"
63 #define ZBX_PROTO_TAG_SNMP_ERROR	"snmp_error"
64 #define ZBX_PROTO_TAG_IPMI_ERROR	"ipmi_error"
65 #define ZBX_PROTO_TAG_JMX_ERROR		"jmx_error"
66 #define ZBX_PROTO_TAG_USERNAME		"username"
67 #define ZBX_PROTO_TAG_PASSWORD		"password"
68 #define ZBX_PROTO_TAG_SID		"sid"
69 
70 #define ZBX_PROTO_VALUE_FAILED		"failed"
71 #define ZBX_PROTO_VALUE_SUCCESS		"success"
72 
73 #define ZBX_PROTO_VALUE_GET_ACTIVE_CHECKS	"active checks"
74 #define ZBX_PROTO_VALUE_PROXY_CONFIG		"proxy config"
75 #define ZBX_PROTO_VALUE_PROXY_HEARTBEAT		"proxy heartbeat"
76 #define ZBX_PROTO_VALUE_DISCOVERY_DATA		"discovery data"
77 #define ZBX_PROTO_VALUE_HOST_AVAILABILITY	"host availability"
78 #define ZBX_PROTO_VALUE_HISTORY_DATA		"history data"
79 #define ZBX_PROTO_VALUE_AUTO_REGISTRATION_DATA	"auto registration"
80 #define ZBX_PROTO_VALUE_SENDER_DATA		"sender data"
81 #define ZBX_PROTO_VALUE_AGENT_DATA		"agent data"
82 #define ZBX_PROTO_VALUE_COMMAND			"command"
83 #define ZBX_PROTO_VALUE_JAVA_GATEWAY_INTERNAL	"java gateway internal"
84 #define ZBX_PROTO_VALUE_JAVA_GATEWAY_JMX	"java gateway jmx"
85 #define ZBX_PROTO_VALUE_GET_QUEUE		"queue.get"
86 
87 #define ZBX_PROTO_VALUE_GET_QUEUE_OVERVIEW	"overview"
88 #define ZBX_PROTO_VALUE_GET_QUEUE_PROXY		"overview by proxy"
89 #define ZBX_PROTO_VALUE_GET_QUEUE_DETAILS	"details"
90 
91 typedef enum
92 {
93 	ZBX_JSON_TYPE_UNKNOWN = 0,
94 	ZBX_JSON_TYPE_STRING,
95 	ZBX_JSON_TYPE_INT,
96 	ZBX_JSON_TYPE_ARRAY,
97 	ZBX_JSON_TYPE_OBJECT,
98 	ZBX_JSON_TYPE_NULL
99 }
100 zbx_json_type_t;
101 
102 typedef enum
103 {
104 	ZBX_JSON_EMPTY = 0,
105 	ZBX_JSON_COMMA
106 }
107 zbx_json_status_t;
108 
109 #define ZBX_JSON_STAT_BUF_LEN 4096
110 
111 struct zbx_json
112 {
113 	char			*buffer;
114 	char			buf_stat[ZBX_JSON_STAT_BUF_LEN];
115 	size_t			buffer_allocated;
116 	size_t			buffer_offset;
117 	size_t			buffer_size;
118 	zbx_json_status_t	status;
119 	int			level;
120 };
121 
122 struct zbx_json_parse
123 {
124 	const char		*start;
125 	const char		*end;
126 };
127 
128 const char	*zbx_json_strerror(void);
129 
130 void	zbx_json_init(struct zbx_json *j, size_t allocate);
131 void	zbx_json_clean(struct zbx_json *j);
132 void	zbx_json_free(struct zbx_json *j);
133 void	zbx_json_addobject(struct zbx_json *j, const char *name);
134 void	zbx_json_addarray(struct zbx_json *j, const char *name);
135 void	zbx_json_addstring(struct zbx_json *j, const char *name, const char *string, zbx_json_type_t type);
136 void	zbx_json_adduint64(struct zbx_json *j, const char *name, zbx_uint64_t value);
137 int	zbx_json_close(struct zbx_json *j);
138 
139 int		zbx_json_open(const char *buffer, struct zbx_json_parse *jp);
140 const char	*zbx_json_next(const struct zbx_json_parse *jp, const char *p);
141 const char	*zbx_json_next_value(const struct zbx_json_parse *jp, const char *p, char *string, size_t len, int *is_null);
142 const char	*zbx_json_next_value_dyn(const struct zbx_json_parse *jp, const char *p, char **string,
143 		size_t *string_alloc, int *is_null);
144 const char	*zbx_json_pair_next(const struct zbx_json_parse *jp, const char *p, char *name, size_t len);
145 const char	*zbx_json_pair_by_name(const struct zbx_json_parse *jp, const char *name);
146 int		zbx_json_value_by_name(const struct zbx_json_parse *jp, const char *name, char *string, size_t len);
147 int		zbx_json_value_by_name_dyn(const struct zbx_json_parse *jp, const char *name, char **string, size_t *string_alloc);
148 int		zbx_json_brackets_open(const char *p, struct zbx_json_parse *out);
149 int		zbx_json_brackets_by_name(const struct zbx_json_parse *jp, const char *name, struct zbx_json_parse *out);
150 int		zbx_json_object_is_empty(const struct zbx_json_parse *jp);
151 int		zbx_json_count(const struct zbx_json_parse *jp);
152 
153 #endif /* ZABBIX_ZJSON_H */
154