]>
Commit | Line | Data |
---|---|---|
50e24903 DS |
1 | /* PTM Library |
2 | * Copyright (C) 2015 Cumulus Networks, Inc. | |
c43ed2e4 | 3 | * |
50e24903 | 4 | * This file is part of Quagga. |
c43ed2e4 | 5 | * |
50e24903 DS |
6 | * Quagga is free software; you can redistribute it and/or modify it |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * Quagga is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
c43ed2e4 | 19 | */ |
c43ed2e4 DS |
20 | #include <stdio.h> |
21 | #include <stdlib.h> | |
22 | #include <stdbool.h> | |
23 | #include <stddef.h> | |
24 | #include <string.h> | |
25 | #include <ctype.h> | |
26 | #include <unistd.h> | |
27 | #include <errno.h> | |
28 | #include <sys/socket.h> | |
29 | #include "csv.h" | |
30 | #include "ptm_lib.h" | |
31 | ||
32 | #define DEBUG_E 0 | |
33 | #define DEBUG_V 0 | |
34 | ||
d62a17ae | 35 | #define ERRLOG(fmt, ...) \ |
36 | do { \ | |
37 | if (DEBUG_E) \ | |
38 | fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, \ | |
39 | __LINE__, __func__, ##__VA_ARGS__); \ | |
40 | } while (0) | |
41 | ||
42 | #define DLOG(fmt, ...) \ | |
43 | do { \ | |
44 | if (DEBUG_V) \ | |
45 | fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, \ | |
46 | __LINE__, __func__, ##__VA_ARGS__); \ | |
47 | } while (0) | |
c43ed2e4 DS |
48 | |
49 | typedef struct ptm_lib_msg_ctxt_s { | |
d62a17ae | 50 | int cmd_id; |
51 | csv_t *csv; | |
52 | ptmlib_msg_type type; | |
c43ed2e4 DS |
53 | } ptm_lib_msg_ctxt_t; |
54 | ||
d62a17ae | 55 | static csv_record_t *_ptm_lib_encode_header(csv_t *csv, csv_record_t *rec, |
56 | int msglen, int version, int type, | |
57 | int cmd_id, char *client_name) | |
c43ed2e4 | 58 | { |
d62a17ae | 59 | char msglen_buf[16], vers_buf[16], type_buf[16], cmdid_buf[16]; |
60 | char client_buf[32]; | |
61 | csv_record_t *rec1; | |
62 | ||
63 | sprintf(msglen_buf, "%4u", msglen); | |
64 | sprintf(vers_buf, "%4u", version); | |
65 | sprintf(type_buf, "%4u", type); | |
66 | sprintf(cmdid_buf, "%4u", cmd_id); | |
67 | snprintf(client_buf, 17, "%16.16s", client_name); | |
68 | if (rec) { | |
69 | rec1 = csv_encode_record(csv, rec, 5, msglen_buf, vers_buf, | |
70 | type_buf, cmdid_buf, client_buf); | |
71 | } else { | |
72 | rec1 = csv_encode(csv, 5, msglen_buf, vers_buf, type_buf, | |
73 | cmdid_buf, client_buf); | |
74 | } | |
75 | return (rec1); | |
c43ed2e4 DS |
76 | } |
77 | ||
d62a17ae | 78 | static int _ptm_lib_decode_header(csv_t *csv, int *msglen, int *version, |
79 | int *type, int *cmd_id, char *client_name) | |
c43ed2e4 | 80 | { |
d62a17ae | 81 | char *hdr; |
82 | csv_record_t *rec; | |
83 | csv_field_t *fld; | |
84 | int i, j; | |
85 | ||
86 | csv_decode(csv, NULL); | |
87 | rec = csv_record_iter(csv); | |
88 | if (rec == NULL) { | |
89 | DLOG("malformed CSV\n"); | |
90 | return (-1); | |
91 | } | |
92 | hdr = csv_field_iter(rec, &fld); | |
93 | if (hdr == NULL) { | |
94 | DLOG("malformed CSV\n"); | |
95 | return (-1); | |
96 | } | |
97 | *msglen = atoi(hdr); | |
98 | hdr = csv_field_iter_next(&fld); | |
99 | if (hdr == NULL) { | |
100 | DLOG("malformed CSV\n"); | |
101 | return (-1); | |
102 | } | |
103 | *version = atoi(hdr); | |
104 | hdr = csv_field_iter_next(&fld); | |
105 | if (hdr == NULL) { | |
106 | DLOG("malformed CSV\n"); | |
107 | return (-1); | |
108 | } | |
109 | *type = atoi(hdr); | |
110 | hdr = csv_field_iter_next(&fld); | |
111 | if (hdr == NULL) { | |
112 | DLOG("malformed CSV\n"); | |
113 | return (-1); | |
114 | } | |
115 | *cmd_id = atoi(hdr); | |
116 | hdr = csv_field_iter_next(&fld); | |
117 | if (hdr == NULL) { | |
118 | DLOG("malformed CSV\n"); | |
119 | return (-1); | |
120 | } | |
121 | /* remove leading spaces */ | |
122 | for (i = j = 0; i < csv_field_len(fld); i++) { | |
123 | if (!isspace(hdr[i])) { | |
124 | client_name[j] = hdr[i]; | |
125 | j++; | |
126 | } | |
127 | } | |
128 | client_name[j] = '\0'; | |
129 | ||
130 | return (0); | |
c43ed2e4 DS |
131 | } |
132 | ||
d62a17ae | 133 | int ptm_lib_append_msg(ptm_lib_handle_t *hdl, void *ctxt, const char *key, |
134 | const char *val) | |
c43ed2e4 | 135 | { |
d62a17ae | 136 | ptm_lib_msg_ctxt_t *p_ctxt = ctxt; |
137 | csv_t *csv; | |
138 | csv_record_t *mh_rec, *rec; | |
139 | ||
140 | if (!p_ctxt) { | |
141 | ERRLOG("%s: no context \n", __FUNCTION__); | |
142 | return -1; | |
143 | } | |
144 | ||
145 | csv = p_ctxt->csv; | |
146 | mh_rec = csv_record_iter(csv); | |
147 | rec = csv_record_iter_next(mh_rec); | |
148 | ||
149 | /* append to the hdr record */ | |
150 | rec = csv_append_record(csv, rec, 1, key); | |
151 | if (!rec) { | |
152 | ERRLOG("%s: Could not append key \n", __FUNCTION__); | |
153 | return -1; | |
154 | } | |
155 | ||
156 | rec = csv_record_iter_next(rec); | |
157 | /* append to the data record */ | |
158 | rec = csv_append_record(csv, rec, 1, val); | |
159 | if (!rec) { | |
160 | ERRLOG("%s: Could not append val \n", __FUNCTION__); | |
161 | return -1; | |
162 | } | |
163 | ||
164 | /* update the msg hdr */ | |
165 | _ptm_lib_encode_header(csv, mh_rec, (csvlen(csv) - PTMLIB_MSG_HDR_LEN), | |
166 | PTMLIB_MSG_VERSION, p_ctxt->type, p_ctxt->cmd_id, | |
167 | hdl->client_name); | |
168 | ||
169 | return 0; | |
c43ed2e4 DS |
170 | } |
171 | ||
d62a17ae | 172 | int ptm_lib_init_msg(ptm_lib_handle_t *hdl, int cmd_id, int type, void *in_ctxt, |
173 | void **out_ctxt) | |
c43ed2e4 | 174 | { |
d62a17ae | 175 | ptm_lib_msg_ctxt_t *p_ctxt; |
176 | ptm_lib_msg_ctxt_t *p_in_ctxt = in_ctxt; | |
177 | csv_t *csv; | |
178 | csv_record_t *rec, *d_rec; | |
179 | ||
180 | /* Initialize csv for using discrete record buffers */ | |
181 | csv = csv_init(NULL, NULL, PTMLIB_MSG_SZ); | |
182 | ||
183 | if (!csv) { | |
184 | ERRLOG("%s: Could not allocate csv \n", __FUNCTION__); | |
185 | return -1; | |
186 | } | |
187 | ||
188 | rec = _ptm_lib_encode_header(csv, NULL, 0, PTMLIB_MSG_VERSION, type, | |
189 | cmd_id, hdl->client_name); | |
190 | ||
191 | if (!rec) { | |
192 | ERRLOG("%s: Could not allocate record \n", __FUNCTION__); | |
193 | csv_clean(csv); | |
194 | csv_free(csv); | |
195 | return -1; | |
196 | } | |
197 | ||
198 | p_ctxt = calloc(1, sizeof(*p_ctxt)); | |
199 | if (!p_ctxt) { | |
200 | ERRLOG("%s: Could not allocate context \n", __FUNCTION__); | |
201 | csv_clean(csv); | |
202 | csv_free(csv); | |
203 | return -1; | |
204 | } | |
205 | ||
206 | p_ctxt->csv = csv; | |
207 | p_ctxt->cmd_id = cmd_id; | |
208 | p_ctxt->type = type; | |
209 | ||
210 | *(ptm_lib_msg_ctxt_t **)out_ctxt = p_ctxt; | |
211 | ||
212 | /* caller supplied a context to initialize with? */ | |
213 | if (p_in_ctxt) { | |
214 | /* insert the hdr rec */ | |
215 | rec = csv_record_iter(p_in_ctxt->csv); | |
216 | csv_clone_record(p_in_ctxt->csv, rec, &d_rec); | |
217 | csv_insert_record(csv, d_rec); | |
218 | /* insert the data rec */ | |
219 | rec = csv_record_iter_next(rec); | |
220 | csv_clone_record(p_in_ctxt->csv, rec, &d_rec); | |
221 | csv_insert_record(csv, d_rec); | |
222 | } | |
223 | return 0; | |
c43ed2e4 DS |
224 | } |
225 | ||
d62a17ae | 226 | int ptm_lib_complete_msg(ptm_lib_handle_t *hdl, void *ctxt, char *buf, int *len) |
c43ed2e4 | 227 | { |
d62a17ae | 228 | ptm_lib_msg_ctxt_t *p_ctxt = ctxt; |
229 | csv_t *csv; | |
230 | csv_record_t *rec; | |
231 | ||
232 | if (!p_ctxt) { | |
233 | ERRLOG("%s: no context \n", __FUNCTION__); | |
234 | return -1; | |
235 | } | |
236 | ||
237 | csv = p_ctxt->csv; | |
238 | rec = csv_record_iter(csv); | |
239 | ||
240 | _ptm_lib_encode_header(csv, rec, (csvlen(csv) - PTMLIB_MSG_HDR_LEN), | |
241 | PTMLIB_MSG_VERSION, p_ctxt->type, p_ctxt->cmd_id, | |
242 | hdl->client_name); | |
243 | ||
244 | /* parse csv contents into string */ | |
245 | if (buf && len) { | |
246 | if (csv_serialize(csv, buf, *len)) { | |
247 | ERRLOG("%s: cannot serialize\n", __FUNCTION__); | |
248 | return -1; | |
249 | } | |
250 | *len = csvlen(csv); | |
251 | } | |
252 | ||
253 | csv_clean(csv); | |
254 | csv_free(csv); | |
255 | free(p_ctxt); | |
256 | ||
257 | return 0; | |
c43ed2e4 DS |
258 | } |
259 | ||
d62a17ae | 260 | int ptm_lib_find_key_in_msg(void *ctxt, const char *key, char *val) |
c43ed2e4 | 261 | { |
d62a17ae | 262 | ptm_lib_msg_ctxt_t *p_ctxt = ctxt; |
263 | csv_t *csv = p_ctxt->csv; | |
264 | csv_record_t *hrec, *drec; | |
265 | csv_field_t *hfld, *dfld; | |
266 | char *hstr, *dstr; | |
267 | ||
268 | /** | |
269 | * skip over ptm hdr if present | |
270 | * The next hdr is the keys (column name) | |
271 | * The next hdr is the data | |
272 | */ | |
273 | if (csv_num_records(csv) > 2) { | |
274 | hrec = csv_record_iter(csv); | |
275 | hrec = csv_record_iter_next(hrec); | |
276 | } else { | |
277 | hrec = csv_record_iter(csv); | |
278 | } | |
279 | drec = csv_record_iter_next(hrec); | |
280 | val[0] = '\0'; | |
281 | for (hstr = csv_field_iter(hrec, &hfld), | |
282 | dstr = csv_field_iter(drec, &dfld); | |
283 | (hstr && dstr); hstr = csv_field_iter_next(&hfld), | |
284 | dstr = csv_field_iter_next(&dfld)) { | |
285 | if (!strncmp(hstr, key, csv_field_len(hfld))) { | |
286 | snprintf(val, csv_field_len(dfld) + 1, "%s", dstr); | |
287 | return 0; | |
288 | } | |
289 | } | |
290 | ||
291 | return -1; | |
c43ed2e4 DS |
292 | } |
293 | ||
d62a17ae | 294 | static int _ptm_lib_read_ptm_socket(int fd, char *buf, int len) |
c43ed2e4 | 295 | { |
d62a17ae | 296 | int retries = 0, rc; |
297 | int bytes_read = 0; | |
298 | ||
299 | while (bytes_read != len) { | |
300 | rc = recv(fd, (void *)(buf + bytes_read), (len - bytes_read), | |
301 | MSG_DONTWAIT); | |
302 | if (rc <= 0) { | |
303 | if (errno && (errno != EAGAIN) | |
304 | && (errno != EWOULDBLOCK)) { | |
305 | ERRLOG("fatal recv error(%s), closing connection, rc %d\n", | |
306 | strerror(errno), rc); | |
307 | return (rc); | |
308 | } else { | |
309 | if (retries++ < 2) { | |
310 | usleep(10000); | |
311 | continue; | |
312 | } | |
313 | DLOG("max retries - recv error(%d - %s) bytes read %d (%d)\n", | |
314 | errno, strerror(errno), bytes_read, len); | |
315 | return (bytes_read); | |
316 | } | |
317 | break; | |
318 | } else { | |
319 | bytes_read += rc; | |
320 | } | |
321 | } | |
322 | ||
323 | return bytes_read; | |
c43ed2e4 DS |
324 | } |
325 | ||
d62a17ae | 326 | int ptm_lib_process_msg(ptm_lib_handle_t *hdl, int fd, char *inbuf, int inlen, |
327 | void *arg) | |
c43ed2e4 | 328 | { |
d62a17ae | 329 | int rc, len; |
330 | char client_name[32]; | |
331 | int cmd_id, type, ver, msglen; | |
332 | csv_t *csv; | |
333 | ptm_lib_msg_ctxt_t *p_ctxt; | |
334 | ||
335 | len = _ptm_lib_read_ptm_socket(fd, inbuf, PTMLIB_MSG_HDR_LEN); | |
336 | if (len <= 0) | |
337 | return (len); | |
338 | ||
339 | csv = csv_init(NULL, inbuf, PTMLIB_MSG_HDR_LEN); | |
340 | ||
341 | if (!csv) { | |
342 | DLOG("Cannot allocate csv for hdr\n"); | |
343 | return (-1); | |
344 | } | |
345 | ||
346 | rc = _ptm_lib_decode_header(csv, &msglen, &ver, &type, &cmd_id, | |
347 | client_name); | |
348 | ||
349 | csv_clean(csv); | |
350 | csv_free(csv); | |
351 | ||
352 | if (rc < 0) { | |
353 | /* could not decode the CSV - maybe its legacy cmd? | |
354 | * get the entire cmd from the socket and see if we can process | |
355 | * it | |
356 | */ | |
357 | if (len == PTMLIB_MSG_HDR_LEN) { | |
358 | len += _ptm_lib_read_ptm_socket( | |
359 | fd, (inbuf + PTMLIB_MSG_HDR_LEN), | |
360 | inlen - PTMLIB_MSG_HDR_LEN); | |
361 | if (len <= 0) | |
362 | return (len); | |
363 | } | |
364 | ||
365 | inbuf[len] = '\0'; | |
366 | /* we only support the get-status cmd */ | |
367 | if (strcmp(inbuf, PTMLIB_CMD_GET_STATUS)) { | |
368 | DLOG("unsupported legacy cmd %s\n", inbuf); | |
369 | return (-1); | |
370 | } | |
371 | /* internally create a csv-style cmd */ | |
372 | ptm_lib_init_msg(hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, | |
373 | (void *)&p_ctxt); | |
374 | if (!p_ctxt) { | |
375 | DLOG("couldnt allocate context\n"); | |
376 | return (-1); | |
377 | } | |
378 | ptm_lib_append_msg(hdl, p_ctxt, "cmd", PTMLIB_CMD_GET_STATUS); | |
379 | ||
380 | } else { | |
381 | ||
382 | if (msglen > inlen) { | |
383 | DLOG("msglen [%d] > inlen [%d]\n", msglen, inlen); | |
384 | return -1; | |
385 | } | |
386 | ||
387 | /* read the rest of the msg */ | |
388 | len = _ptm_lib_read_ptm_socket(fd, inbuf, msglen); | |
389 | if (len <= 0) { | |
390 | return (len); | |
391 | } | |
392 | ||
393 | inbuf[len] = '\0'; | |
394 | ||
395 | csv = csv_init(NULL, NULL, PTMLIB_MSG_SZ); | |
396 | if (!csv) { | |
397 | ERRLOG("Cannot allocate csv for msg\n"); | |
398 | return -1; | |
399 | } | |
400 | ||
401 | csv_decode(csv, inbuf); | |
402 | p_ctxt = calloc(1, sizeof(*p_ctxt)); | |
403 | if (!p_ctxt) { | |
404 | ERRLOG("%s: Could not allocate context \n", | |
405 | __FUNCTION__); | |
406 | csv_clean(csv); | |
407 | csv_free(csv); | |
408 | return -1; | |
409 | } | |
410 | ||
411 | p_ctxt->csv = csv; | |
412 | p_ctxt->cmd_id = cmd_id; | |
413 | p_ctxt->type = type; | |
414 | } | |
415 | ||
416 | switch (p_ctxt->type) { | |
417 | case PTMLIB_MSG_TYPE_NOTIFICATION: | |
418 | if (hdl->notify_cb) | |
419 | hdl->notify_cb(arg, p_ctxt); | |
420 | break; | |
421 | case PTMLIB_MSG_TYPE_CMD: | |
422 | if (hdl->cmd_cb) | |
423 | hdl->cmd_cb(arg, p_ctxt); | |
424 | break; | |
425 | case PTMLIB_MSG_TYPE_RESPONSE: | |
426 | if (hdl->response_cb) | |
427 | hdl->response_cb(arg, p_ctxt); | |
428 | break; | |
429 | default: | |
430 | return -1; | |
431 | } | |
432 | ||
433 | csv_clean(p_ctxt->csv); | |
434 | csv_free(p_ctxt->csv); | |
435 | free(p_ctxt); | |
436 | ||
437 | return len; | |
c43ed2e4 DS |
438 | } |
439 | ||
d62a17ae | 440 | ptm_lib_handle_t *ptm_lib_register(char *client_name, ptm_cmd_cb cmd_cb, |
441 | ptm_notify_cb notify_cb, | |
442 | ptm_response_cb response_cb) | |
c43ed2e4 | 443 | { |
d62a17ae | 444 | ptm_lib_handle_t *hdl; |
c43ed2e4 | 445 | |
d62a17ae | 446 | hdl = calloc(1, sizeof(*hdl)); |
c43ed2e4 | 447 | |
d62a17ae | 448 | if (hdl) { |
449 | strncpy(hdl->client_name, client_name, PTMLIB_MAXNAMELEN - 1); | |
450 | hdl->cmd_cb = cmd_cb; | |
451 | hdl->notify_cb = notify_cb; | |
452 | hdl->response_cb = response_cb; | |
453 | } | |
c43ed2e4 | 454 | |
d62a17ae | 455 | return hdl; |
c43ed2e4 DS |
456 | } |
457 | ||
d62a17ae | 458 | void ptm_lib_deregister(ptm_lib_handle_t *hdl) |
c43ed2e4 | 459 | { |
d62a17ae | 460 | if (hdl) { |
461 | memset(hdl, 0x00, sizeof(*hdl)); | |
462 | free(hdl); | |
463 | } | |
c43ed2e4 | 464 | } |