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