From 9bcbcae2e428bfd2b8d3a95368e459a338c14eed Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Tue, 6 Mar 2018 17:09:36 -0500 Subject: [PATCH] zebra: add struct zmsghdr Formalize the ZAPI header by documenting it in code and providing it to message handlers free of charge to reduce complexity. Signed-off-by: Quentin Young --- zebra/zserv.c | 21 +++++++++++++++++++++ zebra/zserv.h | 9 +++++++++ 2 files changed, 30 insertions(+) diff --git a/zebra/zserv.c b/zebra/zserv.c index f3ee38fef1..3ecc82be42 100644 --- a/zebra/zserv.c +++ b/zebra/zserv.c @@ -2681,10 +2681,31 @@ stream_failure: return; } +/* + * Reads header from zmsg stream. + * + * Note this advances the stream getp by the size of the header. + */ +static bool zserv_read_header(struct stream *msg, struct zmsghdr *hdr) +{ + STREAM_GETW(msg, hdr->length); + STREAM_GETC(msg, hdr->marker); + STREAM_GETC(msg, hdr->version); + STREAM_GETL(msg, hdr->vrf_id); + STREAM_GETW(msg, hdr->command); + return true; +stream_failure: + return false; +} + static inline void zserv_handle_commands(struct zserv *client, uint16_t command, uint16_t length, struct zebra_vrf *zvrf) { + struct zmsghdr hdr; + stream_set_getp(client->ibuf, 0); + zserv_read_header(client->ibuf, &hdr); + switch (command) { case ZEBRA_ROUTER_ID_ADD: zread_router_id_add(client, length, zvrf); diff --git a/zebra/zserv.h b/zebra/zserv.h index 8519693726..a09baeff7a 100644 --- a/zebra/zserv.h +++ b/zebra/zserv.h @@ -129,6 +129,15 @@ struct zserv { int last_write_cmd; }; +/* ZAPI protocol message header */ +struct zmsghdr { + uint16_t length; + uint8_t marker; + uint8_t version; + uint32_t vrf_id; + uint16_t command; +}; + /* Zebra instance */ struct zebra_t { /* Thread master */ -- 2.39.5