github.com/openziti/transport@v0.1.5/tcp/dialer.go (about) 1 /* 2 Copyright NetFoundry, Inc. 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 package tcp 18 19 import ( 20 "github.com/openziti/transport" 21 "net" 22 "time" 23 ) 24 25 func Dial(destination, name string, timeout time.Duration) (transport.Connection, error) { 26 socket, err := net.DialTimeout("tcp", destination, timeout) 27 if err != nil { 28 return nil, err 29 } 30 31 return &Connection{ 32 detail: &transport.ConnectionDetail{ 33 Address: "tcp:" + destination, 34 InBound: false, 35 Name: name, 36 }, 37 socket: socket, 38 }, nil 39 } 40 41 func DialWithLocalBinding(destination, name, localBinding string, timeout time.Duration) (transport.Connection, error) { 42 43 dialer, err := transport.NewDialerWithLocalBinding("tcp", timeout, localBinding) 44 45 if err != nil { 46 return nil, err 47 } 48 49 socket, err := dialer.Dial("tcp", destination) 50 51 if err != nil { 52 return nil, err 53 } 54 55 return &Connection{ 56 detail: &transport.ConnectionDetail{ 57 Address: "tcp:" + destination, 58 InBound: false, 59 Name: name, 60 }, 61 socket: socket, 62 }, nil 63 }