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