telemetry: fix error checking for strchr function
[dpdk.git] / lib / librte_telemetry / telemetry_legacy.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <sys/socket.h>
7 #include <sys/un.h>
8 #include <pthread.h>
9
10 /* we won't link against libbsd, so just always use DPDKs-specific strlcpy */
11 #undef RTE_USE_LIBBSD
12 #include <rte_string_fns.h>
13 #include <rte_common.h>
14 #include <rte_spinlock.h>
15
16 #include "rte_telemetry_legacy.h"
17
18 #define MAX_LEN 128
19 #define BUF_SIZE 1024
20 #define CLIENTS_UNREG_ACTION "\"action\":2"
21 #define CLIENTS_CMD "\"command\":\"clients\""
22 #define CLIENTS_DATA "\"data\":{\"client_path\":\""
23 #define STATS_ACTION "\"action\":0"
24 #define DATA_REQ_LABEL "\"data\":"
25 #define TELEMETRY_LEGACY_MAX_CALLBACKS 4
26
27
28 static int
29 register_client(const char *cmd __rte_unused,
30                 const char *params __rte_unused,
31                 char *buffer, int buf_len);
32
33 struct json_command {
34         char action[MAX_LEN];
35         char cmd[MAX_LEN];
36         char data[MAX_LEN];
37         telemetry_legacy_cb fn;
38
39 };
40
41 struct json_command callbacks[TELEMETRY_LEGACY_MAX_CALLBACKS] = {
42                 {
43                         .action = "\"action\":1",
44                         .cmd = CLIENTS_CMD,
45                         .data = CLIENTS_DATA,
46                         .fn = register_client
47                 }
48 };
49 int num_legacy_callbacks = 1;
50 static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER;
51
52 int
53 rte_telemetry_legacy_register(const char *cmd,
54                 enum rte_telemetry_legacy_data_req data_req,
55                 telemetry_legacy_cb fn)
56 {
57         if (fn == NULL)
58                 return -EINVAL;
59         if (num_legacy_callbacks >= (int) RTE_DIM(callbacks))
60                 return -ENOENT;
61
62         rte_spinlock_lock(&callback_sl);
63         strlcpy(callbacks[num_legacy_callbacks].action, STATS_ACTION, MAX_LEN);
64         snprintf(callbacks[num_legacy_callbacks].cmd, MAX_LEN,
65                         "\"command\":\"%s\"", cmd);
66         snprintf(callbacks[num_legacy_callbacks].data, MAX_LEN,
67                         data_req ? "%s{\"" : "%snull",
68                         DATA_REQ_LABEL);
69         callbacks[num_legacy_callbacks].fn = fn;
70         num_legacy_callbacks++;
71         rte_spinlock_unlock(&callback_sl);
72
73         return 0;
74 }
75
76 static int
77 register_client(const char *cmd __rte_unused, const char *params,
78                 char *buffer __rte_unused, int buf_len __rte_unused)
79 {
80         pthread_t th;
81         char data[BUF_SIZE];
82         int fd;
83         struct sockaddr_un addrs;
84
85         if (!strchr(params, ':')) {
86                 fprintf(stderr, "Invalid data\n");
87                 return -1;
88         }
89         strlcpy(data, strchr(params, ':'), sizeof(data));
90         memcpy(data, &data[strlen(":\"")], strlen(data));
91         if (!strchr(data, '\"')) {
92                 fprintf(stderr, "Invalid client data\n");
93                 return -1;
94         }
95         *strchr(data, '\"') = 0;
96
97         fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
98         addrs.sun_family = AF_UNIX;
99         strlcpy(addrs.sun_path, data, sizeof(addrs.sun_path));
100
101         if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
102                 perror("\nClient connection error\n");
103                 return -1;
104         }
105         pthread_create(&th, NULL, &legacy_client_handler,
106                         (void *)(uintptr_t)fd);
107         return 0;
108 }
109
110 static int
111 send_error_response(int s, int err)
112 {
113         const char *desc;
114         char out_buf[100000];
115
116         switch (err) {
117         case -ENOMEM:
118                 desc = "Memory Allocation Error";
119                 break;
120         case -EINVAL:
121                 desc = "Invalid Argument 404";
122                 break;
123         case -EPERM:
124                 desc = "Unknown";
125                 break;
126         default:
127                 /* Default case keeps behaviour of Telemetry library */
128                 printf("\nInvalid error type: %d\n", err);
129                 return -EINVAL;
130         }
131         int used = snprintf(out_buf, sizeof(out_buf), "{\"status_code\": "
132                         "\"Status Error: %s\", \"data\": null}", desc);
133         if (write(s, out_buf, used) < 0) {
134                 perror("Error writing to socket");
135                 return -1;
136         }
137         return 0;
138 }
139
140 static void
141 perform_command(telemetry_legacy_cb fn, const char *param, int s)
142 {
143         char out_buf[100000];
144         int ret, used = 0;
145
146         ret = fn("", param, out_buf, sizeof(out_buf));
147         if (ret < 0) {
148                 ret = send_error_response(s, ret);
149                 if (ret < 0)
150                         printf("\nCould not send error response\n");
151                 return;
152         }
153         used += ret;
154         if (write(s, out_buf, used) < 0)
155                 perror("Error writing to socket");
156 }
157
158 static int
159 parse_client_request(char *buffer, int buf_len, int s)
160 {
161         int i;
162         char *data = buffer + buf_len;
163         telemetry_legacy_cb fn = NULL;
164         const char *valid_sep = ",}";
165         if (buffer[0] != '{' || buffer[buf_len - 1] != '}')
166                 return -EPERM;
167
168         if (strstr(buffer, CLIENTS_UNREG_ACTION) && strstr(buffer, CLIENTS_CMD)
169                         && strstr(buffer, CLIENTS_DATA))
170                 return 0;
171
172         for (i = 0; i < num_legacy_callbacks; i++) {
173                 char *action_ptr = strstr(buffer, callbacks[i].action);
174                 char *cmd_ptr = strstr(buffer, callbacks[i].cmd);
175                 char *data_ptr = strstr(buffer, callbacks[i].data);
176                 if (!action_ptr || !cmd_ptr || !data_ptr)
177                         continue;
178
179                 char action_sep = action_ptr[strlen(callbacks[i].action)];
180                 char cmd_sep = cmd_ptr[strlen(callbacks[i].cmd)];
181                 if (!(strchr(valid_sep, action_sep) && strchr(valid_sep,
182                                 cmd_sep)))
183                         return -EPERM;
184                 char data_sep;
185
186                 if (!strchr(data_ptr, '{'))
187                         data_sep = data_ptr[strlen(callbacks[i].data)];
188                 else {
189                         if (!strchr(data_ptr, '}'))
190                                 return -EINVAL;
191                         char *data_end = strchr(data_ptr, '}');
192                         data = data_ptr + strlen(DATA_REQ_LABEL);
193                         data_sep = data_end[1];
194                         data_end[1] = 0;
195                 }
196                 if (!strchr(valid_sep, data_sep))
197                         return -EPERM;
198                 fn = callbacks[i].fn;
199                 break;
200         }
201
202         if (!fn)
203                 return -EINVAL;
204         perform_command(fn, data, s);
205         return 0;
206 }
207
208 void *
209 legacy_client_handler(void *sock_id)
210 {
211         int s = (int)(uintptr_t)sock_id;
212         int ret;
213         char buffer_recv[BUF_SIZE];
214         /* receive data is not null terminated */
215         int bytes = read(s, buffer_recv, sizeof(buffer_recv));
216
217         while (bytes > 0) {
218                 buffer_recv[bytes] = 0;
219                 int i, j;
220                 char buffer[BUF_SIZE];
221                 for (i = 0, j = 0; buffer_recv[i] != '\0'; i++) {
222                         buffer[j] = buffer_recv[i];
223                         j += !isspace(buffer_recv[i]);
224                 }
225                 buffer[j] = 0;
226                 ret = parse_client_request(buffer, j, s);
227                 if (ret < 0) {
228                         ret = send_error_response(s, ret);
229                         if (ret < 0)
230                                 printf("\nCould not send error response\n");
231                 }
232                 bytes = read(s, buffer_recv, sizeof(buffer_recv));
233         }
234         close(s);
235         return NULL;
236 }