2011-04-12 19:44:56 +00:00
package dns
2013-01-28 19:41:17 +00:00
// A client implementation.
2011-04-12 19:44:56 +00:00
import (
2014-01-04 08:30:08 +00:00
"bytes"
2017-06-30 11:44:44 +00:00
"context"
2016-01-04 12:05:29 +00:00
"crypto/tls"
2016-06-08 15:38:42 +00:00
"encoding/binary"
2011-04-15 21:55:27 +00:00
"io"
"net"
2012-01-20 11:13:47 +00:00
"time"
2011-04-12 19:44:56 +00:00
)
2015-01-15 22:11:11 +00:00
const dnsTimeout time . Duration = 2 * time . Second
2013-10-19 21:00:17 +00:00
const tcpIdleTimeout time . Duration = 8 * time . Second
2013-01-28 19:41:17 +00:00
2013-10-11 21:34:04 +00:00
// A Conn represents a connection to a DNS server.
2013-09-28 19:31:29 +00:00
type Conn struct {
2013-10-11 16:18:37 +00:00
net . Conn // a net.Conn holding the connection
2014-07-31 08:48:26 +00:00
UDPSize uint16 // minimum receive buffer for UDP messages
TsigSecret map [ string ] string // secret(s) for Tsig map[<zonename>]<base64 secret>, zonename must be fully qualified
2012-05-04 21:18:29 +00:00
rtt time . Duration
t time . Time
2013-10-11 16:18:37 +00:00
tsigRequestMAC string
2011-04-17 19:56:40 +00:00
}
2013-11-21 13:27:20 +00:00
// A Client defines parameters for a DNS client.
2011-04-12 19:44:56 +00:00
type Client struct {
2016-01-08 13:21:03 +00:00
Net string // if "tcp" or "tcp-tls" (DNS over TLS) a TCP query will be initiated, otherwise an UDP one (default is "" for UDP)
2014-07-31 08:48:26 +00:00
UDPSize uint16 // minimum receive buffer for UDP messages
2016-01-08 13:21:03 +00:00
TLSConfig * tls . Config // TLS connection configuration
2016-04-19 10:29:51 +00:00
Timeout time . Duration // a cumulative timeout for dial, write and read, defaults to 0 (disabled) - overrides DialTimeout, ReadTimeout and WriteTimeout when non-zero
DialTimeout time . Duration // net.DialTimeout, defaults to 2 seconds - overridden by Timeout when that value is non-zero
ReadTimeout time . Duration // net.Conn.SetReadTimeout value for connections, defaults to 2 seconds - overridden by Timeout when that value is non-zero
WriteTimeout time . Duration // net.Conn.SetWriteTimeout value for connections, defaults to 2 seconds - overridden by Timeout when that value is non-zero
2013-09-06 09:49:07 +00:00
TsigSecret map [ string ] string // secret(s) for Tsig map[<zonename>]<base64 secret>, zonename must be fully qualified
SingleInflight bool // if true suppress multiple outstanding queries for the same Qname, Qtype and Qclass
group singleflight
2013-08-23 22:56:34 +00:00
}
2013-09-29 09:22:27 +00:00
// Exchange performs a synchronous UDP query. It sends the message m to the address
2016-10-03 14:10:00 +00:00
// contained in a and waits for a reply. Exchange does not retry a failed query, nor
2013-12-30 16:46:53 +00:00
// will it fall back to TCP in case of truncation.
2016-03-02 09:40:40 +00:00
// See client.Exchange for more information on setting larger buffer sizes.
2013-09-28 20:58:08 +00:00
func Exchange ( m * Msg , a string ) ( r * Msg , err error ) {
2015-05-06 06:20:46 +00:00
var co * Conn
co , err = DialTimeout ( "udp" , a , dnsTimeout )
if err != nil {
return nil , err
}
defer co . Close ( )
opt := m . IsEdns0 ( )
// If EDNS0 is used use that for size.
if opt != nil && opt . UDPSize ( ) >= MinMsgSize {
co . UDPSize = opt . UDPSize ( )
}
2015-10-06 11:30:01 +00:00
co . SetWriteDeadline ( time . Now ( ) . Add ( dnsTimeout ) )
2015-05-06 06:20:46 +00:00
if err = co . WriteMsg ( m ) ; err != nil {
return nil , err
}
2015-10-06 11:30:01 +00:00
co . SetReadDeadline ( time . Now ( ) . Add ( dnsTimeout ) )
2015-05-06 06:20:46 +00:00
r , err = co . ReadMsg ( )
if err == nil && r . Id != m . Id {
err = ErrId
}
return r , err
2013-09-28 19:31:29 +00:00
}
2017-06-30 11:44:44 +00:00
// ExchangeContext performs a synchronous UDP query, like Exchange. It
// additionally obeys deadlines from the passed Context.
func ExchangeContext ( ctx context . Context , m * Msg , a string ) ( r * Msg , err error ) {
// Combine context deadline with built-in timeout. Context chooses whichever
// is sooner.
timeoutCtx , cancel := context . WithTimeout ( ctx , dnsTimeout )
defer cancel ( )
deadline , _ := timeoutCtx . Deadline ( )
co := new ( Conn )
dialer := net . Dialer { }
co . Conn , err = dialer . DialContext ( timeoutCtx , "udp" , a )
if err != nil {
return nil , err
}
defer co . Conn . Close ( )
opt := m . IsEdns0 ( )
// If EDNS0 is used use that for size.
if opt != nil && opt . UDPSize ( ) >= MinMsgSize {
co . UDPSize = opt . UDPSize ( )
}
co . SetWriteDeadline ( deadline )
if err = co . WriteMsg ( m ) ; err != nil {
return nil , err
}
co . SetReadDeadline ( deadline )
r , err = co . ReadMsg ( )
if err == nil && r . Id != m . Id {
err = ErrId
}
return r , err
}
2013-10-12 11:44:02 +00:00
// ExchangeConn performs a synchronous query. It sends the message m via the connection
// c and waits for a reply. The connection c is not closed by ExchangeConn.
// This function is going away, but can easily be mimicked:
//
2013-10-18 09:14:31 +00:00
// co := &dns.Conn{Conn: c} // c is your net.Conn
2013-10-12 11:44:02 +00:00
// co.WriteMsg(m)
// in, _ := co.ReadMsg()
2013-10-18 09:14:31 +00:00
// co.Close()
2013-10-12 11:44:02 +00:00
//
2013-10-12 11:35:09 +00:00
func ExchangeConn ( c net . Conn , m * Msg ) ( r * Msg , err error ) {
2013-10-12 11:44:02 +00:00
println ( "dns: this function is deprecated" )
co := new ( Conn )
co . Conn = c
2013-10-12 11:35:09 +00:00
if err = co . WriteMsg ( m ) ; err != nil {
return nil , err
}
r , err = co . ReadMsg ( )
2015-05-06 05:56:42 +00:00
if err == nil && r . Id != m . Id {
err = ErrId
}
2013-10-12 11:35:09 +00:00
return r , err
}
2016-10-03 14:10:00 +00:00
// Exchange performs a synchronous query. It sends the message m to the address
// contained in a and waits for a reply. Basic use pattern with a *dns.Client:
2013-09-28 20:58:08 +00:00
//
// c := new(dns.Client)
// in, rtt, err := c.Exchange(message, "127.0.0.1:53")
//
2014-01-04 08:30:08 +00:00
// Exchange does not retry a failed query, nor will it fall back to TCP in
2013-12-27 07:48:30 +00:00
// case of truncation.
2016-03-02 09:40:40 +00:00
// It is up to the caller to create a message that allows for larger responses to be
// returned. Specifically this means adding an EDNS0 OPT RR that will advertise a larger
2017-03-19 15:10:46 +00:00
// buffer, see SetEdns0. Messages without an OPT RR will fallback to the historic limit
2016-03-02 09:40:40 +00:00
// of 512 bytes.
2013-09-28 20:58:08 +00:00
func ( c * Client ) Exchange ( m * Msg , a string ) ( r * Msg , rtt time . Duration , err error ) {
2017-06-30 11:44:44 +00:00
return c . ExchangeContext ( context . Background ( ) , m , a )
}
// ExchangeContext acts like Exchange, but honors the deadline on the provided
// context, if present. If there is both a context deadline and a configured
// timeout on the client, the earliest of the two takes effect.
func ( c * Client ) ExchangeContext ( ctx context . Context , m * Msg , a string ) (
r * Msg ,
rtt time . Duration ,
err error ) {
2013-09-06 09:49:07 +00:00
if ! c . SingleInflight {
2017-06-30 11:44:44 +00:00
return c . exchange ( ctx , m , a )
2013-08-23 22:56:34 +00:00
}
// This adds a bunch of garbage, TODO(miek).
t := "nop"
2013-08-24 02:28:58 +00:00
if t1 , ok := TypeToString [ m . Question [ 0 ] . Qtype ] ; ok {
2013-08-23 22:56:34 +00:00
t = t1
}
cl := "nop"
2013-08-24 02:28:58 +00:00
if cl1 , ok := ClassToString [ m . Question [ 0 ] . Qclass ] ; ok {
2013-08-23 22:56:34 +00:00
cl = cl1
}
2013-08-24 02:28:58 +00:00
r , rtt , err , shared := c . group . Do ( m . Question [ 0 ] . Name + t + cl , func ( ) ( * Msg , time . Duration , error ) {
2017-06-30 11:44:44 +00:00
return c . exchange ( ctx , m , a )
2013-08-23 22:56:34 +00:00
} )
2017-02-17 11:38:00 +00:00
if r != nil && shared {
r = r . Copy ( )
}
2013-08-23 22:56:34 +00:00
if err != nil {
return r , rtt , err
}
return r , rtt , nil
2011-04-17 19:56:40 +00:00
}
2015-05-06 06:23:53 +00:00
func ( c * Client ) dialTimeout ( ) time . Duration {
2016-04-19 10:29:51 +00:00
if c . Timeout != 0 {
return c . Timeout
}
2013-09-29 10:21:18 +00:00
if c . DialTimeout != 0 {
2015-05-06 06:23:53 +00:00
return c . DialTimeout
}
return dnsTimeout
}
func ( c * Client ) readTimeout ( ) time . Duration {
if c . ReadTimeout != 0 {
return c . ReadTimeout
2011-08-04 09:27:56 +00:00
}
2015-05-06 06:23:53 +00:00
return dnsTimeout
}
func ( c * Client ) writeTimeout ( ) time . Duration {
if c . WriteTimeout != 0 {
return c . WriteTimeout
}
return dnsTimeout
}
2017-06-30 11:44:44 +00:00
func ( c * Client ) exchange ( ctx context . Context , m * Msg , a string ) ( r * Msg , rtt time . Duration , err error ) {
2015-05-06 06:23:53 +00:00
var co * Conn
2016-01-04 12:05:29 +00:00
network := "udp"
2016-01-08 13:21:03 +00:00
tls := false
switch c . Net {
case "tcp-tls" :
network = "tcp"
tls = true
case "tcp4-tls" :
network = "tcp4"
tls = true
case "tcp6-tls" :
network = "tcp6"
tls = true
default :
if c . Net != "" {
network = c . Net
}
}
2016-04-19 10:29:51 +00:00
var deadline time . Time
if c . Timeout != 0 {
deadline = time . Now ( ) . Add ( c . Timeout )
}
2017-06-30 11:44:44 +00:00
dialDeadline := deadlineOrTimeoutOrCtx ( ctx , deadline , c . dialTimeout ( ) )
dialTimeout := dialDeadline . Sub ( time . Now ( ) )
2016-01-08 13:21:03 +00:00
if tls {
2017-06-30 11:44:44 +00:00
co , err = DialTimeoutWithTLS ( network , a , c . TLSConfig , dialTimeout )
2013-09-28 20:58:08 +00:00
} else {
2017-06-30 11:44:44 +00:00
co , err = DialTimeout ( network , a , dialTimeout )
2011-08-08 11:10:35 +00:00
}
2016-01-04 12:05:29 +00:00
2016-01-07 15:36:36 +00:00
if err != nil {
2013-01-28 19:30:13 +00:00
return nil , 0 , err
}
2013-09-28 20:58:08 +00:00
defer co . Close ( )
2015-05-06 06:23:53 +00:00
2013-09-29 10:21:18 +00:00
opt := m . IsEdns0 ( )
2014-07-31 08:48:26 +00:00
// If EDNS0 is used use that for size.
2013-09-29 10:21:18 +00:00
if opt != nil && opt . UDPSize ( ) >= MinMsgSize {
co . UDPSize = opt . UDPSize ( )
}
2014-07-31 08:48:26 +00:00
// Otherwise use the client's configured UDP size.
2014-02-14 22:14:41 +00:00
if opt == nil && c . UDPSize >= MinMsgSize {
co . UDPSize = c . UDPSize
}
2015-05-06 06:23:53 +00:00
2013-09-29 10:21:18 +00:00
co . TsigSecret = c . TsigSecret
2017-06-30 11:44:44 +00:00
co . SetWriteDeadline ( deadlineOrTimeoutOrCtx ( ctx , deadline , c . writeTimeout ( ) ) )
2013-09-29 10:21:18 +00:00
if err = co . WriteMsg ( m ) ; err != nil {
2013-01-28 19:30:13 +00:00
return nil , 0 , err
}
2015-10-06 11:30:01 +00:00
2017-06-30 11:44:44 +00:00
co . SetReadDeadline ( deadlineOrTimeoutOrCtx ( ctx , deadline , c . readTimeout ( ) ) )
2013-09-29 10:21:18 +00:00
r , err = co . ReadMsg ( )
2015-05-06 05:56:42 +00:00
if err == nil && r . Id != m . Id {
err = ErrId
}
2013-09-28 20:58:08 +00:00
return r , co . rtt , err
2011-08-08 11:10:35 +00:00
}
2013-09-29 10:21:18 +00:00
// ReadMsg reads a message from the connection co.
2013-10-11 21:34:04 +00:00
// If the received message contains a TSIG record the transaction
// signature is verified.
2013-09-29 10:21:18 +00:00
func ( co * Conn ) ReadMsg ( ) ( * Msg , error ) {
2015-07-03 08:31:53 +00:00
p , err := co . ReadMsgHeader ( nil )
2015-06-24 19:09:46 +00:00
if err != nil {
2011-04-18 20:08:12 +00:00
return nil , err
}
2015-06-24 19:09:46 +00:00
2015-06-29 00:20:16 +00:00
m := new ( Msg )
2012-10-09 19:17:54 +00:00
if err := m . Unpack ( p ) ; err != nil {
2015-11-01 20:46:57 +00:00
// If ErrTruncated was returned, we still want to allow the user to use
// the message, but naively they can just check err if they don't want
// to use a truncated message
if err == ErrTruncated {
return m , err
}
2012-10-09 19:17:54 +00:00
return nil , err
2011-04-18 20:08:12 +00:00
}
2012-08-25 09:24:01 +00:00
if t := m . IsTsig ( ) ; t != nil {
2013-09-29 10:21:18 +00:00
if _ , ok := co . TsigSecret [ t . Hdr . Name ] ; ! ok {
2012-10-16 07:42:38 +00:00
return m , ErrSecret
2011-04-22 14:37:26 +00:00
}
2012-02-25 20:42:08 +00:00
// Need to work on the original message p, as that was used to calculate the tsig.
2013-10-11 16:18:37 +00:00
err = TsigVerify ( p , co . TsigSecret [ t . Hdr . Name ] , co . tsigRequestMAC , false )
2011-04-22 14:37:26 +00:00
}
2013-09-29 09:22:27 +00:00
return m , err
2011-04-17 19:56:40 +00:00
}
2011-04-15 21:55:27 +00:00
2015-07-03 08:31:53 +00:00
// ReadMsgHeader reads a DNS message, parses and populates hdr (when hdr is not nil).
// Returns message as a byte slice to be parsed with Msg.Unpack later on.
2015-07-03 02:39:49 +00:00
// Note that error handling on the message body is not possible as only the header is parsed.
2015-07-03 08:31:53 +00:00
func ( co * Conn ) ReadMsgHeader ( hdr * Header ) ( [ ] byte , error ) {
2015-06-29 12:06:49 +00:00
var (
p [ ] byte
n int
err error
)
2015-06-29 00:20:16 +00:00
2016-01-07 15:27:07 +00:00
switch t := co . Conn . ( type ) {
case * net . TCPConn , * tls . Conn :
r := t . ( io . Reader )
2015-07-03 02:39:49 +00:00
// First two bytes specify the length of the entire message.
2016-01-07 15:27:07 +00:00
l , err := tcpMsgLen ( r )
2015-06-29 12:06:49 +00:00
if err != nil {
return nil , err
}
p = make ( [ ] byte , l )
2016-01-07 15:27:07 +00:00
n , err = tcpRead ( r , p )
2016-02-28 16:36:19 +00:00
co . rtt = time . Since ( co . t )
2016-01-07 15:27:07 +00:00
default :
2015-06-24 19:09:46 +00:00
if co . UDPSize > MinMsgSize {
p = make ( [ ] byte , co . UDPSize )
} else {
p = make ( [ ] byte , MinMsgSize )
}
2015-06-29 12:06:49 +00:00
n , err = co . Read ( p )
2016-02-28 16:36:19 +00:00
co . rtt = time . Since ( co . t )
2015-06-24 19:09:46 +00:00
}
if err != nil {
return nil , err
2015-07-03 08:31:53 +00:00
} else if n < headerSize {
2015-06-24 19:09:46 +00:00
return nil , ErrShortRead
}
p = p [ : n ]
if hdr != nil {
2016-06-12 20:06:46 +00:00
dh , _ , err := unpackMsgHdr ( p , 0 )
if err != nil {
2015-06-24 19:09:46 +00:00
return nil , err
}
2016-06-12 20:06:46 +00:00
* hdr = dh
2015-06-24 19:09:46 +00:00
}
return p , err
}
2015-07-03 02:39:49 +00:00
// tcpMsgLen is a helper func to read first two bytes of stream as uint16 packet length.
2016-01-07 15:27:07 +00:00
func tcpMsgLen ( t io . Reader ) ( int , error ) {
2015-07-03 02:39:49 +00:00
p := [ ] byte { 0 , 0 }
n , err := t . Read ( p )
2015-06-29 12:06:49 +00:00
if err != nil {
return 0 , err
2015-07-03 02:39:49 +00:00
}
2017-02-07 22:33:10 +00:00
// As seen with my local router/switch, retursn 1 byte on the above read,
// resulting a a ShortRead. Just write it out (instead of loop) and read the
// other byte.
if n == 1 {
n1 , err := t . Read ( p [ 1 : ] )
if err != nil {
return 0 , err
}
n += n1
}
2015-07-03 02:39:49 +00:00
if n != 2 {
2015-06-29 12:06:49 +00:00
return 0 , ErrShortRead
}
2016-06-08 15:38:42 +00:00
l := binary . BigEndian . Uint16 ( p )
2015-06-29 12:06:49 +00:00
if l == 0 {
return 0 , ErrShortRead
}
return int ( l ) , nil
}
2015-07-03 02:39:49 +00:00
// tcpRead calls TCPConn.Read enough times to fill allocated buffer.
2016-01-07 15:27:07 +00:00
func tcpRead ( t io . Reader , p [ ] byte ) ( int , error ) {
2015-06-29 12:06:49 +00:00
n , err := t . Read ( p )
if err != nil {
return n , err
}
for n < len ( p ) {
j , err := t . Read ( p [ n : ] )
if err != nil {
return n , err
}
n += j
}
return n , err
}
2013-09-29 10:21:18 +00:00
// Read implements the net.Conn read method.
2013-09-28 20:58:08 +00:00
func ( co * Conn ) Read ( p [ ] byte ) ( n int , err error ) {
if co . Conn == nil {
2011-11-02 22:06:54 +00:00
return 0 , ErrConnEmpty
2011-04-17 19:56:40 +00:00
}
2012-08-28 19:36:25 +00:00
if len ( p ) < 2 {
2012-05-26 08:24:47 +00:00
return 0 , io . ErrShortBuffer
}
2016-01-07 15:27:07 +00:00
switch t := co . Conn . ( type ) {
case * net . TCPConn , * tls . Conn :
r := t . ( io . Reader )
l , err := tcpMsgLen ( r )
2013-01-11 18:20:49 +00:00
if err != nil {
2015-06-29 12:06:49 +00:00
return 0 , err
2013-01-11 18:20:49 +00:00
}
2015-06-29 12:06:49 +00:00
if l > len ( p ) {
return int ( l ) , io . ErrShortBuffer
2011-04-18 20:08:12 +00:00
}
2016-01-07 15:27:07 +00:00
return tcpRead ( r , p [ : l ] )
2013-10-12 17:32:53 +00:00
}
2014-02-14 22:47:53 +00:00
// UDP connection
2013-10-12 17:47:11 +00:00
n , err = co . Conn . Read ( p )
2013-10-12 17:32:53 +00:00
if err != nil {
return n , err
2011-04-17 08:54:34 +00:00
}
2013-01-11 18:20:49 +00:00
return n , err
2011-04-15 21:55:27 +00:00
}
2016-01-19 22:24:18 +00:00
// WriteMsg sends a message through the connection co.
2011-04-18 20:08:12 +00:00
// If the message m contains a TSIG record the transaction
// signature is calculated.
2013-09-29 10:21:18 +00:00
func ( co * Conn ) WriteMsg ( m * Msg ) ( err error ) {
2012-03-02 20:19:37 +00:00
var out [ ] byte
2012-08-25 09:24:01 +00:00
if t := m . IsTsig ( ) ; t != nil {
2012-03-02 20:19:37 +00:00
mac := ""
2013-09-29 10:21:18 +00:00
if _ , ok := co . TsigSecret [ t . Hdr . Name ] ; ! ok {
2011-09-10 14:50:27 +00:00
return ErrSecret
2011-04-22 14:37:26 +00:00
}
2013-10-11 16:18:37 +00:00
out , mac , err = TsigGenerate ( m , co . TsigSecret [ t . Hdr . Name ] , co . tsigRequestMAC , false )
2016-01-19 22:23:11 +00:00
// Set for the next read, although only used in zone transfers
2013-10-11 16:18:37 +00:00
co . tsigRequestMAC = mac
2012-03-02 20:19:37 +00:00
} else {
2012-10-09 19:17:54 +00:00
out , err = m . Pack ( )
2012-10-15 18:00:49 +00:00
}
if err != nil {
return err
2011-04-15 21:55:27 +00:00
}
2013-09-28 20:58:08 +00:00
co . t = time . Now ( )
if _ , err = co . Write ( out ) ; err != nil {
2012-03-02 20:19:37 +00:00
return err
}
2011-04-15 21:55:27 +00:00
return nil
}
2013-09-29 10:21:18 +00:00
// Write implements the net.Conn Write method.
2013-09-28 20:58:08 +00:00
func ( co * Conn ) Write ( p [ ] byte ) ( n int , err error ) {
2016-01-07 15:27:07 +00:00
switch t := co . Conn . ( type ) {
case * net . TCPConn , * tls . Conn :
w := t . ( io . Writer )
2014-01-04 11:17:13 +00:00
lp := len ( p )
if lp < 2 {
2011-04-15 21:55:27 +00:00
return 0 , io . ErrShortBuffer
}
2014-01-04 08:30:08 +00:00
if lp > MaxMsgSize {
return 0 , & Error { err : "message too large" }
2011-04-15 21:55:27 +00:00
}
2014-01-04 08:30:08 +00:00
l := make ( [ ] byte , 2 , lp + 2 )
2016-06-08 15:38:42 +00:00
binary . BigEndian . PutUint16 ( l , uint16 ( lp ) )
2014-01-04 08:30:08 +00:00
p = append ( l , p ... )
2016-01-07 15:27:07 +00:00
n , err := io . Copy ( w , bytes . NewReader ( p ) )
2014-01-04 08:30:08 +00:00
return int ( n ) , err
2011-04-15 21:55:27 +00:00
}
2017-02-11 08:53:59 +00:00
n , err = co . Conn . Write ( p )
2013-09-28 20:58:08 +00:00
return n , err
2011-04-15 21:55:27 +00:00
}
2012-05-04 21:18:29 +00:00
2013-11-01 09:40:44 +00:00
// Dial connects to the address on the named network.
func Dial ( network , address string ) ( conn * Conn , err error ) {
2013-11-01 09:50:55 +00:00
conn = new ( Conn )
2013-11-01 09:40:44 +00:00
conn . Conn , err = net . Dial ( network , address )
if err != nil {
return nil , err
}
return conn , nil
}
2015-02-19 09:58:33 +00:00
// DialTimeout acts like Dial but takes a timeout.
2013-11-01 09:40:44 +00:00
func DialTimeout ( network , address string , timeout time . Duration ) ( conn * Conn , err error ) {
2013-11-01 09:50:55 +00:00
conn = new ( Conn )
2013-11-01 09:40:44 +00:00
conn . Conn , err = net . DialTimeout ( network , address , timeout )
if err != nil {
return nil , err
}
return conn , nil
}
2016-01-04 12:05:29 +00:00
// DialWithTLS connects to the address on the named network with TLS.
2016-01-04 12:30:39 +00:00
func DialWithTLS ( network , address string , tlsConfig * tls . Config ) ( conn * Conn , err error ) {
2016-01-04 12:05:29 +00:00
conn = new ( Conn )
2016-01-04 12:30:39 +00:00
conn . Conn , err = tls . Dial ( network , address , tlsConfig )
2016-01-04 12:05:29 +00:00
if err != nil {
return nil , err
}
return conn , nil
}
// DialTimeoutWithTLS acts like DialWithTLS but takes a timeout.
2016-01-04 12:30:39 +00:00
func DialTimeoutWithTLS ( network , address string , tlsConfig * tls . Config , timeout time . Duration ) ( conn * Conn , err error ) {
2016-01-04 12:05:29 +00:00
var dialer net . Dialer
dialer . Timeout = timeout
conn = new ( Conn )
2016-01-04 12:30:39 +00:00
conn . Conn , err = tls . DialWithDialer ( & dialer , network , address , tlsConfig )
2016-01-04 12:05:29 +00:00
if err != nil {
return nil , err
}
return conn , nil
}
2016-04-19 10:29:51 +00:00
2017-06-30 11:44:44 +00:00
// deadlineOrTimeout chooses between the provided deadline and timeout
// by always preferring the deadline so long as it's non-zero (regardless
// of which is bigger), and returns the equivalent deadline value.
2016-04-19 10:29:51 +00:00
func deadlineOrTimeout ( deadline time . Time , timeout time . Duration ) time . Time {
if deadline . IsZero ( ) {
return time . Now ( ) . Add ( timeout )
}
return deadline
}
2017-06-30 11:44:44 +00:00
// deadlineOrTimeoutOrCtx returns the earliest of: a context deadline, or the
// output of deadlineOrtimeout.
func deadlineOrTimeoutOrCtx ( ctx context . Context , deadline time . Time , timeout time . Duration ) time . Time {
result := deadlineOrTimeout ( deadline , timeout )
if ctxDeadline , ok := ctx . Deadline ( ) ; ok && ctxDeadline . Before ( result ) {
result = ctxDeadline
}
return result
}