From 124ead27dbd7e61e79a334533adc702fdf172ddb Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Wed, 7 Mar 2018 11:23:58 -0500 Subject: [PATCH] lib, zebra: use existing zapi header struct Nobody uses it, but it's got the same definition. Move the parser function into zclient.c and use it. Signed-off-by: Quentin Young --- lib/zclient.c | 12 +++++++++ lib/zclient.h | 62 ++++++++++++++++++++++++++++++++++++++++----- zebra/zebra_vxlan.c | 2 +- zebra/zserv.h | 9 ------- 4 files changed, 68 insertions(+), 17 deletions(-) diff --git a/lib/zclient.c b/lib/zclient.c index fa3a5f669..20aaaca16 100644 --- a/lib/zclient.c +++ b/lib/zclient.c @@ -322,6 +322,18 @@ stream_failure: return 0; } +bool zapi_parse_header(struct stream *zmsg, struct zmsghdr *hdr) +{ + STREAM_GETW(zmsg, hdr->length); + STREAM_GETC(zmsg, hdr->marker); + STREAM_GETC(zmsg, hdr->version); + STREAM_GETC(zmsg, hdr->vrf_id); + STREAM_GETW(zmsg, hdr->command); + return true; +stream_failure: + return false; +} + /* Send simple Zebra message. */ static int zebra_message_send(struct zclient *zclient, int command, vrf_id_t vrf_id) diff --git a/lib/zclient.h b/lib/zclient.h index 1aa94b641..c8574946d 100644 --- a/lib/zclient.h +++ b/lib/zclient.h @@ -237,14 +237,14 @@ struct zclient { */ #define ZAPI_MESSAGE_TABLEID 0x80 +#define ZSERV_VERSION 5 /* Zserv protocol message header */ -struct zserv_header { +struct zmsghdr { uint16_t length; - uint8_t marker; /* corresponds to command field in old zserv - * always set to 255 in new zserv. - */ + /* corresponds to command field in old zserv + * always set to 255 in new zserv. */ + uint8_t marker; uint8_t version; -#define ZSERV_VERSION 5 vrf_id_t vrf_id; uint16_t command; }; @@ -449,9 +449,58 @@ extern int zclient_send_message(struct zclient *); /* create header for command, length to be filled in by user later */ extern void zclient_create_header(struct stream *, uint16_t, vrf_id_t); +/* + * Read sizeof(struct zmsghdr) bytes from the provided socket and parse the + * received data into the specified fields. If this is successful, read the + * rest of the packet into the provided stream. + * + * s + * The stream to read into + * + * sock + * The socket to read from + * + * size + * Parsed message size will be placed in the pointed-at integer + * + * marker + * Parsed marker will be placed in the pointed-at byte + * + * version + * Parsed version will be placed in the pointed-at byte + * + * vrf_id + * Parsed VRF ID will be placed in the pointed-at vrf_id_t + * + * cmd + * Parsed command number will be placed in the pointed-at integer + * + * Returns: + * -1 if: + * - insufficient data for header was read + * - a version mismatch was detected + * - a marker mismatch was detected + * - header size field specified more data than could be read + */ extern int zclient_read_header(struct stream *s, int sock, u_int16_t *size, u_char *marker, u_char *version, vrf_id_t *vrf_id, u_int16_t *cmd); +/* + * Parse header from ZAPI message stream into struct zmsghdr. + * This function assumes the stream getp points at the first byte of the header. + * If the function is successful then the stream getp will point to the byte + * immediately after the last byte of the header. + * + * zmsg + * The stream containing the header + * + * hdr + * The header struct to parse into. + * + * Returns: + * true if parsing succeeded, false otherwise + */ +extern bool zapi_parse_header(struct stream *zmsg, struct zmsghdr *hdr); extern void zclient_interface_set_master(struct zclient *client, struct interface *master, @@ -469,8 +518,7 @@ extern void zebra_interface_if_set_value(struct stream *, struct interface *); extern void zebra_router_id_update_read(struct stream *s, struct prefix *rid); #if CONFDATE > 20180823 -CPP_NOTICE( - "zapi_ipv4_route, zapi_ipv6_route, zapi_ipv4_route_ipv6_nexthop as well as the zapi_ipv4 and zapi_ipv6 data structures should be removed now"); +CPP_NOTICE("zapi_ipv4_route, zapi_ipv6_route, zapi_ipv4_route_ipv6_nexthop as well as the zapi_ipv4 and zapi_ipv6 data structures should be removed now"); #endif extern int zapi_ipv4_route(u_char, struct zclient *, struct prefix_ipv4 *, diff --git a/zebra/zebra_vxlan.c b/zebra/zebra_vxlan.c index a8d184872..256fa9e4b 100644 --- a/zebra/zebra_vxlan.c +++ b/zebra/zebra_vxlan.c @@ -6612,7 +6612,7 @@ void zebra_vxlan_advertise_gw_macip(ZAPI_HANDLER_ARGS) zvni = zvni_lookup(vni); if (!zvni) - return 0; + return; if (IS_ZEBRA_DEBUG_VXLAN) zlog_debug( diff --git a/zebra/zserv.h b/zebra/zserv.h index a055a28c0..62707510c 100644 --- a/zebra/zserv.h +++ b/zebra/zserv.h @@ -133,15 +133,6 @@ struct zserv { int last_write_cmd; }; -/* ZAPI protocol structs */ -struct zmsghdr { - uint16_t length; - uint8_t marker; - uint8_t version; - uint32_t vrf_id; - uint16_t command; -}; - #define ZAPI_HANDLER_ARGS \ struct zserv *client, struct zmsghdr *hdr, struct stream *msg, \ struct zebra_vrf *zvrf -- 2.39.5