pub struct ProxyV2Parser<'t, EXT: PP2TlvRestore = ProxyV2Dummy> { /* private fields */ }
Expand description
A HaProxy V2 parser main instance.
Implementations§
Source§impl<'t> ProxyV2Parser<'t>
impl<'t> ProxyV2Parser<'t>
Sourcepub fn try_from_slice(value: &'t [u8]) -> HaProxRes<Self>
pub fn try_from_slice(value: &'t [u8]) -> HaProxRes<Self>
Constructs the instance from the temporary reference to the buffer.
Source§impl<'t, EXT: PP2TlvRestore> ProxyV2Parser<'t, EXT>
impl<'t, EXT: PP2TlvRestore> ProxyV2Parser<'t, EXT>
Sourcepub fn try_from_slice_custom(value: &'t [u8]) -> HaProxRes<Self>
pub fn try_from_slice_custom(value: &'t [u8]) -> HaProxRes<Self>
Constructs the instance from the temporary reference to the buffer with custom extenal TLV parser for custom TLV extensions.
Examples found in repository?
68fn main() -> HaProxRes<()>
69{
70
71 let pkt_ssl =
72b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a\x21\x11\x00\x1e\
73\x7f\x00\x00\x01\x7f\x00\x00\x43\x9b\x4a\x2e\x6b\x20\x00\x0f\x01\
74\x00\x00\x00\x00\x21\x00\x07\x54\x4c\x53\x76\x31\x2e\x32\xE0\x00\
75\x08\x01\x02\x03\x04\x05\x06\x07\x08";
76
77 let dec = ProxyV2Parser::<ProxyV2Dummy2>::try_from_slice_custom(pkt_ssl.as_slice()).unwrap();
78
79 assert_eq!(dec.get_transport().is_ok(), true);
80 assert_eq!(dec.get_transport().unwrap(), ProxyTransportFam::STREAM);
81
82 assert_eq!(dec.get_proto_version(), ProtocolVersion::V2);
83 assert_eq!(dec.get_proto_command(), HdrV2Command::PROXY);
84
85 assert_eq!(dec.get_address_family().is_ok(), true);
86 assert_eq!(dec.get_address_family().unwrap(), ProxyV2AddrType::AfInet);
87
88 let addr = dec.get_address().unwrap();
89
90 assert_eq!(addr.is_some(), true);
91
92 let addr = addr.unwrap();
93 let maddr = ProxyV2Addr::try_from(("127.0.0.1:39754", "127.0.0.67:11883")).unwrap();
94
95 assert_eq!(addr, maddr);
96
97 let tlv_iter = dec.get_tlvs_iter();
98
99 assert_eq!(tlv_iter.is_some(), true);
100
101 let mut tlv_iter = tlv_iter.unwrap();
102
103 let type_ssl = tlv_iter.next().unwrap().take_internal().unwrap();
104
105 assert_eq!(type_ssl.get_type(), PP2Tlvs::TYPE_SSL);
106 let PP2Tlvs::TypeSsl { client, verify } = type_ssl else {panic!("wrong")};
107
108 assert_eq!(client, PP2TlvClient::PP2_CLIENT_SSL);
109 assert_eq!(verify, 0);
110
111 // --
112 let type_ssl_version = tlv_iter.next().unwrap().take_internal().unwrap();
113
114 assert_eq!(type_ssl_version.get_type(), PP2Tlvs::TYPE_SUBTYPE_SSL_VERSION);
115
116 let PP2Tlvs::TypeSubtypeSslVersion(ssl_version) = type_ssl_version else { panic!("wrong") };
117
118 assert_eq!(ssl_version, "TLSv1.2");
119
120 // ---
121 let ext_type_e0 = tlv_iter.next().unwrap().take_external().unwrap();
122
123 assert_eq!(ext_type_e0.get_type(), 0xE0);
124
125 let ProxyV2Dummy2::SomeTlvName(arg0, arg1) = ext_type_e0 else {panic!("wrong")};
126
127 assert_eq!(arg0, 0x01020304);
128 assert_eq!(arg1, 0x05060708);
129
130
131 return Ok(());
132}
Source§impl<'t, EXT: PP2TlvRestore> ProxyV2Parser<'t, EXT>
impl<'t, EXT: PP2TlvRestore> ProxyV2Parser<'t, EXT>
Sourcepub fn get_proto_version(&self) -> ProtocolVersion
pub fn get_proto_version(&self) -> ProtocolVersion
Extracts and decodes the protocol version from the packet.
Examples found in repository?
68fn main() -> HaProxRes<()>
69{
70
71 let pkt_ssl =
72b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a\x21\x11\x00\x1e\
73\x7f\x00\x00\x01\x7f\x00\x00\x43\x9b\x4a\x2e\x6b\x20\x00\x0f\x01\
74\x00\x00\x00\x00\x21\x00\x07\x54\x4c\x53\x76\x31\x2e\x32\xE0\x00\
75\x08\x01\x02\x03\x04\x05\x06\x07\x08";
76
77 let dec = ProxyV2Parser::<ProxyV2Dummy2>::try_from_slice_custom(pkt_ssl.as_slice()).unwrap();
78
79 assert_eq!(dec.get_transport().is_ok(), true);
80 assert_eq!(dec.get_transport().unwrap(), ProxyTransportFam::STREAM);
81
82 assert_eq!(dec.get_proto_version(), ProtocolVersion::V2);
83 assert_eq!(dec.get_proto_command(), HdrV2Command::PROXY);
84
85 assert_eq!(dec.get_address_family().is_ok(), true);
86 assert_eq!(dec.get_address_family().unwrap(), ProxyV2AddrType::AfInet);
87
88 let addr = dec.get_address().unwrap();
89
90 assert_eq!(addr.is_some(), true);
91
92 let addr = addr.unwrap();
93 let maddr = ProxyV2Addr::try_from(("127.0.0.1:39754", "127.0.0.67:11883")).unwrap();
94
95 assert_eq!(addr, maddr);
96
97 let tlv_iter = dec.get_tlvs_iter();
98
99 assert_eq!(tlv_iter.is_some(), true);
100
101 let mut tlv_iter = tlv_iter.unwrap();
102
103 let type_ssl = tlv_iter.next().unwrap().take_internal().unwrap();
104
105 assert_eq!(type_ssl.get_type(), PP2Tlvs::TYPE_SSL);
106 let PP2Tlvs::TypeSsl { client, verify } = type_ssl else {panic!("wrong")};
107
108 assert_eq!(client, PP2TlvClient::PP2_CLIENT_SSL);
109 assert_eq!(verify, 0);
110
111 // --
112 let type_ssl_version = tlv_iter.next().unwrap().take_internal().unwrap();
113
114 assert_eq!(type_ssl_version.get_type(), PP2Tlvs::TYPE_SUBTYPE_SSL_VERSION);
115
116 let PP2Tlvs::TypeSubtypeSslVersion(ssl_version) = type_ssl_version else { panic!("wrong") };
117
118 assert_eq!(ssl_version, "TLSv1.2");
119
120 // ---
121 let ext_type_e0 = tlv_iter.next().unwrap().take_external().unwrap();
122
123 assert_eq!(ext_type_e0.get_type(), 0xE0);
124
125 let ProxyV2Dummy2::SomeTlvName(arg0, arg1) = ext_type_e0 else {panic!("wrong")};
126
127 assert_eq!(arg0, 0x01020304);
128 assert_eq!(arg1, 0x05060708);
129
130
131 return Ok(());
132}
Sourcepub fn get_proto_command(&self) -> HdrV2Command
pub fn get_proto_command(&self) -> HdrV2Command
Extracts and decodes the protocol command code from the packet.
Examples found in repository?
68fn main() -> HaProxRes<()>
69{
70
71 let pkt_ssl =
72b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a\x21\x11\x00\x1e\
73\x7f\x00\x00\x01\x7f\x00\x00\x43\x9b\x4a\x2e\x6b\x20\x00\x0f\x01\
74\x00\x00\x00\x00\x21\x00\x07\x54\x4c\x53\x76\x31\x2e\x32\xE0\x00\
75\x08\x01\x02\x03\x04\x05\x06\x07\x08";
76
77 let dec = ProxyV2Parser::<ProxyV2Dummy2>::try_from_slice_custom(pkt_ssl.as_slice()).unwrap();
78
79 assert_eq!(dec.get_transport().is_ok(), true);
80 assert_eq!(dec.get_transport().unwrap(), ProxyTransportFam::STREAM);
81
82 assert_eq!(dec.get_proto_version(), ProtocolVersion::V2);
83 assert_eq!(dec.get_proto_command(), HdrV2Command::PROXY);
84
85 assert_eq!(dec.get_address_family().is_ok(), true);
86 assert_eq!(dec.get_address_family().unwrap(), ProxyV2AddrType::AfInet);
87
88 let addr = dec.get_address().unwrap();
89
90 assert_eq!(addr.is_some(), true);
91
92 let addr = addr.unwrap();
93 let maddr = ProxyV2Addr::try_from(("127.0.0.1:39754", "127.0.0.67:11883")).unwrap();
94
95 assert_eq!(addr, maddr);
96
97 let tlv_iter = dec.get_tlvs_iter();
98
99 assert_eq!(tlv_iter.is_some(), true);
100
101 let mut tlv_iter = tlv_iter.unwrap();
102
103 let type_ssl = tlv_iter.next().unwrap().take_internal().unwrap();
104
105 assert_eq!(type_ssl.get_type(), PP2Tlvs::TYPE_SSL);
106 let PP2Tlvs::TypeSsl { client, verify } = type_ssl else {panic!("wrong")};
107
108 assert_eq!(client, PP2TlvClient::PP2_CLIENT_SSL);
109 assert_eq!(verify, 0);
110
111 // --
112 let type_ssl_version = tlv_iter.next().unwrap().take_internal().unwrap();
113
114 assert_eq!(type_ssl_version.get_type(), PP2Tlvs::TYPE_SUBTYPE_SSL_VERSION);
115
116 let PP2Tlvs::TypeSubtypeSslVersion(ssl_version) = type_ssl_version else { panic!("wrong") };
117
118 assert_eq!(ssl_version, "TLSv1.2");
119
120 // ---
121 let ext_type_e0 = tlv_iter.next().unwrap().take_external().unwrap();
122
123 assert_eq!(ext_type_e0.get_type(), 0xE0);
124
125 let ProxyV2Dummy2::SomeTlvName(arg0, arg1) = ext_type_e0 else {panic!("wrong")};
126
127 assert_eq!(arg0, 0x01020304);
128 assert_eq!(arg1, 0x05060708);
129
130
131 return Ok(());
132}
Sourcepub fn get_transport(&self) -> HaProxRes<ProxyTransportFam>
pub fn get_transport(&self) -> HaProxRes<ProxyTransportFam>
Extracts and decodes the transport type from the packet.
Examples found in repository?
68fn main() -> HaProxRes<()>
69{
70
71 let pkt_ssl =
72b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a\x21\x11\x00\x1e\
73\x7f\x00\x00\x01\x7f\x00\x00\x43\x9b\x4a\x2e\x6b\x20\x00\x0f\x01\
74\x00\x00\x00\x00\x21\x00\x07\x54\x4c\x53\x76\x31\x2e\x32\xE0\x00\
75\x08\x01\x02\x03\x04\x05\x06\x07\x08";
76
77 let dec = ProxyV2Parser::<ProxyV2Dummy2>::try_from_slice_custom(pkt_ssl.as_slice()).unwrap();
78
79 assert_eq!(dec.get_transport().is_ok(), true);
80 assert_eq!(dec.get_transport().unwrap(), ProxyTransportFam::STREAM);
81
82 assert_eq!(dec.get_proto_version(), ProtocolVersion::V2);
83 assert_eq!(dec.get_proto_command(), HdrV2Command::PROXY);
84
85 assert_eq!(dec.get_address_family().is_ok(), true);
86 assert_eq!(dec.get_address_family().unwrap(), ProxyV2AddrType::AfInet);
87
88 let addr = dec.get_address().unwrap();
89
90 assert_eq!(addr.is_some(), true);
91
92 let addr = addr.unwrap();
93 let maddr = ProxyV2Addr::try_from(("127.0.0.1:39754", "127.0.0.67:11883")).unwrap();
94
95 assert_eq!(addr, maddr);
96
97 let tlv_iter = dec.get_tlvs_iter();
98
99 assert_eq!(tlv_iter.is_some(), true);
100
101 let mut tlv_iter = tlv_iter.unwrap();
102
103 let type_ssl = tlv_iter.next().unwrap().take_internal().unwrap();
104
105 assert_eq!(type_ssl.get_type(), PP2Tlvs::TYPE_SSL);
106 let PP2Tlvs::TypeSsl { client, verify } = type_ssl else {panic!("wrong")};
107
108 assert_eq!(client, PP2TlvClient::PP2_CLIENT_SSL);
109 assert_eq!(verify, 0);
110
111 // --
112 let type_ssl_version = tlv_iter.next().unwrap().take_internal().unwrap();
113
114 assert_eq!(type_ssl_version.get_type(), PP2Tlvs::TYPE_SUBTYPE_SSL_VERSION);
115
116 let PP2Tlvs::TypeSubtypeSslVersion(ssl_version) = type_ssl_version else { panic!("wrong") };
117
118 assert_eq!(ssl_version, "TLSv1.2");
119
120 // ---
121 let ext_type_e0 = tlv_iter.next().unwrap().take_external().unwrap();
122
123 assert_eq!(ext_type_e0.get_type(), 0xE0);
124
125 let ProxyV2Dummy2::SomeTlvName(arg0, arg1) = ext_type_e0 else {panic!("wrong")};
126
127 assert_eq!(arg0, 0x01020304);
128 assert_eq!(arg1, 0x05060708);
129
130
131 return Ok(());
132}
Sourcepub fn get_address_family(&self) -> HaProxRes<ProxyV2AddrType>
pub fn get_address_family(&self) -> HaProxRes<ProxyV2AddrType>
Extracts and decodes the address family from the packet. It doesnot extract address.
Examples found in repository?
68fn main() -> HaProxRes<()>
69{
70
71 let pkt_ssl =
72b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a\x21\x11\x00\x1e\
73\x7f\x00\x00\x01\x7f\x00\x00\x43\x9b\x4a\x2e\x6b\x20\x00\x0f\x01\
74\x00\x00\x00\x00\x21\x00\x07\x54\x4c\x53\x76\x31\x2e\x32\xE0\x00\
75\x08\x01\x02\x03\x04\x05\x06\x07\x08";
76
77 let dec = ProxyV2Parser::<ProxyV2Dummy2>::try_from_slice_custom(pkt_ssl.as_slice()).unwrap();
78
79 assert_eq!(dec.get_transport().is_ok(), true);
80 assert_eq!(dec.get_transport().unwrap(), ProxyTransportFam::STREAM);
81
82 assert_eq!(dec.get_proto_version(), ProtocolVersion::V2);
83 assert_eq!(dec.get_proto_command(), HdrV2Command::PROXY);
84
85 assert_eq!(dec.get_address_family().is_ok(), true);
86 assert_eq!(dec.get_address_family().unwrap(), ProxyV2AddrType::AfInet);
87
88 let addr = dec.get_address().unwrap();
89
90 assert_eq!(addr.is_some(), true);
91
92 let addr = addr.unwrap();
93 let maddr = ProxyV2Addr::try_from(("127.0.0.1:39754", "127.0.0.67:11883")).unwrap();
94
95 assert_eq!(addr, maddr);
96
97 let tlv_iter = dec.get_tlvs_iter();
98
99 assert_eq!(tlv_iter.is_some(), true);
100
101 let mut tlv_iter = tlv_iter.unwrap();
102
103 let type_ssl = tlv_iter.next().unwrap().take_internal().unwrap();
104
105 assert_eq!(type_ssl.get_type(), PP2Tlvs::TYPE_SSL);
106 let PP2Tlvs::TypeSsl { client, verify } = type_ssl else {panic!("wrong")};
107
108 assert_eq!(client, PP2TlvClient::PP2_CLIENT_SSL);
109 assert_eq!(verify, 0);
110
111 // --
112 let type_ssl_version = tlv_iter.next().unwrap().take_internal().unwrap();
113
114 assert_eq!(type_ssl_version.get_type(), PP2Tlvs::TYPE_SUBTYPE_SSL_VERSION);
115
116 let PP2Tlvs::TypeSubtypeSslVersion(ssl_version) = type_ssl_version else { panic!("wrong") };
117
118 assert_eq!(ssl_version, "TLSv1.2");
119
120 // ---
121 let ext_type_e0 = tlv_iter.next().unwrap().take_external().unwrap();
122
123 assert_eq!(ext_type_e0.get_type(), 0xE0);
124
125 let ProxyV2Dummy2::SomeTlvName(arg0, arg1) = ext_type_e0 else {panic!("wrong")};
126
127 assert_eq!(arg0, 0x01020304);
128 assert_eq!(arg1, 0x05060708);
129
130
131 return Ok(());
132}
Sourcepub fn get_palyload_len(&self) -> u16
pub fn get_palyload_len(&self) -> u16
Returns the size of the packet (without header len).
Sourcepub fn get_full_len(&self) -> u16
pub fn get_full_len(&self) -> u16
Returns the full size of the packet.
Sourcepub fn get_address(&self) -> HaProxRes<Option<ProxyV2Addr>>
pub fn get_address(&self) -> HaProxRes<Option<ProxyV2Addr>>
Extracts the address section from the packet. The address family can be obtained from the returned instance.
§Returns
A HaProxRes is returned with:
-
Result::Ok - the Option with:
-
- Option::Some - a parsed address is returned in form of ProxyV2Addr
-
- Option::None - if address family is unspecified.
-
Result::Err - an error description.
Examples found in repository?
68fn main() -> HaProxRes<()>
69{
70
71 let pkt_ssl =
72b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a\x21\x11\x00\x1e\
73\x7f\x00\x00\x01\x7f\x00\x00\x43\x9b\x4a\x2e\x6b\x20\x00\x0f\x01\
74\x00\x00\x00\x00\x21\x00\x07\x54\x4c\x53\x76\x31\x2e\x32\xE0\x00\
75\x08\x01\x02\x03\x04\x05\x06\x07\x08";
76
77 let dec = ProxyV2Parser::<ProxyV2Dummy2>::try_from_slice_custom(pkt_ssl.as_slice()).unwrap();
78
79 assert_eq!(dec.get_transport().is_ok(), true);
80 assert_eq!(dec.get_transport().unwrap(), ProxyTransportFam::STREAM);
81
82 assert_eq!(dec.get_proto_version(), ProtocolVersion::V2);
83 assert_eq!(dec.get_proto_command(), HdrV2Command::PROXY);
84
85 assert_eq!(dec.get_address_family().is_ok(), true);
86 assert_eq!(dec.get_address_family().unwrap(), ProxyV2AddrType::AfInet);
87
88 let addr = dec.get_address().unwrap();
89
90 assert_eq!(addr.is_some(), true);
91
92 let addr = addr.unwrap();
93 let maddr = ProxyV2Addr::try_from(("127.0.0.1:39754", "127.0.0.67:11883")).unwrap();
94
95 assert_eq!(addr, maddr);
96
97 let tlv_iter = dec.get_tlvs_iter();
98
99 assert_eq!(tlv_iter.is_some(), true);
100
101 let mut tlv_iter = tlv_iter.unwrap();
102
103 let type_ssl = tlv_iter.next().unwrap().take_internal().unwrap();
104
105 assert_eq!(type_ssl.get_type(), PP2Tlvs::TYPE_SSL);
106 let PP2Tlvs::TypeSsl { client, verify } = type_ssl else {panic!("wrong")};
107
108 assert_eq!(client, PP2TlvClient::PP2_CLIENT_SSL);
109 assert_eq!(verify, 0);
110
111 // --
112 let type_ssl_version = tlv_iter.next().unwrap().take_internal().unwrap();
113
114 assert_eq!(type_ssl_version.get_type(), PP2Tlvs::TYPE_SUBTYPE_SSL_VERSION);
115
116 let PP2Tlvs::TypeSubtypeSslVersion(ssl_version) = type_ssl_version else { panic!("wrong") };
117
118 assert_eq!(ssl_version, "TLSv1.2");
119
120 // ---
121 let ext_type_e0 = tlv_iter.next().unwrap().take_external().unwrap();
122
123 assert_eq!(ext_type_e0.get_type(), 0xE0);
124
125 let ProxyV2Dummy2::SomeTlvName(arg0, arg1) = ext_type_e0 else {panic!("wrong")};
126
127 assert_eq!(arg0, 0x01020304);
128 assert_eq!(arg1, 0x05060708);
129
130
131 return Ok(());
132}
Sourcepub fn get_tlvs_iter(&self) -> Option<ProxyV2TlvIter<'_, EXT>>
pub fn get_tlvs_iter(&self) -> Option<ProxyV2TlvIter<'_, EXT>>
Creates an iterator over TLVs which are found in the packet. This is flat iterator i.e if TLV contains a subcodes, it will be returned as it is just in a 1D array. You should track if item contains a subcode and a subcode is returned.
Examples found in repository?
68fn main() -> HaProxRes<()>
69{
70
71 let pkt_ssl =
72b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a\x21\x11\x00\x1e\
73\x7f\x00\x00\x01\x7f\x00\x00\x43\x9b\x4a\x2e\x6b\x20\x00\x0f\x01\
74\x00\x00\x00\x00\x21\x00\x07\x54\x4c\x53\x76\x31\x2e\x32\xE0\x00\
75\x08\x01\x02\x03\x04\x05\x06\x07\x08";
76
77 let dec = ProxyV2Parser::<ProxyV2Dummy2>::try_from_slice_custom(pkt_ssl.as_slice()).unwrap();
78
79 assert_eq!(dec.get_transport().is_ok(), true);
80 assert_eq!(dec.get_transport().unwrap(), ProxyTransportFam::STREAM);
81
82 assert_eq!(dec.get_proto_version(), ProtocolVersion::V2);
83 assert_eq!(dec.get_proto_command(), HdrV2Command::PROXY);
84
85 assert_eq!(dec.get_address_family().is_ok(), true);
86 assert_eq!(dec.get_address_family().unwrap(), ProxyV2AddrType::AfInet);
87
88 let addr = dec.get_address().unwrap();
89
90 assert_eq!(addr.is_some(), true);
91
92 let addr = addr.unwrap();
93 let maddr = ProxyV2Addr::try_from(("127.0.0.1:39754", "127.0.0.67:11883")).unwrap();
94
95 assert_eq!(addr, maddr);
96
97 let tlv_iter = dec.get_tlvs_iter();
98
99 assert_eq!(tlv_iter.is_some(), true);
100
101 let mut tlv_iter = tlv_iter.unwrap();
102
103 let type_ssl = tlv_iter.next().unwrap().take_internal().unwrap();
104
105 assert_eq!(type_ssl.get_type(), PP2Tlvs::TYPE_SSL);
106 let PP2Tlvs::TypeSsl { client, verify } = type_ssl else {panic!("wrong")};
107
108 assert_eq!(client, PP2TlvClient::PP2_CLIENT_SSL);
109 assert_eq!(verify, 0);
110
111 // --
112 let type_ssl_version = tlv_iter.next().unwrap().take_internal().unwrap();
113
114 assert_eq!(type_ssl_version.get_type(), PP2Tlvs::TYPE_SUBTYPE_SSL_VERSION);
115
116 let PP2Tlvs::TypeSubtypeSslVersion(ssl_version) = type_ssl_version else { panic!("wrong") };
117
118 assert_eq!(ssl_version, "TLSv1.2");
119
120 // ---
121 let ext_type_e0 = tlv_iter.next().unwrap().take_external().unwrap();
122
123 assert_eq!(ext_type_e0.get_type(), 0xE0);
124
125 let ProxyV2Dummy2::SomeTlvName(arg0, arg1) = ext_type_e0 else {panic!("wrong")};
126
127 assert_eq!(arg0, 0x01020304);
128 assert_eq!(arg1, 0x05060708);
129
130
131 return Ok(());
132}