hickory_proto/tcp/
mod.rs

1/*
2 * Copyright (C) 2015 Benjamin Fry <benjaminfry@me.com>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! TCP protocol related components for DNS
18mod tcp_client_stream;
19mod tcp_stream;
20
21pub use self::tcp_client_stream::{TcpClientConnect, TcpClientStream};
22pub use self::tcp_stream::{Connect, DnsTcpStream, TcpStream};
23
24#[cfg(feature = "tokio-runtime")]
25#[doc(hidden)]
26pub mod tokio {
27    use std::io;
28    use std::net::SocketAddr;
29    use tokio::net::TcpSocket as TokioTcpSocket;
30    use tokio::net::TcpStream as TokioTcpStream;
31
32    pub async fn connect(addr: &SocketAddr) -> Result<TokioTcpStream, io::Error> {
33        connect_with_bind(addr, &None).await
34    }
35
36    pub async fn connect_with_bind(
37        addr: &SocketAddr,
38        bind_addr: &Option<SocketAddr>,
39    ) -> Result<TokioTcpStream, io::Error> {
40        let stream = match bind_addr {
41            Some(bind_addr) => {
42                let socket = match bind_addr {
43                    SocketAddr::V4(_) => TokioTcpSocket::new_v4()?,
44                    SocketAddr::V6(_) => TokioTcpSocket::new_v6()?,
45                };
46                socket.bind(*bind_addr)?;
47                socket.connect(*addr).await?
48            }
49            None => TokioTcpStream::connect(addr).await?,
50        };
51        stream.set_nodelay(true)?;
52        Ok(stream)
53    }
54}