Skip to content

Commit

Permalink
Added static asserts to fixed length header structures used for decod…
Browse files Browse the repository at this point in the history
…ing layer data. (#1693)
  • Loading branch information
Dimi1010 authored Feb 2, 2025
1 parent 2ae8837 commit 4ea938b
Show file tree
Hide file tree
Showing 37 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions Packet++/header/BgpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace pcpp
uint8_t messageType;
};
#pragma pack(pop)
static_assert(sizeof(bgp_common_header) == 19, "bgp_common_header size is not 19 bytes");

/**
* @return BGP message type
Expand Down
5 changes: 3 additions & 2 deletions Packet++/header/CotpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ namespace pcpp
* Represents a COTP protocol header
*/
#pragma pack(push, 1)
typedef struct
struct cotphdr
{
/** length */
uint8_t length;
/** PDU type identifier */
uint8_t pduType;
/** TPDU number sequence*/
uint8_t tpduNumber;
} cotphdr;
};
#pragma pack(pop)
static_assert(sizeof(cotphdr) == 3, "cotphdr size is not 3 bytes");

/**
* @class CotpLayer
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/DhcpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace pcpp
uint32_t magicNumber;
};
#pragma pack(pop)
static_assert(sizeof(dhcp_header) == 240, "dhcp_header size is not 240 bytes");

/**
* BootP opcodes
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/DhcpV6Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ namespace pcpp
/** DHCPv6 transaction ID (last byte) */
uint8_t transactionId3;
};
static_assert(sizeof(dhcpv6_header) == 4, "dhcpv6_header size is not 4 bytes");

/**
* @class DhcpV6Layer
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/DnsLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ namespace pcpp
uint16_t numberOfAdditional;
};
#pragma pack(pop)
static_assert(sizeof(dnshdr) == 12, "dnshdr size is not 12 bytes");

// forward declarations
class DnsQuery;
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/EthDot3Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace pcpp
uint16_t length;
};
#pragma pack(pop)
static_assert(sizeof(ether_dot3_header) == 14, "ether_dot3_header size is not 14 bytes");

/**
* @class EthDot3Layer
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/EthLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace pcpp
uint16_t etherType;
};
#pragma pack(pop)
static_assert(sizeof(ether_header) == 14, "ether_header size is not 14 bytes");

/* Ethernet protocol ID's */

Expand Down
3 changes: 3 additions & 0 deletions Packet++/header/GreLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace pcpp
uint16_t protocol;
};
#pragma pack(pop)
static_assert(sizeof(gre_basic_header) == 4, "gre_basic_header size is not 4 bytes");

/**
* @struct gre1_header
Expand All @@ -79,6 +80,7 @@ namespace pcpp
uint16_t callID;
};
#pragma pack(pop)
static_assert(sizeof(gre1_header) == 8, "gre1_header size is not 8 bytes");

/**
* @struct ppp_pptp_header
Expand All @@ -95,6 +97,7 @@ namespace pcpp
uint16_t protocol;
};
#pragma pack(pop)
static_assert(sizeof(ppp_pptp_header) == 4, "ppp_pptp_header size is not 4 bytes");

/**
* @class GreLayer
Expand Down
2 changes: 1 addition & 1 deletion Packet++/header/GtpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ namespace pcpp
* tunnel */
uint32_t teid;
};

#pragma pack(pop)
static_assert(sizeof(gtpv1_header) == 8, "gtpv1_header size is not 8 bytes");

/**
* An enum representing the possible GTP v1 message types.
Expand Down
2 changes: 2 additions & 0 deletions Packet++/header/IPSecLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace pcpp
uint32_t sequenceNumber;
};
#pragma pack(pop)
static_assert(sizeof(ipsec_authentication_header) == 12, "ipsec_authentication_header size is not 12 bytes");

/**
* @struct ipsec_esp
Expand All @@ -43,6 +44,7 @@ namespace pcpp
uint32_t sequenceNumber;
};
#pragma pack(pop)
static_assert(sizeof(ipsec_esp) == 8, "ipsec_esp size is not 8 bytes");

/**
* @class AuthenticationHeaderLayer
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/IPv4Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace pcpp
/*The options start here. */
};
#pragma pack(pop)
static_assert(sizeof(iphdr) == 20, "iphdr size is not 20 bytes");

/**
* An enum for all possible IPv4 and IPv6 protocol types
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/IPv6Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace pcpp
uint8_t ipDst[16];
};
#pragma pack(pop)
static_assert(sizeof(ip6_hdr) == 40, "ip6_hdr size is not 40 bytes");

/**
* @class IPv6Layer
Expand Down
15 changes: 15 additions & 0 deletions Packet++/header/IcmpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace pcpp
uint16_t checksum;
} icmphdr;
#pragma pack(pop)
static_assert(sizeof(icmphdr) == 4, "icmphdr size is not 4 bytes");

/**
* An enum of all supported ICMP message types
Expand Down Expand Up @@ -134,6 +135,7 @@ namespace pcpp
uint64_t timestamp;
} icmp_echo_hdr;
#pragma pack(pop)
static_assert(sizeof(icmp_echo_hdr) == 16, "icmp_echo_hdr size is not 16 bytes");

/**
* @struct icmp_echo_request
Expand Down Expand Up @@ -174,12 +176,14 @@ namespace pcpp
uint32_t transmitTimestamp;
} icmp_timestamp_request;
#pragma pack(pop)
static_assert(sizeof(icmp_timestamp_request) == 20, "icmp_timestamp_request size is not 20 bytes");

/**
* @typedef icmp_timestamp_reply
* ICMP timestamp reply message structure, same as icmp_timestamp_request
*/
typedef icmp_timestamp_request icmp_timestamp_reply;
static_assert(sizeof(icmp_timestamp_reply) == 20, "icmp_timestamp_reply size is not 20 bytes");

/**
* @struct icmp_destination_unreachable
Expand All @@ -194,6 +198,7 @@ namespace pcpp
uint16_t nextHopMTU;
} icmp_destination_unreachable;
#pragma pack(pop)
static_assert(sizeof(icmp_destination_unreachable) == 8, "icmp_destination_unreachable size is not 8 bytes");

/**
* @struct icmp_time_exceeded
Expand All @@ -206,12 +211,14 @@ namespace pcpp
uint32_t unused;
} icmp_time_exceeded;
#pragma pack(pop)
static_assert(sizeof(icmp_time_exceeded) == 8, "icmp_time_exceeded size is not 8 bytes");

/**
* @typedef icmp_source_quench
* ICMP source quence message structure, same as icmp_time_exceeded
*/
typedef icmp_time_exceeded icmp_source_quench;
static_assert(sizeof(icmp_source_quench) == 8, "icmp_source_quench size is not 8 bytes");

/**
* @struct icmp_param_problem
Expand All @@ -229,12 +236,14 @@ namespace pcpp
uint16_t unused2;
} icmp_param_problem;
#pragma pack(pop)
static_assert(sizeof(icmp_param_problem) == 8, "icmp_param_problem size is not 8 bytes");

/**
* @typedef icmp_router_solicitation
* ICMP router solicitation message structure, same as icmphdr
*/
typedef icmphdr icmp_router_solicitation;
static_assert(sizeof(icmp_router_solicitation) == 4, "icmp_router_solicitation size is not 4 bytes");

/**
* @struct icmp_redirect
Expand All @@ -247,6 +256,7 @@ namespace pcpp
uint32_t gatewayAddress;
} icmp_redirect;
#pragma pack(pop)
static_assert(sizeof(icmp_redirect) == 8, "icmp_redirect size is not 8 bytes");

/**
* @struct icmp_router_address_structure
Expand Down Expand Up @@ -278,6 +288,7 @@ namespace pcpp
}
};
#pragma pack(pop)
static_assert(sizeof(icmp_router_address_structure) == 8, "icmp_router_address_structure size is not 8 bytes");

/**
* @struct icmp_router_advertisement_hdr
Expand All @@ -296,6 +307,7 @@ namespace pcpp
uint16_t lifetime;
} icmp_router_advertisement_hdr;
#pragma pack(pop)
static_assert(sizeof(icmp_router_advertisement_hdr) == 8, "icmp_router_advertisement_hdr size is not 8 bytes");

/**
* @struct icmp_router_advertisement
Expand Down Expand Up @@ -331,6 +343,7 @@ namespace pcpp
uint32_t addressMask;
} icmp_address_mask_request;
#pragma pack(pop)
static_assert(sizeof(icmp_address_mask_request) == 12, "icmp_address_mask_request size is not 12 bytes");

/**
* @typedef icmp_address_mask_reply
Expand All @@ -351,12 +364,14 @@ namespace pcpp
uint16_t sequence;
} icmp_info_request;
#pragma pack(pop)
static_assert(sizeof(icmp_info_request) == 8, "icmp_info_request size is not 8 bytes");

/**
* @typedef icmp_info_reply
* ICMP information reply message structure, same as icmp_info_request
*/
typedef icmp_info_request icmp_info_reply;
static_assert(sizeof(icmp_info_reply) == 8, "icmp_info_reply size is not 8 bytes");

/**
* @class IcmpLayer
Expand Down
2 changes: 2 additions & 0 deletions Packet++/header/IcmpV6Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ namespace pcpp
uint16_t checksum;
};
#pragma pack(pop)
static_assert(sizeof(icmpv6hdr) == 4, "icmpv6hdr size is not 4 bytes");

/**
* @struct icmpv6_echo_hdr
Expand All @@ -124,6 +125,7 @@ namespace pcpp
uint16_t sequence;
} icmpv6_echo_hdr;
#pragma pack(pop)
static_assert(sizeof(icmpv6_echo_hdr) == 8, "icmpv6_echo_hdr size is not 8 bytes");

/**
* @class IcmpV6Layer
Expand Down
3 changes: 3 additions & 0 deletions Packet++/header/IgmpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace pcpp
*/
uint32_t groupAddress;
};
static_assert(sizeof(igmp_header) == 8, "igmp_header size is not 8 bytes");

/**
* @struct igmpv3_query_header
Expand All @@ -52,6 +53,7 @@ namespace pcpp
/** This field specifies the number of source addresses present in the Query */
uint16_t numOfSources;
};
static_assert(sizeof(igmpv3_query_header) == 12, "igmpv3_query_header size is not 12 bytes");

/**
* @struct igmpv3_report_header
Expand All @@ -70,6 +72,7 @@ namespace pcpp
/** This field specifies the number of group records present in the Report */
uint16_t numOfGroupRecords;
};
static_assert(sizeof(igmpv3_report_header) == 8, "igmpv3_report_header size is not 8 bytes");

/**
* @struct igmpv3_group_record
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/LLCLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace pcpp
control;
};
#pragma pack(pop)
static_assert(sizeof(llc_header) == 3, "llc_header size is not 3 bytes");

/**
* @class LLCLayer
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/MplsLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace pcpp
uint8_t ttl;
};
#pragma pack(pop)
static_assert(sizeof(mpls_header) == 4, "mpls_header size is not 4 bytes");

mpls_header* getMplsHeader() const
{
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/NflogLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace pcpp
uint16_t resourceId;
};
#pragma pack(pop)
static_assert(sizeof(nflog_header) == 4, "nflog_header size is not 4 bytes");

/**
* @enum NflogTlvType
Expand Down
6 changes: 5 additions & 1 deletion Packet++/header/NtpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ namespace pcpp
transmitTimestamp;
};
#pragma pack(pop)
static_assert(sizeof(ntp_header) == 48, "ntp_header size is not 48 bytes");

#pragma pack(push, 1)
struct ntp_v3_auth
Expand All @@ -126,6 +127,7 @@ namespace pcpp
uint8_t dgst[8]; // 64 bit DES based
};
#pragma pack(pop)
static_assert(sizeof(ntp_v3_auth) == 12, "ntp_v3_auth size is not 12 bytes");

#pragma pack(push, 1)
struct ntp_v4_auth_md5
Expand All @@ -136,6 +138,7 @@ namespace pcpp
uint8_t dgst[16];
};
#pragma pack(pop)
static_assert(sizeof(ntp_v4_auth_md5) == 20, "ntp_v4_auth_md5 size is not 20 bytes");

#pragma pack(push, 1)
struct ntp_v4_auth_sha1
Expand All @@ -146,10 +149,11 @@ namespace pcpp
uint8_t dgst[20];
};
#pragma pack(pop)
static_assert(sizeof(ntp_v4_auth_sha1) == 24, "ntp_v4_auth_sha1 size is not 24 bytes");

ntp_header* getNtpHeader() const
{
return (ntp_header*)m_Data;
return reinterpret_cast<ntp_header*>(m_Data);
}

public:
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/PPPoELayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace pcpp
uint16_t payloadLength;
};
#pragma pack(pop)
static_assert(sizeof(pppoe_header) == 6, "pppoe_header size is not 6 bytes");

/**
* @class PPPoELayer
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/RadiusLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace pcpp
uint8_t authenticator[16];
};
#pragma pack(pop)
static_assert(sizeof(radius_header) == 20, "radius_header size is not 20 bytes");

/**
* @class RadiusAttribute
Expand Down
2 changes: 2 additions & 0 deletions Packet++/header/S7CommLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace pcpp
uint16_t dataLength;
} s7commhdr;
#pragma pack(pop)
static_assert(sizeof(s7commhdr) == 10, "s7commhdr size is not 10 bytes");

/**
* @struct s7comm_ack_data_hdr
Expand All @@ -40,6 +41,7 @@ namespace pcpp
uint8_t errorCode;
};
#pragma pack(pop)
static_assert(sizeof(s7comm_ack_data_hdr) == 12, "s7comm_ack_data_hdr size is not 12 bytes");

/**
* @class S7CommParameter
Expand Down
1 change: 1 addition & 0 deletions Packet++/header/SSHLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ namespace pcpp
uint8_t messageCode;
};
#pragma pack(pop)
static_assert(sizeof(ssh_message_base) == 6, "ssh_message_base size is not 6 bytes");

// this layer supports only parsing
SSHHandshakeMessage();
Expand Down
Loading

0 comments on commit 4ea938b

Please sign in to comment.