| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package udp |
| 17 |
|
| 18 |
import ( |
| 19 |
"inet.af/netstack/tcpip" |
| 20 |
"inet.af/netstack/tcpip/buffer" |
| 21 |
"inet.af/netstack/tcpip/header" |
| 22 |
"inet.af/netstack/tcpip/header/parse" |
| 23 |
"inet.af/netstack/tcpip/stack" |
| 24 |
"inet.af/netstack/tcpip/transport/raw" |
| 25 |
"inet.af/netstack/waiter" |
| 26 |
) |
| 27 |
|
| 28 |
const ( |
| 29 |
|
| 30 |
ProtocolNumber = header.UDPProtocolNumber |
| 31 |
|
| 32 |
|
| 33 |
MinBufferSize = 4 << 10 |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
DefaultSendBufferSize = 32 << 10 |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
DefaultReceiveBufferSize = 32 << 10 |
| 42 |
|
| 43 |
|
| 44 |
MaxBufferSize = 4 << 20 |
| 45 |
) |
| 46 |
|
| 47 |
type protocol struct { |
| 48 |
stack *stack.Stack |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
func (*protocol) Number() tcpip.TransportProtocolNumber { |
| 53 |
return ProtocolNumber |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
func (p *protocol) NewEndpoint(netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error) { |
| 58 |
return newEndpoint(p.stack, netProto, waiterQueue), nil |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
func (p *protocol) NewRawEndpoint(netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error) { |
| 64 |
return raw.NewEndpoint(p.stack, netProto, header.UDPProtocolNumber, waiterQueue) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func (*protocol) MinimumPacketSize() int { |
| 69 |
return header.UDPMinimumSize |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
func (*protocol) ParsePorts(v buffer.View) (src, dst uint16, err tcpip.Error) { |
| 75 |
h := header.UDP(v) |
| 76 |
return h.SourcePort(), h.DestinationPort(), nil |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
func (p *protocol) HandleUnknownDestinationPacket(id stack.TransportEndpointID, pkt *stack.PacketBuffer) stack.UnknownDestinationPacketDisposition { |
| 82 |
hdr := header.UDP(pkt.TransportHeader().View()) |
| 83 |
if int(hdr.Length()) > pkt.Data().Size()+header.UDPMinimumSize { |
| 84 |
p.stack.Stats().UDP.MalformedPacketsReceived.Increment() |
| 85 |
return stack.UnknownDestinationPacketMalformed |
| 86 |
} |
| 87 |
|
| 88 |
if !verifyChecksum(hdr, pkt) { |
| 89 |
p.stack.Stats().UDP.ChecksumErrors.Increment() |
| 90 |
return stack.UnknownDestinationPacketMalformed |
| 91 |
} |
| 92 |
|
| 93 |
return stack.UnknownDestinationPacketUnhandled |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
func (*protocol) SetOption(tcpip.SettableTransportProtocolOption) tcpip.Error { |
| 98 |
return &tcpip.ErrUnknownProtocolOption{} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
func (*protocol) Option(tcpip.GettableTransportProtocolOption) tcpip.Error { |
| 103 |
return &tcpip.ErrUnknownProtocolOption{} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
func (*protocol) Close() {} |
| 108 |
|
| 109 |
|
| 110 |
func (*protocol) Wait() {} |
| 111 |
|
| 112 |
|
| 113 |
func (*protocol) Parse(pkt *stack.PacketBuffer) bool { |
| 114 |
return parse.UDP(pkt) |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
func NewProtocol(s *stack.Stack) stack.TransportProtocol { |
| 119 |
return &protocol{stack: s} |
| 120 |
} |