Line data Source code
1 : #ifndef HEADER_fd_src_waltz_quic_fd_quic_proto_h 2 : #define HEADER_fd_src_waltz_quic_fd_quic_proto_h 3 : 4 : #include "fd_quic_proto_structs.h" 5 : 6 : #include "fd_quic_common.h" 7 : #include "fd_quic_types.h" 8 : 9 : #include "templ/fd_quic_parsers_decl.h" 10 : #include "templ/fd_quic_templ.h" 11 : #include "templ/fd_quic_frames_templ.h" 12 : #include "templ/fd_quic_undefs.h" 13 : 14 : #include "templ/fd_quic_templ_dump_decl.h" 15 : #include "templ/fd_quic_templ.h" 16 : #include "templ/fd_quic_frames_templ.h" 17 : #include "templ/fd_quic_undefs.h" 18 : 19 : #include "templ/fd_quic_max_footprint.h" 20 : #include "templ/fd_quic_templ.h" 21 : #include "templ/fd_quic_frames_templ.h" 22 : #include "templ/fd_quic_undefs.h" 23 : 24 : #include "templ/fd_quic_encoders_decl.h" 25 : #include "templ/fd_quic_templ.h" 26 : #include "templ/fd_quic_frames_templ.h" 27 : #include "templ/fd_quic_undefs.h" 28 : 29 : #include "../../util/net/fd_ip4.h" 30 : #include "../../util/net/fd_udp.h" 31 : 32 : /* Parses an IPv4 header into out with host byte order. buf points to 33 : the first byte of the IPv4 header on the wire. */ 34 : 35 : static inline ulong 36 : fd_quic_decode_ip4( fd_ip4_hdr_t * FD_RESTRICT out, 37 : uchar const * FD_RESTRICT buf, 38 14369924 : ulong sz ) { 39 14369924 : if( FD_UNLIKELY( sz < sizeof( fd_ip4_hdr_t ) ) ) { 40 0 : return FD_QUIC_PARSE_FAIL; 41 0 : } 42 : 43 : /* FIXME unaligned accesses */ 44 14369924 : fd_ip4_hdr_t const * peek = (fd_ip4_hdr_t const *)fd_type_pun_const( buf ); 45 14369924 : ulong hdr_len = FD_IP4_GET_LEN(*peek); 46 14369924 : ulong version = FD_IP4_GET_VERSION(*peek); 47 14369924 : if( FD_UNLIKELY( (version!=4) | (hdr_len<20UL) | (sz<hdr_len) ) ) { 48 0 : return FD_QUIC_PARSE_FAIL; 49 0 : } 50 : 51 14369924 : *out = *peek; 52 14369924 : fd_ip4_hdr_bswap( out ); 53 14369924 : return hdr_len; 54 14369924 : } 55 : 56 : /* Encodes a short IPv4 header into buf suitable for transmit over the 57 : wire. sz is the number of bytes that buf can hold. frame is an IPv4 58 : header in host byte order. Returns the number of bytes written or 59 : FD_QUIC_PARSE_FAIL if sz is too small. */ 60 : 61 : static inline ulong 62 : fd_quic_encode_ip4( uchar * buf, 63 : ulong sz, 64 14370107 : fd_ip4_hdr_t const * frame ) { 65 14370107 : if( FD_UNLIKELY( sz < sizeof(fd_ip4_hdr_t) ) ) { 66 0 : return FD_QUIC_PARSE_FAIL; 67 0 : } 68 14370107 : fd_ip4_hdr_t netorder = *frame; 69 14370107 : fd_ip4_hdr_bswap( &netorder ); 70 14370107 : memcpy( buf, &netorder, sizeof(fd_ip4_hdr_t) ); 71 14370107 : return sizeof(fd_ip4_hdr_t); 72 14370107 : } 73 : 74 : /* Parses a UDP header into out with host byte order. buf points to the 75 : first byte of the UDP header on the wire. */ 76 : 77 : static inline ulong 78 : fd_quic_decode_udp( fd_udp_hdr_t * FD_RESTRICT out, 79 : uchar const * FD_RESTRICT buf, 80 14369924 : ulong sz ) { 81 14369924 : if( FD_UNLIKELY( sz < sizeof(fd_udp_hdr_t) ) ) { 82 0 : return FD_QUIC_PARSE_FAIL; 83 0 : } 84 14369924 : memcpy( out, buf, sizeof(fd_udp_hdr_t) ); 85 14369924 : fd_udp_hdr_bswap( out ); 86 14369924 : return sizeof(fd_udp_hdr_t); 87 14369924 : } 88 : 89 : /* Encodes a UDP header into buf suitable for transmit over the wire. 90 : sz is the number of bytes that buf can hold. frame is a UDP header 91 : in host byte order. Returns the number of bytes written or 92 : FD_QUIC_PARSE_FAIL if sz is too small.*/ 93 : 94 : static inline ulong 95 : fd_quic_encode_udp( uchar * buf, 96 : ulong sz, 97 14370107 : fd_udp_hdr_t const * frame ) { 98 14370107 : if( FD_UNLIKELY( sz < sizeof(fd_udp_hdr_t) ) ) { 99 0 : return FD_QUIC_PARSE_FAIL; 100 0 : } 101 14370107 : fd_udp_hdr_t netorder = *frame; 102 14370107 : fd_udp_hdr_bswap( &netorder ); 103 14370107 : memcpy( buf, &netorder, sizeof(fd_udp_hdr_t) ); 104 14370107 : return sizeof(fd_udp_hdr_t); 105 14370107 : } 106 : 107 : #endif /* HEADER_fd_src_waltz_quic_fd_quic_proto_h */ 108 :