1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 use std::net::SocketAddr; 15 16 #[cfg(feature = "http3")] 17 use crate::async_impl::QuicConn; 18 use crate::{ConnProtocol, TimeGroup}; 19 20 /// `ConnDetail` trait, which is used to obtain information about the current 21 /// connection. 22 pub trait ConnInfo { 23 /// Whether the current connection is a proxy. is_proxy(&self) -> bool24 fn is_proxy(&self) -> bool; 25 26 /// Gets connection information data. conn_data(&self) -> ConnData27 fn conn_data(&self) -> ConnData; 28 29 /// Gets quic information 30 #[cfg(feature = "http3")] quic_conn(&mut self) -> Option<QuicConn>31 fn quic_conn(&mut self) -> Option<QuicConn>; 32 } 33 34 /// Tcp connection information. 35 #[derive(Clone)] 36 pub struct ConnDetail { 37 /// Transport layer protocol type. 38 pub(crate) protocol: ConnProtocol, 39 /// local socket address. 40 pub(crate) local: SocketAddr, 41 /// peer socket address. 42 pub(crate) peer: SocketAddr, 43 /// peer domain information. 44 pub(crate) addr: String, 45 } 46 47 impl ConnDetail { 48 /// Gets the transport layer protocol for the connection. protocol(&self) -> &ConnProtocol49 pub fn protocol(&self) -> &ConnProtocol { 50 &self.protocol 51 } 52 53 /// Gets the local socket address of the connection. local(&self) -> SocketAddr54 pub fn local(&self) -> SocketAddr { 55 self.local 56 } 57 58 /// Gets the peer socket address of the connection. peer(&self) -> SocketAddr59 pub fn peer(&self) -> SocketAddr { 60 self.peer 61 } 62 63 /// Gets the peer domain address of the connection. addr(&self) -> &str64 pub fn addr(&self) -> &str { 65 &self.addr 66 } 67 } 68 69 /// Negotiated http version information. 70 #[derive(Default, Clone)] 71 pub struct NegotiateInfo { 72 alpn: Option<Vec<u8>>, 73 } 74 75 impl NegotiateInfo { 76 /// Constructs NegotiateInfo with apln extensions. 77 #[cfg(feature = "__tls")] from_alpn(alpn: Option<Vec<u8>>) -> Self78 pub fn from_alpn(alpn: Option<Vec<u8>>) -> Self { 79 Self { alpn } 80 } 81 82 /// tls alpn Indicates extended information. alpn(&self) -> Option<&[u8]>83 pub fn alpn(&self) -> Option<&[u8]> { 84 self.alpn.as_deref() 85 } 86 } 87 88 /// Transport layer connection establishment information data. 89 #[derive(Clone)] 90 pub struct ConnData { 91 detail: ConnDetail, 92 #[cfg(feature = "http2")] 93 negotiate: NegotiateInfo, 94 proxy: bool, 95 time_group: TimeGroup, 96 } 97 98 impl ConnData { 99 /// Construct a `ConnDataBuilder`. builder() -> ConnDataBuilder100 pub fn builder() -> ConnDataBuilder { 101 ConnDataBuilder::default() 102 } 103 detail(self) -> ConnDetail104 pub(crate) fn detail(self) -> ConnDetail { 105 self.detail 106 } 107 108 #[cfg(feature = "http2")] negotiate(&self) -> &NegotiateInfo109 pub(crate) fn negotiate(&self) -> &NegotiateInfo { 110 &self.negotiate 111 } 112 is_proxy(&self) -> bool113 pub(crate) fn is_proxy(&self) -> bool { 114 self.proxy 115 } 116 time_group_mut(&mut self) -> &mut TimeGroup117 pub(crate) fn time_group_mut(&mut self) -> &mut TimeGroup { 118 &mut self.time_group 119 } 120 } 121 122 /// ConnData's builder, which builds ConnData through cascading calls. 123 #[derive(Default)] 124 pub struct ConnDataBuilder { 125 #[cfg(feature = "http2")] 126 negotiate: NegotiateInfo, 127 proxy: bool, 128 time_group: TimeGroup, 129 } 130 131 impl ConnDataBuilder { 132 /// Sets the http negotiation result. 133 #[cfg(all(feature = "__tls", feature = "http2"))] negotiate(mut self, negotiate: NegotiateInfo) -> Self134 pub fn negotiate(mut self, negotiate: NegotiateInfo) -> Self { 135 self.negotiate = negotiate; 136 self 137 } 138 139 /// Sets whether the peer is a proxy. proxy(mut self, proxy: bool) -> Self140 pub fn proxy(mut self, proxy: bool) -> Self { 141 self.proxy = proxy; 142 self 143 } 144 145 /// Set the time required for each phase of connection establishment. time_group(mut self, time_group: TimeGroup) -> Self146 pub fn time_group(mut self, time_group: TimeGroup) -> Self { 147 self.time_group = time_group; 148 self 149 } 150 151 /// Construct ConnData by setting the individual endpoint information. build(self, detail: ConnDetail) -> ConnData152 pub fn build(self, detail: ConnDetail) -> ConnData { 153 ConnData { 154 detail, 155 #[cfg(feature = "http2")] 156 negotiate: self.negotiate, 157 proxy: self.proxy, 158 time_group: self.time_group, 159 } 160 } 161 } 162