#ifndef _IPV4_HELPERS_H #define _IPV4_HELPERS_H #ifdef __cpluscplus extern "C" { #endif #include #include /* * Internet Header Format * * 0 1 2 3 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * |Version| IHL |Type of Service| Total Length | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Identification |Flags| Fragment Offset | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Time to Live | Protocol | Header Checksum | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Source Address | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Destination Address | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Options | Padding | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ static inline uint8_t ipv4_hdr_get_version(const struct ip *hdr) { return hdr->ip_v; } static inline uint8_t ipv4_hdr_get_hl(const struct ip *hdr) { return hdr->ip_hl << 2; } static inline uint8_t ipv4_hdr_get_tos(const struct ip *hdr) { return hdr->ip_tos; } static inline uint16_t ipv4_hdr_get_len(const struct ip *hdr) { return ntohs(hdr->ip_len); } static inline uint16_t ipv4_hdr_get_ipid(const struct ip *hdr) { return ntohs(hdr->ip_id); } static inline uint8_t ipv4_hdr_get_flags(const struct ip *hdr) { return (ntohs(hdr->ip_off) & (~IP_OFFMASK)) >> 13; } static inline bool ipv4_hdr_has_flag_rf(const struct ip *hdr) { return (ntohs(hdr->ip_off) & IP_RF) != 0; } static inline bool ipv4_hdr_has_flag_df(const struct ip *hdr) { return (ntohs(hdr->ip_off) & IP_DF) != 0; } static inline bool ipv4_hdr_has_flag_mf(const struct ip *hdr) { return (ntohs(hdr->ip_off) & IP_MF) != 0; } static inline uint16_t ipv4_hdr_get_frag_offset(const struct ip *hdr) { return ntohs(hdr->ip_off) & IP_OFFMASK; } static inline uint8_t ipv4_hdr_get_ttl(const struct ip *hdr) { return hdr->ip_ttl; } static inline uint8_t ipv4_hdr_get_protocol(const struct ip *hdr) { return hdr->ip_p; } static inline uint16_t ipv4_hdr_get_checksum(const struct ip *hdr) { return ntohs(hdr->ip_sum); } static inline uint32_t ipv4_hdr_get_host_order_saddr(const struct ip *hdr) { return ntohl(hdr->ip_src.s_addr); } static inline uint32_t ipv4_hdr_get_host_order_daddr(const struct ip *hdr) { return ntohl(hdr->ip_dst.s_addr); } static inline struct in_addr ipv4_hdr_get_net_order_saddr(const struct ip *hdr) { return hdr->ip_src; } static inline struct in_addr ipv4_hdr_get_net_order_daddr(const struct ip *hdr) { return hdr->ip_dst; } static inline uint8_t ipv4_hdr_get_opt_len(const struct ip *hdr) { return ipv4_hdr_get_hl(hdr) - sizeof(struct ip); } static inline const uint8_t *ipv4_hdr_get_opt_ptr(const struct ip *hdr) { return (const uint8_t *)hdr + sizeof(struct ip); } #ifdef __cpluscplus } #endif #endif