2020-09-03 23:45:41 +01:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-01-11 17:56:03 +00:00
|
|
|
// netstack doesn't build on 32-bit machines (https://github.com/google/gvisor/issues/5241)
|
|
|
|
// +build amd64 arm64 ppc64le riscv64 s390x
|
|
|
|
|
2020-09-03 23:45:41 +01:00
|
|
|
// Package netstack wires up gVisor's netstack into Tailscale.
|
|
|
|
package netstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-02-25 19:18:16 +00:00
|
|
|
"io"
|
2020-09-03 23:45:41 +01:00
|
|
|
"log"
|
2021-02-26 21:39:48 +00:00
|
|
|
"net"
|
2021-02-26 22:06:00 +00:00
|
|
|
"strconv"
|
2020-09-03 23:45:41 +01:00
|
|
|
"strings"
|
2021-02-26 22:06:00 +00:00
|
|
|
"sync"
|
2020-09-03 23:45:41 +01:00
|
|
|
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/buffer"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/header"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/link/channel"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
|
2021-02-25 19:18:16 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
|
2020-09-03 23:45:41 +01:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/transport/icmp"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
|
|
|
|
"gvisor.dev/gvisor/pkg/waiter"
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/net/packet"
|
|
|
|
"tailscale.com/types/logger"
|
2021-02-05 23:44:46 +00:00
|
|
|
"tailscale.com/types/netmap"
|
2021-02-26 22:06:00 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2020-09-03 23:45:41 +01:00
|
|
|
"tailscale.com/wgengine"
|
|
|
|
"tailscale.com/wgengine/filter"
|
|
|
|
"tailscale.com/wgengine/magicsock"
|
|
|
|
"tailscale.com/wgengine/tstun"
|
|
|
|
)
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
// Impl contains the state for the netstack implementation,
|
|
|
|
// and implements wgengine.FakeImpl to act as a userspace network
|
|
|
|
// stack when Tailscale is running in fake mode.
|
|
|
|
type Impl struct {
|
|
|
|
ipstack *stack.Stack
|
|
|
|
linkEP *channel.Endpoint
|
|
|
|
tundev *tstun.TUN
|
|
|
|
e wgengine.Engine
|
|
|
|
mc *magicsock.Conn
|
|
|
|
logf logger.Logf
|
2021-02-26 22:06:00 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
2021-03-01 18:08:53 +00:00
|
|
|
dns DNSMap
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const nicID = 1
|
|
|
|
|
|
|
|
// Create creates and populates a new Impl.
|
2021-03-01 18:08:53 +00:00
|
|
|
func Create(logf logger.Logf, tundev *tstun.TUN, e wgengine.Engine, mc *magicsock.Conn) (*Impl, error) {
|
2020-09-03 23:45:41 +01:00
|
|
|
if mc == nil {
|
2021-02-25 19:18:16 +00:00
|
|
|
return nil, errors.New("nil magicsock.Conn")
|
2020-09-03 23:45:41 +01:00
|
|
|
}
|
|
|
|
if tundev == nil {
|
2021-02-25 19:18:16 +00:00
|
|
|
return nil, errors.New("nil tundev")
|
2020-09-03 23:45:41 +01:00
|
|
|
}
|
|
|
|
if logf == nil {
|
2021-02-25 19:18:16 +00:00
|
|
|
return nil, errors.New("nil logger")
|
2020-09-03 23:45:41 +01:00
|
|
|
}
|
|
|
|
if e == nil {
|
2021-02-25 19:18:16 +00:00
|
|
|
return nil, errors.New("nil Engine")
|
2020-09-03 23:45:41 +01:00
|
|
|
}
|
|
|
|
ipstack := stack.New(stack.Options{
|
2021-02-25 19:18:16 +00:00
|
|
|
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
|
|
|
|
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol, icmp.NewProtocol4, icmp.NewProtocol6},
|
2020-09-03 23:45:41 +01:00
|
|
|
})
|
|
|
|
const mtu = 1500
|
|
|
|
linkEP := channel.New(512, mtu, "")
|
2021-02-25 19:18:16 +00:00
|
|
|
if tcpipProblem := ipstack.CreateNIC(nicID, linkEP); tcpipProblem != nil {
|
|
|
|
return nil, fmt.Errorf("could not create netstack NIC: %v", tcpipProblem)
|
|
|
|
}
|
|
|
|
// Add IPv4 and IPv6 default routes, so all incoming packets from the Tailscale side
|
|
|
|
// are handled by the one fake NIC we use.
|
|
|
|
ipv4Subnet, _ := tcpip.NewSubnet(tcpip.Address(strings.Repeat("\x00", 4)), tcpip.AddressMask(strings.Repeat("\x00", 4)))
|
|
|
|
ipv6Subnet, _ := tcpip.NewSubnet(tcpip.Address(strings.Repeat("\x00", 16)), tcpip.AddressMask(strings.Repeat("\x00", 16)))
|
|
|
|
ipstack.SetRouteTable([]tcpip.Route{
|
|
|
|
{
|
|
|
|
Destination: ipv4Subnet,
|
|
|
|
NIC: nicID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: ipv6Subnet,
|
|
|
|
NIC: nicID,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
ns := &Impl{
|
|
|
|
logf: logf,
|
|
|
|
ipstack: ipstack,
|
|
|
|
linkEP: linkEP,
|
|
|
|
tundev: tundev,
|
|
|
|
e: e,
|
|
|
|
mc: mc,
|
|
|
|
}
|
|
|
|
return ns, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start sets up all the handlers so netstack can start working. Implements
|
|
|
|
// wgengine.FakeImpl.
|
|
|
|
func (ns *Impl) Start() error {
|
|
|
|
ns.e.AddNetworkMapCallback(ns.updateIPs)
|
|
|
|
// size = 0 means use default buffer size
|
|
|
|
const tcpReceiveBufferSize = 0
|
|
|
|
const maxInFlightConnectionAttempts = 16
|
|
|
|
tcpFwd := tcp.NewForwarder(ns.ipstack, tcpReceiveBufferSize, maxInFlightConnectionAttempts, ns.acceptTCP)
|
|
|
|
udpFwd := udp.NewForwarder(ns.ipstack, ns.acceptUDP)
|
|
|
|
ns.ipstack.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpFwd.HandlePacket)
|
|
|
|
ns.ipstack.SetTransportProtocolHandler(udp.ProtocolNumber, udpFwd.HandlePacket)
|
|
|
|
go ns.injectOutbound()
|
|
|
|
ns.tundev.PostFilterIn = ns.injectInbound
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 18:08:53 +00:00
|
|
|
// DNSMap maps MagicDNS names (both base + FQDN) to their first IP.
|
|
|
|
// It should not be mutated once created.
|
|
|
|
type DNSMap map[string]netaddr.IP
|
|
|
|
|
|
|
|
func DNSMapFromNetworkMap(nm *netmap.NetworkMap) DNSMap {
|
|
|
|
ret := make(DNSMap)
|
2021-02-26 22:06:00 +00:00
|
|
|
suffix := nm.MagicDNSSuffix()
|
|
|
|
|
|
|
|
if nm.Name != "" && len(nm.Addresses) > 0 {
|
|
|
|
ip := nm.Addresses[0].IP
|
2021-03-01 18:08:53 +00:00
|
|
|
ret[strings.TrimRight(nm.Name, ".")] = ip
|
2021-02-26 22:06:00 +00:00
|
|
|
if dnsname.HasSuffix(nm.Name, suffix) {
|
2021-03-01 18:08:53 +00:00
|
|
|
ret[dnsname.TrimSuffix(nm.Name, suffix)] = ip
|
2021-02-26 22:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, p := range nm.Peers {
|
|
|
|
if p.Name != "" && len(p.Addresses) > 0 {
|
|
|
|
ip := p.Addresses[0].IP
|
2021-03-01 18:08:53 +00:00
|
|
|
ret[strings.TrimRight(p.Name, ".")] = ip
|
2021-02-26 22:06:00 +00:00
|
|
|
if dnsname.HasSuffix(p.Name, suffix) {
|
2021-03-01 18:08:53 +00:00
|
|
|
ret[dnsname.TrimSuffix(p.Name, suffix)] = ip
|
2021-02-26 22:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 18:08:53 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *Impl) updateDNS(nm *netmap.NetworkMap) {
|
|
|
|
ns.mu.Lock()
|
|
|
|
defer ns.mu.Unlock()
|
|
|
|
ns.dns = DNSMapFromNetworkMap(nm)
|
2021-02-26 22:06:00 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
func (ns *Impl) updateIPs(nm *netmap.NetworkMap) {
|
2021-02-26 22:06:00 +00:00
|
|
|
ns.updateDNS(nm)
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
oldIPs := make(map[tcpip.Address]bool)
|
|
|
|
for _, ip := range ns.ipstack.AllAddresses()[nicID] {
|
|
|
|
oldIPs[ip.AddressWithPrefix.Address] = true
|
|
|
|
}
|
|
|
|
newIPs := make(map[tcpip.Address]bool)
|
|
|
|
for _, ip := range nm.Addresses {
|
|
|
|
newIPs[tcpip.Address(ip.IP.IPAddr().IP)] = true
|
2020-09-03 23:45:41 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
ipsToBeAdded := make(map[tcpip.Address]bool)
|
|
|
|
for ip := range newIPs {
|
|
|
|
if !oldIPs[ip] {
|
|
|
|
ipsToBeAdded[ip] = true
|
2021-01-15 14:16:28 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
|
|
|
ipsToBeRemoved := make(map[tcpip.Address]bool)
|
|
|
|
for ip := range oldIPs {
|
|
|
|
if !newIPs[ip] {
|
|
|
|
ipsToBeRemoved[ip] = true
|
2021-01-15 14:16:28 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
2021-01-15 14:16:28 +00:00
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
for ip := range ipsToBeRemoved {
|
|
|
|
err := ns.ipstack.RemoveAddress(nicID, ip)
|
|
|
|
if err != nil {
|
|
|
|
ns.logf("netstack: could not deregister IP %s: %v", ip, err)
|
|
|
|
} else {
|
|
|
|
ns.logf("[v2] netstack: deregistered IP %s", ip)
|
2021-01-15 14:16:28 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
|
|
|
for ip := range ipsToBeAdded {
|
|
|
|
var err *tcpip.Error
|
|
|
|
if ip.To4() == "" {
|
|
|
|
err = ns.ipstack.AddAddress(nicID, ipv6.ProtocolNumber, ip)
|
|
|
|
} else {
|
|
|
|
err = ns.ipstack.AddAddress(nicID, ipv4.ProtocolNumber, ip)
|
2021-01-15 14:16:28 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
if err != nil {
|
|
|
|
ns.logf("netstack: could not register IP %s: %v", ip, err)
|
|
|
|
} else {
|
|
|
|
ns.logf("[v2] netstack: registered IP %s", ip)
|
2021-01-15 14:16:28 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 18:08:53 +00:00
|
|
|
// Resolve resolves addr into an IP:port using first the MagicDNS contents
|
|
|
|
// of m, else using the system resolver.
|
|
|
|
func (m DNSMap) Resolve(ctx context.Context, addr string) (netaddr.IPPort, error) {
|
2021-02-26 22:06:00 +00:00
|
|
|
ipp, pippErr := netaddr.ParseIPPort(addr)
|
|
|
|
if pippErr == nil {
|
|
|
|
return ipp, nil
|
|
|
|
}
|
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
// addr is malformed.
|
|
|
|
return netaddr.IPPort{}, err
|
|
|
|
}
|
|
|
|
if net.ParseIP(host) != nil {
|
|
|
|
// The host part of addr was an IP, so the netaddr.ParseIPPort above should've
|
|
|
|
// passed. Must've been a bad port number. Return the original error.
|
|
|
|
return netaddr.IPPort{}, pippErr
|
|
|
|
}
|
|
|
|
port16, err := strconv.ParseUint(port, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return netaddr.IPPort{}, fmt.Errorf("invalid port in address %q", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Host is not an IP, so assume it's a DNS name.
|
|
|
|
|
|
|
|
// Try MagicDNS first, else otherwise a real DNS lookup.
|
2021-03-01 18:08:53 +00:00
|
|
|
ip := m[host]
|
2021-02-26 22:06:00 +00:00
|
|
|
if !ip.IsZero() {
|
|
|
|
return netaddr.IPPort{IP: ip, Port: uint16(port16)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// No Magic DNS name so try real DNS.
|
|
|
|
var r net.Resolver
|
|
|
|
ips, err := r.LookupIP(ctx, "ip", host)
|
|
|
|
if err != nil {
|
|
|
|
return netaddr.IPPort{}, err
|
|
|
|
}
|
|
|
|
if len(ips) == 0 {
|
|
|
|
return netaddr.IPPort{}, fmt.Errorf("DNS lookup returned no results for %q", host)
|
|
|
|
}
|
|
|
|
ip, _ = netaddr.FromStdIP(ips[0])
|
|
|
|
return netaddr.IPPort{IP: ip, Port: uint16(port16)}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 18:08:53 +00:00
|
|
|
func (ns *Impl) DialContextTCP(ctx context.Context, addr string) (*gonet.TCPConn, error) {
|
|
|
|
ns.mu.Lock()
|
|
|
|
dnsMap := ns.dns
|
|
|
|
ns.mu.Unlock()
|
|
|
|
|
|
|
|
remoteIPPort, err := dnsMap.Resolve(ctx, addr)
|
2021-02-25 19:18:16 +00:00
|
|
|
if err != nil {
|
2021-02-26 22:06:00 +00:00
|
|
|
return nil, err
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
|
|
|
remoteAddress := tcpip.FullAddress{
|
|
|
|
NIC: nicID,
|
|
|
|
Addr: tcpip.Address(remoteIPPort.IP.IPAddr().IP),
|
|
|
|
Port: remoteIPPort.Port,
|
|
|
|
}
|
|
|
|
var ipType tcpip.NetworkProtocolNumber
|
|
|
|
if remoteIPPort.IP.Is4() {
|
|
|
|
ipType = ipv4.ProtocolNumber
|
|
|
|
} else {
|
|
|
|
ipType = ipv6.ProtocolNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
return gonet.DialContextTCP(ctx, ns.ipstack, remoteAddress, ipType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *Impl) injectOutbound() {
|
|
|
|
for {
|
|
|
|
packetInfo, ok := ns.linkEP.ReadContext(context.Background())
|
|
|
|
if !ok {
|
|
|
|
ns.logf("[v2] ReadContext-for-write = ok=false")
|
|
|
|
continue
|
2021-01-15 14:16:28 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
pkt := packetInfo.Pkt
|
|
|
|
hdrNetwork := pkt.NetworkHeader()
|
|
|
|
hdrTransport := pkt.TransportHeader()
|
2020-09-03 23:45:41 +01:00
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
full := make([]byte, 0, pkt.Size())
|
|
|
|
full = append(full, hdrNetwork.View()...)
|
|
|
|
full = append(full, hdrTransport.View()...)
|
|
|
|
full = append(full, pkt.Data.ToView()...)
|
2020-09-03 23:45:41 +01:00
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
ns.logf("[v2] packet Write out: % x", full)
|
|
|
|
if err := ns.tundev.InjectOutbound(full); err != nil {
|
|
|
|
log.Printf("netstack inject outbound: %v", err)
|
2020-09-03 23:45:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *Impl) injectInbound(p *packet.Parsed, t *tstun.TUN) filter.Response {
|
|
|
|
var pn tcpip.NetworkProtocolNumber
|
|
|
|
switch p.IPVersion {
|
|
|
|
case 4:
|
|
|
|
pn = header.IPv4ProtocolNumber
|
|
|
|
case 6:
|
|
|
|
pn = header.IPv6ProtocolNumber
|
|
|
|
}
|
|
|
|
ns.logf("[v2] packet in (from %v): % x", p.Src, p.Buffer())
|
|
|
|
vv := buffer.View(append([]byte(nil), p.Buffer()...)).ToVectorisedView()
|
|
|
|
packetBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{
|
|
|
|
Data: vv,
|
2020-09-03 23:45:41 +01:00
|
|
|
})
|
2021-02-25 19:18:16 +00:00
|
|
|
ns.linkEP.InjectInbound(pn, packetBuf)
|
|
|
|
return filter.Accept
|
|
|
|
}
|
2020-09-03 23:45:41 +01:00
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
func (ns *Impl) acceptTCP(r *tcp.ForwarderRequest) {
|
|
|
|
ns.logf("[v2] ForwarderRequest: %v", r)
|
|
|
|
var wq waiter.Queue
|
|
|
|
ep, err := r.CreateEndpoint(&wq)
|
|
|
|
if err != nil {
|
|
|
|
r.Complete(true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
localAddr, err := ep.GetLocalAddress()
|
|
|
|
ns.logf("[v2] forwarding port %v to 100.101.102.103:80", localAddr.Port)
|
|
|
|
if err != nil {
|
|
|
|
r.Complete(true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.Complete(false)
|
|
|
|
c := gonet.NewTCPConn(&wq, ep)
|
|
|
|
go ns.forwardTCP(c, &wq, "100.101.102.103:80")
|
|
|
|
}
|
2020-09-03 23:45:41 +01:00
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
func (ns *Impl) forwardTCP(client *gonet.TCPConn, wq *waiter.Queue, address string) {
|
|
|
|
defer client.Close()
|
|
|
|
ns.logf("[v2] netstack: forwarding to address %s", address)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
waitEntry, notifyCh := waiter.NewChannelEntry(nil)
|
|
|
|
wq.EventRegister(&waitEntry, waiter.EventHUp)
|
|
|
|
defer wq.EventUnregister(&waitEntry)
|
|
|
|
done := make(chan bool)
|
|
|
|
// netstack doesn't close the notification channel automatically if there was no
|
|
|
|
// hup signal, so we close done after we're done to not leak the goroutine below.
|
|
|
|
defer close(done)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-notifyCh:
|
|
|
|
case <-done:
|
2020-09-03 23:45:41 +01:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
cancel()
|
2020-09-03 23:45:41 +01:00
|
|
|
}()
|
2021-03-01 18:08:53 +00:00
|
|
|
server, err := ns.DialContextTCP(ctx, address)
|
2021-02-25 19:18:16 +00:00
|
|
|
if err != nil {
|
|
|
|
ns.logf("netstack: could not connect to server %s: %s", address, err)
|
|
|
|
return
|
2020-09-03 23:45:41 +01:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
defer server.Close()
|
|
|
|
connClosed := make(chan bool, 2)
|
|
|
|
go func() {
|
|
|
|
io.Copy(server, client)
|
|
|
|
connClosed <- true
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
io.Copy(client, server)
|
|
|
|
connClosed <- true
|
|
|
|
}()
|
|
|
|
<-connClosed
|
|
|
|
ns.logf("[v2] netstack: forwarder connection to %s closed", address)
|
2020-09-03 23:45:41 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
func (ns *Impl) acceptUDP(r *udp.ForwarderRequest) {
|
|
|
|
ns.logf("[v2] UDP ForwarderRequest: %v", r)
|
|
|
|
var wq waiter.Queue
|
|
|
|
ep, err := r.CreateEndpoint(&wq)
|
|
|
|
if err != nil {
|
|
|
|
ns.logf("Could not create endpoint, exiting")
|
|
|
|
return
|
2020-09-03 23:45:41 +01:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
c := gonet.NewUDPConn(ns.ipstack, &wq, ep)
|
|
|
|
go echoUDP(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func echoUDP(c *gonet.UDPConn) {
|
2020-09-03 23:45:41 +01:00
|
|
|
buf := make([]byte, 1500)
|
|
|
|
for {
|
|
|
|
n, err := c.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c.Write(buf[:n])
|
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
c.Close()
|
2020-09-03 23:45:41 +01:00
|
|
|
}
|