| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package muxed |
| 17 |
|
| 18 |
import ( |
| 19 |
"inet.af/netstack/tcpip" |
| 20 |
"inet.af/netstack/tcpip/header" |
| 21 |
"inet.af/netstack/tcpip/stack" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
type InjectableEndpoint struct { |
| 29 |
routes map[tcpip.Address]stack.InjectableLinkEndpoint |
| 30 |
dispatcher stack.NetworkDispatcher |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
func (m *InjectableEndpoint) MTU() uint32 { |
| 35 |
minMTU := ^uint32(0) |
| 36 |
for _, endpoint := range m.routes { |
| 37 |
if endpointMTU := endpoint.MTU(); endpointMTU < minMTU { |
| 38 |
minMTU = endpointMTU |
| 39 |
} |
| 40 |
} |
| 41 |
return minMTU |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func (m *InjectableEndpoint) Capabilities() stack.LinkEndpointCapabilities { |
| 46 |
minCapabilities := stack.LinkEndpointCapabilities(^uint(0)) |
| 47 |
for _, endpoint := range m.routes { |
| 48 |
minCapabilities &= endpoint.Capabilities() |
| 49 |
} |
| 50 |
return minCapabilities |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
func (m *InjectableEndpoint) MaxHeaderLength() uint16 { |
| 55 |
minHeaderLen := ^uint16(0) |
| 56 |
for _, endpoint := range m.routes { |
| 57 |
if headerLen := endpoint.MaxHeaderLength(); headerLen < minHeaderLen { |
| 58 |
minHeaderLen = headerLen |
| 59 |
} |
| 60 |
} |
| 61 |
return minHeaderLen |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
func (m *InjectableEndpoint) LinkAddress() tcpip.LinkAddress { |
| 66 |
return "" |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func (m *InjectableEndpoint) Attach(dispatcher stack.NetworkDispatcher) { |
| 71 |
for _, endpoint := range m.routes { |
| 72 |
endpoint.Attach(dispatcher) |
| 73 |
} |
| 74 |
m.dispatcher = dispatcher |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
func (m *InjectableEndpoint) IsAttached() bool { |
| 79 |
return m.dispatcher != nil |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
func (m *InjectableEndpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { |
| 84 |
m.dispatcher.DeliverNetworkPacket("" , "" , protocol, pkt) |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
func (m *InjectableEndpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, protocol tcpip.NetworkProtocolNumber) (int, tcpip.Error) { |
| 91 |
endpoint, ok := m.routes[r.RemoteAddress] |
| 92 |
if !ok { |
| 93 |
return 0, &tcpip.ErrNoRoute{} |
| 94 |
} |
| 95 |
return endpoint.WritePackets(r, pkts, protocol) |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
func (m *InjectableEndpoint) WritePacket(r stack.RouteInfo, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error { |
| 102 |
if endpoint, ok := m.routes[r.RemoteAddress]; ok { |
| 103 |
return endpoint.WritePacket(r, protocol, pkt) |
| 104 |
} |
| 105 |
return &tcpip.ErrNoRoute{} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
func (m *InjectableEndpoint) InjectOutbound(dest tcpip.Address, packet []byte) tcpip.Error { |
| 111 |
endpoint, ok := m.routes[dest] |
| 112 |
if !ok { |
| 113 |
return &tcpip.ErrNoRoute{} |
| 114 |
} |
| 115 |
return endpoint.InjectOutbound(dest, packet) |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func (m *InjectableEndpoint) Wait() { |
| 120 |
for _, ep := range m.routes { |
| 121 |
ep.Wait() |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
func (*InjectableEndpoint) ARPHardwareType() header.ARPHardwareType { |
| 127 |
panic("unsupported operation") |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
func (*InjectableEndpoint) AddHeader(local, remote tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
func (*InjectableEndpoint) WriteRawPacket(*stack.PacketBuffer) tcpip.Error { |
| 136 |
return &tcpip.ErrNotSupported{} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
func NewInjectableEndpoint(routes map[tcpip.Address]stack.InjectableLinkEndpoint) *InjectableEndpoint { |
| 141 |
return &InjectableEndpoint{ |
| 142 |
routes: routes, |
| 143 |
} |
| 144 |
} |