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 * http://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 */
1617//! TCP protocol related components for DNS
18mod tcp_client_stream;
19mod tcp_stream;
2021pub use self::tcp_client_stream::{TcpClientConnect, TcpClientStream};
22pub use self::tcp_stream::{Connect, DnsTcpStream, TcpStream};
2324#[cfg(feature = "tokio-runtime")]
25#[doc(hidden)]
26pub mod tokio {
27use std::io;
28use std::net::SocketAddr;
29use tokio::net::TcpSocket as TokioTcpSocket;
30use tokio::net::TcpStream as TokioTcpStream;
3132pub async fn connect(addr: &SocketAddr) -> Result<TokioTcpStream, io::Error> {
33 connect_with_bind(addr, &None).await
34}
3536pub async fn connect_with_bind(
37 addr: &SocketAddr,
38 bind_addr: &Option<SocketAddr>,
39 ) -> Result<TokioTcpStream, io::Error> {
40let stream = match bind_addr {
41Some(bind_addr) => {
42let 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}
49None => TokioTcpStream::connect(addr).await?,
50 };
51 stream.set_nodelay(true)?;
52Ok(stream)
53 }
54}