github.com/openziti/transport@v0.1.5/udp/address.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  	"errors"
    21  	"fmt"
    22  	"github.com/openziti/foundation/identity/identity"
    23  	"github.com/openziti/transport"
    24  	"io"
    25  	"net"
    26  	"strconv"
    27  	"strings"
    28  	"time"
    29  )
    30  
    31  var _ transport.Address = (*address)(nil) // enforce that address implements transport.Address
    32  
    33  type address struct {
    34  	hostname string
    35  	port     uint16
    36  }
    37  
    38  func (a address) Dial(name string, i *identity.TokenId, timeout time.Duration, _ transport.Configuration) (transport.Connection, error) {
    39  	addr, err := a.bindableAddress()
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return Dial(addr, name, i, timeout)
    44  }
    45  
    46  func (a address) DialWithLocalBinding(name string, localBinding string, _ *identity.TokenId, timeout time.Duration, _ transport.Configuration) (transport.Connection, error) {
    47  	addr, err := a.bindableAddress()
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return DialWithLocalBinding(addr, name, localBinding, timeout)
    52  }
    53  
    54  func (a address) Listen(name string, i *identity.TokenId, incoming chan transport.Connection, _ transport.Configuration) (io.Closer, error) {
    55  	addr, err := a.bindableAddress()
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	return Listen(addr, name, i, incoming)
    60  }
    61  
    62  func (a address) MustListen(name string, i *identity.TokenId, incoming chan transport.Connection, tcfg transport.Configuration) io.Closer {
    63  	closer, err := a.Listen(name, i, incoming, tcfg)
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  	return closer
    68  }
    69  
    70  func (a address) String() string {
    71  	return fmt.Sprintf("udp:%v:%v", a.hostname, a.port)
    72  }
    73  
    74  func (a address) bindableAddress() (*net.UDPAddr, error) {
    75  	return net.ResolveUDPAddr("udp", fmt.Sprintf("%v:%v", a.hostname, a.port))
    76  }
    77  
    78  func (a address) Type() string {
    79  	return "udp"
    80  }
    81  
    82  type AddressParser struct{}
    83  
    84  func (ap AddressParser) Parse(s string) (transport.Address, error) {
    85  	tokens := strings.Split(s, ":")
    86  	if len(tokens) < 2 {
    87  		return nil, errors.New("invalid format")
    88  	}
    89  
    90  	if tokens[0] == "udp" {
    91  		if len(tokens) != 3 {
    92  			return nil, errors.New("invalid format")
    93  		}
    94  
    95  		port, err := strconv.ParseUint(tokens[2], 10, 16)
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  
   100  		return &address{hostname: tokens[1], port: uint16(port)}, nil
   101  	}
   102  
   103  	return nil, errors.New("invalid format")
   104  
   105  }