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 #include "common.h"
21 #include "comms.h"
22 #include "zbxjson.h"
23 #include "log.h"
24
25 /******************************************************************************
26 * *
27 * Function: zbx_send_response_ext *
28 * *
29 * Purpose: send json SUCCEED or FAIL to socket along with an info message *
30 * *
31 * Parameters: sock - [IN] socket descriptor *
32 * result - [IN] SUCCEED or FAIL *
33 * info - [IN] info message *
34 * timeout - [IN] timeout for this operation *
35 * *
36 * Return value: SUCCEED - data successfully transmited *
37 * NETWORK_ERROR - network related error occurred *
38 * *
39 * Author: Alexander Vladishev, Alexei Vladishev *
40 * *
41 ******************************************************************************/
zbx_send_response_ext(zbx_socket_t * sock,int result,const char * info,int protocol,int timeout)42 int zbx_send_response_ext(zbx_socket_t *sock, int result, const char *info, int protocol, int timeout)
43 {
44 const char *__function_name = "zbx_send_response_ext";
45
46 struct zbx_json json;
47 const char *resp;
48 int ret = SUCCEED;
49
50 zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
51
52 zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN);
53
54 resp = SUCCEED == result ? ZBX_PROTO_VALUE_SUCCESS : ZBX_PROTO_VALUE_FAILED;
55
56 zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE, resp, ZBX_JSON_TYPE_STRING);
57
58 if (NULL != info && '\0' != *info)
59 zbx_json_addstring(&json, ZBX_PROTO_TAG_INFO, info, ZBX_JSON_TYPE_STRING);
60
61 zabbix_log(LOG_LEVEL_DEBUG, "%s() '%s'", __function_name, json.buffer);
62
63 if (FAIL == (ret = zbx_tcp_send_ext(sock, json.buffer, strlen(json.buffer), protocol, timeout)))
64 {
65 zabbix_log(LOG_LEVEL_DEBUG, "Error sending result back: %s", zbx_socket_strerror());
66 ret = NETWORK_ERROR;
67 }
68
69 zbx_json_free(&json);
70
71 zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
72
73 return ret;
74 }
75
76 /******************************************************************************
77 * *
78 * Function: zbx_recv_response *
79 * *
80 * Purpose: read a response message (in JSON format) from socket, optionally *
81 * extract "info" value. *
82 * *
83 * Parameters: sock - [IN] socket descriptor *
84 * timeout - [IN] timeout for this operation *
85 * error - [OUT] pointer to error message *
86 * *
87 * Return value: SUCCEED - "response":"success" successfully retrieved *
88 * FAIL - otherwise *
89 * Comments: *
90 * Allocates memory. *
91 * *
92 * If an error occurs, the function allocates dynamic memory for an error *
93 * message and writes its address into location pointed to by "error" *
94 * parameter. *
95 * *
96 * When the "info" value is present in the response message then function *
97 * copies the "info" value into the "error" buffer as additional *
98 * information *
99 * *
100 * IMPORTANT: it is a responsibility of the caller to release the *
101 * "error" memory ! *
102 * *
103 ******************************************************************************/
zbx_recv_response(zbx_socket_t * sock,int timeout,char ** error)104 int zbx_recv_response(zbx_socket_t *sock, int timeout, char **error)
105 {
106 const char *__function_name = "zbx_recv_response";
107
108 struct zbx_json_parse jp;
109 char value[16];
110 int ret = FAIL;
111
112 zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
113
114 if (SUCCEED != zbx_tcp_recv_to(sock, timeout))
115 {
116 /* since we have successfully sent data earlier, we assume the other */
117 /* side is just too busy processing our data if there is no response */
118 *error = zbx_strdup(*error, zbx_socket_strerror());
119 goto out;
120 }
121
122 zabbix_log(LOG_LEVEL_DEBUG, "%s() '%s'", __function_name, sock->buffer);
123
124 /* deal with empty string here because zbx_json_open() does not produce an error message in this case */
125 if ('\0' == *sock->buffer)
126 {
127 *error = zbx_strdup(*error, "empty string received");
128 goto out;
129 }
130
131 if (SUCCEED != zbx_json_open(sock->buffer, &jp))
132 {
133 *error = zbx_strdup(*error, zbx_json_strerror());
134 goto out;
135 }
136
137 if (SUCCEED != zbx_json_value_by_name(&jp, ZBX_PROTO_TAG_RESPONSE, value, sizeof(value)))
138 {
139 *error = zbx_strdup(*error, "no \"" ZBX_PROTO_TAG_RESPONSE "\" tag");
140 goto out;
141 }
142
143 if (0 != strcmp(value, ZBX_PROTO_VALUE_SUCCESS))
144 {
145 char *info = NULL;
146 size_t info_alloc = 0;
147
148 if (SUCCEED == zbx_json_value_by_name_dyn(&jp, ZBX_PROTO_TAG_INFO, &info, &info_alloc))
149 *error = zbx_strdup(*error, info);
150 else
151 *error = zbx_dsprintf(*error, "negative response \"%s\"", value);
152 zbx_free(info);
153 goto out;
154 }
155
156 ret = SUCCEED;
157 out:
158 zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
159
160 return ret;
161 }
162