--- embedaddon/libnet/src/libnet_build_bgp.c 2012/02/21 22:14:23 1.1.1.1 +++ embedaddon/libnet/src/libnet_build_bgp.c 2023/09/27 11:11:38 1.1.1.3 @@ -1,5 +1,5 @@ /* - * $Id: libnet_build_bgp.c,v 1.1.1.1 2012/02/21 22:14:23 misho Exp $ + * $Id: libnet_build_bgp.c,v 1.1.1.3 2023/09/27 11:11:38 misho Exp $ * * libnet * libnet_build_bgp.c - BGP packet assembler (RFC 1771) @@ -30,21 +30,14 @@ * */ -#if (HAVE_CONFIG_H) -#include "../include/config.h" -#endif -#if (!(_WIN32) || (__CYGWIN__)) -#include "../include/libnet.h" -#else -#include "../include/win32/libnet.h" -#endif +#include "common.h" libnet_ptag_t -libnet_build_bgp4_header(u_int8_t marker[LIBNET_BGP4_MARKER_SIZE], - u_int16_t len, u_int8_t type, u_int8_t *payload, u_int32_t payload_s, - libnet_t *l, libnet_ptag_t ptag) +libnet_build_bgp4_header(uint8_t marker[LIBNET_BGP4_MARKER_SIZE], +uint16_t len, uint8_t type, const uint8_t *payload, uint32_t payload_s, +libnet_t *l, libnet_ptag_t ptag) { - u_int32_t n, h; + uint32_t n, h; libnet_pblock_t *p; struct libnet_bgp4_header_hdr bgp4_hdr; @@ -66,34 +59,20 @@ libnet_build_bgp4_header(u_int8_t marker[LIBNET_BGP4_M return (-1); } - memset(&bgp4_hdr, 0, sizeof(bgp4_hdr)); - memcpy(bgp4_hdr.marker, marker, LIBNET_BGP4_MARKER_SIZE * sizeof (u_int8_t)); + memset(&bgp4_hdr, 0, sizeof(bgp4_hdr)); + memcpy(bgp4_hdr.marker, marker, LIBNET_BGP4_MARKER_SIZE * sizeof(uint8_t)); bgp4_hdr.len = htons(len); bgp4_hdr.type = type; - n = libnet_pblock_append(l, p, (u_int8_t *)&bgp4_hdr, LIBNET_BGP4_HEADER_H); + n = libnet_pblock_append(l, p, (uint8_t *)&bgp4_hdr, LIBNET_BGP4_HEADER_H); if (n == -1) { goto bad; } - if ((payload && !payload_s) || (!payload && payload_s)) - { - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): payload inconsistency\n", __func__); - goto bad; - } - - if (payload && payload_s) - { - n = libnet_pblock_append(l, p, payload, payload_s); - if (n == -1) - { - /* err msg set in libnet_pblock_append() */ - goto bad; - } - } - + /* boilerplate payload sanity check / append macro */ + LIBNET_DO_PAYLOAD(l, p); + return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_BGP4_HEADER_H)); bad: @@ -102,13 +81,13 @@ bad: } libnet_ptag_t -libnet_build_bgp4_open(u_int8_t version, u_int16_t src_as, u_int16_t hold_time, - u_int32_t bgp_id, u_int8_t opt_len, u_int8_t *payload, u_int32_t payload_s, - libnet_t *l, libnet_ptag_t ptag) +libnet_build_bgp4_open(uint8_t version, uint16_t src_as, uint16_t hold_time, +uint32_t bgp_id, uint8_t opt_len, const uint8_t *payload, uint32_t payload_s, +libnet_t *l, libnet_ptag_t ptag) { - u_int32_t n, h; + uint32_t n, h; libnet_pblock_t *p; - u_int16_t val; + uint16_t val; if (l == NULL) { @@ -129,56 +108,42 @@ libnet_build_bgp4_open(u_int8_t version, u_int16_t src } /* for memory alignment reason, we need to append each field separately */ - n = libnet_pblock_append(l, p, (u_int8_t *)&version, sizeof (version)); + n = libnet_pblock_append(l, p, (uint8_t *)&version, sizeof (version)); if (n == -1) { goto bad; } val = htons(src_as); - n = libnet_pblock_append(l, p, (u_int8_t *)&val, sizeof(src_as)); + n = libnet_pblock_append(l, p, (uint8_t *)&val, sizeof(src_as)); if (n == -1) { goto bad; } val = htons(hold_time); - n = libnet_pblock_append(l, p, (u_int8_t *)&val, sizeof(hold_time)); + n = libnet_pblock_append(l, p, (uint8_t *)&val, sizeof(hold_time)); if (n == -1) { goto bad; } n = htonl(bgp_id); - n = libnet_pblock_append(l, p, (u_int8_t *)&n, sizeof(bgp_id)); + n = libnet_pblock_append(l, p, (uint8_t *)&n, sizeof(bgp_id)); if (n == -1) { goto bad; } - n = libnet_pblock_append(l, p, (u_int8_t *)&opt_len, sizeof(opt_len)); + n = libnet_pblock_append(l, p, (uint8_t *)&opt_len, sizeof(opt_len)); if (n == -1) { goto bad; } - if ((payload && !payload_s) || (!payload && payload_s)) - { - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): payload inconsistency\n", __func__); - goto bad; - } - - if (payload && payload_s) - { - n = libnet_pblock_append(l, p, payload, payload_s); - if (n == -1) - { - /* err msg set in libnet_pblock_append() */ - goto bad; - } - } - + /* boilerplate payload sanity check / append macro */ + LIBNET_DO_PAYLOAD(l, p); + return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_BGP4_OPEN_H)); bad: @@ -187,14 +152,14 @@ bad: } libnet_ptag_t -libnet_build_bgp4_update(u_int16_t unfeasible_rt_len, u_int8_t *withdrawn_rt, - u_int16_t total_path_attr_len, u_int8_t *path_attributes, - u_int16_t info_len, u_int8_t *reachability_info, - u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag) +libnet_build_bgp4_update(uint16_t unfeasible_rt_len, const uint8_t *withdrawn_rt, +uint16_t total_path_attr_len, const uint8_t *path_attributes, uint16_t info_len, +uint8_t *reachability_info, const uint8_t *payload, uint32_t payload_s, +libnet_t *l, libnet_ptag_t ptag) { - u_int32_t n, h; + uint32_t n, h; libnet_pblock_t *p; - u_int16_t length; + uint16_t length; if (l == NULL) { @@ -220,8 +185,8 @@ libnet_build_bgp4_update(u_int16_t unfeasible_rt_len, /* for memory alignment reason, we need to append each field separately */ length = htons(unfeasible_rt_len); - n = libnet_pblock_append(l, p, (u_int8_t *)&length, - sizeof (unfeasible_rt_len)); + n = libnet_pblock_append(l, p, (uint8_t *)&length, + sizeof (unfeasible_rt_len)); if (n == -1) { goto bad; @@ -237,7 +202,7 @@ libnet_build_bgp4_update(u_int16_t unfeasible_rt_len, } length = htons(total_path_attr_len); - n = libnet_pblock_append(l, p, (u_int8_t *)&length, + n = libnet_pblock_append(l, p, (uint8_t *)&length, sizeof (total_path_attr_len)); if (n == -1) { @@ -262,23 +227,9 @@ libnet_build_bgp4_update(u_int16_t unfeasible_rt_len, } } - if ((payload && !payload_s) || (!payload && payload_s)) - { - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): payload inconsistency\n", __func__); - goto bad; - } - - if (payload && payload_s) - { - n = libnet_pblock_append(l, p, payload, payload_s); - if (n == -1) - { - /* err msg set in libnet_pblock_append() */ - goto bad; - } - } - + /* boilerplate payload sanity check / append macro */ + LIBNET_DO_PAYLOAD(l, p); + return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_BGP4_UPDATE_H)); bad: @@ -287,10 +238,10 @@ bad: } libnet_ptag_t -libnet_build_bgp4_notification(u_int8_t err_code, u_int8_t err_subcode, - u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag) +libnet_build_bgp4_notification(uint8_t err_code, uint8_t err_subcode, +const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag) { - u_int32_t n, h; + uint32_t n, h; libnet_pblock_t *p; struct libnet_bgp4_notification_hdr bgp4_hdr; @@ -312,33 +263,19 @@ libnet_build_bgp4_notification(u_int8_t err_code, u_in return (-1); } - memset(&bgp4_hdr, 0, sizeof(bgp4_hdr)); - bgp4_hdr.err_code = err_code; + memset(&bgp4_hdr, 0, sizeof(bgp4_hdr)); + bgp4_hdr.err_code = err_code; bgp4_hdr.err_subcode = err_subcode; - n = libnet_pblock_append(l, p, (u_int8_t *)&bgp4_hdr, + n = libnet_pblock_append(l, p, (uint8_t *)&bgp4_hdr, LIBNET_BGP4_NOTIFICATION_H); if (n == -1) { goto bad; } - if ((payload && !payload_s) || (!payload && payload_s)) - { - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): payload inconsistency\n", __func__); - goto bad; - } - - if (payload && payload_s) - { - n = libnet_pblock_append(l, p, payload, payload_s); - if (n == -1) - { - /* err msg set in libnet_pblock_append() */ - goto bad; - } - } + /* boilerplate payload sanity check / append macro */ + LIBNET_DO_PAYLOAD(l, p); return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_BGP4_NOTIFICATION_H)); @@ -347,4 +284,9 @@ bad: return (-1); } -/* EOF */ +/** + * Local Variables: + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + */