github.com/braveheart12/insolar-09-08-19@v0.8.7/network/transport/transport.go (about) 1 /* 2 * The Clear BSD License 3 * 4 * Copyright (c) 2019 Insolar Technologies 5 * 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met: 9 * 10 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 11 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 12 * Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 13 * 14 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 15 * 16 */ 17 18 package transport 19 20 import ( 21 "context" 22 "net" 23 24 "github.com/insolar/insolar/configuration" 25 "github.com/insolar/insolar/network" 26 "github.com/insolar/insolar/network/transport/packet" 27 "github.com/insolar/insolar/network/transport/relay" 28 "github.com/insolar/insolar/network/transport/resolver" 29 30 "github.com/pkg/errors" 31 ) 32 33 // Transport is an interface for network transport. 34 type Transport interface { 35 // SendRequest sends packet to destination. Sequence number is generated automatically. 36 SendRequest(context.Context, *packet.Packet) (Future, error) 37 38 // SendResponse sends response packet for request with passed request id. 39 SendResponse(context.Context, network.RequestID, *packet.Packet) error 40 41 // SendPacket low-level send packet without requestId and without spawning a waiting future 42 SendPacket(ctx context.Context, p *packet.Packet) error 43 44 // Listen starts thread to listen incoming packets. 45 Listen(ctx context.Context) error 46 47 // Stop gracefully stops listening. 48 Stop() 49 50 // Close disposing all transport underlying structures after stopped are called. 51 Close() 52 53 // Packets returns channel to listen incoming packets. 54 Packets() <-chan *packet.Packet 55 56 // Stopped returns signal channel to support graceful shutdown. 57 Stopped() <-chan bool 58 59 // PublicAddress returns PublicAddress 60 PublicAddress() string 61 } 62 63 // NewTransport creates new Transport with particular configuration 64 func NewTransport(cfg configuration.Transport, proxy relay.Proxy) (Transport, error) { 65 switch cfg.Protocol { 66 case "TCP": 67 listener, err := net.Listen("tcp", cfg.Address) 68 if err != nil { 69 return nil, errors.Wrap(err, "failed to listen UDP") 70 } 71 publicAddress, err := Resolve(cfg, listener.Addr().String()) 72 if err != nil { 73 return nil, errors.Wrap(err, "failed to resolve public address") 74 } 75 return newTCPTransport(listener, proxy, publicAddress) 76 case "PURE_UDP": 77 conn, err := net.ListenPacket("udp", cfg.Address) 78 if err != nil { 79 return nil, errors.Wrap(err, "failed to listen TCP") 80 } 81 publicAddress, err := Resolve(cfg, conn.LocalAddr().String()) 82 if err != nil { 83 return nil, errors.Wrap(err, "failed to resolve public address") 84 } 85 return newUDPTransport(conn, proxy, publicAddress) 86 default: 87 return nil, errors.New("invalid transport configuration") 88 } 89 } 90 91 // Resolve resolves public address 92 func Resolve(cfg configuration.Transport, address string) (string, error) { 93 resolver, err := createResolver(cfg) 94 if err != nil { 95 return "", errors.Wrap(err, "[ Resolve ] Failed to create resolver") 96 } 97 publicAddress, err := resolver.Resolve(address) 98 if err != nil { 99 return "", errors.Wrap(err, "[ Resolve ] Failed to resolve public address") 100 } 101 return publicAddress, nil 102 } 103 104 func createResolver(cfg configuration.Transport) (resolver.PublicAddressResolver, error) { 105 if cfg.FixedPublicAddress != "" { 106 return resolver.NewFixedAddressResolver(cfg.FixedPublicAddress), nil 107 } 108 return resolver.NewExactResolver(), nil 109 }