Skip to content

Commit

Permalink
ConnectByName() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
shuque committed May 27, 2020
1 parent 295bfe4 commit b4173c3
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
56 changes: 56 additions & 0 deletions byname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package dane

import (
"crypto/tls"
"fmt"
)

//
// ConnectByName takes a hostname and port, resolves the addresses for
// the hostname (IPv6 followed by IPv4), and then attempts to connect to
// them and establish TLS. It returns a TLS connection and dane config for
// the first address that succeeds.
//
// Uses a default DANE configuration. For a custom DANE configuration,
// use the DialTLS or DialStartTLS functions instead.
//
func ConnectByName(hostname string, port int) (*tls.Conn, *Config, error) {

var conn *tls.Conn

resolver, err := GetResolver("")
if err != nil {
return nil, nil, fmt.Errorf("Error obtaining resolver address: %s", err.Error())
}

tlsa, err := GetTLSA(resolver, hostname, port)
if err != nil {
return nil, nil, fmt.Errorf("GetTLSA: %s", err.Error())
}

needSecure := (tlsa != nil)
iplist, err := GetAddresses(resolver, hostname, needSecure)
if err != nil {
return nil, nil, fmt.Errorf("GetAddresses: %s", err.Error())
}

if len(iplist) == 0 {
return nil, nil, fmt.Errorf("No addresses found")
}

for _, ip := range iplist {

server := NewServer(hostname, ip, port)
config := NewConfig()
config.SetServer(server)
config.SetTLSA(tlsa)
conn, err = DialTLS(config)
if err != nil {
fmt.Printf("Connection failed to %s: %s\n", server.Address(), err.Error())
continue
}
return conn, config, err
}

return conn, nil, fmt.Errorf("Failed to connect to any server address")
}
31 changes: 31 additions & 0 deletions byname_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dane

/*
* Note: these test routines may not work unless you adapt this file
* to use validating DNS resolvers and appropriately configured DANE TLS
* servers you have access to.
*/

import (
"fmt"
"testing"
)

func TestConnectByName(t *testing.T) {

var hostname = "www.example.com"
var port = 443

conn, config, err := ConnectByName(hostname, port)
if err != nil {
t.Fatalf("%s\n", err.Error())
}
fmt.Printf("ConnectByName: Success connecting to %s %d\n", hostname, port)
if config.Okdane {
fmt.Printf("DANE OK\n")
} else if config.Okpkix {
fmt.Printf("PKIX OK\n")
}
fmt.Printf("\n")
conn.Close()
}

0 comments on commit b4173c3

Please sign in to comment.