From 643c0776155ec3165198c21708f7a2f88fc02adc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Ter=C3=A4s?= Date: Mon, 13 Jan 2014 13:29:10 +0200 Subject: [PATCH] use anonymous union to provide bsd and gnu variants of struct tcphdr and udphdr --- include/netinet/tcp.h | 92 ++++++++++++++++++++++++++++++++++----------------- include/netinet/udp.h | 26 ++++++++++----- 2 files changed, 79 insertions(+), 39 deletions(-) diff --git a/include/netinet/tcp.h b/include/netinet/tcp.h index 5639b89..09c9279 100644 --- a/include/netinet/tcp.h +++ b/include/netinet/tcp.h @@ -44,42 +44,74 @@ #define SOL_TCP 6 #include #include -#endif - -#ifdef _GNU_SOURCE #include -struct tcphdr -{ - u_int16_t source; - u_int16_t dest; - u_int32_t seq; - u_int32_t ack_seq; + +typedef u_int32_t tcp_seq; + +#define TH_FIN 0x01 +#define TH_SYN 0x02 +#define TH_RST 0x04 +#define TH_PUSH 0x08 +#define TH_ACK 0x10 +#define TH_URG 0x20 + +struct tcphdr { +#ifdef __GNUC__ + __extension__ +#endif + union { + struct { + u_int16_t source; + u_int16_t dest; + u_int32_t seq; + u_int32_t ack_seq; +#if __BYTE_ORDER == __LITTLE_ENDIAN + u_int16_t res1:4; + u_int16_t doff:4; + u_int16_t fin:1; + u_int16_t syn:1; + u_int16_t rst:1; + u_int16_t psh:1; + u_int16_t ack:1; + u_int16_t urg:1; + u_int16_t res2:2; +#else + u_int16_t doff:4; + u_int16_t res1:4; + u_int16_t res2:2; + u_int16_t urg:1; + u_int16_t ack:1; + u_int16_t psh:1; + u_int16_t rst:1; + u_int16_t syn:1; + u_int16_t fin:1; +#endif + u_int16_t window; + u_int16_t check; + u_int16_t urg_ptr; + }; + struct { + u_int16_t th_sport; + u_int16_t th_dport; + tcp_seq th_seq; + tcp_seq th_ack; #if __BYTE_ORDER == __LITTLE_ENDIAN - u_int16_t res1:4; - u_int16_t doff:4; - u_int16_t fin:1; - u_int16_t syn:1; - u_int16_t rst:1; - u_int16_t psh:1; - u_int16_t ack:1; - u_int16_t urg:1; - u_int16_t res2:2; + u_int8_t th_x2:4; + u_int8_t th_off:4; #else - u_int16_t doff:4; - u_int16_t res1:4; - u_int16_t res2:2; - u_int16_t urg:1; - u_int16_t ack:1; - u_int16_t psh:1; - u_int16_t rst:1; - u_int16_t syn:1; - u_int16_t fin:1; + u_int8_t th_off:4; + u_int8_t th_x2:4; #endif - u_int16_t window; - u_int16_t check; - u_int16_t urg_ptr; + u_int8_t th_flags; + u_int16_t th_win; + u_int16_t th_sum; + u_int16_t th_urp; + }; + }; }; +#endif +#ifdef _GNU_SOURCE #define TCPI_OPT_TIMESTAMPS 1 #define TCPI_OPT_SACK 2 #define TCPI_OPT_WSCALE 4 diff --git a/include/netinet/udp.h b/include/netinet/udp.h index 15b9145..223a5e3 100644 --- a/include/netinet/udp.h +++ b/include/netinet/udp.h @@ -8,17 +8,25 @@ extern "C" { #include struct udphdr { - uint16_t source; - uint16_t dest; - uint16_t len; - uint16_t check; +#ifdef __GNUC__ + __extension__ +#endif + union { + struct { + uint16_t source; + uint16_t dest; + uint16_t len; + uint16_t check; + }; + struct { + uint16_t uh_sport; + uint16_t uh_dport; + uint16_t uh_ulen; + uint16_t uh_sum; + }; + }; }; -#define uh_sport source -#define uh_dport dest -#define uh_ulen len -#define uh_sum check - #define UDP_CORK 1 #define UDP_ENCAP 100 -- 1.8.5.2