From 1f08215cab519953234d32be3a69bd807f81699b Mon Sep 17 00:00:00 2001 From: Ivan Egorov Date: Tue, 29 Dec 2015 10:41:49 +0000 Subject: DialTLSDialer added && gofmt --- conn.go | 88 ++++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 51 insertions(+), 37 deletions(-) diff --git a/conn.go b/conn.go index 9f40317..253e58e 100644 --- a/conn.go +++ b/conn.go @@ -10,7 +10,8 @@ import ( "log" "net" "sync" - "time" + "time" + "github.com/nmcclain/asn1-ber" ) @@ -57,13 +58,13 @@ func Dial(network, addr string) (*Conn, error) { // DialTimeout connects to the given address on the given network using net.DialTimeout // and then returns a new Conn for the connection. Acts like Dial but takes a timeout. func DialTimeout(network, addr string, timeout time.Duration) (*Conn, error) { - c, err := net.DialTimeout(network, addr, timeout) - if err != nil { - return nil, NewError(ErrorNetwork, err) - } - conn := NewConn(c) - conn.start() - return conn, nil + c, err := net.DialTimeout(network, addr, timeout) + if err != nil { + return nil, NewError(ErrorNetwork, err) + } + conn := NewConn(c) + conn.start() + return conn, nil } // DialTLS connects to the given address on the given network using tls.Dial @@ -79,6 +80,19 @@ func DialTLS(network, addr string, config *tls.Config) (*Conn, error) { return conn, nil } +// DialTLSDialer connects to the given address on the given network using tls.DialWithDialer +// and then returns a new Conn for the connection. +func DialTLSDialer(network, addr string, config *tls.Config, dialer *net.Dialer) (*Conn, error) { + c, err := tls.DialWithDialer(dialer, network, addr, config) + if err != nil { + return nil, NewError(ErrorNetwork, err) + } + conn := NewConn(c) + conn.isTLS = true + conn.start() + return conn, nil +} + // NewConn returns a new Conn using conn for network I/O. func NewConn(conn net.Conn) *Conn { return &Conn{ @@ -291,36 +305,36 @@ func (l *Conn) reader() { } } + // Use Abandon operation to perform connection keepalives func (l *Conn) Ping() error { - messageID := l.nextMessageID() - - packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request") - packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, messageID, "MessageID")) - abandonRequest := ber.Encode(ber.ClassApplication, ber.TypePrimitive, ApplicationAbandonRequest, nil, "Abandon Request") - packet.AppendChild(abandonRequest) - - if l.Debug { - ber.PrintPacket(packet) - } - - channel, err := l.sendMessage(packet) - if err != nil { - return err - } - if channel == nil { - return NewError(ErrorNetwork, errors.New("ldap: could not send message")) - } - defer l.finishMessage(messageID) - - if l.Debug { - if err := addLDAPDescriptions(packet); err != nil { - return err - } - ber.PrintPacket(packet) - } - - return nil -} + messageID := l.nextMessageID() + packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request") + packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, messageID, "MessageID")) + abandonRequest := ber.Encode(ber.ClassApplication, ber.TypePrimitive, ApplicationAbandonRequest, nil, "Abandon Request") + packet.AppendChild(abandonRequest) + + if l.Debug { + ber.PrintPacket(packet) + } + + channel, err := l.sendMessage(packet) + if err != nil { + return err + } + if channel == nil { + return NewError(ErrorNetwork, errors.New("ldap: could not send message")) + } + defer l.finishMessage(messageID) + + if l.Debug { + if err := addLDAPDescriptions(packet); err != nil { + return err + } + ber.PrintPacket(packet) + } + + return nil +} -- cgit v1.2.3